Search in sources :

Example 41 with XSDParticle

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

the class UtilTest method testGetRealKeyInfos.

@Test
public void testGetRealKeyInfos() throws Exception {
    XSDSchema xsdSchema = createSchema();
    XSDElementDeclaration elementDeclaration = null;
    EList<XSDElementDeclaration> declarations = xsdSchema.getElementDeclarations();
    for (XSDElementDeclaration xed : declarations) {
        if (xed.getName().equals("opo")) {
            // $NON-NLS-1$
            elementDeclaration = xed;
            break;
        }
    }
    if (elementDeclaration != null) {
        XSDComplexTypeDefinition childType = (XSDComplexTypeDefinition) elementDeclaration.getType();
        childType = (XSDComplexTypeDefinition) childType.getBaseTypeDefinition();
        childType = (XSDComplexTypeDefinition) childType.getBaseTypeDefinition();
        XSDParticle xsdParticle = (XSDParticle) childType.getContent();
        XSDModelGroup modelGroup = (XSDModelGroup) xsdParticle.getTerm();
        EList<XSDParticle> particles = modelGroup.getParticles();
        XSDParticle primaryKeyParticle = null;
        XSDParticle nonePrimaryKeyParticle = null;
        for (XSDParticle particle : particles) {
            XSDElementDeclaration term = (XSDElementDeclaration) particle.getTerm();
            if (term.getName().equals("Id")) {
                // $NON-NLS-1$
                primaryKeyParticle = particle;
                break;
            } else {
                nonePrimaryKeyParticle = particle;
            }
        }
        if (nonePrimaryKeyParticle == null) {
            nonePrimaryKeyParticle = particles.get(particles.size() - 1);
        }
        assertNull(Util.getRealKeyInfos(null, null));
        assertNull(Util.getRealKeyInfos(elementDeclaration, null));
        assertNull(Util.getRealKeyInfos(null, primaryKeyParticle));
        assertEquals(Util.getRealKeyInfos(elementDeclaration, nonePrimaryKeyParticle).size(), 0);
        List<Object> realKeyInfos = Util.getRealKeyInfos(elementDeclaration, primaryKeyParticle);
        assertNotNull(realKeyInfos);
        assertEquals(realKeyInfos.size(), 2);
        assertTrue((realKeyInfos.get(0) instanceof XSDIdentityConstraintDefinition));
        assertTrue((realKeyInfos.get(1) instanceof XSDXPathDefinition));
        // $NON-NLS-1$
        assertEquals(((XSDXPathDefinition) realKeyInfos.get(1)).getValue(), "Id");
    }
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) Test(org.junit.Test)

Example 42 with XSDParticle

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

the class UtilTest method testGetTopElement.

