Search in sources :

Example 6 with XSTypeDefinition

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

the class ToXml method handleElement.

public void handleElement(XSElementDeclaration elementDeclaration, N node) throws SAXException {
    String name = elementDeclaration.getName();
    String elementNamespace = elementDeclaration.getNamespace();
    String qname = getQName(elementNamespace, name);
    if (log.isTraceEnabled())
        log.trace("handleNode() name [" + name + "] elementNamespace [" + elementNamespace + "]");
    newLine();
    AttributesImpl attributes = new AttributesImpl();
    Map<String, String> nodeAttributes = getAttributes(elementDeclaration, node);
    if (log.isTraceEnabled())
        log.trace("node [" + name + "] search for attributeDeclaration");
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    XSObjectList attributeUses = getAttributeUses(typeDefinition);
    XSWildcard wildcard = typeDefinition instanceof XSComplexTypeDefinition ? ((XSComplexTypeDefinition) typeDefinition).getAttributeWildcard() : null;
    if ((attributeUses == null || attributeUses.getLength() == 0) && wildcard == null) {
        if (nodeAttributes != null && nodeAttributes.size() > 0) {
            log.warn("node [" + name + "] found [" + nodeAttributes.size() + "] attributes, but no declared AttributeUses or wildcard");
        } else {
            if (log.isTraceEnabled())
                log.trace("node [" + name + "] no attributeUses or wildcard, no attributes");
        }
    } else {
        if (nodeAttributes == null || nodeAttributes.isEmpty()) {
            log.warn("node [" + name + "] declared [" + attributeUses.getLength() + "] attributes, but no attributes found");
        } else {
            for (int i = 0; i < attributeUses.getLength(); i++) {
                XSAttributeUse attributeUse = (XSAttributeUse) attributeUses.item(i);
                XSAttributeDeclaration attributeDeclaration = attributeUse.getAttrDeclaration();
                // XSSimpleTypeDefinition attTypeDefinition=attributeDeclaration.getTypeDefinition();
                // if (log.isTraceEnabled()) log.trace("node ["+name+"] attTypeDefinition ["+ToStringBuilder.reflectionToString(attTypeDefinition)+"]");
                String attName = attributeDeclaration.getName();
                if (nodeAttributes.containsKey(attName)) {
                    String value = nodeAttributes.remove(attName);
                    String uri = attributeDeclaration.getNamespace();
                    String attqname = getQName(uri, attName);
                    String type = null;
                    if (log.isTraceEnabled())
                        log.trace("node [" + name + "] adding attribute [" + attName + "] value [" + value + "]");
                    attributes.addAttribute(uri, attName, attqname, type, value);
                }
            }
            if (wildcard != null) {
                nodeAttributes.forEach((attName, value) -> {
                    if (log.isTraceEnabled())
                        log.trace("node [" + name + "] adding attribute [" + attName + "] value [" + value + "] via wildcard");
                    attributes.addAttribute("", attName, attName, null, value);
                });
                nodeAttributes.clear();
            }
        }
    }
    if (isNil(elementDeclaration, node)) {
        validatorHandler.startPrefixMapping(XSI_PREFIX_MAPPING, XML_SCHEMA_INSTANCE_NAMESPACE);
        attributes.addAttribute(XML_SCHEMA_INSTANCE_NAMESPACE, XML_SCHEMA_NIL_ATTRIBUTE, XSI_PREFIX_MAPPING + ":" + XML_SCHEMA_NIL_ATTRIBUTE, "xs:boolean", "true");
        validatorHandler.startElement(elementNamespace, name, qname, attributes);
        validatorHandler.endElement(elementNamespace, name, qname);
        validatorHandler.endPrefixMapping(XSI_PREFIX_MAPPING);
    } else {
        if (isMultipleOccurringChildElement(name) && node instanceof List) {
            for (Object o : (List) node) {
                doHandleElement(elementDeclaration, (N) o, elementNamespace, name, qname, attributes);
            }
        } else {
            doHandleElement(elementDeclaration, node, elementNamespace, name, qname, attributes);
        }
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) AttributesImpl(org.xml.sax.helpers.AttributesImpl) XSObjectList(org.apache.xerces.xs.XSObjectList) XSAttributeDeclaration(org.apache.xerces.xs.XSAttributeDeclaration) XSAttributeUse(org.apache.xerces.xs.XSAttributeUse) XSObjectList(org.apache.xerces.xs.XSObjectList) LinkedList(java.util.LinkedList) List(java.util.List) XSWildcard(org.apache.xerces.xs.XSWildcard) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Example 7 with XSTypeDefinition

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

the class XsContentDFA method getElementDeclaration.

@Nullable
private static XSElementDeclaration getElementDeclaration(XmlTag tag, XSModel xsModel) {
    List<XmlTag> ancestors = new ArrayList<>();
    for (XmlTag t = tag; t != null; t = t.getParentTag()) {
        ancestors.add(t);
    }
    Collections.reverse(ancestors);
    XSElementDeclaration declaration = null;
    SubstitutionGroupHandler fSubGroupHandler = new SubstitutionGroupHandler(new MyXSElementDeclHelper());
    CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
    for (XmlTag ancestor : ancestors) {
        if (declaration == null) {
            declaration = xsModel.getElementDeclaration(ancestor.getLocalName(), ancestor.getNamespace());
            if (declaration == null)
                return null;
            else
                continue;
        }
        XSTypeDefinition typeDefinition = declaration.getTypeDefinition();
        if (!(typeDefinition instanceof XSComplexTypeDecl)) {
            return null;
        }
        XSCMValidator model = ((XSComplexTypeDecl) typeDefinition).getContentModel(cmBuilder);
        int[] ints = model.startContentModel();
        for (XmlTag subTag : ancestor.getParentTag().getSubTags()) {
            QName qName = createQName(subTag);
            Object o = model.oneTransition(qName, ints, fSubGroupHandler);
            if (subTag == ancestor) {
                if (o instanceof XSElementDecl) {
                    declaration = (XSElementDecl) o;
                    break;
                } else
                    return null;
            }
        }
    }
    return declaration;
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) QName(org.apache.xerces.xni.QName) ArrayList(java.util.ArrayList) CMNodeFactory(org.apache.xerces.impl.xs.models.CMNodeFactory) XSCMValidator(org.apache.xerces.impl.xs.models.XSCMValidator) CMBuilder(org.apache.xerces.impl.xs.models.CMBuilder) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with XSTypeDefinition

use of org.apache.xerces.xs.XSTypeDefinition 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
 */
// FIXME this is specifically for xml backends and therefore broken under the new canonical model
public static void deriveWPD(TEntityType ci, List<String> errors, IRepository repository) {
    BackendUtils.LOGGER.trace("deriveWPD");
    TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
    if (propertiesDefinition == null) {
        // if there's no properties definition, there's nothing to derive because we're in YAML mode
        return;
    }
    if (!(propertiesDefinition instanceof TEntityType.XmlElementDefinition)) {
        BackendUtils.LOGGER.debug("only works for an element definition, not for types");
        return;
    }
    final QName element = ((TEntityType.XmlElementDefinition) propertiesDefinition).getElement();
    BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
    // fetch the XSD defining the element
    final XsdImportManager xsdImportManager = repository.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, repository);
    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)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
        return;
    }
    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");
        return;
    }
    XSTerm term = particle.getTerm();
    if (!(term instanceof XSModelGroup)) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
        return;
    }
    XSModelGroup modelGroup = (XSModelGroup) term;
    if (modelGroup.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
        return;
    }
    XSObjectList particles = modelGroup.getParticles();
    int len = particles.getLength();
    boolean everyThingIsASimpleType = true;
    List<PropertyDefinitionKV> list = new ArrayList<>();
    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) {
        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
        return;
    }
    // everything went alright, we can add a WPD
    WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
    wpd.setIsDerivedFromXSD(Boolean.TRUE);
    wpd.setElementName(element.getLocalPart());
    wpd.setNamespace(element.getNamespaceURI());
    wpd.setPropertyDefinitions(list);
    ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
    BackendUtils.LOGGER.debug("Successfully generated WPD");
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) XSParticle(org.apache.xerces.xs.XSParticle) TEntityType(org.eclipse.winery.model.tosca.TEntityType) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleType(org.apache.xerces.impl.dv.XSSimpleType) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) XSModelGroup(org.apache.xerces.xs.XSModelGroup) XSTerm(org.apache.xerces.xs.XSTerm) QName(javax.xml.namespace.QName) HasTargetNamespace(org.eclipse.winery.model.tosca.HasTargetNamespace) Namespace(org.eclipse.winery.model.ids.Namespace) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 9 with XSTypeDefinition

