matthiasblaesing commented on issue #6226:
URL: https://github.com/apache/netbeans/issues/6226#issuecomment-1646092937
We are drifting from the usecase of an issue tracker, but I think you are
looking for is the code below.
```java
package multischema.validate;
import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.SAXException;
public class ValidateXmlXsd {
private static final String SCHEMA_BASE = "xml/";
private static final String NS_XMLSCHEMA =
"http://www.w3.org/2001/XMLSchema";
private static final String ELEMENT_SCHEMA = "schema";
private static final String ATTR_TNS = "targetNamespace";
public static void main(String... args) throws Exception {
Map<String, URI> namespaceFileMap = buildNamespaceFilemap();
DOMImplementationLS dls = (DOMImplementationLS)
DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
SchemaFactory mySchemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema mySchema = mySchemaFactory.newSchema();
Validator validator = mySchema.newValidator();
validator.setResourceResolver(new LSResourceResolver() {
@Override
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
if(namespaceFileMap.containsKey(namespaceURI)) {
LSInput lsi = dls.createLSInput();
lsi.setSystemId(namespaceFileMap.get(namespaceURI).toASCIIString());
return lsi;
} else {
return null;
}
}
});
validator.setErrorHandler(new XsdErrorHandler());
validator.validate(new StreamSource(new File(SCHEMA_BASE +
"mtn_2020-06-29_full_S1_R0.xml")));
System.out.println("Validation completed - please check for
validation errors.");
}
private static Map<String,URI> buildNamespaceFilemap() throws
SAXException, ParserConfigurationException, IOException {
DocumentBuilder db = DocumentBuilderFactory
.newDefaultNSInstance()
.newDocumentBuilder();
Map<String, URI> namespaceFileMap = new HashMap<>();
for(File f: new File("xml").listFiles()) {
URI fileUri = f.toURI();
if(f.getName().endsWith(".xsd")) {
Document d = db.parse(fileUri.toASCIIString());
NodeList schemaNodes =
d.getElementsByTagNameNS(NS_XMLSCHEMA, ELEMENT_SCHEMA);
for(int i = 0; i < schemaNodes.getLength(); i++) {
Element schema = (Element) schemaNodes.item(i);
String targetNamespace = schema.getAttribute(ATTR_TNS);
if(! targetNamespace.isBlank()) {
namespaceFileMap.put(targetNamespace, fileUri);
}
}
}
}
return namespaceFileMap;
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists