Search in sources :

Example 1 with AtlasRewritingXSVisitor

use of io.atlasmap.xml.core.schema.AtlasRewritingXSVisitor in project atlasmap by atlasmap.

the class XmlModule method enforceSchema.

private Document enforceSchema(Document doc) {
    if (getDataSourceMetadata() == null || getDataSourceMetadata().getInspectionType() != InspectionType.SCHEMA || getDataSourceMetadata().getSpecification() == null || getDataSourceMetadata().getSpecification().length == 0) {
        return doc;
    }
    try {
        byte[] bytes = getDataSourceMetadata().getSpecification();
        AtlasXmlSchemaSetParser schemaParser = new AtlasXmlSchemaSetParser(getClassLoader());
        XSSchemaSet schemaSet = schemaParser.parse(new ByteArrayInputStream(bytes));
        Element sourceRoot = doc.getDocumentElement();
        String namespaceUri = sourceRoot.getNamespaceURI();
        if (namespaceUri == null) {
            namespaceUri = XMLConstants.NULL_NS_URI;
        }
        String localName = sourceRoot.getLocalName();
        if (XMLConstants.NULL_NS_URI.equals(namespaceUri)) {
            localName = sourceRoot.getTagName();
        }
        XSElementDecl rootDecl = schemaSet.getElementDecl(namespaceUri, localName);
        if (rootDecl == null) {
            LOG.warn("Declaration of the root element '{}' was not found in the schema", namespaceUri != null ? namespaceUri + ":" + localName : localName);
            return doc;
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document targetDoc = dbf.newDocumentBuilder().newDocument();
        rootDecl.visit(new AtlasRewritingXSVisitor(doc, targetDoc));
        return targetDoc;
    } catch (Exception e) {
        LOG.warn("Failed to load XML schema for the document '{}': {} - ignoring", getDocId(), e.getMessage());
        if (LOG.isDebugEnabled()) {
            LOG.debug("", e);
        }
        return doc;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) AtlasRewritingXSVisitor(io.atlasmap.xml.core.schema.AtlasRewritingXSVisitor) AtlasXmlSchemaSetParser(io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser) XSElementDecl(com.sun.xml.xsom.XSElementDecl) Document(org.w3c.dom.Document) AtlasValidationException(io.atlasmap.api.AtlasValidationException) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)1 AtlasException (io.atlasmap.api.AtlasException)1 AtlasValidationException (io.atlasmap.api.AtlasValidationException)1 AtlasRewritingXSVisitor (io.atlasmap.xml.core.schema.AtlasRewritingXSVisitor)1 AtlasXmlSchemaSetParser (io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1