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;
}
}
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);
}
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();
}
Aggregations