Search in sources :

Example 1 with XSSchemaSet

use of com.sun.xml.xsom.XSSchemaSet in project midpoint by Evolveum.

the class SchemaProcessor method getComplexTypeToElementName.

private Map<QName, List<QName>> getComplexTypeToElementName(ClassOutline classOutline) {
    Map<QName, List<QName>> complexTypeToElementName = new HashMap<QName, List<QName>>();
    XSSchemaSet schemaSet = classOutline.target.getSchemaComponent().getRoot();
    for (XSSchema schema : schemaSet.getSchemas()) {
        Map<String, XSElementDecl> elemDecls = schema.getElementDecls();
        for (Entry<String, XSElementDecl> entry : elemDecls.entrySet()) {
            XSElementDecl decl = entry.getValue();
            XSType xsType = decl.getType();
            if (xsType.getName() == null) {
                continue;
            }
            QName type = new QName(xsType.getTargetNamespace(), xsType.getName());
            List<QName> qnames = complexTypeToElementName.get(type);
            if (qnames == null) {
                qnames = new ArrayList<QName>();
                complexTypeToElementName.put(type, qnames);
            }
            qnames.add(new QName(decl.getTargetNamespace(), decl.getName()));
        }
    }
    return complexTypeToElementName;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) XSSchema(com.sun.xml.xsom.XSSchema) XSType(com.sun.xml.xsom.XSType) PrismReferenceArrayList(com.evolveum.midpoint.prism.xjc.PrismReferenceArrayList) List(java.util.List) ArrayList(java.util.ArrayList) PrismContainerArrayList(com.evolveum.midpoint.prism.xjc.PrismContainerArrayList) XSElementDecl(com.sun.xml.xsom.XSElementDecl)

Example 2 with XSSchemaSet

use of com.sun.xml.xsom.XSSchemaSet in project midpoint by Evolveum.

the class SchemaProcessor method updateObjectFactoryElements.

/**
     * Marks ObjectFactory.createXYZ methods for elements with a:rawType annotation as @Raw.
     */
private void updateObjectFactoryElements(Outline outline) {
    XSSchemaSet schemaSet = outline.getModel().schemaComponent;
    for (CElementInfo elementInfo : outline.getModel().getAllElements()) {
        QName name = elementInfo.getElementName();
        XSComponent elementDecl;
        if (elementInfo.getSchemaComponent() != null) {
            // it's strange but elements seem not to have this filled-in...
            elementDecl = elementInfo.getSchemaComponent();
        } else {
            elementDecl = schemaSet.getElementDecl(name.getNamespaceURI(), name.getLocalPart());
        }
        boolean isRaw = hasAnnotation(elementDecl, A_RAW_TYPE);
        if (isRaw) {
            print("*** Raw element found: " + elementInfo.getElementName());
            JDefinedClass objectFactory = outline.getPackageContext(elementInfo._package()).objectFactory();
            // finding method corresponding to the given element
            boolean methodFound = false;
            for (JMethod method : objectFactory.methods()) {
                for (JAnnotationUse annotationUse : method.annotations()) {
                    if (XmlElementDecl.class.getName().equals(annotationUse.getAnnotationClass().fullName())) {
                        // ugly method of finding the string value of the annotation members (couldn't find any better)
                        JAnnotationValue namespaceValue = annotationUse.getAnnotationMembers().get("namespace");
                        StringWriter namespaceWriter = new StringWriter();
                        JFormatter namespaceFormatter = new JFormatter(namespaceWriter);
                        namespaceValue.generate(namespaceFormatter);
                        JAnnotationValue nameValue = annotationUse.getAnnotationMembers().get("name");
                        StringWriter nameWriter = new StringWriter();
                        JFormatter nameFormatter = new JFormatter(nameWriter);
                        nameValue.generate(nameFormatter);
                        if (("\"" + name.getNamespaceURI() + "\"").equals(namespaceWriter.toString()) && ("\"" + name.getLocalPart() + "\"").equals(nameWriter.toString())) {
                            print("*** Annotating method as @Raw: " + method.name());
                            method.annotate(Raw.class);
                            methodFound = true;
                            break;
                        }
                    }
                }
            }
            if (!methodFound) {
                throw new IllegalStateException("No factory method found for element " + name);
            }
        }
    }
}
Also used : XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) XSComponent(com.sun.xml.xsom.XSComponent) StringWriter(java.io.StringWriter)

