Search in sources :

Example 6 with XSDSchemaContent

use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.

the class SchemaTreeContentProvider method getXSDSchemaChildren.

protected Object[] getXSDSchemaChildren(XSDSchema schema) {
    List<XSDElementDeclaration> declarations = new ArrayList<XSDElementDeclaration>();
    List<Object> attributeDeclarations = new ArrayList<Object>();
    if (null != xsdSchema) {
        for (XSDSchemaContent cnt : xsdSchema.getContents()) {
            if (cnt instanceof XSDInclude) {
                XSDInclude incu = (XSDInclude) cnt;
                String schemaLocation = incu.getSchemaLocation();
                XSDSchema schemaInc = createSchema(schemaLocation);
                addElementDeclarationFromSchema(schemaInc, declarations);
            } else if (cnt instanceof XSDAttributeDeclaration) {
                XSDAttributeDeclaration attriDec = (XSDAttributeDeclaration) cnt;
                attributeDeclarations.add(attriDec);
            }
        }
    }
    addElementDeclarationFromSchema(schema, declarations);
    Object[] schemaChildren = Util.filterOutDuplicatedElems(declarations.toArray(new XSDNamedComponent[declarations.size()]));
    attributeDeclarations.addAll(Arrays.asList(schemaChildren));
    return attributeDeclarations.toArray();
}
Also used : XSDNamedComponent(org.eclipse.xsd.XSDNamedComponent) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ArrayList(java.util.ArrayList) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) TreeObject(com.amalto.workbench.models.TreeObject) XSDAttributeDeclaration(org.eclipse.xsd.XSDAttributeDeclaration) XSDInclude(org.eclipse.xsd.XSDInclude) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 7 with XSDSchemaContent

use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.

the class UtilTest method testGetTypeDefinition.

@Test
public void testGetTypeDefinition() {
    // $NON-NLS-1$
    String s_name = "simpleType";
    // $NON-NLS-1$
    String c_name = "complexType";
    // $NON-NLS-1$
    String e_name = "xsdElementDeclaration";
    XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();
    XSDSimpleTypeDefinition xsdSimpleTypeDefinition1 = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
    XSDSimpleTypeDefinition xsdSimpleTypeDefinition2 = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
    XSDComplexTypeDefinition xsdComplexTypeDefinition1 = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
    XSDComplexTypeDefinition xsdComplexTypeDefinition2 = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
    xsdSimpleTypeDefinition1.setName(s_name + 1);
    xsdSimpleTypeDefinition2.setName(s_name + 2);
    xsdComplexTypeDefinition1.setName(c_name + 1);
    xsdComplexTypeDefinition2.setName(c_name + 2);
    xsdSimpleTypeDefinition1.setBaseTypeDefinition(// $NON-NLS-1$ anyType
    xsdSchema.resolveSimpleTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "string"));
    xsdSimpleTypeDefinition2.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveSimpleTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "string"));
    xsdComplexTypeDefinition1.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    xsdComplexTypeDefinition2.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    EList<XSDSchemaContent> contents = xsdSchema.getContents();
    contents.add(xsdSimpleTypeDefinition1);
    contents.add(xsdSimpleTypeDefinition2);
    contents.add(xsdComplexTypeDefinition1);
    contents.add(xsdComplexTypeDefinition2);
    XSDElementDeclaration xsdElementDeclaration = XSDFactory.eINSTANCE.createXSDElementDeclaration();
    xsdElementDeclaration.setName(e_name);
    contents.add(xsdElementDeclaration);
    Map<String, XSDTypeDefinition> typeDefinition = Util.getTypeDefinition(xsdSchema);
    assertNotNull(typeDefinition);
    assertTrue(typeDefinition.size() == 4);
    assertTrue(typeDefinition.keySet().contains(s_name + 1));
    assertTrue(typeDefinition.keySet().contains(s_name + 2));
    assertTrue(typeDefinition.keySet().contains(c_name + 1));
    assertTrue(typeDefinition.keySet().contains(c_name + 2));
    assertTrue(typeDefinition.values().contains(xsdSimpleTypeDefinition1));
    assertTrue(typeDefinition.values().contains(xsdSimpleTypeDefinition2));
    assertTrue(typeDefinition.values().contains(xsdComplexTypeDefinition1));
    assertTrue(typeDefinition.values().contains(xsdComplexTypeDefinition2));
}
Also used : XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) Test(org.junit.Test)

Example 8 with XSDSchemaContent

use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.

the class SelectImportedModulesDialog method okPressed.

@Override
protected void okPressed() {
    addContent.clear();
    Object[] objs = entityViewer.getCheckedElements();
    if (null != objs) {
        for (Object obj : objs) {
            if (obj instanceof XSDSchemaContent) {
                addContent.add((XSDSchemaContent) obj);
            }
        }
    }
    objs = typeViewer.getCheckedElements();
    if (null != objs) {
        for (Object obj : objs) {
            if (obj instanceof XSDSchemaContent) {
                addContent.add((XSDSchemaContent) obj);
            }
        }
    }
    super.okPressed();
}
Also used : XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) TreeObject(com.amalto.workbench.models.TreeObject)

