Search in sources :

Example 1 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition in project intellij-community by JetBrains.

the class XmlConstraintsTest method testXercesGrammar.

public void testXercesGrammar() throws Exception {
    XSModel xsModel = getXSModel("test.xml", "test.xsd");
    XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration("a", "");
    XSComplexTypeDefinition typeDefinition = (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
    CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
    XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
    int[] ints = validator.startContentModel();
    Vector vector = validator.whatCanGoHere(ints);
    XSElementDecl o = (XSElementDecl) vector.get(0);
    assertEquals("b", o.getName());
}
Also used : CMBuilder(org.apache.xerces.impl.xs.models.CMBuilder) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) CMNodeFactory(org.apache.xerces.impl.xs.models.CMNodeFactory) XSElementDecl(org.apache.xerces.impl.xs.XSElementDecl) XSCMValidator(org.apache.xerces.impl.xs.models.XSCMValidator) Vector(java.util.Vector) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Example 2 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition in project intellij-community by JetBrains.

the class XmlConstraintsTest method testXercesForCompletion.

public void testXercesForCompletion() throws Exception {
    XSModel xsModel = getXSModel("testCompletion.xml", "test.xsd");
    PsiElement element = myFixture.getFile().findElementAt(getEditor().getCaretModel().getOffset());
    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    assert tag != null;
    XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(tag.getLocalName(), tag.getNamespace());
    XSComplexTypeDefinition typeDefinition = (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
    CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
    XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
    int[] ints = validator.startContentModel();
    Vector vector = validator.whatCanGoHere(ints);
    XSElementDecl o = (XSElementDecl) vector.get(0);
    assertEquals("b", o.getName());
}
Also used : CMBuilder(org.apache.xerces.impl.xs.models.CMBuilder) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) CMNodeFactory(org.apache.xerces.impl.xs.models.CMNodeFactory) XSElementDecl(org.apache.xerces.impl.xs.XSElementDecl) XSCMValidator(org.apache.xerces.impl.xs.models.XSCMValidator) Vector(java.util.Vector) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Example 3 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition in project winery by eclipse.

the class BackendUtils method deriveWPD.

/**
 * Derives Winery's Properties Definition from an existing properties definition
 *
 * @param ci     the entity type to try to modify the WPDs
 * @param errors the list to add errors to
 */
public static void deriveWPD(TEntityType ci, List<String> errors) {
    BackendUtils.LOGGER.trace("deriveWPD");
    PropertiesDefinition propertiesDefinition = ci.getPropertiesDefinition();
    QName element = propertiesDefinition.getElement();
    if (element == null) {
        BackendUtils.LOGGER.debug("only works for an element definition, not for types");
    } else {
        BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
        // fetch the XSD defining the element
        final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
        Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
        RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
        if (ref == null) {
            String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
            BackendUtils.LOGGER.debug(msg);
            errors.add(msg);
            return;
        }
        final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref);
        if (!xsModelOptional.isPresent()) {
            LOGGER.error("no XSModel found");
        }
        XSModel xsModel = xsModelOptional.get();
        XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
        if (elementDeclaration == null) {
            String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
            BackendUtils.LOGGER.debug(msg);
            errors.add(msg);
            return;
        }
        // go through the XSD definition and
        XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
        if (typeDefinition instanceof XSComplexTypeDefinition) {
            XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
            XSParticle particle = cTypeDefinition.getParticle();
            if (particle == null) {
                BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
            } else {
                XSTerm term = particle.getTerm();
                if (term instanceof XSModelGroup) {
                    XSModelGroup modelGroup = (XSModelGroup) term;
                    if (modelGroup.getCompositor() == XSModelGroup.COMPOSITOR_SEQUENCE) {
                        XSObjectList particles = modelGroup.getParticles();
                        int len = particles.getLength();
                        boolean everyThingIsASimpleType = true;
                        PropertyDefinitionKVList list = new PropertyDefinitionKVList();
                        if (len != 0) {
                            for (int i = 0; i < len; i++) {
                                XSParticle innerParticle = (XSParticle) particles.item(i);
                                XSTerm innerTerm = innerParticle.getTerm();
                                if (innerTerm instanceof XSElementDeclaration) {
                                    XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
                                    String name = innerElementDeclaration.getName();
                                    XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
                                    if (innerTypeDefinition instanceof XSSimpleType) {
                                        XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
                                        String typeNS = xsSimpleType.getNamespace();
                                        String typeName = xsSimpleType.getName();
                                        if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                                            PropertyDefinitionKV def = new PropertyDefinitionKV();
                                            def.setKey(name);
                                            // convention at WPD: use "xsd" as prefix for XML Schema Definition
                                            def.setType("xsd:" + typeName);
                                            list.add(def);
                                        } else {
                                            everyThingIsASimpleType = false;
                                            break;
                                        }
                                    } else {
                                        everyThingIsASimpleType = false;
                                        break;
                                    }
                                } else {
                                    everyThingIsASimpleType = false;
                                    break;
                                }
                            }
                        }
                        if (everyThingIsASimpleType) {
                            // everything went allright, we can add a WPD
                            WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
                            wpd.setIsDerivedFromXSD(Boolean.TRUE);
                            wpd.setElementName(element.getLocalPart());
                            wpd.setNamespace(element.getNamespaceURI());
                            wpd.setPropertyDefinitionKVList(list);
                            ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
                            BackendUtils.LOGGER.debug("Successfully generated WPD");
                        } else {
                            BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
                        }
                    } else {
                        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
                    }
                } else {
                    BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
                }
            }
        } else {
            BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
        }
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) XSParticle(org.apache.xerces.xs.XSParticle) XSTerm(org.apache.xerces.xs.XSTerm) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Namespace(org.eclipse.winery.common.ids.Namespace) HasTargetNamespace(org.eclipse.winery.model.tosca.HasTargetNamespace) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleType(org.apache.xerces.impl.dv.XSSimpleType) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) PropertyDefinitionKVList(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKVList) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) XSModelGroup(org.apache.xerces.xs.XSModelGroup)