use of org.apache.xerces.xs.XSTypeDefinition 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 (log.isTraceEnabled())
                log.trace("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 (log.isTraceEnabled())
                        log.trace("handleElementContents complexTypeDefinition.contentType is Empty, no child elements");
                    return;
                case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
                    if (log.isTraceEnabled())
                        log.trace("handleElementContents complexTypeDefinition.contentType is Simple, no child elements (only characters)");
                    handleSimpleTypedElement(elementDeclaration, null, node);
                    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)

Example 10 with XSTypeDefinition

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

the class ToXml method getBestChildElementPath.

public List<XSParticle> getBestChildElementPath(XSElementDeclaration elementDeclaration, N node, boolean silent) throws SAXException {
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    if (typeDefinition == null) {
        log.warn("getBestChildElementPath typeDefinition is null");
        return null;
    }
    switch(typeDefinition.getTypeCategory()) {
        case XSTypeDefinition.SIMPLE_TYPE:
            if (log.isTraceEnabled())
                log.trace("getBestChildElementPath typeDefinition.typeCategory is SimpleType, no child elements");
            return null;
        case XSTypeDefinition.COMPLEX_TYPE:
            XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
            switch(complexTypeDefinition.getContentType()) {
                case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
                    if (log.isTraceEnabled())
                        log.trace("getBestChildElementPath complexTypeDefinition.contentType is Empty, no child elements");
                    return null;
                case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
                    if (log.isTraceEnabled())
                        log.trace("getBestChildElementPath complexTypeDefinition.contentType is Simple, no child elements (only characters)");
                    return null;
                case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
                case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
                    XSParticle particle = complexTypeDefinition.getParticle();
                    if (particle == null) {
                        throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.particle is null for Element or Mixed contentType");
                    // log.warn("typeDefinition particle is null, is this a problem?");
                    // return null;
                    }
                    if (log.isTraceEnabled())
                        log.trace("typeDefinition particle [" + ToStringBuilder.reflectionToString(particle, ToStringStyle.MULTI_LINE_STYLE) + "]");
                    List<XSParticle> result = new LinkedList<XSParticle>();
                    List<String> failureReasons = new LinkedList<String>();
                    if (getBestMatchingElementPath(elementDeclaration, node, particle, result, failureReasons)) {
                        return result;
                    }
                    String msg = "Cannot find path:";
                    for (String reason : failureReasons) {
                        msg += '\n' + reason;
                    }
                    if (!silent) {
                        handleError(msg);
                    }
                    return null;
                default:
                    throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but [" + complexTypeDefinition.getContentType() + "]");
            }
        default:
            throw new IllegalStateException("getBestChildElementPath typeDefinition.typeCategory is not SimpleType or ComplexType, but [" + typeDefinition.getTypeCategory() + "] class [" + typeDefinition.getClass().getName() + "]");
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSParticle(org.apache.xerces.xs.XSParticle) LinkedList(java.util.LinkedList) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Aggregations

XSTypeDefinition (org.apache.xerces.xs.XSTypeDefinition)12 XSComplexTypeDefinition (org.apache.xerces.xs.XSComplexTypeDefinition)6 XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)6 XSObjectList (org.apache.xerces.xs.XSObjectList)4 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 XSModel (org.apache.xerces.xs.XSModel)3 XSParticle (org.apache.xerces.xs.XSParticle)3 XSTerm (org.apache.xerces.xs.XSTerm)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 JsonStructure (javax.json.JsonStructure)2 QName (javax.xml.namespace.QName)2 XSSimpleType (org.apache.xerces.impl.dv.XSSimpleType)2 ElementPSVI (org.apache.xerces.xs.ElementPSVI)2 XSAttributeDeclaration (org.apache.xerces.xs.XSAttributeDeclaration)2 XSAttributeUse (org.apache.xerces.xs.XSAttributeUse)2 XSModelGroup (org.apache.xerces.xs.XSModelGroup)2 XSWildcard (org.apache.xerces.xs.XSWildcard)2 HasTargetNamespace (org.eclipse.winery.model.tosca.HasTargetNamespace)2 XsdImportManager (org.eclipse.winery.repository.backend.xsd.XsdImportManager)2