Skip to content

Commit 7c4c15f

Browse files
committed
HBX-2916: Move XMLPrettyPrinter to the API and offer the ability to specify the XMLPrettyPrinterStrategy
- Add new test case 'org.hibernate.tool.api.xml.XMLPrettyPrinterTest#testXmlPrettyPrintWithStrategy()' - Add new method 'org.hibernate.tool.api.xml.XMLPrettyPrinter#prettyPrintFile(File,XMLPrettyPrinterStrategy)' Signed-off-by: Koen Aers <[email protected]>
1 parent 002503d commit 7c4c15f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
*
1919
*/
2020
public final class XMLPrettyPrinter {
21-
21+
2222
public static void prettyPrintFile(File file) throws IOException {
23+
prettyPrintFile(file, null);
24+
}
25+
26+
public static void prettyPrintFile(File file, XMLPrettyPrinterStrategy strategy) throws IOException {
2327
String input = readFile(file.getAbsolutePath(), Charset.defaultCharset());
24-
String output = prettyFormat(input);
28+
String output = prettyFormat(input, strategy);
2529
PrintWriter writer = new PrintWriter(file);
2630
writer.print(output);
2731
writer.flush();
@@ -33,9 +37,12 @@ private static String readFile(String path, Charset encoding) throws IOException
3337
return new String(encoded, encoding);
3438
}
3539

36-
private static String prettyFormat(String input) {
40+
private static String prettyFormat(String input, XMLPrettyPrinterStrategy strategy) {
3741
try {
38-
return XMLPrettyPrinterStrategyFactory.newXMLPrettyPrinterStrategy().prettyPrint(input);
42+
if (strategy == null) {
43+
strategy = XMLPrettyPrinterStrategyFactory.newXMLPrettyPrinterStrategy();
44+
}
45+
return strategy.prettyPrint(input);
3946
} catch (Exception e) {
4047
throw new RuntimeException(e); // simple exception handling, please review it
4148
}

orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class XMLPrettyPrinterTest {
2020
" <bar>foobar</bar>\n" +
2121
"</foo>\n";
2222

23+
private static final String XML_COMMENT = "<!-- Just a comment! -->";
24+
2325
private static final String fileName = "foobarfile.xml";
2426

2527
@TempDir
@@ -43,4 +45,18 @@ public void testXmlPrettyPrintDefault() throws Exception {
4345
assertEquals(XML_AFTER, result);
4446
}
4547

48+
@Test
49+
public void testXmlPrettyPrintWithStrategy() throws Exception {
50+
XMLPrettyPrinter.prettyPrintFile(xmlFile, new FooBarStrategy());
51+
String result = Files.readString(xmlFile.toPath());
52+
assertEquals(XML_AFTER + XML_COMMENT, result);
53+
}
54+
55+
public static class FooBarStrategy implements XMLPrettyPrinterStrategy {
56+
@Override
57+
public String prettyPrint(String xml) throws Exception {
58+
return XML_AFTER + XML_COMMENT;
59+
}
60+
}
61+
4662
}

0 commit comments

Comments
 (0)