@@ -32,23 +32,20 @@ public class XslTransformer {
32
32
public static final String UBL_I_VALIDATION_STRING = "Invoice" ;
33
33
public static final String UBL_C_VALIDATION_STRING = "CreditNote" ;
34
34
public static final Pattern REGEX = Pattern .compile ("[<:](CrossIndustryInvoice|Invoice|CreditNote)" );
35
- private static final XMLReader xmlReader ;
35
+ private static final SAXParserFactory saxParserFactory ;
36
36
37
37
static {
38
38
try {
39
- SAXParserFactory factory = SAXParserFactory .newInstance ();
40
- SAXParser saxParser = factory .newSAXParser ();
41
- xmlReader = saxParser .getXMLReader ();
42
- xmlReader .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , true );
43
- xmlReader .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
44
- xmlReader .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
39
+ saxParserFactory = SAXParserFactory .newInstance ();
40
+ saxParserFactory .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , true );
41
+ saxParserFactory .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
42
+ saxParserFactory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
45
43
} catch (Exception e ) {
46
- logger .log (Level .SEVERE , "Error initializing XMLReader " , e );
47
- throw new RuntimeException (e );
44
+ logger .log (Level .SEVERE , "Error initializing SAXParserFactory " , e );
45
+ throw new ExceptionInInitializerError (e );
48
46
}
49
47
}
50
48
51
-
52
49
enum DocumentType {
53
50
CII ("cii-xr.xsl" ),
54
51
UBL_I ("ubl-invoice-xr.xsl" ),
@@ -105,10 +102,22 @@ public static void validateFiles() {
105
102
}
106
103
}
107
104
105
+ private static XMLReader newXmlReader () {
106
+ try {
107
+ SAXParser saxParser = saxParserFactory .newSAXParser ();
108
+ return saxParser .getXMLReader ();
109
+ } catch (Exception e ) {
110
+ // should not happen due to setFeature calls on saxParserFactory
111
+ logger .log (Level .SEVERE , "Error initializing XMLReader" , e );
112
+ throw new RuntimeException (e );
113
+ }
114
+ }
115
+
108
116
private static DOMSource transformXmlToXr (String inputXml , DocumentType type ) throws TransformerException {
109
117
TransformerFactory factory = TransformerFactory .newInstance ();
110
118
StreamSource source = new StreamSource ("data/xsl/" + type .getXslName ());
111
119
Transformer transformer = factory .newTransformer (source );
120
+ XMLReader xmlReader = newXmlReader ();
112
121
SAXSource saxSource = new SAXSource (xmlReader , new InputSource (new StringReader (inputXml )));
113
122
DOMResult domResult = new DOMResult ();
114
123
transformer .transform (saxSource , domResult );
0 commit comments