Search in sources :

Example 11 with XSDSchemaContent

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

the class Util method addImport.

private static void addImport(XSDSchema xsdSchema, List<XSDImport> imports) {
    Map<String, String> nsMap = xsdSchema.getQNamePrefixToNamespaceMap();
    int imp = 0;
    for (XSDImport xsdImport : imports) {
        String ns = xsdImport.getNamespace();
        if (ns.equals("")) {
            // $NON-NLS-1$
            continue;
        }
        // $NON-NLS-1$
        int last = ns.lastIndexOf("/");
        if (!nsMap.containsValue(ns)) {
            if (ns.equals("http://www.w3.org/XML/1998/namespace")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                nsMap.put("xml", ns);
            } else {
                // $NON-NLS-1$//$NON-NLS-2$
                nsMap.put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            }
        }
        boolean exist = false;
        for (XSDSchemaContent cnt : xsdSchema.getContents()) {
            if (cnt instanceof XSDImportImpl) {
                XSDImportImpl cntImpl = (XSDImportImpl) cnt;
                if (cntImpl.getNamespace().equals(ns)) {
                    exist = true;
                    break;
                }
            }
        }
        if (!exist) {
            xsdSchema.getContents().add(imp++, xsdImport);
        }
    }
    xsdSchema.updateElement();
}
Also used : XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport)

Example 12 with XSDSchemaContent

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

the class XSDUtil method buildEntityUsedComplexTypeMap.

public static Map<XSDElementDeclaration, List<XSDComplexTypeDefinition>> buildEntityUsedComplexTypeMap(XSDSchema schema) {
    Map<XSDElementDeclaration, List<XSDComplexTypeDefinition>> entityMapComplexType = new HashMap<XSDElementDeclaration, List<XSDComplexTypeDefinition>>();
    EList<XSDSchemaContent> contents = schema.getContents();
    for (XSDSchemaContent content : contents) {
        if (content instanceof XSDElementDeclaration) {
            XSDElementDeclaration concept = (XSDElementDeclaration) content;
            XSDComplexTypeDefinition ctypeDef = (XSDComplexTypeDefinition) concept.getTypeDefinition();
            List<XSDComplexTypeDefinition> ctypes = new ArrayList<XSDComplexTypeDefinition>();
            ctypes.add(ctypeDef);
            while (ctypeDef.getBaseTypeDefinition() != ((XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition()).getBaseTypeDefinition()) {
                ctypes.add((XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition());
                ctypeDef = (XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition();
            }
            entityMapComplexType.put(concept, ctypes);
        }
    }
    return entityMapComplexType;
}
Also used : HashMap(java.util.HashMap) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ArrayList(java.util.ArrayList) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition)

Example 13 with XSDSchemaContent

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

the class UtilMockTest method testAddImport.

@Test
public void testAddImport() {
    // 2184
    // $NON-NLS-1$
    String namespace1 = "http://www.w3.org/XML/1998/namespace1";
    // $NON-NLS-1$
    String namespace2 = "http://www.w3.org/XML/1998/namespace2";
    // $NON-NLS-1$
    String namespace3 = "http://www.w3.org/XML/1998/namespace3";
    String[] namespaces = { namespace1, namespace2, namespace3 };
    // $NON-NLS-1$
    String methodToExecute = "addImport";
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDSchema xsdschema = factory.createXSDSchema();
    List<XSDImport> imports = new ArrayList<XSDImport>();
    for (int i = 0; i < 3; i++) {
        XSDImport importt = factory.createXSDImport();
        importt.setNamespace(namespaces[i]);
        imports.add(importt);
    }
    XSDImport importt = factory.createXSDImport();
    // $NON-NLS-1$
    importt.setNamespace("");
    imports.add(importt);
    xsdschema.getContents().add(imports.get(0));
    PowerMockito.mockStatic(Util.class);
    try {
        PowerMockito.when(Util.class, methodToExecute, any(XSDSchema.class), anyListOf(XSDImport.class)).thenCallRealMethod();
        Whitebox.invokeMethod(Util.class, methodToExecute, xsdschema, imports);
        EList<XSDSchemaContent> contents = xsdschema.getContents();
        assertTrue(contents.size() == 3);
        assertTrue(contents.contains(imports.get(0)));
        assertTrue(contents.contains(imports.get(1)));
        assertTrue(contents.contains(imports.get(2)));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) ArrayList(java.util.ArrayList) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDSchema(org.eclipse.xsd.XSDSchema) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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