2424import org .slf4j .LoggerFactory ;
2525import org .xml .sax .SAXException ;
2626
27- @ SuppressWarnings ("unused" )
2827public class XmlUtil {
2928
3029 private static final Logger logger = LoggerFactory .getLogger (XmlUtil .class );
3130
31+ private XmlUtil () {
32+ }
33+
3234 public static void write (Document document , Path file ) throws IOException {
3335 OutputFormat format = OutputFormat .createPrettyPrint ();
3436 format .setTrimText (false );
@@ -37,31 +39,29 @@ public static void write(Document document, Path file) throws IOException {
3739 writer .close ();
3840 }
3941
40- // TODO
4142 // https://stackoverflow.com/questions/55571046/eclipse-is-confused-by-imports-accessible-from-more-than-one-module
42- // public static void validate(String file, String xsd, String schema_dir)
43- // throws ParserConfigurationException, SAXException, IOException {
44- // XmlUtil.validate(new FileInputStream(file), xsd, schema_dir);
45- // }
46- //
47- // public static void validate(InputStream in, String xsd, String schema_dir)
48- // throws ParserConfigurationException, SAXException, IOException {
49- // // See javax.xml.validation
50- // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
51- // dbf.setNamespaceAware(true);
52- // DocumentBuilder parser = dbf.newDocumentBuilder();
53- // org.w3c.dom.Document document = parser.parse(in);
54- // SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
55- // factory.setResourceResolver(new XmlResourceResolver(schema_dir));
56- // ArrayList<Source> sources = new ArrayList<>();
57- // // for (String xsd : xsds) {
58- // logger.info("Schema: " + xsd);
59- // Source schema_file = new StreamSource(XmlUtil.class.getClassLoader().getResourceAsStream(xsd));
60- // sources.add(schema_file);
61- // // }
62- // javax.xml.validation.Schema schema = factory.newSchema(sources.toArray(new Source[sources.size()]));
63- // Validator validator = schema.newValidator();
64- // validator.validate(new DOMSource(document));
65- // }
43+
44+ public static void validate (String file , String xsd , String schema_dir )
45+ throws ParserConfigurationException , SAXException , IOException {
46+ XmlUtil .validate (new FileInputStream (file ), xsd , schema_dir );
47+ }
48+
49+ public static void validate (InputStream in , String xsd , String schema_dir )
50+ throws ParserConfigurationException , SAXException , IOException {
51+ // See javax.xml.validation
52+ DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
53+ dbf .setNamespaceAware (true );
54+ DocumentBuilder parser = dbf .newDocumentBuilder ();
55+ org .w3c .dom .Document document = parser .parse (in );
56+ SchemaFactory factory = SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
57+ factory .setResourceResolver (new XmlResourceResolver (schema_dir ));
58+ ArrayList <Source > sources = new ArrayList <>();
59+ logger .info ("Schema: " + xsd );
60+ Source schema_file = new StreamSource (XmlUtil .class .getClassLoader ().getResourceAsStream (xsd ));
61+ sources .add (schema_file );
62+ javax .xml .validation .Schema schema = factory .newSchema (sources .toArray (new Source [sources .size ()]));
63+ Validator validator = schema .newValidator ();
64+ validator .validate (new DOMSource (document ));
65+ }
6666
6767}
0 commit comments