diff --git a/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java b/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java index ed5dde0fde..5f9bc03cf3 100644 --- a/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java +++ b/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java @@ -18,10 +18,14 @@ * */ public final class XMLPrettyPrinter { - + public static void prettyPrintFile(File file) throws IOException { + prettyPrintFile(file, null); + } + + public static void prettyPrintFile(File file, XMLPrettyPrinterStrategy strategy) throws IOException { String input = readFile(file.getAbsolutePath(), Charset.defaultCharset()); - String output = prettyFormat(input); + String output = prettyFormat(input, strategy); PrintWriter writer = new PrintWriter(file); writer.print(output); writer.flush(); @@ -33,9 +37,12 @@ private static String readFile(String path, Charset encoding) throws IOException return new String(encoded, encoding); } - private static String prettyFormat(String input) { + private static String prettyFormat(String input, XMLPrettyPrinterStrategy strategy) { try { - return XMLPrettyPrinterStrategyFactory.newXMLPrettyPrinterStrategy().prettyPrint(input); + if (strategy == null) { + strategy = XMLPrettyPrinterStrategyFactory.newXMLPrettyPrinterStrategy(); + } + return strategy.prettyPrint(input); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } diff --git a/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java b/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java index 176be73829..5cb4a909b9 100644 --- a/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java +++ b/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java @@ -20,6 +20,8 @@ public class XMLPrettyPrinterTest { " foobar\n" + "\n"; + private static final String XML_COMMENT = ""; + private static final String fileName = "foobarfile.xml"; @TempDir @@ -43,4 +45,18 @@ public void testXmlPrettyPrintDefault() throws Exception { assertEquals(XML_AFTER, result); } + @Test + public void testXmlPrettyPrintWithStrategy() throws Exception { + XMLPrettyPrinter.prettyPrintFile(xmlFile, new FooBarStrategy()); + String result = Files.readString(xmlFile.toPath()); + assertEquals(XML_AFTER + XML_COMMENT, result); + } + + public static class FooBarStrategy implements XMLPrettyPrinterStrategy { + @Override + public String prettyPrint(String xml) throws Exception { + return XML_AFTER + XML_COMMENT; + } + } + }