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