diff --git a/exist-core/src/main/java/org/exist/util/serializer/IndentingXMLWriter.java b/exist-core/src/main/java/org/exist/util/serializer/IndentingXMLWriter.java index 12a09abe46e..79f3152de8c 100644 --- a/exist-core/src/main/java/org/exist/util/serializer/IndentingXMLWriter.java +++ b/exist-core/src/main/java/org/exist/util/serializer/IndentingXMLWriter.java @@ -151,7 +151,7 @@ public void endDocument() throws TransformerException { @Override public void endDocumentType() throws TransformerException { super.endDocumentType(); - super.characters("\n"); + indent(); sameline = false; } @@ -197,11 +197,7 @@ protected void pushWhitespacePreserve(final CharSequence value) { protected void popWhitespacePreserve() { if (!whitespacePreserveStack.isEmpty() && Math.abs(whitespacePreserveStack.peek()) > level) { whitespacePreserveStack.pop(); - if (whitespacePreserveStack.isEmpty() || whitespacePreserveStack.peek() >= 0) { - whitespacePreserve = false; - } else { - whitespacePreserve = true; - } + whitespacePreserve = !whitespacePreserveStack.isEmpty() && whitespacePreserveStack.peek() < 0; } } diff --git a/exist-core/src/test/java/org/exist/util/serializer/HTML5WriterTest.java b/exist-core/src/test/java/org/exist/util/serializer/HTML5WriterTest.java index fc061031b91..a2bc2097f23 100644 --- a/exist-core/src/test/java/org/exist/util/serializer/HTML5WriterTest.java +++ b/exist-core/src/test/java/org/exist/util/serializer/HTML5WriterTest.java @@ -42,7 +42,7 @@ public void setUp() throws Exception { @Test public void testAttributeWithBooleanValue() throws Exception { - final String expected = "\n"; + final String expected = ""; final QName elQName = new QName("input"); writer.startElement(elQName); writer.attribute("checked", "checked"); @@ -54,7 +54,7 @@ public void testAttributeWithBooleanValue() throws Exception { @Test public void testAttributeWithNonBooleanValue() throws Exception { - final String expected = "\n"; + final String expected = ""; final QName elQName = new QName("input"); writer.startElement(elQName); writer.attribute("name", "name"); @@ -66,7 +66,7 @@ public void testAttributeWithNonBooleanValue() throws Exception { @Test public void testAttributeQNameWithBooleanValue() throws Exception { - final String expected = "\n"; + final String expected = ""; final QName elQName = new QName("input"); final QName attrQName = new QName("checked"); writer.startElement(elQName); @@ -79,7 +79,7 @@ public void testAttributeQNameWithBooleanValue() throws Exception { @Test public void testAttributeQNameWithNonBooleanValue() throws Exception { - final String expected = "\n"; + final String expected = ""; final QName elQName = new QName("input"); final QName attrQName = new QName("name"); writer.startElement(elQName); diff --git a/exist-core/src/test/java/org/exist/xmldb/SerializationTest.java b/exist-core/src/test/java/org/exist/xmldb/SerializationTest.java index b4de05229db..e087e8f7d77 100644 --- a/exist-core/src/test/java/org/exist/xmldb/SerializationTest.java +++ b/exist-core/src/test/java/org/exist/xmldb/SerializationTest.java @@ -43,10 +43,12 @@ import org.xmlunit.builder.Input; import org.xmlunit.diff.Diff; +import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import java.util.Arrays; +import static javax.xml.transform.OutputKeys.INDENT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -93,8 +95,11 @@ public class SerializationTest { private static final XmldbURI TEST_XML_DOC_WITH_DOCTYPE_URI = XmldbURI.create("test-with-doctype.xml"); private static final String XML_WITH_DOCTYPE = - "\n" + - ""; + """ + + + The Title + """; private static final XmldbURI TEST_XML_DOC_WITH_XMLDECL_URI = XmldbURI.create("test-with-xmldecl.xml"); @@ -118,7 +123,7 @@ public static java.util.Collection data() { private Collection testCollection; - private final String getBaseUri() { + private String getBaseUri() { return baseUri.replace(PORT_PLACEHOLDER, Integer.toString(existWebServer.getPort())); } @@ -186,7 +191,7 @@ public void getDocTypeNo() throws XMLDBException { try { final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString()); testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no"); - assertEquals("", res.getContent()); + assertEquals("\n The Title\n", res.getContent()); } finally { if (prevOutputDocType != null) { testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, prevOutputDocType); @@ -215,7 +220,7 @@ public void getXmlDeclDefault() throws XMLDBException { } @Test - public void getXmlDeclNo() throws XMLDBException { + public void getOmitOriginalXmlDeclNo() throws XMLDBException { final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION); try { final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString()); @@ -229,7 +234,7 @@ public void getXmlDeclNo() throws XMLDBException { } @Test - public void getXmlDeclYes() throws XMLDBException { + public void getOmitOriginalXmlDeclYes() throws XMLDBException { final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION); try { final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString()); @@ -241,6 +246,52 @@ public void getXmlDeclYes() throws XMLDBException { } } } + @Test + public void getOmitXmlDeclNo() throws XMLDBException { + final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION); + try { + final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString()); + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + assertEquals(XML_WITH_XMLDECL, res.getContent()); + } finally { + if (prevOmitXmlDecl != null) { + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl); + } + } + } + + @Test + public void getOmitXmlDeclYes() throws XMLDBException { + final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION); + try { + final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString()); + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + assertEquals(XML_WITH_XMLDECL, res.getContent()); + } finally { + if (prevOmitXmlDecl != null) { + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl); + } + } + } + + @Test + public void getOmitAllXmlDeclYes() throws XMLDBException { + final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION); + final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION); + try { + final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString()); + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "yes"); + assertEquals("", res.getContent()); + } finally { + if (prevOmitXmlDecl != null) { + testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl); + } + if (prevOmitOriginalXmlDecl != null) { + testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, prevOmitOriginalXmlDecl); + } + } + } private static void assertXMLEquals(final String expected, final Resource actual) throws XMLDBException { final Source srcExpected = Input.fromString(expected).build(); @@ -271,7 +322,11 @@ public void setUp() throws XMLDBException { final XMLResource res2 = testCollection.createResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString(), XMLResource.class); res2.setContent(XML_WITH_XMLDECL); testCollection.storeResource(res2); - } + + // FIXME (JL): local and remote collections apparently have different output properties set + // local collections have INDENT set to "yes" whereas remote collections have "no" by default + testCollection.setProperty(INDENT, "yes"); + } @After public void tearDown() throws XMLDBException { diff --git a/exist-core/src/test/xquery/xquery3/serialize.xql b/exist-core/src/test/xquery/xquery3/serialize.xql index bea438d425f..fd49227a8e8 100644 --- a/exist-core/src/test/xquery/xquery3/serialize.xql +++ b/exist-core/src/test/xquery/xquery3/serialize.xql @@ -847,23 +847,27 @@ function ser:serialize-xml-134() { }; declare - %test:assertEquals(' ') + %test:assertEquals('') function ser:serialize-html-5-boolean-attribute-names() { -