Example 3 with XSSchemaSet

use of com.sun.xml.xsom.XSSchemaSet in project atlasmap by atlasmap.

the class SchemaInspector method doInspect.

private void doInspect(InputStream is) throws Exception {
    xmlDocument = AtlasXmlModelFactory.createXmlDocument();
    Fields fields = new Fields();
    xmlDocument.setFields(fields);
    namespaceContext = new AtlasXmlNamespaceContext();
    rootNamespace = null;
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    parser.setAnnotationParser(new DomAnnotationParserFactory());
    parser.setErrorHandler(new XSOMErrorHandler());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(is);
    Element root = doc.getDocumentElement();
    if (root == null) {
        throw new XmlInspectionException("XML schema document is empty");
    } else if ("SchemaSet".equals(root.getLocalName())) {
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(namespaceContext);
        NodeList subSchemas = (NodeList) xpath.evaluate(String.format("/%s:SchemaSet/%s:AdditionalSchemas/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODESET);
        for (int i = 0; i < subSchemas.getLength(); i++) {
            Element e = (Element) subSchemas.item(i);
            inheritNamespaces(e, false);
            parser.parse(toInputStream(transformer, e));
        }
        Element rootSchema = (Element) xpath.evaluate(String.format("/%s:SchemaSet/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODE);
        if (rootSchema == null) {
            throw new XmlInspectionException("The root schema '/SchemaSet/schema' must be specified once and only once");
        }
        rootNamespace = getTargetNamespace(rootSchema);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
        inheritNamespaces(rootSchema, true);
        parser.parse(toInputStream(transformer, rootSchema));
    } else if ("schema".equals(root.getLocalName())) {
        parser.parse(toInputStream(transformer, root));
        rootNamespace = getTargetNamespace(root);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
    } else {
        throw new XmlInspectionException(String.format("Unsupported document element '%s': root element must be 'schema' or 'SchemaSet'", root.getLocalName()));
    }
    XSSchemaSet schemaSet = parser.getResult();
    printSchemaSet(schemaSet);
    populateNamespaces();
}
Also used : XPath(javax.xml.xpath.XPath) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) Transformer(javax.xml.transform.Transformer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XmlDocument(io.atlasmap.xml.v2.XmlDocument) XmlFields(io.atlasmap.xml.v2.XmlFields) Fields(io.atlasmap.v2.Fields) DomAnnotationParserFactory(com.sun.xml.xsom.util.DomAnnotationParserFactory)

Aggregations

XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)3 QName (javax.xml.namespace.QName)2 PrismContainerArrayList (com.evolveum.midpoint.prism.xjc.PrismContainerArrayList)1 PrismReferenceArrayList (com.evolveum.midpoint.prism.xjc.PrismReferenceArrayList)1 CElementInfo (com.sun.tools.xjc.model.CElementInfo)1 XSComponent (com.sun.xml.xsom.XSComponent)1 XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 XSSchema (com.sun.xml.xsom.XSSchema)1 XSType (com.sun.xml.xsom.XSType)1 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)1 DomAnnotationParserFactory (com.sun.xml.xsom.util.DomAnnotationParserFactory)1 Fields (io.atlasmap.v2.Fields)1 XmlDocument (io.atlasmap.xml.v2.XmlDocument)1 XmlFields (io.atlasmap.xml.v2.XmlFields)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1