Search in sources :

Example 81 with XSDTypeDefinition

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

the class XSDParser method main.

/**
 * Print a particle with markup for the document.
 *
 * @param xsdParticle a particle.
 * @param rootElementDeclarationMarkup the markup.
 */
/*
     * public void printParticle(XSDParticle xsdParticle, String rootElementDeclarationMarkup) { int minOccurs =
     * xsdParticle.getMinOccurs(); int maxOccurs = xsdParticle.getMaxOccurs(); XSDTerm xsdTerm = xsdParticle.getTerm();
     * if (xsdTerm instanceof XSDElementDeclaration) { XSDElementDeclaration xsdElementDeclaration =
     * (XSDElementDeclaration) xsdTerm; String elementDeclarationName = xsdElementDeclaration.getName(); String
     * elementDeclarationMarkup = null; if (rootElementDeclarationMarkup == null) { elementDeclarationMarkup =
     * getElementDeclarationMarkup(elementDeclarationName); } if (elementDeclarationMarkup != null) {
     * System.out.print("<span class='"); System.out.print(elementDeclarationMarkup); System.out.print("'>"); }
     * System.out.print("<a href='#" + getLocalAnchor(xsdElementDeclaration) + "'>");
     * System.out.print(elementDeclarationName.charAt(0)); System.out.print("</a>");
     * System.out.print(elementDeclarationName.substring(1)); if (elementDeclarationMarkup != null) {
     * System.out.print("</span>"); } if (minOccurs == 0) { if (maxOccurs == 1) { System.out.print("?"); } else {
     * System.out.print("*"); } } else if (maxOccurs == -1) { System.out.print("+"); } } else if (xsdTerm instanceof
     * XSDModelGroup) { XSDModelGroup xsdModelGroup = (XSDModelGroup) xsdTerm; List particles =
     * xsdModelGroup.getParticles(); boolean isRedundant = particles.size() == 1 && minOccurs == 1 && maxOccurs == 1 &&
     * ((XSDParticle) particles.get(0)).getTerm() instanceof XSDModelGroup; if (!isRedundant) { System.out.print("(");
     * // ) }
     * 
     * String separator = XSDCompositor.CHOICE_LITERAL == xsdModelGroup .getCompositor() ? " | " :
     * XSDCompositor.SEQUENCE_LITERAL == xsdModelGroup .getCompositor() ? ",  " : "  &  ";
     * 
     * for (Iterator i = xsdModelGroup.getParticles().iterator(); i .hasNext();) { XSDParticle childParticle =
     * (XSDParticle) i.next(); printParticle(childParticle, rootElementDeclarationMarkup); if (i.hasNext()) {
     * System.out.print(separator); } }
     * 
     * if (!isRedundant) { // ( System.out.print(")"); if (minOccurs == 0) { if (maxOccurs == 1) {
     * System.out.print("?"); } else { System.out.print("*"); } } else if (maxOccurs == -1) { System.out.print("+"); } }
     * } else if (xsdTerm instanceof XSDWildcard) { System.out.print("<em>{any}</em>"); } }
     */
/**
 * Print a simple type definition for the document.
 *
 * @param xsdSimpleTypeDefinition a simple type definition in the schema for schema.
 */
/*
     * public void printSimpleTypeDefinition( XSDSimpleTypeDefinition xsdSimpleTypeDefinition) { if
     * (xsdSimpleTypeDefinition == null) { } else if (xsdSimpleTypeDefinition.getEffectiveEnumerationFacet() != null) {
     * List value = xsdSimpleTypeDefinition.getEffectiveEnumerationFacet() .getValue(); if (value.size() > 1) {
     * System.out.print("("); } for (Iterator enumerators = value.iterator(); enumerators.hasNext();) { String
     * enumerator = enumerators.next().toString(); System.out.print("<em>"); System.out.print(enumerator);
     * System.out.print("</em>"); if (enumerators.hasNext()) { System.out.print("&nbsp;|&nbsp;"); } } if (value.size() >
     * 1) { System.out.print(")"); } } else if (xsdSimpleTypeDefinition.getElement() != null &&
     * xsdSimpleTypeDefinition.getElement().hasAttribute( XSDConstants.ID_ATTRIBUTE)) { System.out.print("<a href='#" +
     * xsdSimpleTypeDefinition.getName() + "-simple-type'>"); System.out.print(xsdSimpleTypeDefinition.getName());
     * System.out.print("</a>"); } else if (XSDVariety.UNION_LITERAL == xsdSimpleTypeDefinition .getVariety()) {
     * System.out.print("("); for (Iterator members = xsdSimpleTypeDefinition .getMemberTypeDefinitions().iterator();
     * members.hasNext();) { XSDSimpleTypeDefinition memberTypeDefinition = (XSDSimpleTypeDefinition) members .next();
     * printSimpleTypeDefinition(memberTypeDefinition); if (members.hasNext()) { System.out.print("&nbsp;|&nbsp;"); } }
     * System.out.print(")"); } else if (XSDVariety.UNION_LITERAL == xsdSimpleTypeDefinition .getVariety()) {
     * System.out.print("List&nbsp;of&nbsp;"); printSimpleTypeDefinition(xsdSimpleTypeDefinition
     * .getItemTypeDefinition()); } else if (xsdSimpleTypeDefinition.getName() != null) { if
     * ("public".equals(xsdSimpleTypeDefinition.getName())) { System.out.print("<a target='Part2' href='" +
     * XSDConstants.PART2 + "#anyURI'>anyURI</a>&nbsp;&nbsp;"); System.out.print("<a target='Errata' href='" + errata +
     * "#pfipublic'><em>public</em></a>"); } else { System.out.print("<b><em>");
     * System.out.print(xsdSimpleTypeDefinition.getName()); System.out.print("</em></b>"); } } else if
     * (xsdSimpleTypeDefinition.getEffectivePatternFacet() != null) { //
     * System.out.print(xsdSimpleTypeDefinition.getEffectivePatternFacet().getLexicalValue());
     * 
     * System.out.print("<em>"); System.out.print("<a target='Part1' href='" + XSDConstants.PART1 +
     * "#coss-identity-constraint'>"); System.out.print("a restricted xpath expression"); System.out.print("</a>");
     * System.out.print("</em>"); } else { System.out.print("***"); } }
     */
