Search in sources :

Example 71 with XSDModelGroup

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

the class Util method retrieveXSDComponentPath.

public static List<String> retrieveXSDComponentPath(Object component, XSDSchema schema, List<String> buffer) {
    String name = null;
    if (component instanceof XSDElementDeclaration) {
        XSDElementDeclaration decl = (XSDElementDeclaration) component;
        name = decl.getName();
        // $NON-NLS-1$//$NON-NLS-2$
        buffer.add("//xsd:element[@name='" + name + "']");
        if (decl.getContainer() instanceof XSDSchemaImpl) {
            return buffer;
        } else {
            return retrieveXSDComponentPath(decl.getContainer(), schema, buffer);
        }
    } else if (component instanceof XSDParticle) {
        XSDParticle particle = (XSDParticle) component;
        XSDTerm term = particle.getTerm();
        if (term instanceof XSDElementDeclaration && !(((XSDElementDeclaration) term).getContainer() instanceof XSDParticle)) {
            String prefix = null;
            String ns = ((XSDElementDeclaration) term).getTargetNamespace();
            Iterator<Map.Entry<String, String>> iter = schema.getQNamePrefixToNamespaceMap().entrySet().iterator();
            while (iter.hasNext() && ns != null) {
                Map.Entry<String, String> entry = iter.next();
                if (entry.getValue().equals(ns)) {
                    prefix = entry.getKey();
                }
            }
            name = ((XSDElementDeclaration) term).getName();
            buffer.add(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
            "//xsd:element[@name='" + name + "' or @ref='" + (prefix != null ? (prefix + ":" + name) : name) + "']");
            return retrieveXSDComponentPath(particle.getContainer(), schema, buffer);
        } else {
            retrieveXSDComponentPath(particle.getContainer(), schema, buffer);
        }
    } else if (component instanceof XSDComplexTypeDefinition) {
        XSDComplexTypeDefinition type = (XSDComplexTypeDefinition) component;
        name = type.getName();
        // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
        buffer.add("//xsd:complexType" + (name != null ? "[@name='" + name + "']" : ""));
        if (type.getContainer() instanceof XSDSchemaImpl) {
            return buffer;
        }
        return retrieveXSDComponentPath(type.getContainer(), schema, buffer);
    } else if (component instanceof XSDSimpleTypeDefinition) {
        // TreePath tPath=((TreeSelection)selection).getPaths()[0];
        // Object elem = tPath.getSegment(0);
        // return retrieveXSDComponentPath(elem, schema, buffer, selection);
        String typeName = ((XSDSimpleTypeDefinition) component).getName();
        // $NON-NLS-1$//$NON-NLS-2$
        buffer.add("//xsd:simpleType[@name='" + typeName + "']");
        return buffer;
    } else if (component instanceof XSDModelGroup) {
        XSDModelGroup group = (XSDModelGroup) component;
        String literal = group.getCompositor().getLiteral();
        // $NON-NLS-1$
        buffer.add("//xsd:" + literal);
        return retrieveXSDComponentPath(group.getContainer(), schema, buffer);
    } else if (component instanceof XSDIdentityConstraintDefinition) {
        XSDIdentityConstraintDefinition identify = (XSDIdentityConstraintDefinition) component;
        XSDConcreteComponent c = identify.getContainer();
        // $NON-NLS-1$//$NON-NLS-2$
        buffer.add("//xsd:unique[@name='" + identify.getName() + "']");
        return retrieveXSDComponentPath(c, schema, buffer);
    } else if (component instanceof XSDXPathDefinition) {
        XSDXPathDefinition path = (XSDXPathDefinition) component;
        // $NON-NLS-1$//$NON-NLS-2$
        buffer.add("//xsd:field[@xpath='" + path.getValue() + "']");
        return retrieveXSDComponentPath(path.getContainer(), schema, buffer);
    } else if (component instanceof XSDAnnotation) {
        XSDAnnotation annon = (XSDAnnotation) component;
        // $NON-NLS-1$
        buffer.add("//xsd:annotation");
        return retrieveXSDComponentPath(annon.getContainer(), schema, buffer);
    } else {
        return buffer;
    }
    return buffer;
}
Also used : XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) JarEntry(java.util.jar.JarEntry) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSchemaImpl(org.eclipse.xsd.impl.XSDSchemaImpl) Iterator(java.util.Iterator) XSDTerm(org.eclipse.xsd.XSDTerm) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultiKeyMap(org.apache.commons.collections.map.MultiKeyMap) HashMap(java.util.HashMap) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 72 with XSDModelGroup

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

