Search in sources :

Example 1 with AtlasXmlSchemaSetParser

use of io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser 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)

Example 2 with AtlasXmlSchemaSetParser

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

the class AtlasMapJsonToXmlSchemaTest method test.

@Test
@DirtiesContext
public void test() throws Exception {
    result.setExpectedCount(1);
    final ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    InputStream payloadIs = tccl.getResourceAsStream("json-source.json");
    producerTemplate.sendBody("direct:start", payloadIs);
    MockEndpoint.assertIsSatisfied(camelContext);
    final String body = result.getExchanges().get(0).getIn().getBody(String.class);
    assertNotNull(body);
    LOG.debug(">>>>> {}", body);
    InputStream schemaIs = tccl.getResourceAsStream("xml-target-schemaset.xml");
    AtlasXmlSchemaSetParser schemaParser = new AtlasXmlSchemaSetParser(tccl);
    Validator validator = schemaParser.createSchema(schemaIs).newValidator();
    StreamSource source = new StreamSource(new StringReader(body));
    validator.validate(source);
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) AtlasXmlSchemaSetParser(io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser) Validator(javax.xml.validation.Validator) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 3 with AtlasXmlSchemaSetParser

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

the class XmlSchemaInspector method doInspect.

private void doInspect(InputStream is) throws Exception {
    xmlDocument = AtlasXmlModelFactory.createXmlDocument();
    Fields fields = new Fields();
    xmlDocument.setFields(fields);
    AtlasXmlSchemaSetParser parser = new AtlasXmlSchemaSetParser(this.classLoader);
    XSSchemaSet schemaSet = parser.parse(is);
    this.namespaceContext = parser.getNamespaceContext();
    this.rootNamespace = parser.getRootNamespace();
    printSchemaSet(schemaSet);
    populateNamespaces();
}
Also used : XmlFields(io.atlasmap.xml.v2.XmlFields) XmlEnumFields(io.atlasmap.xml.v2.XmlEnumFields) Fields(io.atlasmap.v2.Fields) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) AtlasXmlSchemaSetParser(io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser)

Aggregations

AtlasXmlSchemaSetParser (io.atlasmap.xml.core.schema.AtlasXmlSchemaSetParser)3 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)2 XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 AtlasException (io.atlasmap.api.AtlasException)1 AtlasValidationException (io.atlasmap.api.AtlasValidationException)1 Fields (io.atlasmap.v2.Fields)1 AtlasRewritingXSVisitor (io.atlasmap.xml.core.schema.AtlasRewritingXSVisitor)1 XmlEnumFields (io.atlasmap.xml.v2.XmlEnumFields)1 XmlFields (io.atlasmap.xml.v2.XmlFields)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Validator (javax.xml.validation.Validator)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 Test (org.junit.Test)1 DirtiesContext (org.springframework.test.annotation.DirtiesContext)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1