@@ -48,10 +48,14 @@ public class XMLWriter implements SerializerWriter {
48
48
49
49
protected final static Properties defaultProperties = new Properties ();
50
50
static {
51
+ defaultProperties .setProperty (EXistOutputKeys .OMIT_ORIGINAL_XML_DECLARATION , "no" );
51
52
defaultProperties .setProperty (OutputKeys .OMIT_XML_DECLARATION , "no" );
52
53
defaultProperties .setProperty (EXistOutputKeys .XDM_SERIALIZATION , "no" );
53
54
}
54
55
56
+ private static final String DEFAULT_XML_VERSION = "1.0" ;
57
+ private static final String DEFAULT_XML_ENCODING = UTF_8 .name ();
58
+
55
59
protected Writer writer = null ;
56
60
57
61
protected CharacterSet charSet ;
@@ -132,7 +136,7 @@ public void setOutputProperties(final Properties properties) {
132
136
outputProperties = properties ;
133
137
}
134
138
135
- final String encoding = outputProperties .getProperty (OutputKeys .ENCODING , UTF_8 . name () );
139
+ final String encoding = outputProperties .getProperty (OutputKeys .ENCODING , DEFAULT_XML_ENCODING );
136
140
this .charSet = CharacterSet .getCharacterSet (encoding );
137
141
if (this .charSet == null ) {
138
142
throw EX_CHARSET_NULL ;
@@ -531,11 +535,28 @@ protected void writeDeclaration() throws TransformerException {
531
535
outputProperties = new Properties (defaultProperties );
532
536
}
533
537
declarationWritten = true ;
538
+
534
539
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
+
539
560
try {
540
561
writer .write ("<?xml version=\" " );
541
562
writer .write (version );
0 commit comments