Skip to content

Commit

Permalink
Falling back to standard JDK implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 6, 2014
1 parent c8ff7d9 commit 883ad69
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/main/java/org/jboss/forge/parser/xml/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

Expand Down Expand Up @@ -49,14 +50,14 @@ public static byte[] toXMLByteArray(final Node node)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = createDocumentBuilderFactory();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document root = builder.newDocument();

writeRecursive(root, node);

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Transformer transformer = createTransformerFactory().newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

Expand All @@ -72,6 +73,38 @@ public static byte[] toXMLByteArray(final Node node)
}
}

public static TransformerFactory createTransformerFactory() throws TransformerFactoryConfigurationError
{
TransformerFactory transformerFactory;
try
{
transformerFactory = TransformerFactory.newInstance();
}
catch (Exception e)
{
transformerFactory = TransformerFactory.newInstance(
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
Thread.currentThread().getContextClassLoader());
}
return transformerFactory;
}

public static DocumentBuilderFactory createDocumentBuilderFactory()
{
DocumentBuilderFactory factory;
try
{
factory = DocumentBuilderFactory.newInstance();
}
catch (Exception e)
{
factory = DocumentBuilderFactory.newInstance(
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", Thread.currentThread()
.getContextClassLoader());
}
return factory;
}

public static Node parse(final File file) throws XMLParserException, FileNotFoundException
{
FileInputStream fis = null;
Expand Down Expand Up @@ -112,7 +145,7 @@ public static Node parse(final InputStream stream) throws XMLParserException
return null;
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = createDocumentBuilderFactory();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(stream);
Expand Down Expand Up @@ -231,7 +264,7 @@ private static boolean onlyTextChildren(final org.w3c.dom.Node source)
return true;
}

public enum NodeType
private enum NodeType
{
COMMENT("#comment"),
CDATA_SECTION("#cdata-section");
Expand Down

0 comments on commit 883ad69

Please sign in to comment.