Example 9 with XSDSchemaContent

use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.

the class DataModelMainPage method save.

public int save(String xsd) {
    try {
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        wsObject.setDescription(desAntionComposite.getText());
        String schema = xsd;
        if (xsd == null) {
            schema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXSDSchemaAsString();
            xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
        }
        XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
        // $NON-NLS-1$
        xsdImport.setNamespace("http://www.w3.org/2001/XMLSchema");
        if (xsdSchema == null) {
            xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
        }
        if (xsdSchema == null || (xsd != null && !xsd.equals(wsObject.getXsdSchema())) || (xsd != null && !xsd.equals(getXSDSchemaString()))) {
            // 
            xsdSchema = Util.createXsdSchema(schema, xobject);
            // 
            getSchemaContentProvider().setXsdSchema(xsdSchema);
        }
        wsObject.setXsdSchema(schema);
        EList<XSDSchemaContent> elist = xsdSchema.getContents();
        for (XSDSchemaContent cnt : elist) {
            if (cnt instanceof XSDImport) {
                XSDImport imp = (XSDImport) cnt;
                if (imp.getNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
                    // $NON-NLS-1$
                    xsdImport = null;
                    break;
                }
            }
        }
        if (xsdImport != null) {
            xsdSchema.getContents().add(0, xsdImport);
            wsObject.setXsdSchema(schema);
        }
        validateSchema();
        // save to db
        doSave(wsObject);
        dirty = false;
    } catch (Exception e) {
        dirty = true;
        log.error(e.getMessage(), e);
    }
    firePropertyChange(PROP_DIRTY);
    return dirty ? 1 : 0;
}
Also used : WSDataModel(com.amalto.workbench.webservices.WSDataModel) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) SAXParseException(org.xml.sax.SAXParseException)

Example 10 with XSDSchemaContent

use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.

the class DataModelMainPage method importSchema.

protected void importSchema(InputSource source, String uri) throws Exception {
    // $NON-NLS-1$
    String ns = "";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);
    // $NON-NLS-1$
    ns = document.getDocumentElement().getAttribute("targetNamespace");
    if (xsdSchema == null) {
        xsdSchema = getXSDSchema(Util.nodeToString(document));
    } else {
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        xsdSchema = Util.createXsdSchema(wsObject.getXsdSchema(), xobject);
    }
    boolean exist = false;
    for (int i = 0; i < xsdSchema.getContents().size(); i++) {
        XSDSchemaContent xsdComp = xsdSchema.getContents().get(i);
        if (ns != null && !ns.equals("")) {
            // import xsdschema
            if (xsdComp instanceof XSDImport && ((XSDImport) xsdComp).getNamespace().equals(ns)) {
                for (Map.Entry entry : xsdSchema.getQNamePrefixToNamespaceMap().entrySet()) {
                    if (entry.getValue().equals(((XSDImport) xsdComp).getNamespace())) {
                        exist = true;
                        break;
                    }
                }
                break;
            }
        } else {
            // include xsdschema
            if (xsdComp instanceof XSDInclude) {
                String xsdLocation = ((XSDInclude) xsdComp).getSchemaLocation();
                if (xsdLocation.equals(uri)) {
                    exist = true;
                    break;
                }
            }
        }
    }
    if (!exist) {
        if (ns != null && !ns.equals("")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            int last = ns.lastIndexOf("/");
            // $NON-NLS-1$//$NON-NLS-2$
            xsdSchema.getQNamePrefixToNamespaceMap().put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(ns);
            xsdImport.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdImport);
        } else {
            XSDInclude xsdInclude = XSDFactory.eINSTANCE.createXSDInclude();
            xsdInclude.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdInclude);
        }
        String xsd = Util.nodeToString(xsdSchema.getDocument());
        setXsdSchema(xsdSchema);
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        wsObject.setXsdSchema(xsd);
    }
}
Also used : WSDataModel(com.amalto.workbench.webservices.WSDataModel) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) Document(org.w3c.dom.Document) Map(java.util.Map) HashMap(java.util.HashMap) XSDInclude(org.eclipse.xsd.XSDInclude)

Aggregations

XSDSchemaContent (org.eclipse.xsd.XSDSchemaContent)13 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)7 ArrayList (java.util.ArrayList)5 XSDImport (org.eclipse.xsd.XSDImport)5 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)4 TreeObject (com.amalto.workbench.models.TreeObject)3 XSDInclude (org.eclipse.xsd.XSDInclude)3 XSDSchema (org.eclipse.xsd.XSDSchema)3 WSDataModel (com.amalto.workbench.webservices.WSDataModel)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 CoreException (org.eclipse.core.runtime.CoreException)2 PartInitException (org.eclipse.ui.PartInitException)2 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)2 XSDImportImpl (org.eclipse.xsd.impl.XSDImportImpl)2 Test (org.junit.Test)2 SAXParseException (org.xml.sax.SAXParseException)2 TypesLabelProvider (com.amalto.workbench.providers.TypesLabelProvider)1 XSDTreeLabelProvider (com.amalto.workbench.providers.XSDTreeLabelProvider)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1