Skip to content

Commit 0a287d0

Browse files
committed
[feature] Enable output XML Declaration by default in APIs
1 parent f6bcab4 commit 0a287d0

File tree

11 files changed

+153
-69
lines changed

11 files changed

+153
-69
lines changed

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,15 @@ public void doGet(final DBBroker broker, final Txn transaction, final HttpServle
365365
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, option);
366366
} else {
367367
// set omit-xml-declaration by configuration
368-
final String omitXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_XML_DECLARATION, "yes");
368+
final String omitXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_XML_DECLARATION, "no");
369369
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration);
370370
}
371371
if ((option = getParameter(request, Omit_Original_Xml_Declaration)) != null) {
372372
// take user query-string specified omit-original-xml-declaration setting
373373
outputProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, option);
374374
} else {
375375
// set omit-original-xml-declaration by configuration
376-
final String omitOriginalXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION, "yes");
376+
final String omitOriginalXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION, "no");
377377
outputProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, omitOriginalXmlDeclaration);
378378
}
379379
if ((option = getParameter(request, Source)) != null && !safeMode) {

exist-core/src/main/java/org/exist/xmldb/LocalCollection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ public class LocalCollection extends AbstractLocal implements EXistCollection {
9090

9191
private final static Properties defaultProperties = new Properties();
9292
static {
93-
defaultProperties.setProperty(OutputKeys.ENCODING, UTF_8.name());
9493
defaultProperties.setProperty(OutputKeys.INDENT, "yes");
9594
defaultProperties.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes");
9695
defaultProperties.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "no");
9796
defaultProperties.setProperty(NORMALIZE_HTML, "no");
97+
defaultProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
98+
defaultProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no");
9899
defaultProperties.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "yes");
99100
}
100101

exist-core/src/main/java/org/exist/xmlrpc/RpcConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,12 @@ private String getDocumentAsString(final XmldbURI docUri, final Map<String, Obje
664664

665665
private void serialize(final DBBroker broker, final Properties properties, final ConsumerE<Serializer, SAXException> toSaxFunction, final Writer writer) throws SAXException, IOException {
666666
if (!properties.containsKey(OutputKeys.OMIT_XML_DECLARATION)) {
667-
final String omitXmlDeclaration = broker.getConfiguration().getProperty(Serializer.OMIT_XML_DECLARATION_ATTRIBUTE, "yes");
667+
final String omitXmlDeclaration = broker.getConfiguration().getProperty(Serializer.OMIT_XML_DECLARATION_ATTRIBUTE, "no");
668668
properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration);
669669
}
670670

671671
if (!properties.containsKey(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION)) {
672-
final String omitOriginalXmlDeclaration = broker.getConfiguration().getProperty(Serializer.OMIT_ORIGINAL_XML_DECLARATION_ATTRIBUTE, "yes");
672+
final String omitOriginalXmlDeclaration = broker.getConfiguration().getProperty(Serializer.OMIT_ORIGINAL_XML_DECLARATION_ATTRIBUTE, "no");
673673
properties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, omitOriginalXmlDeclaration);
674674
}
675675

exist-core/src/test/java/org/exist/backup/XMLDBBackupTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ public void backupRestore() throws XMLDBException, SAXException, IOException, UR
119119

120120
final Resource doc1 = testCollection.getResource(DOC1_NAME);
121121
assertNotNull(doc1);
122-
final Source expected = Input.fromString(doc1Content).build();
122+
123+
// NOTE(AR) that org.exist.backup.Backup calls defaultOutputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
124+
// NOTE(AR) that org.exist.backup.SystemExport also calls defaultOutputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
125+
// TODO(AR) consider whether the backup/export should be injecting a XML Declaration that was not previously present, or should default to EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION
126+
final Source expected = Input.fromString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + doc1Content).build();
127+
123128
final Source actual = Input.fromString(doc1.getContent().toString()).build();
124129
final Diff diff = DiffBuilder.compare(expected)
125130
.withTest(actual)

exist-core/src/test/java/org/exist/http/RESTServiceTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private static String getResourceUri() {
234234
}
235235

