diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index a5c45043..50378fab 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -271,3 +271,8 @@ Bas Passon (@bpasson) * Reported, contributed fix for #646: Deserializing fails when using builder classes with `Iterable` Collection setters (2.17.1) + +多多冰冰 (@duoduobingbing) + +* Contributed #745: Add feature to include `standalone='yes'` in xml declaration + (2.19.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index f54acff3..56d5dd3c 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -4,7 +4,10 @@ Project: jackson-dataformat-xml === Releases === ------------------------------------------------------------------------ -2.19.0-rc (07-Apr-2025) +#745: Add feature to include `standalone='yes'` in xml declaration + (contributed by @duoduobingbing) + +2.19.0-rc2 (07-Apr-2025) #700: Unify testing structure/tools [JSTEP-10] (fix contributed by Joo Hyuk K) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java index 411317a8..ebe4a07d 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java @@ -51,6 +51,15 @@ public enum Feature implements FormatFeature */ WRITE_XML_DECLARATION(false), + /** + * Feature that controls whether XML declaration should include the standalone attribute + * when generator is initialized (true) or not (false). Only honored when + * {@link Feature#WRITE_XML_DECLARATION WRITE_XML_DECLARATION} is enabled + * + * @since 2.19 + */ + WRITE_STANDALONE_YES_TO_XML_DECLARATION(false), + /** * Feature that controls whether output should be done as XML 1.1; if so, * certain aspects may differ from default (1.0) processing: for example, @@ -297,15 +306,24 @@ public void initGenerator() throws IOException _initialized = true; try { boolean xmlDeclWritten; - if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures)) { - _xmlWriter.writeStartDocument("UTF-8", "1.1"); - xmlDeclWritten = true; - } else if (Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) { - _xmlWriter.writeStartDocument("UTF-8", "1.0"); + + if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) || + Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) { + + String xmlVersion = Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) ? "1.1" : "1.0"; + String encoding = "UTF-8"; + + if (Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION.enabledIn(_formatFeatures)) { + _xmlWriter.writeStartDocument(xmlVersion, encoding, true); + } else { + _xmlWriter.writeStartDocument(encoding, xmlVersion); + } + xmlDeclWritten = true; } else { xmlDeclWritten = false; } + // as per [dataformat-xml#172], try adding indentation if (xmlDeclWritten && (_xmlPrettyPrinter != null)) { // ... but only if it is likely to succeed: diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestXmlDeclaration.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestXmlDeclaration.java index 1a3d36fc..39993066 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestXmlDeclaration.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestXmlDeclaration.java @@ -32,5 +32,26 @@ public void testXml11Declaration() throws Exception String xml = mapper.writeValueAsString(new StringBean("abcd")); assertEquals(xml, "abcd"); } + + @Test + public void testXml11DeclarationWithStandalone() throws Exception + { + XmlMapper mapper = new XmlMapper(); + mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + mapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true); + mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true); + String xml = mapper.writeValueAsString(new StringBean("abcd")); + assertEquals("abcd", xml); + } + + @Test + public void testXml10DeclarationWithStandalone() throws Exception + { + XmlMapper mapper = new XmlMapper(); + mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true); + String xml = mapper.writeValueAsString(new StringBean("abcd")); + assertEquals("abcd", xml); + } }