Search in sources :

Example 21 with XSOMParser

use of com.sun.xml.xsom.parser.XSOMParser in project atlasmap by atlasmap.

the class XSOMParserTest method setUp.

protected void setUp() throws Exception {
    if (docURL == null) {
        docURL = new URL(docURLStr);
        instance = new XSOMParser();
    }
}
Also used : XSOMParser(com.sun.xml.xsom.parser.XSOMParser) URL(java.net.URL)

Example 22 with XSOMParser

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

the class DomToSchemaProcessor method createSchemaParser.

private XSOMParser createSchemaParser() {
    XSOMParser parser = new XSOMParser();
    if (entityResolver == null) {
        entityResolver = prismContext.getEntityResolver();
        if (entityResolver == null) {
            throw new IllegalStateException("Entity resolver is not set (even tried to pull it from prism context)");
        }
    }
    SchemaHandler errorHandler = new SchemaHandler(entityResolver);
    parser.setErrorHandler(errorHandler);
    parser.setAnnotationParser(new DomAnnotationParserFactory());
    parser.setEntityResolver(errorHandler);
    return parser;
}
Also used : XSOMParser(com.sun.xml.xsom.parser.XSOMParser) DomAnnotationParserFactory(com.sun.xml.xsom.util.DomAnnotationParserFactory)

Example 23 with XSOMParser

use of com.sun.xml.xsom.parser.XSOMParser 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)

Example 24 with XSOMParser

use of com.sun.xml.xsom.parser.XSOMParser in project citygml4j by citygml4j.

the class SchemaHandler method parse.

private void parse(InputSource is) throws SAXException {
    if (is == null)
        return;
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    parser.setEntityResolver((publicId, systemId) -> {
        InputSource inputSource = null;
        if (publicId != null) {
            for (Entry<String, String> entry : visited.entrySet()) {
                if (entry.getKey().equals(publicId)) {
                    inputSource = new InputSource(entry.getValue());
                    inputSource.setPublicId(publicId);
                    inputSource.setSystemId(entry.getValue());
                    break;
                }
            }
        }
        if (inputSource == null && publicId != null && schemaEntityResolver != null)
            inputSource = schemaEntityResolver.resolveEntity(publicId, systemId);
        return inputSource;
    });
    if (schemaErrorHandler != null)
        parser.setErrorHandler(schemaErrorHandler);
    if (annotationParserFactory != null)
        parser.setAnnotationParser(annotationParserFactory);
    parser.parse(is);
    XSSchemaSet schemaSet = parser.getResult();
    if (schemaSet != null) {
        for (XSSchema schema : schemaSet.getSchemas()) {
            if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace()))
                continue;
            Locator locator = schema.getLocator();
            if (locator != null) {
                String systemId = locator.getSystemId();
                String visitedId = visited.get(schema.getTargetNamespace());
                if (visitedId == null)
                    visited.put(schema.getTargetNamespace(), systemId);
                else {
                    try {
                        URL cachedURL = new URL(visitedId);
                        URL offeredURL = new URL(systemId);
                        if (!(cachedURL.getProtocol().equals("file") || cachedURL.getProtocol().equals("jar")) && (offeredURL.getProtocol().equals("file") || offeredURL.getProtocol().equals("jar")))
                            visited.put(schema.getTargetNamespace(), systemId);
                    } catch (MalformedURLException e) {
                    // 
                    }
                }
            }
        }
        schemaSets.add(schemaSet);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Locator(org.xml.sax.Locator) MalformedURLException(java.net.MalformedURLException) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) XSSchema(com.sun.xml.xsom.XSSchema) URL(java.net.URL)

Example 25 with XSOMParser

use of com.sun.xml.xsom.parser.XSOMParser in project esb0 by karalus.

the class WSDLArtifact method getXSSchemaSet.

@Override
public XSSchemaSet getXSSchemaSet() throws SAXException {
    if (_schemaSet == null) {
        XSOMParser xsomParser = new XSOMParser(XMLProcessorFactory.getSAXParserFactory());
        xsomParser.setEntityResolver(getResolver());
        for (byte[] schemaContent : _schemas.values()) {
            InputSource is = new InputSource(new ByteArrayInputStream(schemaContent));
            is.setSystemId(getURI());
            xsomParser.parse(is);
        }
        _schemaSet = xsomParser.getResult();
    }
    return _schemaSet;
}
Also used : InputSource(org.xml.sax.InputSource) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

XSOMParser (com.sun.xml.xsom.parser.XSOMParser)38 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)15 SAXException (org.xml.sax.SAXException)14 DomAnnotationParserFactory (com.sun.xml.xsom.util.DomAnnotationParserFactory)10 File (java.io.File)9 InputSource (org.xml.sax.InputSource)7 IOException (java.io.IOException)5 XSSchema (com.sun.xml.xsom.XSSchema)4 OutputStreamWriter (java.io.OutputStreamWriter)4 URL (java.net.URL)4 SAXParserFactory (javax.xml.parsers.SAXParserFactory)4 Transformer (javax.xml.transform.Transformer)4 Document (org.w3c.dom.Document)4 SchemaTreeTraverser (com.sun.xml.xsom.impl.util.SchemaTreeTraverser)3 SchemaWriter (com.sun.xml.xsom.impl.util.SchemaWriter)3 ComponentNameFunction (com.sun.xml.xsom.util.ComponentNameFunction)3 TreeModel (javax.swing.tree.TreeModel)3 DOMSource (javax.xml.transform.dom.DOMSource)3 StreamResult (javax.xml.transform.stream.StreamResult)3 Value (jolie.runtime.Value)3