Search in sources :

Example 31 with XSDTypeDefinition

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

the class UtilTest method testGetParentTypes.

@Test
public void testGetParentTypes() {
    XSDComplexTypeDefinition complexType = (XSDComplexTypeDefinition) schema.getElementDeclarations().get(0).getTypeDefinition();
    List<XSDTypeDefinition> list = Util.getParentTypes(complexType);
    assertEquals(list.size(), 1);
}
Also used : XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) Test(org.junit.Test)

Example 32 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition 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 33 with XSDTypeDefinition

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

the class UtilTest method testGetComplexTypeDefinitionChildren.

/**
 * Test method for
 * {@link com.amalto.workbench.utils.Util#getComplexTypeDefinitionChildren(org.eclipse.xsd.XSDComplexTypeDefinition, boolean)}
 * .
 */
@Test
public void testGetComplexTypeDefinitionChildren() throws Exception {
    // get test model
    XSDSchema xsdSchema = getXSDSchema();
    EList<XSDElementDeclaration> elementDeclarations = xsdSchema.getElementDeclarations();
    // test
    assertEquals(1, elementDeclarations.size());
    XSDTypeDefinition typeDefinition = elementDeclarations.get(0).getTypeDefinition();
    ArrayList<Object> children = Util.getComplexTypeDefinitionChildren((XSDComplexTypeDefinition) typeDefinition, true);
    assertEquals(3, children.size());
    children = Util.getComplexTypeDefinitionChildren((XSDComplexTypeDefinition) typeDefinition, false);
    assertEquals(1, children.size());
}
Also used : XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) Test(org.junit.Test)

Example 34 with XSDTypeDefinition

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

the class XSDUtilTest method testIsFirstLevelChild.