236236
private static String getResourceWithDocTypeUri() {
237-
return getServerUri() + TEST_DOCTYPE_COLLECTION_URI.append(TEST_XML_DOC_WITH_DOCTYPE_URI);
237+
return getServerUri() + TEST_DOCTYPE_COLLECTION_URI.append(TEST_XML_DOC_WITH_DOCTYPE_URI) + "?_omit-xml-declaration=yes";
238238
}
239239

240240
private static String getResourceWithXmlDeclUri() {
@@ -757,7 +757,7 @@ public void requestGetParameterFromModule() throws IOException {
757757

758758
/* execute the stored xquery a few times */
759759
for (int i = 0; i < 5; i++) {
760-
final HttpURLConnection connect = getConnection(getCollectionUri() + "/requestparameter.xql?doc=somedoc" + i);
760+
final HttpURLConnection connect = getConnection(getCollectionUri() + "/requestparameter.xql?doc=somedoc" + i + "&_omit-xml-declaration=yes");
761761
try {
762762
connect.setRequestProperty("Authorization", "Basic " + credentials);
763763
connect.setRequestMethod("GET");
@@ -831,7 +831,7 @@ public void execGuestQueryWithNoAuth() throws IOException {
831831
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwxrw-r-x");
832832

833833
// call the auth.xq
834-
final String uri = getCollectionUri() + "/auth.xq";
834+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
835835
final HttpURLConnection connect = getConnection(uri);
836836
try {
837837
connect.setRequestMethod("GET");
@@ -876,7 +876,7 @@ public void execQueryWithBasicAuth() throws IOException {
876876
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwxrw-r--");
877877

878878
// call the auth.xq
879-
final String uri = getCollectionUri() + "/auth.xq";
879+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
880880
final HttpURLConnection connect = getConnection(uri);
881881
try {
882882
connect.setRequestProperty("Authorization", "Basic " + credentials);
@@ -922,7 +922,7 @@ public void execQueryWithBasicAuthCaseInsensitive() throws IOException {
922922
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwxrw-r--");
923923

924924
// call the auth.xq
925-
final String uri = getCollectionUri() + "/auth.xq";
925+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
926926
final HttpURLConnection connect = getConnection(uri);
927927
try {
928928
connect.setRequestProperty("Authorization", "bAsiC " + credentials); // NOTE(AR): Intentional use of 'bAsiC' to test case-insensitive scheme matching
@@ -968,7 +968,7 @@ public void execSetUidQueryWithNoAuth() throws IOException {
968968
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwsr--r-x");
969969

970970
// call the auth.xq
971-
final String uri = getCollectionUri() + "/auth.xq";
971+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
972972
final HttpURLConnection connect = getConnection(uri);
973973
try {
974974
connect.setRequestMethod("GET");
@@ -1019,7 +1019,7 @@ public void execSetUidQueryWithBasicAuth() throws IOException {
10191019
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwsr--r-x");
10201020

10211021
// call the auth.xq
1022-
final String uri = getCollectionUri() + "/auth.xq";
1022+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
10231023
final HttpURLConnection connect = getConnection(uri);
10241024
try {
10251025
connect.setRequestMethod("GET");
@@ -1088,7 +1088,7 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
10881088
chmod(XmldbURI.ROOT_COLLECTION + "/test/auth.xq", "rwsr--r-x");
10891089

10901090
// call the auth.xq
1091-
final String uri = getCollectionUri() + "/auth.xq";
1091+
final String uri = getCollectionUri() + "/auth.xq?_omit-xml-declaration=yes";
10921092
final HttpURLConnection connect = getConnection(uri);
10931093
try {
10941094
connect.setRequestMethod("GET");
@@ -1136,7 +1136,7 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
11361136
// all the tests with EncodedPath in function declaration aim to test rest server ability to handle special characters
11371137
@Test
11381138
public void doGetEncodedPath() throws IOException {
1139-
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1139+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml?_omit-xml-declaration=yes";
11401140
final HttpURLConnection connect = getConnection(DOC_URI);
11411141
try {
11421142
connect.setRequestMethod("GET");
@@ -1177,7 +1177,7 @@ public void doHeadEncodedPath() throws IOException {
11771177

11781178
@Test
11791179
public void doPutEncodedPath() throws IOException {
1180-
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1180+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml?_omit-xml-declaration=yes";
11811181
final HttpURLConnection connect = getConnection(DOC_URI);
11821182
final HttpURLConnection getConnect = getConnection(DOC_URI);
11831183
String data = "<foobar/>";
@@ -1305,7 +1305,7 @@ public void getDocTypeDefault() throws IOException {
13051305

13061306
@Test
13071307
public void getDocTypeNo() throws IOException {
1308-
final HttpURLConnection connect = getConnection(getResourceWithDocTypeUri() + "?_output-doctype=no");
1308+
final HttpURLConnection connect = getConnection(getResourceWithDocTypeUri() + "&_output-doctype=no");
13091309
try {
13101310
connect.setRequestMethod("GET");
13111311
connect.connect();
@@ -1330,7 +1330,7 @@ public void getDocTypeNo() throws IOException {
13301330

13311331
@Test
13321332
public void getDocTypeYes() throws IOException {
1333-
final HttpURLConnection connect = getConnection(getResourceWithDocTypeUri() + "?_output-doctype=yes");
1333+
final HttpURLConnection connect = getConnection(getResourceWithDocTypeUri() + "&_output-doctype=yes");
13341334
try {
13351335
connect.setRequestMethod("GET");
13361336
connect.connect();
@@ -1419,7 +1419,8 @@ public void getXmlDeclDefault() throws IOException {
14191419

14201420
final String response = readResponse(connect.getInputStream());
14211421

1422-
assertEquals("<bookmap id=\"bookmap-2\"/>\r\n", response);
1422+
assertEquals("<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\r\n" +
1423+
"<bookmap id=\"bookmap-2\"/>\r\n", response);
14231424

14241425
} finally {
14251426
connect.disconnect();
@@ -1515,7 +1516,7 @@ private void doPut(final String data, final String path, final int responseCode)
15151516

15161517
private void doStoredQuery(final boolean cacheHeader, final boolean wrap) throws IOException {
15171518

1518-
String uri = getCollectionUri() + "/test.xq?p=Hello";
1519+
String uri = getCollectionUri() + "/test.xq?p=Hello&&_omit-xml-declaration=yes";
15191520
if(wrap) {
15201521
uri += "&_wrap=yes";
15211522
}

exist-core/src/test/java/org/exist/xmldb/SerializationTest.java

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.xmlunit.builder.Input;
4444
import org.xmlunit.diff.Diff;
4545

46+
import javax.xml.transform.OutputKeys;
4647
import javax.xml.transform.Source;
4748

4849
import java.util.Arrays;
@@ -114,6 +115,12 @@ private final String getBaseUri() {
114115
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
115116
"<bookmap id=\"bookmap-1\"/>";
116117

118+
private static final XmldbURI TEST_XML_DOC_WITH_XMLDECL_URI = XmldbURI.create("test-with-xmldecl.xml");
119+
120+
private static final String XML_WITH_XMLDECL =
121+
"<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n" +
122+
"<bookmap id=\"bookmap-2\"/>";
123+
117124
private Collection testCollection;
118125

119126
@Test
@@ -170,18 +177,31 @@ public void xqueryUpdateNsTest() throws XMLDBException {
170177

171178
@Test
172179
public void getDocTypeDefault() throws XMLDBException {
173-
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
174-
assertEquals(XML_WITH_DOCTYPE, res.getContent());
180+
final String prevOutputOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
181+
try {
182+
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
183+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
184+
assertEquals(XML_WITH_DOCTYPE, res.getContent());
185+
} finally {
186+
if (prevOutputOmitXmlDecl != null) {
187+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOutputOmitXmlDecl);
188+
}
189+
}
175190
}
176191

177192
@Test
178193
public void getDocTypeNo() throws XMLDBException {
194+
final String prevOutputOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
179195
final String prevOutputDocType = testCollection.getProperty(EXistOutputKeys.OUTPUT_DOCTYPE);
180196
try {
181197
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
182-
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "NO");
198+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
199+
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no");
183200
assertEquals("<bookmap id=\"bookmap-1\"/>", res.getContent());
184201
} finally {
202+
if (prevOutputOmitXmlDecl != null) {
203+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOutputOmitXmlDecl);
204+
}
185205
if (prevOutputDocType != null) {
186206
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, prevOutputDocType);
187207
}
@@ -190,18 +210,67 @@ public void getDocTypeNo() throws XMLDBException {
190210

191211
@Test
192212
public void getDocTypeYes() throws XMLDBException {
213+
final String prevOutputOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
193214
final String prevOutputDocType = testCollection.getProperty(EXistOutputKeys.OUTPUT_DOCTYPE);
194215
try {
195216
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
217+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
196218
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "yes");
197219
assertEquals(XML_WITH_DOCTYPE, res.getContent());
198220
} finally {
221+
if (prevOutputOmitXmlDecl != null) {
222+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOutputOmitXmlDecl);
223+
}
199224
if (prevOutputDocType != null) {
200225
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, prevOutputDocType);
201226
}
202227
}
203228
}
204229

230+
@Test
231+
public void getXmlDeclDefault() throws XMLDBException {
232+
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
233+
assertEquals(XML_WITH_XMLDECL, res.getContent());
234+
}
235+
236+
@Test
237+
public void getXmlDeclNo() throws XMLDBException {
238+
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
239+
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
240+
try {
241+
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
242+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
243+
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no");
244+
assertEquals(XML_WITH_XMLDECL, res.getContent());
245+
} finally {
246+
if (prevOmitXmlDecl != null) {
247+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
248+
}
249+
if (prevOmitOriginalXmlDecl != null) {
250+
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, prevOmitOriginalXmlDecl);
251+
}
252+
}
253+
}
254+
255+
@Test
256+
public void getXmlDeclYes() throws XMLDBException {
257+
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
258+
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
259+
try {
260+
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
261+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
262+
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "yes");
263+
assertEquals("<bookmap id=\"bookmap-2\"/>", res.getContent());
264+
} finally {
265+
if (prevOmitXmlDecl != null) {
266+
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
267+
}
268+
if (prevOmitOriginalXmlDecl != null) {
269+
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, prevOmitOriginalXmlDecl);
270+
}
271+
}
272+
}
273+
205274
private static void assertXMLEquals(final String expected, final Resource actual) throws XMLDBException {
206275
final Source srcExpected = Input.fromString(expected).build();
207276
final Source srcActual = Input.fromString(actual.getContent().toString()).build();
@@ -227,6 +296,10 @@ public void setUp() throws XMLDBException {
227296
final XMLResource res1 = testCollection.createResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString(), XMLResource.class);
228297
res1.setContent(XML_WITH_DOCTYPE);
229298
testCollection.storeResource(res1);
299+
300+
final XMLResource res2 = testCollection.createResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString(), XMLResource.class);
301+
res2.setContent(XML_WITH_XMLDECL);
302+
testCollection.storeResource(res2);
230303
}
231304

232305
@After

exist-distribution/src/main/config/conf.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@
824824
825825
-->
826826
<serializer add-exist-id="none" compress-output="no"
827-
omit-xml-declaration="yes" omit-original-xml-declaration="yes"
827+
omit-xml-declaration="no" omit-original-xml-declaration="no"
828828
output-doctype="yes" enable-xinclude="yes"
829829
enable-xsl="no" indent="yes" match-tagging-attributes="no"
830830
match-tagging-elements="no">

exist-distribution/src/main/config/webdav.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@
2828
#process-xsl-pi=no
2929
#encoding=UTF-8
3030
#omit-xml-declaration=no
31-
#output-doctype=yes
31+
#output-doctype=yes
32+
#omit-xml-declaration=no
33+
#omit-original-xml-declaration=no

extensions/webdav/src/main/resources/org/exist/webdav/webdav.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
#expand-xincludes=no
2828
#process-xsl-pi=no
2929
#encoding=UTF-8
30-
omit-xml-declaration=yes
31-
omit-original-xml-declaration=yes
30+
omit-xml-declaration=no
31+
omit-original-xml-declaration=no
3232
output-doctype=yes

0 commit comments

Comments
 (0)