Search in sources :

Example 11 with XSDParticle

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

the class XSDParser method loadAndPrint.

/**
 * Load the XML Schema file and print the documentation based on it.
 *
 * @param xsdFile the name of an XML Schema file.
 */
public void loadAndPrint(String xsd) throws Exception {
    // XSDFactory xsdFactory = XSDFactory.eINSTANCE;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    InputSource source = new InputSource(new StringReader(xsd));
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);
    XSDSchema xsdSchema = XSDSchemaImpl.createSchema(document.getDocumentElement());
    // Create a resource set and load the main schema file into it.
    // 
    /*
         * ResourceSet resourceSet = new ResourceSetImpl(); XSDResourceImpl xsdResource = (XSDResourceImpl) resourceSet
         * .getResource(URI.createFileURI(xsdFile), true);
         * 
         * XSDResourceImpl res = new XSDResourceImpl();
         * 
         * XSDSchema xsdSchema = xsdResource.getSchema();
         */
    // $NON-NLS-1$
    String elementContentHeaderDocumentation = getContentDocumentation("element-header");
    if (elementContentHeaderDocumentation != null) {
        System.out.println(elementContentHeaderDocumentation);
    }
    List all = new ArrayList(xsdSchema.getElementDeclarations());
    for (Iterator iter = all.iterator(); iter.hasNext(); ) {
        XSDElementDeclaration concept = (XSDElementDeclaration) iter.next();
        // $NON-NLS-1$ //$NON-NLS-2$
        System.out.println("CONCEPT: " + concept.getName() + "-- " + concept.getContainer().getClass().getName());
        // name of the concept in English
        XSDAnnotation annotation = concept.getAnnotation();
        if (annotation != null) {
            EList list = annotation.getApplicationInformation();
            for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
                Element appinfo = (Element) iterator.next();
                // $NON-NLS-1$
                String language = appinfo.getAttribute("source");
                if ("EN".equals(language.toUpperCase())) {
                    // $NON-NLS-1$
                    if (appinfo.getFirstChild() != null)
                        // $NON-NLS-1$
                        System.out.println("     " + appinfo.getFirstChild().getNodeValue());
                }
            }
        }
        // children
        XSDTypeDefinition typedef = concept.getTypeDefinition();
        if (typedef instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeContent xsdContent = ((XSDComplexTypeDefinition) typedef).getContent();
            XSDParticle xsdParticle = (XSDParticle) xsdContent;
            // $NON-NLS-1$
            String ident = "    ";
            processParticle(xsdParticle, ident);
        }
    }
// for elements
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) EList(org.eclipse.emf.common.util.EList) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 12 with XSDParticle

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

the class XSDUtil method iterateParticle.

private static void iterateParticle(XSDParticle particle, IXPathSelectionFilter filter, Set<String> paths, String prefix) {
    XSDTerm term = particle.getTerm();
    if (term instanceof XSDModelGroup) {
        EList<XSDParticle> contents = ((XSDModelGroup) term).getContents();
        for (XSDParticle p : contents) {
            XSDTerm t = p.getTerm();
            if (t instanceof XSDElementDeclaration) {
                XSDElementDeclaration element = ((XSDElementDeclaration) t);
                FilterResult r = filter.check(p);
                if (r == FilterResult.ENABLE) {
                    String path = prefix + element.getName();
                    paths.add(path);
                } else {
                    XSDTypeDefinition type = element.getType();
                    if (type instanceof XSDComplexTypeDefinition) {
                        String nextPrefix = prefix + element.getName() + DIVIDE;
                        XSDParticle cp = type.getComplexType();
                        iterateParticle(cp, filter, paths, nextPrefix);
                    }
                }
            }
        }
    }
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDTerm(org.eclipse.xsd.XSDTerm) FilterResult(com.amalto.workbench.dialogs.datamodel.IXPathSelectionFilter.FilterResult) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 13 with XSDParticle

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

the class XSDUtil method getValidElementPaths.

public static Set<String> getValidElementPaths(XSDElementDeclaration ed, IXPathSelectionFilter filter) {
    String curPrefix = ed.getName() + DIVIDE;
    XSDTypeDefinition type = ed.getType();
    Set<String> paths = new HashSet<String>();
    if (type instanceof XSDComplexTypeDefinition) {
        XSDParticle particle = type.getComplexType();
        iterateParticle(particle, filter, paths, curPrefix);
    }
    return paths;
}
Also used : XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) HashSet(java.util.HashSet)

Example 14 with XSDParticle

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

the class Util method getAllObject.

public static Object[] getAllObject(Object elem, List<Object> objList, IStructuredContentProvider provider) {
    Object[] elems = provider.getElements(elem);
    for (Object obj : elems) {
        if (obj == null) {
            continue;
        }
        if (obj instanceof XSDModelGroup || obj instanceof XSDElementDeclaration || obj instanceof XSDParticle || obj instanceof XSDTypeDefinition) {
            if (!objList.contains(obj)) {
                objList.add(obj);
                getAllObject(obj, objList, provider);
            }
        }
    }
    return objList.toArray();
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TreeObject(com.amalto.workbench.models.TreeObject) EObject(org.eclipse.emf.ecore.EObject) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 15 with XSDParticle

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

the class Util method getComplexChilds.

/**
 * use the map from path to XSDParticle,path is separated by '/' or '//'
 */
private static Map<String, XSDParticle> getComplexChilds(String parentxpath, XSDComplexTypeDefinition ctype, boolean onlyTopLevel, final Set<Object> visited) throws Exception {
    Map<String, XSDParticle> childDecls = new HashMap<String, XSDParticle>();
    if (ctype.getContent() instanceof XSDParticle) {
        XSDParticleImpl particle = (XSDParticleImpl) ctype.getContent();
        if (particle.getTerm() instanceof XSDModelGroup) {
            XSDModelGroup group = (XSDModelGroup) particle.getTerm();
            EList<XSDParticle> particles = group.getParticles();
            for (XSDParticle part : particles) {
                if (part.getTerm() instanceof XSDElementDeclaration) {
                    XSDElementDeclaration el = (XSDElementDeclaration) part.getTerm();
                    if (el.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
                        // $NON-NLS-1$
                        String child = parentxpath.length() == 0 ? el.getName() : parentxpath + "/" + el.getName();
                        childDecls.put(child, part);
                    } else {
                        String complexTypeChildPath = parentxpath.length() == 0 ? "//" + el.getName() : // $NON-NLS-1$
                        parentxpath + "//" + // $NON-NLS-1$
                        el.getName();
                        childDecls.put(complexTypeChildPath, part);
                        if (!onlyTopLevel && el.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                            // $NON-NLS-1$
                            String child = parentxpath.length() == 0 ? el.getName() : parentxpath + "/" + el.getName();
                            childDecls.putAll(getChildElements(child, (XSDComplexTypeDefinition) el.getTypeDefinition(), onlyTopLevel, visited));
                        }
                    }
                }
            }
        }
    }
    return childDecls;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDParticleImpl(org.eclipse.xsd.impl.XSDParticleImpl) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

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