@Test
public void testGetTopElement() {
    try {
        XSDFactory factory = XSDFactory.eINSTANCE;
        // 
        XSDElementDeclaration concept = factory.createXSDElementDeclaration();
        // concept with three children
        // $NON-NLS-1$
        String element1 = "Id";
        // $NON-NLS-1$
        String element2 = "code";
        // $NON-NLS-1$
        String element3 = "address";
        XSDSchema xsdSchema = factory.createXSDSchema();
        xsdSchema.getContents().add(concept);
        XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
        xsdComplexTypeDef.setBaseTypeDefinition(// $NON-NLS-1$
        xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
        xsdSchema.getContents().add(xsdComplexTypeDef);
        XSDParticle xsdParticle = factory.createXSDParticle();
        xsdComplexTypeDef.setContent(xsdParticle);
        XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
        xsdParticle.setContent(xsdModelGroup);
        concept.setAnonymousTypeDefinition(xsdComplexTypeDef);
        XSDParticle childParticle1 = factory.createXSDParticle();
        XSDElementDeclaration childElement1 = factory.createXSDElementDeclaration();
        childElement1.setName(element1);
        childParticle1.setContent(childElement1);
        XSDParticle childParticle2 = factory.createXSDParticle();
        XSDElementDeclaration childElement2 = factory.createXSDElementDeclaration();
        childElement2.setName(element2);
        childParticle2.setContent(childElement2);
        XSDParticle childParticle3 = factory.createXSDParticle();
        XSDElementDeclaration childElement3 = factory.createXSDElementDeclaration();
        childElement3.setName(element3);
        childParticle3.setContent(childElement3);
        xsdModelGroup.getContents().add(childParticle1);
        xsdModelGroup.getContents().add(childParticle2);
        xsdModelGroup.getContents().add(childParticle3);
        Object primaryKey = Util.getTopElement(concept, childElement1);
        assertEquals(element1, primaryKey);
        XSDElementDeclaration elementDecl = factory.createXSDElementDeclaration();
        elementDecl.setTypeDefinition(factory.createXSDSimpleTypeDefinition());
        primaryKey = Util.getTopElement(elementDecl, childElement1);
        assertNull(primaryKey);
        // 
        XSDComplexTypeDefinition xsdComplexTypeDef2 = factory.createXSDComplexTypeDefinition();
        xsdComplexTypeDef2.setBaseTypeDefinition(xsdComplexTypeDef);
        XSDParticle xsdParticle2 = factory.createXSDParticle();
        xsdComplexTypeDef2.setContent(xsdParticle2);
        XSDModelGroup xsdModelGroup2 = factory.createXSDModelGroup();
        xsdParticle2.setContent(xsdModelGroup2);
        XSDParticle childParticle_2 = factory.createXSDParticle();
        XSDElementDeclaration childElement_2 = factory.createXSDElementDeclaration();
        childElement_2.setName(element1);
        childParticle_2.setContent(childElement_2);
        xsdModelGroup2.getContents().add(childParticle_2);
        xsdSchema.getContents().add(xsdComplexTypeDef2);
        concept.setTypeDefinition(xsdComplexTypeDef2);
        primaryKey = Util.getTopElement(concept, childElement1);
        assertEquals(element1, primaryKey);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDSchema(org.eclipse.xsd.XSDSchema) Test(org.junit.Test)

Example 43 with XSDParticle

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

the class UtilTest method testGetTopParent.

@Test
public void testGetTopParent() {
    // 
    List<Object> topParent = Util.getTopParent(null);
    assertNull(topParent);
    XSDFactory factory = XSDFactory.eINSTANCE;
    // 
    XSDElementDeclaration concept = factory.createXSDElementDeclaration();
    topParent = Util.getTopParent(concept);
    assertNull(topParent);
    // concept with three children
    // $NON-NLS-1$
    String element1 = "Id";
    // $NON-NLS-1$
    String element2 = "code";
    // $NON-NLS-1$
    String element3 = "address";
    XSDSchema xsdSchema = factory.createXSDSchema();
    xsdSchema.getContents().add(concept);
    XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
    xsdComplexTypeDef.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    concept.setAnonymousTypeDefinition(xsdComplexTypeDef);
    XSDParticle xsdParticle = factory.createXSDParticle();
    xsdComplexTypeDef.setContent(xsdParticle);
    XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
    xsdParticle.setContent(xsdModelGroup);
    XSDParticle childParticle1 = factory.createXSDParticle();
    XSDElementDeclaration childElement1 = factory.createXSDElementDeclaration();
    childElement1.setName(element1);
    childParticle1.setContent(childElement1);
    XSDParticle childParticle2 = factory.createXSDParticle();
    XSDElementDeclaration childElement2 = factory.createXSDElementDeclaration();
    childElement2.setName(element2);
    childParticle2.setContent(childElement2);
    XSDParticle childParticle3 = factory.createXSDParticle();
    XSDElementDeclaration childElement3 = factory.createXSDElementDeclaration();
    childElement3.setName(element3);
    childParticle3.setContent(childElement3);
    xsdModelGroup.getContents().add(childParticle1);
    xsdModelGroup.getContents().add(childParticle2);
    xsdModelGroup.getContents().add(childParticle3);
    XSDIdentityConstraintDefinition xsdIdConsDef = factory.createXSDIdentityConstraintDefinition();
    concept.getIdentityConstraintDefinitions().add(xsdIdConsDef);
    XSDXPathDefinition xsdXPathDefinition1 = factory.createXSDXPathDefinition();
    xsdXPathDefinition1.setValue(element1);
    XSDXPathDefinition xsdXPathDefinition2 = factory.createXSDXPathDefinition();
    xsdXPathDefinition2.setValue(element2);
    xsdIdConsDef.getFields().add(xsdXPathDefinition1);
    xsdIdConsDef.getFields().add(xsdXPathDefinition2);
    // complex type with one child
    XSDComplexTypeDefinition xsdComplexTypeDef2 = factory.createXSDComplexTypeDefinition();
    xsdComplexTypeDef2.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    XSDParticle xsdParticle2 = factory.createXSDParticle();
    xsdComplexTypeDef2.setContent(xsdParticle2);
    XSDModelGroup xsdModelGroup2 = factory.createXSDModelGroup();
    xsdParticle2.setContent(xsdModelGroup2);
    XSDParticle childParticle_2 = factory.createXSDParticle();
    XSDElementDeclaration childElement_2 = factory.createXSDElementDeclaration();
    childElement_2.setName(element1);
    childParticle_2.setContent(childElement_2);
    xsdModelGroup2.getContents().add(childParticle_2);
    xsdSchema.getContents().add(xsdComplexTypeDef2);
    // 
    topParent = Util.getTopParent(childElement1);
    assertNotNull(topParent);
    assertTrue(topParent.size() == 2);
    // 
    topParent = Util.getTopParent(childElement_2);
    assertNull(topParent);
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) Test(org.junit.Test)

Example 44 with XSDParticle

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

the class UtilTest method testIsSimpleTypedParticle.

@Test
public void testIsSimpleTypedParticle() {
    boolean isSimpleTypedParticle = Util.isSimpleTypedParticle(null);
    assertFalse(isSimpleTypedParticle);
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDParticle particle = factory.createXSDParticle();
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertFalse(isSimpleTypedParticle);
    XSDElementDeclaration elementDeclaration = factory.createXSDElementDeclaration();
    elementDeclaration.setTypeDefinition(factory.createXSDSimpleTypeDefinition());
    particle.setContent(elementDeclaration);
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertTrue(isSimpleTypedParticle);
    XSDModelGroup modelGroup = factory.createXSDModelGroup();
    particle.setContent(modelGroup);
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertFalse(isSimpleTypedParticle);
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticle(org.eclipse.xsd.XSDParticle) Test(org.junit.Test)

Example 45 with XSDParticle

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

the class UtilTest method testGetChildElements4TypeArg.

@Test
public void testGetChildElements4TypeArg() throws Exception {
    EList<XSDElementDeclaration> xsdElementDeclarations = schema.getElementDeclarations();
    XSDElementDeclaration conceptEl = null;
    for (XSDElementDeclaration el : xsdElementDeclarations) {
        if (el.getName().equals("Entity")) {
            // $NON-NLS-1$
            conceptEl = el;
            break;
        }
    }
    assertNotNull(conceptEl);
    Map<String, XSDParticle> childElements = Util.getChildElements("", (XSDComplexTypeDefinition) conceptEl.getTypeDefinition(), false, // $NON-NLS-1$
    new HashSet<Object>());
    assertNotNull(childElements);
    assertEquals(7, childElements.size());
}
Also used : XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticle(org.eclipse.xsd.XSDParticle) Test(org.junit.Test)

Aggregations

XSDParticle (org.eclipse.xsd.XSDParticle)146 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)103 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)93 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)75 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)36 ArrayList (java.util.ArrayList)34 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)34 XSDTerm (org.eclipse.xsd.XSDTerm)31 XSDFactory (org.eclipse.xsd.XSDFactory)27 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)24 XSDSchema (org.eclipse.xsd.XSDSchema)24 Test (org.junit.Test)23 Iterator (java.util.Iterator)21 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)21 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)19 EList (org.eclipse.emf.common.util.EList)17 XSDParticleContent (org.eclipse.xsd.XSDParticleContent)17 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)16 Element (org.w3c.dom.Element)16 List (java.util.List)15