Skip to content

Commit a8a7f2a

Browse files
committed
Merge Hibernate Tools into ORM : Fix failing test on Oracle and Graal platforms
1 parent ac62e34 commit a8a7f2a

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

tooling/hibernate-reveng/src/main/java/org/hibernate/tool/reveng/internal/xml/AbstractXMLPrettyPrinterStrategy.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
public abstract class AbstractXMLPrettyPrinterStrategy implements XMLPrettyPrinterStrategy {
2424

25+
private static DocumentBuilderFactory DOCUMENT_BUILDER_FACTORU = null;
26+
2527
protected Document newDocument(String xml, String encoding) throws SAXException, IOException, ParserConfigurationException {
26-
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
27-
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
28-
final Document document = dbf
28+
final Document document = getDocumentBuilderFactory()
2929
.newDocumentBuilder()
3030
.parse(new InputSource(new ByteArrayInputStream(xml.getBytes(encoding))));
3131
document.normalize();
@@ -43,4 +43,39 @@ protected void removeWhitespace(final Document document) throws XPathExpressionE
4343
node.getParentNode().removeChild(node);
4444
}
4545
}
46+
47+
private static DocumentBuilderFactory getDocumentBuilderFactory() {
48+
if (DOCUMENT_BUILDER_FACTORU == null) {
49+
DOCUMENT_BUILDER_FACTORU = createDocumentBuilderFactory();
50+
}
51+
return DOCUMENT_BUILDER_FACTORU;
52+
}
53+
54+
private static DocumentBuilderFactory createDocumentBuilderFactory() {
55+
DocumentBuilderFactory result = null;
56+
// Save the documentBuilderFactoryClass and set it to the default jre version
57+
String documentBuilderFactoryClass = System.getProperty( "javax.xml.parsers.DocumentBuilderFactory" );
58+
System.setProperty(
59+
"javax.xml.parsers.DocumentBuilderFactory",
60+
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
61+
try {
62+
result = DocumentBuilderFactory.newInstance();
63+
result.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
64+
}
65+
catch (ParserConfigurationException e) {
66+
throw new RuntimeException(e);
67+
}
68+
finally {
69+
// Restore the documentBuilderFactory class
70+
if (documentBuilderFactoryClass != null) {
71+
System.setProperty(
72+
"javax.xml.parsers.DocumentBuilderFactory",
73+
documentBuilderFactoryClass );
74+
}
75+
else {
76+
System.clearProperty( "javax.xml.parsers.DocumentBuilderFactory" );
77+
}
78+
}
79+
return result;
80+
}
4681
}

tooling/hibernate-reveng/src/main/java/org/hibernate/tool/reveng/internal/xml/TrAXPrettyPrinterStrategy.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
import javax.xml.transform.dom.DOMSource;
1515
import javax.xml.transform.stream.StreamResult;
1616
import java.io.StringWriter;
17+
import java.util.logging.Logger;
1718

1819
public 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

Comments
 (0)