public static void main(String[] args) {
    try {
        /*
             * String xsd = ""; FileInputStream fis = new FileInputStream(
             * "/home/bgrieder/workspace/XCBL35/XCBL35.xsd"); BufferedReader br = new BufferedReader(new
             * InputStreamReader(fis, "utf-8")); String line; while ((line = br.readLine()) != null) xsd += line + "\n";
             * 
             * XSDParser parser = new XSDParser(); parser.loadAndPrint(xsd);
             */
        // $NON-NLS-1$
        FileWriter fw = new FileWriter("/tmp/xcb35sr.xsd");
        // $NON-NLS-1$
        Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl());
        // $NON-NLS-1$
        String xsdFile = "/home/bgrieder/workspace/XCBL35/XCBL35.xsd";
        ResourceSet resourceSet = new ResourceSetImpl();
        XSDResourceImpl xsdResource = (XSDResourceImpl) resourceSet.getResource(URI.createFileURI(xsdFile), true);
        /*
             * XSDResourceImpl res = new XSDResourceImpl(URI.createFileURI(xsdFile));
             */
        XSDSchema xsdSchema = xsdResource.getSchema();
        String header = // $NON-NLS-1$ //$NON-NLS-2$
        "<xsd:schema " + "elementFormDefault=\"qualified\" " + // $NON-NLS-1$
        "targetNamespace=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\" " + "xmlns=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\" " + // $NON-NLS-1$ //$NON-NLS-2$
        "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
        fw.write(header);
        Iterator it = xsdSchema.getElementDeclarations().iterator();
        for (; it.hasNext(); ) {
            XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) it.next();
            // if ("Order".equals(elementDeclaration.getName())) {
            fw.write(Util.nodeToString(elementDeclaration.getElement()).replaceAll("xmlns:xsd=\"http:\\/\\/www\\.w3\\.org\\/2001\\/XMLSchema\"", // $NON-NLS-1$ //$NON-NLS-2$
            ""));
        // }
        }
        it = xsdSchema.getTypeDefinitions().iterator();
        for (; it.hasNext(); ) {
            XSDTypeDefinition typedef = (XSDTypeDefinition) it.next();
            fw.write(Util.nodeToString(typedef.getElement()));
        }
        // $NON-NLS-1$
        String footer = "</xsd:schema>";
        fw.write(footer);
        fw.close();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) FileWriter(java.io.FileWriter) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDResourceImpl(org.eclipse.xsd.util.XSDResourceImpl) Iterator(java.util.Iterator) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XSDResourceFactoryImpl(org.eclipse.xsd.util.XSDResourceFactoryImpl) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 82 with XSDTypeDefinition

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

the class Util method getAllComplexTypeChildren.

/**
 * get all complex types's complextype children
 *
 * @param complexTypeDefinition
 * @return
 */
