Search in sources :

Example 56 with TreeObject

use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.

the class Util method getXSDSchema.

private static XSDSchema getXSDSchema(String namespaceURI, String rawData, final List<XSDImport> imports, final TreeObject treeObj, boolean uri, final List<Exception> exceptions, final Map<String, Integer> schemaMonitor) throws Exception {
    FileInputStream fin = null;
    try {
        // $NON-NLS-1$//$NON-NLS-2$
        final String xsdFileName = System.getProperty("user.dir") + "/.xsdModel.xml";
        URI fileURI = URI.createFileURI(xsdFileName);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setValidating(false);
        DocumentBuilder documentBuilder;
        XSDSchema schema = null;
        InputSource source = null;
        Document document = null;
        String schemaLocation = rawData;
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        if (rawData == null) {
            // $NON-NLS-1$
            return XSDSchemaImpl.getSchemaForSchema("http://www.w3.org/2001/XMLSchema");
        }
        if (namespaceURI == null && rawData.endsWith(".xsd") && rawData.indexOf(File.separator) > 0) {
            // $NON-NLS-1$
            File rawFile = new File(rawData);
            if (!rawFile.exists()) {
                throw new IllegalArgumentException(rawData);
            }
        }
        // import namespace="http://xxx" schemaLocation="xxxx"
        if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            URL url = new java.net.URI(namespaceURI + "/" + rawData).toURL();
            uri = false;
            rawData = IOUtils.toString(url.openConnection().getInputStream());
            // $NON-NLS-1$//$NON-NLS-2$
            rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
        }
        if (rawData.equals("http://www.w3.org/2001/03/xml.xsd")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            URL url = new java.net.URI("http://www.w3.org/2001/03/xml.xsd").toURL();
            uri = false;
            rawData = IOUtils.toString(url.openConnection().getInputStream());
            // $NON-NLS-1$//$NON-NLS-2$
            rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
        }
        if (uri) {
            File file = new File(rawData);
            if (file.exists()) {
                fin = new FileInputStream(file);
                source = new InputSource(fin);
            } else {
                source = new InputSource(new StringReader(Util.getResponseFromURL(rawData, treeObj)));
            }
        } else {
            source = new InputSource(new StringReader(rawData));
        }
        try {
            document = documentBuilder.parse(source);
        } catch (SAXParseException ex) {
            exceptions.add(ex);
            return null;
        }
        schema = XSDSchemaImpl.createSchema(document.getDocumentElement());
        ResourceSet resourceSet = new ResourceSetImpl();
        Resource resource = resourceSet.createResource(fileURI);
        resourceSet.getAdapterFactories().add(new AdapterFactoryImpl() {

            class SchemaLocator extends AdapterImpl implements XSDSchemaLocator {

                public XSDSchema locateSchema(XSDSchema xsdSchema, String namespaceURI, String rawSchemaLocationURI, String resolvedSchemaLocation) {
                    XSDSchema schema;
                    Integer rawCnt = schemaMonitor.get(rawSchemaLocationURI);
                    if (rawCnt == null) {
                        rawCnt = 0;
                    } else {
                        rawCnt++;
                    }
                    schemaMonitor.put(rawSchemaLocationURI, rawCnt);
                    if (rawCnt >= 10) {
                        schemaMonitor.put(rawSchemaLocationURI, -1);
                        return null;
                    }
                    try {
                        schema = Util.getXSDSchema(namespaceURI, rawSchemaLocationURI, imports, treeObj, true, exceptions, schemaMonitor);
                    } catch (Exception e) {
                        return XSDSchemaImpl.getSchemaForSchema(namespaceURI);
                    }
                    schema.setTargetNamespace(namespaceURI);
                    schema.setElement(schema.getDocument().getDocumentElement());
                    return schema;
                }

                public boolean isAdatperForType(Object type) {
                    return type == XSDSchemaLocator.class;
                }
            }

            protected SchemaLocator schemaLocator = new SchemaLocator();

            @Override
            public boolean isFactoryForType(Object type) {
                return type == XSDSchemaLocator.class;
            }

            @Override
            public Adapter adaptNew(Notifier target, Object type) {
                return schemaLocator;
            }
        });
        // import namespace="http://xxx" schemaLocation="xxxx"
        if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
            // $NON-NLS-1$
            schema.setSchemaLocation(schemaLocation);
        } else {
            schema.setSchemaLocation(fileURI.toString());
            // set the schema for schema QName prefix to "xsd"
            // $NON-NLS-1$
            schema.setSchemaForSchemaQNamePrefix("xsd");
        }
        // catch up the NPE to make sure data model can still run in case of unknown conflict
        try {
            resource.getContents().add(schema);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        // Add the root schema to the resource that was created above
        Iterator<Integer> iter = schemaMonitor.values().iterator();
        while (iter.hasNext()) {
            Integer it = iter.next();
            if (it.intValue() == -1) {
                return schema;
            }
        }
        importSchema(schema, imports, schemaMonitor);
        schema.setElement(document.getDocumentElement());
        return schema;
    } finally {
        if (fin != null) {
            fin.close();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) Document(org.w3c.dom.Document) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL) SAXParseException(org.xml.sax.SAXParseException) XSDSchemaLocator(org.eclipse.xsd.util.XSDSchemaLocator) StringReader(java.io.StringReader) XSDSchema(org.eclipse.xsd.XSDSchema) Notifier(org.eclipse.emf.common.notify.Notifier) AdapterFactoryImpl(org.eclipse.emf.common.notify.impl.AdapterFactoryImpl) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) MissingJarsException(com.amalto.workbench.service.MissingJarsException) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) InaccessibleWSDLException(com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TreeObject(com.amalto.workbench.models.TreeObject) EObject(org.eclipse.emf.ecore.EObject) File(java.io.File) ZipFile(java.util.zip.ZipFile) ZipToFile(org.talend.mdm.commmon.util.workbench.ZipToFile) XSDSchemaLocator(org.eclipse.xsd.util.XSDSchemaLocator)

Example 57 with TreeObject

use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.

the class Bean2EObjUtilTest method testWrapEObjWithTreeObject1Args.

/**
 * test wrapEObjWithTreeObject with one input parameter
 */
@Test
public void testWrapEObjWithTreeObject1Args() {
    EObject eobj = null;
    Bean2EObjUtil util = Bean2EObjUtil.getInstance();
    TreeObject treeObject = util.wrapEObjWithTreeObject(eobj);
    assertNull(treeObject);
    MDMServerObject mdmServerObject = PowerMockito.mock(MDMServerObject.class);
    when(mdmServerObject.getName()).thenReturn("name");
    when(mdmServerObject.getType()).thenReturn(2);
    eobj = mdmServerObject;
    treeObject = util.wrapEObjWithTreeObject(eobj);
    assertNotNull(treeObject);
    Mockito.verify(mdmServerObject, Mockito.atLeastOnce()).getName();
    Mockito.verify(mdmServerObject, Mockito.atLeastOnce()).getType();
    assertEquals("name", treeObject.getName());
    assertEquals(2, treeObject.getType());
}
Also used : EObject(org.eclipse.emf.ecore.EObject) TreeObject(com.amalto.workbench.models.TreeObject) MDMServerObject(org.talend.mdm.repository.model.mdmserverobject.MDMServerObject) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 58 with TreeObject

use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.

the class ResourcesUtilTest method testGetResourcesNameListFromURI.

@Test
public void testGetResourcesNameListFromURI() {
    // $NON-NLS-1$
    String method_private_getXMLString = "getXMLString";
    // $NON-NLS-1$
    String method_private_parsXMLString = "parsXMLString";
    // $NON-NLS-1$
    String responseBody = "responseBody";
    // $NON-NLS-1$
    String uri = "uri";
    TreeObject treeObj = new TreeObject();
    try {
        PowerMockito.mockStatic(ResourcesUtil.class);
        PowerMockito.when(ResourcesUtil.class, method_private_getXMLString, anyString(), any(TreeObject.class)).thenReturn(responseBody);
        Document doc = null;
        PowerMockito.when(ResourcesUtil.class, method_private_parsXMLString, anyString()).thenReturn(doc);
        PowerMockito.when(ResourcesUtil.getResourcesNameListFromURI(anyString(), any(TreeObject.class))).thenCallRealMethod();
        List<String> resourcesNameList = ResourcesUtil.getResourcesNameListFromURI(uri, treeObj);
        assertTrue(resourcesNameList.isEmpty());
        doc = DocumentHelper.createDocument();
        // $NON-NLS-1$
        Element rootElement = DocumentHelper.createElement("root");
        doc.setRootElement(rootElement);
        PowerMockito.when(ResourcesUtil.class, method_private_parsXMLString, anyString()).thenReturn(doc);
        boolean[][] ownNameURIElement = { { true, false }, { false, false } };
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        String[][] localNameUris = { { "name0", "uri0" }, { "name1", "uri1" } };
        // $NON-NLS-1$
        String entry = "entry";
        for (int i = 0; i < ownNameURIElement.length; i++) {
            createChildElement(rootElement, entry + i, localNameUris[i], ownNameURIElement[i][0], ownNameURIElement[i][1]);
        }
        resourcesNameList = ResourcesUtil.getResourcesNameListFromURI(uri, treeObj);
        assertNotNull(resourcesNameList);
        assertEquals(2, resourcesNameList.size());
        List<String> resourcesNamesList = new ArrayList<String>();
        // $NON-NLS-1$
        resourcesNamesList.add("name0");
        // $NON-NLS-1$
        resourcesNamesList.add("entry1");
        assertEquals(resourcesNamesList, resourcesNameList);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Element(org.dom4j.Element) ArrayList(java.util.ArrayList) TreeObject(com.amalto.workbench.models.TreeObject) Document(org.dom4j.Document) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 59 with TreeObject

use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.

the class XObjectBrowserInput2 method getToolTipText.

@Override
public String getToolTipText() {
    TreeObject xobject = (TreeObject) getModel();
    // $NON-NLS-1$
    String sep = " - ";
    String serverName = null;
    if (serverDef != null) {
        serverName = serverDef.getName();
    } else {
        serverName = xobject.getServerRoot().getName();
    }
    if (xobject.getType() == TreeObject.SUBSCRIPTION_ENGINE) {
        return serverName + sep + Messages.XObjectBrowserInput2_eventmanager;
    } else if (xobject.getType() == TreeObject.DATA_CLUSTER) {
        return serverName + sep + TreeObject.getTypeName(xobject.getType()) + sep + xobject.getDisplayName();
    } else {
        return TreeObject.getTypeName(xobject.getType()) + sep + xobject.getDisplayName();
    }
}
Also used : TreeObject(com.amalto.workbench.models.TreeObject)

Example 60 with TreeObject

use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.

the class XObjectEditor2 method saveResourceToRepository.

private boolean saveResourceToRepository() {
    XObjectEditorInput2 editorInput = (XObjectEditorInput2) this.getEditorInput();
    TreeObject xobject = (TreeObject) editorInput.getModel();
    IRepositoryViewObject viewObject = editorInput.getViewObject();
    IRepositoryViewObject refreshViewObject = RepositoryResourceUtil.assertViewObject(viewObject);
    if (viewObject != refreshViewObject) {
        editorInput.updateViewObject(refreshViewObject);
    }
    MDMServerObjectItem serverObjectItem = (MDMServerObjectItem) editorInput.getInputItem();
    MDMServerObject serverObject = serverObjectItem.getMDMServerObject();
    EObject eObj = Bean2EObjUtil.getInstance().convertFromBean2EObj(xobject.getWsObject(), serverObject);
    if (eObj != null) {
        IProxyRepositoryFactory factory = CoreRuntimePlugin.getInstance().getProxyRepositoryFactory();
        try {
            factory.save(serverObjectItem);
            // TODO should call the following,but the page in editor has many call to remote webService ,it will
            // search ServerRoot which cause a NPE
            // xobject.fireEvent(IXObjectModelListener.SAVE, xobject.getParent(), xobject);
            editorDirtyStateChanged();
            refreshDirtyCue();
            DeployService deployService = DeployService.getInstance();
            if (deployService.isAutoDeploy()) {
                autoDeployProcess(deployService);
            } else {
                MDMServerDef lastServerDef = RepositoryResourceUtil.getLastServerDef(serverObjectItem);
                if (lastServerDef != null) {
                    CommandManager.getInstance().pushCommand(ICommand.CMD_MODIFY, editorInput.getViewObject());
                }
            }
            return true;
        } catch (PersistenceException e) {
            log.error(e.getMessage(), e);
        }
    }
    return false;
}
Also used : MDMServerObjectItem(org.talend.mdm.repository.model.mdmproperties.MDMServerObjectItem) EObject(org.eclipse.emf.ecore.EObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) PersistenceException(org.talend.commons.exception.PersistenceException) TreeObject(com.amalto.workbench.models.TreeObject) DeployService(org.talend.mdm.repository.core.service.DeployService) MDMServerObject(org.talend.mdm.repository.model.mdmserverobject.MDMServerObject) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) MDMServerDef(org.talend.mdm.repository.model.mdmmetadata.MDMServerDef)

Aggregations

TreeObject (com.amalto.workbench.models.TreeObject)66 TreeParent (com.amalto.workbench.models.TreeParent)21 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)14 ArrayList (java.util.ArrayList)12 MDMServerObject (org.talend.mdm.repository.model.mdmserverobject.MDMServerObject)12 TMDMService (com.amalto.workbench.webservices.TMDMService)6 WSDataModel (com.amalto.workbench.webservices.WSDataModel)5 WSDataModelPK (com.amalto.workbench.webservices.WSDataModelPK)5 WSGetDataModel (com.amalto.workbench.webservices.WSGetDataModel)5 Document (org.dom4j.Document)5 Element (org.dom4j.Element)5 EObject (org.eclipse.emf.ecore.EObject)5 MDMServerObjectItem (org.talend.mdm.repository.model.mdmproperties.MDMServerObjectItem)5 WSDataClusterPK (com.amalto.workbench.webservices.WSDataClusterPK)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IAvailableModel (com.amalto.workbench.availablemodel.IAvailableModel)3 UserInfo (com.amalto.workbench.utils.UserInfo)3 WSDataCluster (com.amalto.workbench.webservices.WSDataCluster)3 WSDigest (com.amalto.workbench.webservices.WSDigest)3 WSDigestKey (com.amalto.workbench.webservices.WSDigestKey)3