1414import javax .xml .transform .dom .DOMSource ;
1515import javax .xml .transform .stream .StreamResult ;
1616import java .io .StringWriter ;
17+ import java .util .logging .Logger ;
1718
1819public class TrAXPrettyPrinterStrategy extends AbstractXMLPrettyPrinterStrategy {
20+
21+ private static final Logger LOGGER = Logger .getLogger ( TrAXPrettyPrinterStrategy .class .getName () );
22+ private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory .newInstance (
23+ "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" ,
24+ null );
25+
1926 private int indent = 4 ;
2027 private boolean omitXmlDeclaration ;
2128
@@ -32,17 +39,18 @@ public String prettyPrint(String xml) throws Exception {
3239 }
3340
3441 protected Transformer newTransformer (final Document document ) throws TransformerConfigurationException {
35- final TransformerFactory transformerFactory = newTransformerFactory ();
3642
37- final Transformer transformer = transformerFactory .newTransformer ();
43+ final Transformer transformer = TRANSFORMER_FACTORY .newTransformer ();
3844 transformer .setOutputProperty (OutputKeys .METHOD , "xml" );
3945 transformer .setOutputProperty (OutputKeys .ENCODING , "UTF-8" );
4046 transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
4147 transformer .setOutputProperty (OutputKeys .OMIT_XML_DECLARATION , isOmitXmlDeclaration () ? "yes" : "no" );
4248 try {
4349 transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , String .valueOf (getIndent ()));
4450 }
45- catch (IllegalArgumentException ignored ) {
51+ catch (IllegalArgumentException e ) {
52+ LOGGER .severe ( "An IllegalArgumentException happened while adding the 'indent' property." );
53+ throw new RuntimeException (e );
4654 }
4755
4856 final DocumentType doctype = document .getDoctype ();
@@ -54,17 +62,6 @@ protected Transformer newTransformer(final Document document) throws Transformer
5462 return transformer ;
5563 }
5664
57- protected TransformerFactory newTransformerFactory () {
58- final TransformerFactory transformerFactory = TransformerFactory .newInstance ();
59- try {
60- transformerFactory .setAttribute ("indent-number" , getIndent ());
61- }
62- catch (IllegalArgumentException ignored ) {
63- }
64-
65- return transformerFactory ;
66- }
67-
6865 public int getIndent () {
6966 return indent ;
7067 }
@@ -80,4 +77,5 @@ public boolean isOmitXmlDeclaration() {
8077 public void setOmitXmlDeclaration (boolean omitXmlDeclaration ) {
8178 this .omitXmlDeclaration = omitXmlDeclaration ;
8279 }
80+
8381}
0 commit comments