public static ArrayList<Object> getAllComplexTypeChildren(XSDElementDeclaration declaration) {
    XSDComplexTypeDefinition complexTypeDefinition = (XSDComplexTypeDefinition) declaration.getTypeDefinition();
    XSDComplexTypeContent xsdComplexTypeContent = complexTypeDefinition.getContent();
    ArrayList<Object> list = new ArrayList<Object>();
    // Now determine whether ref. If ref look at the base Type definition
    if (xsdComplexTypeContent == null) {
        XSDTypeDefinition typeDefinition = complexTypeDefinition.getBaseTypeDefinition();
        if (typeDefinition instanceof XSDComplexTypeDefinition) {
            list.add(((XSDComplexTypeDefinition) typeDefinition).getContent());
        }
    }
    // check if we are extending a complex Definition
    if ("extension".equals(complexTypeDefinition.getDerivationMethod().getName())) {
        // $NON-NLS-1$
        if (complexTypeDefinition.getBaseTypeDefinition() instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeDefinition complex = (XSDComplexTypeDefinition) complexTypeDefinition.getBaseTypeDefinition();
            XSDParticle particle = (XSDParticle) complex.getContent();
            if (particle.getTerm() instanceof XSDModelGroup) {
                XSDModelGroup group = (XSDModelGroup) particle.getTerm();
                EList<XSDParticle> elist = group.getContents();
                for (XSDParticle pt : elist) {
                    if (pt.getContent() instanceof XSDElementDeclaration) {
                        XSDTypeDefinition typeDef = ((XSDElementDeclaration) pt.getContent()).getTypeDefinition();
                        if (typeDef instanceof XSDComplexTypeDefinition) {
                            list.addAll(getAllComplexTypeChildren((XSDElementDeclaration) pt.getContent()));
                        }
                    }
                }
            }
        }
    }
    // now check what we have in the content
    // if (xsdComplexTypeContent instanceof XSDComplexTypeDefinition) {
    list.add(declaration);
    if (xsdComplexTypeContent instanceof XSDParticle) {
        XSDParticle particle = (XSDParticle) xsdComplexTypeContent;
        if (particle.getTerm() instanceof XSDModelGroup) {
            XSDModelGroup group = (XSDModelGroup) particle.getTerm();
            EList<XSDParticle> elist = group.getContents();
            for (XSDParticle pt : elist) {
                if (pt.getContent() instanceof XSDElementDeclaration) {
                    XSDTypeDefinition typeDef = ((XSDElementDeclaration) pt.getContent()).getTypeDefinition();
                    if (typeDef instanceof XSDComplexTypeDefinition) {
                        list.addAll(getAllComplexTypeChildren((XSDElementDeclaration) pt.getContent()));
                    }
                }
            }
        }
    }
    return list;
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) 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 83 with XSDTypeDefinition

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

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

the class XSDUtilTest method testGetAnnotationValue.

@Test
public void testGetAnnotationValue() throws Exception {
    // $NON-NLS-1$
    String fileName = "Product_0.1.xsd";
    // $NON-NLS-1$
    String elementName = "Family";
    // $NON-NLS-1$
    String fk = "Store/Id";
    String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
    assertNotNull(xsdString);
    XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
    for (XSDElementDeclaration element : xsdSchema.getElementDeclarations()) {
        XSDTypeDefinition typeDefinition = element.getTypeDefinition();
        if (typeDefinition instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeContent xsdComplexTypeContent = ((XSDComplexTypeDefinition) typeDefinition).getContent();
            if (xsdComplexTypeContent instanceof XSDParticle) {
                XSDParticleContent content = ((XSDParticle) xsdComplexTypeContent).getContent();
                if (content instanceof XSDModelGroup) {
                    for (XSDParticle particle : ((XSDModelGroup) content).getParticles()) {
                        XSDTerm term = particle.getTerm();
                        if (term instanceof XSDElementDeclaration) {
                            XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) term;
                            if (elementDeclaration.getName().equals(elementName)) {
                                String value = XSDUtil.getAnnotationValue(elementDeclaration, X_FOREIGN_KEY);
                                assertEquals(fk, value);
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    fail();
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticleContent(org.eclipse.xsd.XSDParticleContent) XSDTerm(org.eclipse.xsd.XSDTerm) 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 85 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project webtools.sourceediting by eclipse.

the class XSDSimpleTypeDefinitionAdapter method getActions.

public String[] getActions(Object object) {
    List list = new ArrayList();
    list.add(AddXSDEnumerationFacetAction.ID);
    list.add(BaseSelectionAction.SEPARATOR_ID);
    list.add(SetBaseTypeAction.ID);
    list.add(BaseSelectionAction.SEPARATOR_ID);
    list.add(DeleteAction.ID);
    list.add(BaseSelectionAction.SEPARATOR_ID);
    Object schema = getEditorSchema();
    XSDTypeDefinition typeDefinition = getXSDTypeDefinition();
    if (typeDefinition.getSchema() == schema) {
        XSDConcreteComponent container = typeDefinition.getContainer();
        if (container == schema || container instanceof XSDRedefine) {
            list.add(SetInputToGraphView.ID);
        }
    } else {
        list.add(OpenInNewEditor.ID);
    }
    list.add(BaseSelectionAction.SEPARATOR_ID);
    list.add(ShowPropertiesViewAction.ID);
    return (String[]) list.toArray(new String[0]);
}
Also used : XSDRedefine(org.eclipse.xsd.XSDRedefine) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IADTObject(org.eclipse.wst.xsd.ui.internal.adt.facade.IADTObject) EObject(org.eclipse.emf.ecore.EObject) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

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