Search in sources :

Example 1 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
 */
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 2 with XSTypeDefinition

use of org.apache.xerces.xs.XSTypeDefinition in project narayana by jbosstm.

the class NBFHandlers method startElement.

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    if (qName.equals(id)) {
        if (index == curIndex) {
            found = true;
        }
        curIndex++;
        ElementPSVI psvi = provider.getElementPSVI();
        if (psvi != null) {
            XSTypeDefinition typeInfo = psvi.getTypeDefinition();
            while (typeInfo != null) {
                String typeName = typeInfo.getName();
                if (typeName != null && (typeName.equals("long") || typeName.equals("string") || typeName.equals("integer") || typeName.equals("float") || typeName.endsWith("_type"))) {
                    type = typeName;
                    log.debug(qName + " has type of " + type);
                    break;
                }
                typeInfo = typeInfo.getBaseType();
            }
        }
    }
}
Also used : ElementPSVI(org.apache.xerces.xs.ElementPSVI) XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition)

Example 3 with XSTypeDefinition

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

the class XmlAligner method getTypeDefinition.

public XSTypeDefinition getTypeDefinition(PSVIProvider psviProvider) {
    ElementPSVI elementPSVI = psviProvider.getElementPSVI();
    // if (log.isTraceEnabled()) log.trace("elementPSVI ["+ToStringBuilder.reflectionToString(elementPSVI)+"]");
    XSElementDeclaration elementDeclaration = elementPSVI.getElementDeclaration();
    // if (log.isTraceEnabled()) log.trace("elementPSVI element declaration ["+ToStringBuilder.reflectionToString(elementDeclaration)+"]");
    if (elementDeclaration == null) {
        return null;
    }
    XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
    // if (log.isTraceEnabled()) log.trace("elementDeclaration typeDefinition ["+ToStringBuilder.reflectionToString(typeDefinition)+"]");
    return typeDefinition;
}
Also used : ElementPSVI(org.apache.xerces.xs.ElementPSVI) XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration)

Example 4 with XSTypeDefinition

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

the class XmlTypeToJsonSchemaConverter method handleElementDeclaration.

private void handleElementDeclaration(JsonObjectBuilder builder, XSElementDeclaration elementDeclaration, boolean multiOccurring, boolean shouldCreateReferences) {
    String elementName = elementDeclaration.getName();
    // if (log.isTraceEnabled()) log.trace("XSElementDeclaration name ["+elementName+"]");
    if (log.isTraceEnabled())
        log.trace("XSElementDeclaration element [" + elementName + "][" + ToStringBuilder.reflectionToString(elementDeclaration, ToStringStyle.MULTI_LINE_STYLE) + "]");
    XSTypeDefinition elementTypeDefinition = elementDeclaration.getTypeDefinition();
    JsonStructure definition;
    if (elementTypeDefinition.getAnonymous() || XML_SCHEMA_NS.equals(elementTypeDefinition.getNamespace())) {
        definition = getDefinition(elementTypeDefinition, shouldCreateReferences);
    } else {
        definition = Json.createObjectBuilder().add("$ref", definitionsPath + elementTypeDefinition.getName()).build();
    }
    if (elementDeclaration.getNillable()) {
        definition = nillable(definition);
    }
    if (multiOccurring) {
        JsonObjectBuilder arrayBuilder = Json.createObjectBuilder();
        arrayBuilder.add("type", "array");
        arrayBuilder.add("items", definition);
        builder.add(elementName, arrayBuilder.build());
    } else {
        if (definition != null) {
            builder.add(elementName, definition);
        }
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonStructure(javax.json.JsonStructure)

Example 5 with XSTypeDefinition

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

the class XmlTypeToJsonSchemaConverter method getDefinitions.

public JsonObject getDefinitions() {
    JsonObjectBuilder definitionsBuilder = Json.createObjectBuilder();
    for (XSModel model : models) {
        XSNamedMap elements = model.getComponents(XSConstants.ELEMENT_DECLARATION);
        for (int i = 0; i < elements.getLength(); i++) {
            XSElementDeclaration elementDecl = (XSElementDeclaration) elements.item(i);
            handleElementDeclaration(definitionsBuilder, elementDecl, false, false);
        }
        XSNamedMap types = model.getComponents(XSConstants.TYPE_DEFINITION);
        for (int i = 0; i < types.getLength(); i++) {
            XSTypeDefinition typeDefinition = (XSTypeDefinition) types.item(i);
            String typeNamespace = typeDefinition.getNamespace();
            if (typeNamespace == null || !typeDefinition.getNamespace().equals(XML_SCHEMA_NS)) {
                definitionsBuilder.add(typeDefinition.getName(), getDefinition(typeDefinition, false));
            }
        }
    }
    return definitionsBuilder.build();
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonObjectBuilder(javax.json.JsonObjectBuilder)

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