the class Util method isReferrenced.

private static boolean isReferrenced(XSDElementDeclaration element, XSDElementDeclaration term) {
    if (element == term) {
        return true;
    }
    if (term.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
        XSDComplexTypeContent fromcomplexType = ((XSDComplexTypeDefinition) term.getTypeDefinition()).getContent();
        if (fromcomplexType instanceof XSDParticle) {
            XSDParticle particle = (XSDParticle) fromcomplexType;
            if (particle.getTerm() instanceof XSDModelGroup) {
                XSDModelGroup modelGroup = ((XSDModelGroup) particle.getTerm());
                EList<?> fromlist = modelGroup.getContents();
                for (Object obj : fromlist) {
                    XSDTerm t = ((XSDParticle) obj).getTerm();
                    if (t == element) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDTerm(org.eclipse.xsd.XSDTerm) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TreeObject(com.amalto.workbench.models.TreeObject) EObject(org.eclipse.emf.ecore.EObject) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 73 with XSDModelGroup

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

the class Util method getComplexTypeDefinitionChildren.

public static ArrayList<Object> getComplexTypeDefinitionChildren(XSDComplexTypeDefinition complexTypeDefinition, boolean ignoreRestriction) {
    XSDComplexTypeContent xsdComplexTypeContent = complexTypeDefinition.getContent();
    ArrayList<Object> list = new ArrayList<Object>();
    // Now determine whether ref. If ref look at the base Type definition
    XSDTypeDefinition baseTypeDefinition = complexTypeDefinition.getBaseTypeDefinition();
    if (xsdComplexTypeContent == null) {
        XSDTypeDefinition typeDefinition = baseTypeDefinition;
        // if a simple type return the simple type
        if (typeDefinition instanceof XSDSimpleTypeDefinition) {
            list.add(typeDefinition);
            return list;
        } else {
        }
        // it is a complex Type
        xsdComplexTypeContent = ((XSDComplexTypeDefinition) typeDefinition).getContent();
    }
    // check if we are extending a complex Definition
    if ("extension".equals(complexTypeDefinition.getDerivationMethod().getName())) {
        if (baseTypeDefinition instanceof XSDComplexTypeDefinition && baseTypeDefinition != complexTypeDefinition) {
            String name = ((XSDComplexTypeDefinition) baseTypeDefinition).getDerivationMethod().getName();
            if (name.equals("restriction") || ignoreRestriction) {
                // $NON-NLS-1$
                list.addAll(getComplexTypeDefinitionChildren((XSDComplexTypeDefinition) baseTypeDefinition, ignoreRestriction));
            // 
            }
        }
    }
    // Attributes
    if (complexTypeDefinition.getAttributeContents() != null) {
        list.addAll(complexTypeDefinition.getAttributeContents());
    }
    // Annotations
    if (complexTypeDefinition.getAnnotations() != null) {
        list.addAll(complexTypeDefinition.getAnnotations());
    }
    // simple type return the simple type
    if (xsdComplexTypeContent instanceof XSDSimpleTypeDefinition) {
        list.add(xsdComplexTypeContent);
        return list;
    }
    // xsd Particle: we have a model group
    if (xsdComplexTypeContent instanceof XSDParticle) {
        // log.info("Model Group?: "+((XSDParticle)xsdComplexTypeContent).getTerm());
        if (((XSDParticle) xsdComplexTypeContent).getTerm() instanceof XSDModelGroup) {
            // return the model group
            list.add(((XSDParticle) xsdComplexTypeContent).getTerm());
            return list;
        } else {
            // wild card or element declaration '?)
            list.add(((XSDParticle) xsdComplexTypeContent).getTerm());
            return list;
        }
    }
    // what else could it be ?
    list.add(xsdComplexTypeContent);
    return list;
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TreeObject(com.amalto.workbench.models.TreeObject) EObject(org.eclipse.emf.ecore.EObject) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 74 with XSDModelGroup

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

the class UtilTest method testGetParticleReferenceName.

@Test
public void testGetParticleReferenceName() {
    XSDComplexTypeDefinition complexType = (XSDComplexTypeDefinition) schema.getElementDeclarations().get(0).getTypeDefinition();
    XSDModelGroup group = (XSDModelGroup) ((XSDParticle) complexType.getContent()).getTerm();
    XSDParticle curXSDParticle = group.getParticles().get(0);
    String name = Util.getParticleReferenceName(curXSDParticle);
    // $NON-NLS-1$
    assertEquals(name, "");
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) Test(org.junit.Test)

Example 75 with XSDModelGroup

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

the class UtilTest method testGetforeignKeyOfElement.

@Test
public void testGetforeignKeyOfElement() {
    // $NON-NLS-1$
    String attKey = "source";
    // $NON-NLS-1$
    String attValue = "X_ForeignKey";
    // $NON-NLS-1$
    String namespaceURI = "http://www.w3.org/XML/1998/namespace";
    // $NON-NLS-1$
    String qualifiedName = "appinfo";
    Set<String> list = new HashSet<String>();
    try {
        Util.getforeignKeyOfElement(list, null);
        assertTrue(list.isEmpty());
        // 
        Document doc = getEmptyDocument();
        Element appinfoElement1 = doc.createElementNS(namespaceURI, qualifiedName);
        appinfoElement1.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appinfoElement1.appendChild(doc.createTextNode("StoreA/Id"));
        // $NON-NLS-1$
        Element appinfoElement2 = doc.createElementNS(namespaceURI, "appinfosssss");
        appinfoElement2.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appinfoElement2.appendChild(doc.createTextNode("StoreB/Id"));
        XSDAnnotation xsdAnnotation = XSDFactory.eINSTANCE.createXSDAnnotation();
        EList<Element> applicationInformations = xsdAnnotation.getApplicationInformation();
        applicationInformations.add(appinfoElement1);
        applicationInformations.add(appinfoElement2);
        // prepare a
        XSDAnnotation xsdAnnotation_a = XSDFactory.eINSTANCE.createXSDAnnotation();
        Element appElement_a = doc.createElementNS(namespaceURI, qualifiedName);
        // $NON-NLS-1$
        appElement_a.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appElement_a.appendChild(doc.createTextNode("StoreC/Id"));
        xsdAnnotation_a.getApplicationInformation().add(appElement_a);
        XSDElementDeclaration xsdElementDeclaration_a = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        xsdElementDeclaration_a.setAnnotation(xsdAnnotation_a);
        XSDParticle xsdParticle_a = XSDFactory.eINSTANCE.createXSDParticle();
        xsdParticle_a.setTerm(xsdElementDeclaration_a);
        // prepare b
        XSDAnnotation xsdAnnotation_b = XSDFactory.eINSTANCE.createXSDAnnotation();
        Element appElement_b = doc.createElementNS(namespaceURI, qualifiedName);
        // $NON-NLS-1$
        appElement_b.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appElement_b.appendChild(doc.createTextNode("StoreD/Id"));
        xsdAnnotation_b.getApplicationInformation().add(appElement_b);
        XSDElementDeclaration xsdElementDeclaration_b = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        xsdElementDeclaration_b.setAnnotation(xsdAnnotation_b);
        XSDParticle xsdParticle_b = XSDFactory.eINSTANCE.createXSDParticle();
        xsdParticle_b.setTerm(xsdElementDeclaration_b);
        // prepare c
        XSDAnnotation xsdAnnotation_c = XSDFactory.eINSTANCE.createXSDAnnotation();
        Element appElement_c = doc.createElementNS(namespaceURI, qualifiedName);
        // $NON-NLS-1$
        appElement_c.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appElement_c.appendChild(doc.createTextNode("StoreE/Id"));
        xsdAnnotation_c.getApplicationInformation().add(appElement_c);
        XSDElementDeclaration xsdElementDeclaration_c = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        xsdElementDeclaration_c.setAnnotation(xsdAnnotation_c);
        XSDComplexTypeDefinition xsdComplexTypeDefinition_c = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
        xsdElementDeclaration_c.setTypeDefinition(xsdComplexTypeDefinition_c);
        XSDParticle xsdParticle_c_1 = XSDFactory.eINSTANCE.createXSDParticle();
        xsdComplexTypeDefinition_c.setContent(xsdParticle_c_1);
        XSDModelGroup xsdModelGroup_c_1 = XSDFactory.eINSTANCE.createXSDModelGroup();
        xsdParticle_c_1.setTerm(xsdModelGroup_c_1);
        XSDParticle xsdParticle_c_2 = XSDFactory.eINSTANCE.createXSDParticle();
        xsdModelGroup_c_1.getContents().add(xsdParticle_c_2);
        XSDElementDeclaration xsdElementDeclaration_c_2 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        xsdParticle_c_2.setTerm(xsdElementDeclaration_c_2);
        XSDAnnotation xsdAnnotation_c_2 = XSDFactory.eINSTANCE.createXSDAnnotation();
        Element appElement_c_2 = doc.createElementNS(namespaceURI, qualifiedName);
        // $NON-NLS-1$
        appElement_c_2.setAttribute(attKey, attValue);
        // $NON-NLS-1$
        appElement_c_2.appendChild(doc.createTextNode("StoreF/Id"));
        xsdAnnotation_c_2.getApplicationInformation().add(appElement_c_2);
        xsdElementDeclaration_c_2.setAnnotation(xsdAnnotation_c_2);
        XSDParticle xsdParticle_c = XSDFactory.eINSTANCE.createXSDParticle();
        xsdParticle_c.setTerm(xsdElementDeclaration_c);
        XSDModelGroup xsdModelGroup_a = XSDFactory.eINSTANCE.createXSDModelGroup();
        xsdModelGroup_a.getContents().add(xsdParticle_a);
        // referecened
        xsdModelGroup_a.getContents().add(xsdParticle_b);
        xsdModelGroup_a.getContents().add(xsdParticle_c);
        XSDParticle xsdParticle_child = XSDFactory.eINSTANCE.createXSDParticle();
        xsdParticle_child.setTerm(xsdModelGroup_a);
        XSDComplexTypeDefinition xsdComplexTypeDefinition = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
        xsdComplexTypeDefinition.setContent(xsdParticle_child);
        XSDElementDeclaration xsdElementDeclaration = xsdElementDeclaration_b;
        xsdElementDeclaration.setAnnotation(xsdAnnotation);
        xsdElementDeclaration.setTypeDefinition(xsdComplexTypeDefinition);
        // run and check
        Util.getforeignKeyOfElement(list, xsdElementDeclaration);
        assertTrue(list.size() == 4);
        // $NON-NLS-1$
        assertTrue(list.contains("StoreA"));
        // $NON-NLS-1$
        assertTrue(list.contains("StoreC"));
        // $NON-NLS-1$
        assertTrue(list.contains("StoreE"));
        // $NON-NLS-1$
        assertTrue(list.contains("StoreF"));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) Element(org.w3c.dom.Element) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Document(org.w3c.dom.Document) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

XSDModelGroup (org.eclipse.xsd.XSDModelGroup)119 XSDParticle (org.eclipse.xsd.XSDParticle)93 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)87 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)76 ArrayList (java.util.ArrayList)33 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)32 XSDTerm (org.eclipse.xsd.XSDTerm)28 XSDFactory (org.eclipse.xsd.XSDFactory)26 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)26 Test (org.junit.Test)25 XSDSchema (org.eclipse.xsd.XSDSchema)23 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)22 EList (org.eclipse.emf.common.util.EList)21 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)20 XSDModelGroupDefinition (org.eclipse.xsd.XSDModelGroupDefinition)19 List (java.util.List)18 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)18 Iterator (java.util.Iterator)16 XSDWildcard (org.eclipse.xsd.XSDWildcard)16 TreeObject (com.amalto.workbench.models.TreeObject)15