11package com .formulasearchengine .mathmltools .xmlhelper ;
22
3- import static org .xmlunit .util .Convert .toInputSource ;
4-
5- import java .io .IOException ;
6- import java .io .InputStream ;
7- import java .io .StringReader ;
8- import java .nio .file .Files ;
9- import java .nio .file .Path ;
10-
11- import javax .xml .parsers .DocumentBuilder ;
12- import javax .xml .parsers .DocumentBuilderFactory ;
13- import javax .xml .parsers .ParserConfigurationException ;
14- import javax .xml .transform .Source ;
153import org .apache .logging .log4j .LogManager ;
164import org .apache .logging .log4j .Logger ;
175import org .w3c .dom .Document ;
219import org .xmlunit .builder .Input ;
2210import org .xmlunit .util .Convert ;
2311
12+ import javax .xml .parsers .DocumentBuilder ;
13+ import javax .xml .parsers .DocumentBuilderFactory ;
14+ import javax .xml .parsers .ParserConfigurationException ;
15+ import javax .xml .transform .Source ;
16+ import java .io .IOException ;
17+ import java .io .InputStream ;
18+ import java .io .StringReader ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .Path ;
21+
22+ import static org .xmlunit .util .Convert .toInputSource ;
23+
2424/**
2525 * Helper class to format XML files to Document and Node types
2626 */
@@ -41,15 +41,13 @@ private XmlDocumentReader() {
4141
4242 public static Document getDocumentFromXML (Path xmlF ) {
4343 final Source source = Input .fromFile (xmlF .toAbsolutePath ().toString ()).build ();
44- final InputSource is = toInputSource ( source ) ;
44+ String orig = null ;
4545 try {
46- return getDocument ( is ); //getDocument(is );
47- } catch (ParserConfigurationException | IOException | SAXException e ) {
48- LOG .warn ("Cannot parse XML file with grammar: " + xmlF .toString (), e );
46+ orig = new String ( Files . readAllBytes ( xmlF ) );
47+ } catch (IOException ioe ) {
48+ LOG .fatal ("Cannot read xml file " + xmlF .toString ());
4949 }
50- return oldgetDocumentFromXML (xmlF );
51-
52-
50+ return getDocumentFromSource (source , orig );
5351 }
5452
5553 private static Document getDocument (InputSource inputStream ) throws ParserConfigurationException , IOException , SAXException {
@@ -97,18 +95,22 @@ public static Document oldgetDocumentFromXML(Path xmlF) {
9795
9896 public static Document getDocumentFromXMLString (String xml ) {
9997 Source source = Input .fromString (xml ).build ();
98+ return getDocumentFromSource (source , xml );
99+ }
100+
101+ private static Document getDocumentFromSource (Source source , String orig ) {
100102 final InputSource is = toInputSource (source );
101103 try {
102104 return getDocument (is );
103105 } catch (ParserConfigurationException | IOException | SAXException e ) {
104- e . printStackTrace ( );
106+ LOG . warn ( "Cannot parse document directly '{}'." , e . getMessage () );
105107 }
106108 try {
107109 return parse (source );
108110 } catch (ParserConfigurationException | IOException | SAXException e ) {
109- e . printStackTrace ( );
111+ LOG . warn ( "Cannot parse on second attempt '{}'." , e . getMessage () );
110112 }
111- return oldgetDocumentFromXMLString (xml );
113+ return oldgetDocumentFromXMLString (orig );
112114 }
113115
114116 private static Document parse (Source s ) throws ParserConfigurationException , IOException , SAXException {
@@ -131,7 +133,7 @@ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationExc
131133
132134 private static DocumentBuilderFactory getDocumentBuilderFactory () throws ParserConfigurationException {
133135 DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
134- dbf .setValidating (true );
136+ dbf .setValidating (false );
135137 dbf .setFeature ("http://xml.org/sax/features/validation" , true );
136138 dbf .setFeature ("http://apache.org/xml/features/nonvalidating/load-dtd-grammar" , false );
137139 dbf .setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
0 commit comments