Skip to content

Commit 24f2c8d

Browse files
committed
U retain doctype when prettifying xml
1 parent 43655c4 commit 24f2c8d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/java/io/github/fvarrui/javapackager/utils/XMLUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.xml.transform.stream.StreamResult;
1212

1313
import org.w3c.dom.Document;
14+
import org.w3c.dom.DocumentType;
1415
import org.w3c.dom.Node;
1516
import org.w3c.dom.NodeList;
1617

@@ -29,13 +30,20 @@ public static final void prettify(File file) throws Exception {
2930
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
3031
DocumentBuilder builder = factory.newDocumentBuilder();
3132
Document document = builder.parse(file);
32-
33+
3334
trimWhitespace(document);
3435

3536
Transformer transformer = TransformerFactory.newInstance().newTransformer();
3637
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
38+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
3739
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
38-
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes");
40+
41+
DocumentType doctype = document.getDoctype();
42+
if(doctype != null) {
43+
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
44+
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
45+
}
46+
3947
transformer.transform(new DOMSource(document), new StreamResult(file));
4048

4149
}

0 commit comments

Comments
 (0)