Skip to content

Commit 7742ab1

Browse files
committed
[enhancement] add new exist serialization option
"exist:insert-final-newline" enforces a newline at the end of an XML document. Since a lot of editors will insert a newline at the end of a file, this option will lead to less meaningless changes in version control (e.g. git). Also, tools like diff can do a better job when each file ends with a newline.
1 parent 13aadd1 commit 7742ab1

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

exist-core/src/main/java/org/exist/storage/serializers/EXistOutputKeys.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,14 @@ public class EXistOutputKeys {
104104
* Set to "yes" to enable xdm-serialization rules, false otherwise.
105105
*/
106106
public final static String XDM_SERIALIZATION = "xdm-serialization";
107+
108+
/**
109+
* Enforce newline at the end of an XML document.
110+
*
111+
* Since a lot of editors set this enforcing it on
112+
* serialization out of exist-db will lead to less
113+
* meaningless changes in git and tools like diff will
114+
* be able to provide more meaningful as well.
115+
*/
116+
public final static String INSERT_FINAL_NEWLINE = "insert-final-newline";
107117
}

exist-core/src/main/java/org/exist/util/serializer/IndentingXMLWriter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ public void processingInstruction(final String target, final String data) throws
140140
afterTag = true;
141141
}
142142

143+
@Override
144+
public void endDocument() throws TransformerException {
145+
super.endDocument();
146+
if ("yes".equals(outputProperties.getProperty(EXistOutputKeys.INSERT_FINAL_NEWLINE, "no"))) {
147+
super.characters("\n");
148+
}
149+
}
150+
143151
@Override
144152
public void endDocumentType() throws TransformerException {
145153
super.endDocumentType();

0 commit comments

Comments
 (0)