Search in sources :

Example 6 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
 */
// 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 7 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 (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 8 with XSComplexTypeDefinition

use of org.apache.xerces.xs.XSComplexTypeDefinition 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)

Example 9 with XSComplexTypeDefinition

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

the class XmlAligner method startElement.

@Override
public void startElement(String namespaceUri, String localName, String qName, Attributes attributes) throws SAXException {
    if (log.isTraceEnabled())
        log.trace("startElement() uri [" + namespaceUri + "] localName [" + localName + "] qName [" + qName + "]");
    // call getChildElementDeclarations with in startElement, to obtain all child elements of the current node
    typeDefinition = getTypeDefinition(psviProvider);
    if (typeDefinition == null) {
        throw new SaxException("No typeDefinition found for element [" + localName + "] in namespace [" + namespaceUri + "] qName [" + qName + "]");
    }
    multipleOccurringElements.push(multipleOccurringChildElements);
    parentOfSingleMultipleOccurringChildElements.push(parentOfSingleMultipleOccurringChildElement);
    // call findMultipleOccurringChildElements, to obtain all child elements that could be part of an array
    if (typeDefinition instanceof XSComplexTypeDefinition) {
        XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
        multipleOccurringChildElements = findMultipleOccurringChildElements(complexTypeDefinition.getParticle());
        parentOfSingleMultipleOccurringChildElement = (ChildOccurrence.ONE_MULTIPLE_OCCURRING_ELEMENT == determineIsParentOfSingleMultipleOccurringChildElement(complexTypeDefinition.getParticle()));
        if (log.isTraceEnabled())
            log.trace("element [" + localName + "] is parentOfSingleMultipleOccurringChildElement [" + parentOfSingleMultipleOccurringChildElement + "]");
    } else {
        multipleOccurringChildElements = null;
        parentOfSingleMultipleOccurringChildElement = false;
        if (log.isTraceEnabled())
            log.trace("element [" + localName + "] is a SimpleType, and therefor not multiple");
    }
    super.startElement(namespaceUri, localName, qName, attributes);
    indentLevel++;
    context = new AlignmentContext(context, namespaceUri, localName, qName, attributes, typeDefinition, indentLevel, multipleOccurringChildElements, parentOfSingleMultipleOccurringChildElement);
}
Also used : SaxException(nl.nn.adapterframework.xml.SaxException) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Example 10 with XSComplexTypeDefinition

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

the class XmlTo method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    boolean xmlArrayContainer = aligner.isParentOfSingleMultipleOccurringChildElement();
    boolean repeatedElement = aligner.isMultipleOccurringChildInParentElement(localName);
    XSTypeDefinition typeDefinition = aligner.getTypeDefinition();
    if (!localName.equals(topElement)) {
        if (topElement != null) {
            if (log.isTraceEnabled())
                log.trace("endElementGroup [" + topElement + "]");
            documentContainer.endElementGroup(topElement);
        }
        if (log.isTraceEnabled())
            log.trace("startElementGroup [" + localName + "]");
        documentContainer.startElementGroup(localName, xmlArrayContainer, repeatedElement, typeDefinition);
        topElement = localName;
    }
    element.push(topElement);
    topElement = null;
    if (log.isTraceEnabled())
        log.trace("startElement [" + localName + "] xml array container [" + aligner.isParentOfSingleMultipleOccurringChildElement() + "] repeated element [" + aligner.isMultipleOccurringChildInParentElement(localName) + "]");
    documentContainer.startElement(localName, xmlArrayContainer, repeatedElement, typeDefinition);
    super.startElement(uri, localName, qName, atts);
    if (aligner.isNil(atts)) {
        documentContainer.setNull();
    } else {
        if (writeAttributes) {
            XSObjectList attributeUses = aligner.getAttributeUses();
            XSWildcard wildcard = typeDefinition instanceof XSComplexTypeDefinition ? ((XSComplexTypeDefinition) typeDefinition).getAttributeWildcard() : null;
            if (attributeUses == null && wildcard == null) {
                if (atts.getLength() > 0) {
                    log.warn("found [" + atts.getLength() + "] attributes, but no declared AttributeUses");
                }
            } else {
                if (wildcard != null) {
                    // if wildcard (xs:anyAttribute namespace="##any" processContents="lax") is present, then any attribute will be parsed
                    for (int i = 0; i < atts.getLength(); i++) {
                        String name = atts.getLocalName(i);
                        String namespace = atts.getURI(i);
                        String value = atts.getValue(i);
                        XSSimpleTypeDefinition attTypeDefinition = findAttributeTypeDefinition(attributeUses, namespace, name);
                        if (log.isTraceEnabled())
                            log.trace("startElement [" + localName + "] attribute [" + namespace + ":" + name + "] value [" + value + "]");
                        if (StringUtils.isNotEmpty(value)) {
                            documentContainer.setAttribute(name, value, attTypeDefinition);
                        }
                    }
                } else {
                    // if no wildcard is found, then only declared attributes will be parsed
                    for (int i = 0; i < attributeUses.getLength(); i++) {
                        XSAttributeUse attributeUse = (XSAttributeUse) attributeUses.item(i);
                        XSAttributeDeclaration attributeDeclaration = attributeUse.getAttrDeclaration();
                        XSSimpleTypeDefinition attTypeDefinition = attributeDeclaration.getTypeDefinition();
                        String attName = attributeDeclaration.getName();
                        String attNS = attributeDeclaration.getNamespace();
                        if (log.isTraceEnabled())
                            log.trace("startElement [" + localName + "] searching attribute [" + attNS + ":" + attName + "]");
                        int attIndex = attNS != null ? atts.getIndex(attNS, attName) : atts.getIndex(attName);
                        if (attIndex >= 0) {
                            String value = atts.getValue(attIndex);
                            if (log.isTraceEnabled())
                                log.trace("startElement [" + localName + "] attribute [" + attNS + ":" + attName + "] value [" + value + "]");
                            if (StringUtils.isNotEmpty(value)) {
                                documentContainer.setAttribute(attName, value, attTypeDefinition);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) XSAttributeDeclaration(org.apache.xerces.xs.XSAttributeDeclaration) XSAttributeUse(org.apache.xerces.xs.XSAttributeUse) XSWildcard(org.apache.xerces.xs.XSWildcard) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleTypeDefinition(org.apache.xerces.xs.XSSimpleTypeDefinition)

Aggregations

XSComplexTypeDefinition (org.apache.xerces.xs.XSComplexTypeDefinition)10 XSTypeDefinition (org.apache.xerces.xs.XSTypeDefinition)6 XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)5 XSModel (org.apache.xerces.xs.XSModel)5 XSObjectList (org.apache.xerces.xs.XSObjectList)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 XSParticle (org.apache.xerces.xs.XSParticle)3 LinkedList (java.util.LinkedList)2 QName (javax.xml.namespace.QName)2 XSSimpleType (org.apache.xerces.impl.dv.XSSimpleType)2 XSAttributeDeclaration (org.apache.xerces.xs.XSAttributeDeclaration)2 XSAttributeUse (org.apache.xerces.xs.XSAttributeUse)2 XSModelGroup (org.apache.xerces.xs.XSModelGroup)2 XSTerm (org.apache.xerces.xs.XSTerm)2 XSWildcard (org.apache.xerces.xs.XSWildcard)2 HasTargetNamespace (org.eclipse.winery.model.tosca.HasTargetNamespace)2