Skip to content

Commit d614204

Browse files
committed
[feature] Serialize preserved XML Declaration by default
1 parent 978738f commit d614204

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class EXistOutputKeys {
2828
*/
2929
public static final String ITEM_SEPARATOR = "item-separator";
3030

31-
public static final String OUTPUT_DOCTYPE = "output-doctype";
31+
public static final String OMIT_ORIGINAL_XML_DECLARATION = "omit-original-xml-declaration";
32+
33+
public static final String OUTPUT_DOCTYPE = "output-doctype";
3234

3335
public static final String EXPAND_XINCLUDES = "expand-xincludes";
3436

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package org.exist.storage.serializers;
2323

2424
import org.exist.Namespaces;
25+
import org.exist.dom.QName;
2526
import org.exist.dom.persistent.AttrImpl;
2627
import org.exist.dom.persistent.CDATASectionImpl;
2728
import org.exist.dom.persistent.CommentImpl;
@@ -32,8 +33,8 @@
3233
import org.exist.dom.persistent.Match;
3334
import org.exist.dom.persistent.NodeProxy;
3435
import org.exist.dom.persistent.ProcessingInstructionImpl;
35-
import org.exist.dom.QName;
3636
import org.exist.dom.persistent.TextImpl;
37+
import org.exist.dom.persistent.XMLDeclarationImpl;
3738
import org.exist.numbering.NodeId;
3839
import org.exist.storage.DBBroker;
3940
import org.exist.util.Configuration;
@@ -116,7 +117,14 @@ protected void serializeToReceiver(DocumentImpl doc, boolean generateDocEvent) t
116117
documentStarted = true;
117118
}
118119

119-
if (doc.getDoctype() != null){
120+
if (doc.getXmlDeclaration() != null){
121+
if ("no".equals(getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no"))) {
122+
final XMLDeclarationImpl xmlDecl = doc.getXmlDeclaration();
123+
receiver.declaration(xmlDecl.getVersion(), xmlDecl.getEncoding(), xmlDecl.getStandalone());
124+
}
125+
}
126+
127+
if (doc.getDoctype() != null) {
120128
if ("yes".equals(getProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no"))) {
121129
final DocumentTypeImpl docType = (DocumentTypeImpl)doc.getDoctype();
122130
serializeToReceiver(docType, null, docType.getOwnerDocument(), true, null, new TreeSet<>());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public abstract class AbstractSerializer {
6262
protected static final Properties defaultProperties = new Properties();
6363

6464
static {
65-
defaultProperties.setProperty(OutputKeys.ENCODING, UTF_8.name());
66-
defaultProperties.setProperty(OutputKeys.INDENT, "false");
65+
defaultProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no");
66+
defaultProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
67+
defaultProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "no");
6768
}
6869

6970
protected Properties outputProperties;

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ public class XMLWriter implements SerializerWriter {
4848

4949
protected final static Properties defaultProperties = new Properties();
5050
static {
51+
defaultProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no");
5152
defaultProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
5253
defaultProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "no");
5354
}
5455

56+
private static final String DEFAULT_XML_VERSION = "1.0";
57+
private static final String DEFAULT_XML_ENCODING = UTF_8.name();
58+
5559
protected Writer writer = null;
5660

5761
protected CharacterSet charSet;
@@ -132,7 +136,7 @@ public void setOutputProperties(final Properties properties) {
132136
outputProperties = properties;
133137
}
134138

135-
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING, UTF_8.name());
139+
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING, DEFAULT_XML_ENCODING);
136140
this.charSet = CharacterSet.getCharacterSet(encoding);
137141
if(this.charSet == null) {
138142
throw EX_CHARSET_NULL;
@@ -531,11 +535,28 @@ protected void writeDeclaration() throws TransformerException {
531535
outputProperties = new Properties(defaultProperties);
532536
}
533537
declarationWritten = true;
538+
534539
final String omitXmlDecl = outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
535-
if("no".equals(omitXmlDecl)) {
536-
final String version = outputProperties.getProperty(OutputKeys.VERSION, "1.0");
537-
final String standalone = outputProperties.getProperty(OutputKeys.STANDALONE);
538-
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING, UTF_8.name());
540+
if ("no".equals(omitXmlDecl)) {
541+
542+
final String version;
543+
final String encoding;
544+
@Nullable final String standalone;
545+
546+
final String omitOriginalXmlDecl = outputProperties.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "yes");
547+
if (originalXmlDecl != null && "no".equals(omitOriginalXmlDecl)) {
548+
// get the fields of the persisted xml declaration, but overridden with any properties from the serialization properties
549+
version = outputProperties.getProperty(OutputKeys.VERSION, (originalXmlDecl.version != null ? originalXmlDecl.version : DEFAULT_XML_VERSION));
550+
encoding = outputProperties.getProperty(OutputKeys.ENCODING, (originalXmlDecl.encoding != null ? originalXmlDecl.encoding : DEFAULT_XML_ENCODING));
551+
standalone = outputProperties.getProperty(OutputKeys.STANDALONE, originalXmlDecl.standalone);
552+
553+
} else {
554+
// get the fields of the declaration from the serialization properties
555+
version = outputProperties.getProperty(OutputKeys.VERSION, DEFAULT_XML_VERSION);
556+
encoding = outputProperties.getProperty(OutputKeys.ENCODING, DEFAULT_XML_ENCODING);
557+
standalone = outputProperties.getProperty(OutputKeys.STANDALONE);
558+
}
559+
539560
try {
540561
writer.write("<?xml version=\"");
541562
writer.write(version);

0 commit comments

Comments
 (0)