Example 4 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition in project intellij-community by JetBrains.

the class XmlConstraintsTest method testXercesIncomplete.

public void testXercesIncomplete() throws Exception {
    XSModel xsModel = getXSModel("testIncomplete.xml", "test.xsd");
    XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration("a", "");
    XSComplexTypeDefinition typeDefinition = (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
    CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
    XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
    int[] ints = validator.startContentModel();
    Vector vector = validator.whatCanGoHere(ints);
    XSElementDecl o = (XSElementDecl) vector.get(0);
    assertEquals("b", o.getName());
}
Also used : CMBuilder(org.apache.xerces.impl.xs.models.CMBuilder) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) CMNodeFactory(org.apache.xerces.impl.xs.models.CMNodeFactory) XSElementDecl(org.apache.xerces.impl.xs.XSElementDecl) XSCMValidator(org.apache.xerces.impl.xs.models.XSCMValidator) Vector(java.util.Vector) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Example 5 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition in project iaf by ibissource.

the class ToXml method handleElementContents.

public void handleElementContents(XSElementDeclaration elementDeclaration, N node) throws SAXException {
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    if (typeDefinition == null) {
        log.warn("handleElementContents typeDefinition is null");
        handleSimpleTypedElement(elementDeclaration, null, node);
        return;
    }
    switch(typeDefinition.getTypeCategory()) {
        case XSTypeDefinition.SIMPLE_TYPE:
            if (DEBUG)
                log.debug("handleElementContents typeDefinition.typeCategory is SimpleType, no child elements");
            handleSimpleTypedElement(elementDeclaration, (XSSimpleTypeDefinition) typeDefinition, node);
            return;
        case XSTypeDefinition.COMPLEX_TYPE:
            XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
            switch(complexTypeDefinition.getContentType()) {
                case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
                    if (DEBUG)
                        log.debug("handleElementContents complexTypeDefinition.contentType is Empty, no child elements");
                    return;
                case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
                    if (DEBUG)
                        log.debug("handleElementContents complexTypeDefinition.contentType is Simple, no child elements (only characters)");
                    return;
                case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
                case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
                    handleComplexTypedElement(elementDeclaration, node);
                    return;
                default:
                    throw new IllegalStateException("handleElementContents complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but [" + complexTypeDefinition.getContentType() + "]");
            }
        default:
            throw new IllegalStateException("handleElementContents typeDefinition.typeCategory is not SimpleType or ComplexType, but [" + typeDefinition.getTypeCategory() + "] class [" + typeDefinition.getClass().getName() + "]");
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Aggregations

XSComplexTypeDefinition (org.apache.xerces.xs.XSComplexTypeDefinition)7 XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)4 XSModel (org.apache.xerces.xs.XSModel)4 Vector (java.util.Vector)3 XSElementDecl (org.apache.xerces.impl.xs.XSElementDecl)3 CMBuilder (org.apache.xerces.impl.xs.models.CMBuilder)3 CMNodeFactory (org.apache.xerces.impl.xs.models.CMNodeFactory)3 XSCMValidator (org.apache.xerces.impl.xs.models.XSCMValidator)3 XSTypeDefinition (org.apache.xerces.xs.XSTypeDefinition)3 XSParticle (org.apache.xerces.xs.XSParticle)2 PsiElement (com.intellij.psi.PsiElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 LinkedList (java.util.LinkedList)1 QName (javax.xml.namespace.QName)1 XSSimpleType (org.apache.xerces.impl.dv.XSSimpleType)1 XSModelGroup (org.apache.xerces.xs.XSModelGroup)1 XSObjectList (org.apache.xerces.xs.XSObjectList)1 XSTerm (org.apache.xerces.xs.XSTerm)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 Namespace (org.eclipse.winery.common.ids.Namespace)1