@Test
public void testIsFirstLevelChild() throws Exception {
    // $NON-NLS-1$
    String fileName = "Product_0.1.xsd";
    // $NON-NLS-1$
    String elementName = "Product";
    String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
    assertNotNull(xsdString);
    XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
    XSDElementDeclaration decl = null;
    for (XSDElementDeclaration element : xsdSchema.getElementDeclarations()) {
        if (element.getName().equals(elementName)) {
            decl = element;
        }
    }
    if (decl != null) {
        XSDComplexTypeDefinition ctypeDefinition = (XSDComplexTypeDefinition) decl.getTypeDefinition();
        XSDComplexTypeContent content = ctypeDefinition.getContent();
        if (content instanceof XSDParticle) {
            XSDParticle xsdParticle = (XSDParticle) content;
            XSDParticleContent particleContent = xsdParticle.getContent();
            if (particleContent instanceof XSDModelGroup) {
                XSDModelGroup modelGroup = (XSDModelGroup) particleContent;
                EList<XSDParticle> contents = modelGroup.getContents();
                for (XSDParticle particle : contents) {
                    assertTrue(XSDUtil.isFirstLevelChild(particle));
                    if (particle.getTerm() instanceof XSDElementDeclaration) {
                        XSDElementDeclaration xsdElementDecl = (XSDElementDeclaration) particle.getTerm();
                        XSDTypeDefinition typeDefinition = xsdElementDecl.getTypeDefinition();
                        if (typeDefinition instanceof XSDComplexTypeDefinition) {
                            XSDParticle childPart = (XSDParticle) ((XSDComplexTypeDefinition) typeDefinition).getContent();
                            XSDModelGroup childModelGroup = (XSDModelGroup) childPart.getContent();
                            for (XSDParticle childParticle : childModelGroup.getContents()) {
                                assertFalse(XSDUtil.isFirstLevelChild(childParticle));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticleContent(org.eclipse.xsd.XSDParticleContent) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) Test(org.junit.Test)

Example 35 with XSDTypeDefinition

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

the class UtilMockTest method testGetComplexChilds.

@Test
public void testGetComplexChilds() {
    // $NON-NLS-1$
    String methodToExecute = "getComplexChilds";
    // $NON-NLS-1$
    String method_getChildElements = "getChildElements";
    // $NON-NLS-1$
    String parentxpath = "";
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    String[] names = { "simpleA", "complexB", "simpleC", "complexD" };
    // $NON-NLS-1$ //$NON-NLS-2$
    String[] childpath = { "child/aa", "child/bb" };
    boolean onlyTopLevel = false;
    Set<Object> visited = new HashSet<Object>();
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDComplexTypeDefinition complexTypeDef = factory.createXSDComplexTypeDefinition();
    PowerMockito.mockStatic(Util.class);
    try {
        PowerMockito.when(Util.class, methodToExecute, anyString(), any(XSDComplexTypeDefinition.class), anyBoolean(), anySet()).thenCallRealMethod();
        Map<String, XSDParticle> complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, true, null);
        assertNotNull(complexChilds);
        assertTrue(complexChilds.isEmpty());
        // 
        Map<String, XSDParticle> childElements1 = new HashMap<String, XSDParticle>();
        XSDParticle childParticle1 = factory.createXSDParticle();
        XSDParticle childParticle2 = factory.createXSDParticle();
        childElements1.put(childpath[0], childParticle1);
        childElements1.put(childpath[1], childParticle2);
        PowerMockito.when(Util.class, method_getChildElements, anyString(), any(XSDComplexTypeDefinition.class), anyBoolean(), anySet()).thenReturn(childElements1);
        XSDModelGroup group = factory.createXSDModelGroup();
        for (int i = 0; i < names.length; i++) {
            XSDParticle particle = factory.createXSDParticle();
            XSDElementDeclaration elementDecl = factory.createXSDElementDeclaration();
            XSDTypeDefinition xsdType = factory.createXSDSimpleTypeDefinition();
            if (i % 2 != 0) {
                xsdType = factory.createXSDComplexTypeDefinition();
            }
            elementDecl.setTypeDefinition(xsdType);
            elementDecl.setName(names[i]);
            particle.setTerm(elementDecl);
            group.getParticles().add(particle);
        }
        XSDParticle typeParticle = factory.createXSDParticle();
        typeParticle.setTerm(group);
        complexTypeDef.setContent(typeParticle);
        complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("simpleA"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("simpleC"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("//complexB"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("//complexD"));
        if (complexChilds.size() == 6) {
            assertTrue(complexChilds.keySet().contains(childpath[0]));
            assertTrue(complexChilds.keySet().contains(childpath[1]));
        }
        onlyTopLevel = true;
        complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("simpleA"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("simpleC"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("//complexB"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("//complexD"));
        // 
        // $NON-NLS-1$
        parentxpath = "parentXPath";
        onlyTopLevel = false;
        visited.clear();
        complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath/simpleA"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath/simpleC"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath//complexB"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath//complexD"));
        if (complexChilds.size() == 6) {
            assertTrue(complexChilds.keySet().contains(childpath[0]));
            assertTrue(complexChilds.keySet().contains(childpath[1]));
        }
        // 
        onlyTopLevel = true;
        visited.clear();
        complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath/simpleA"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath/simpleC"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath//complexB"));
        // $NON-NLS-1$
        assertTrue(complexChilds.keySet().contains("parentXPath//complexD"));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) HashMap(java.util.HashMap) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)119 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)66 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)57 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)46 ArrayList (java.util.ArrayList)39 XSDParticle (org.eclipse.xsd.XSDParticle)36 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)32 Iterator (java.util.Iterator)26 XSDSchema (org.eclipse.xsd.XSDSchema)26 List (java.util.List)22 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)17 EList (org.eclipse.emf.common.util.EList)16 TreeObject (com.amalto.workbench.models.TreeObject)14 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)14 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)13 XSDTerm (org.eclipse.xsd.XSDTerm)13 XSDFactory (org.eclipse.xsd.XSDFactory)12 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)11 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)10 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)10