Skip to content

Commit 361d2ff

Browse files
Add feature to include standalone='yes' in xml declaration (#745)
1 parent 1cf8c8c commit 361d2ff

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,8 @@ Bas Passon (@bpasson)
271271
* Reported, contributed fix for #646: Deserializing fails when using builder classes
272272
with `Iterable` Collection setters
273273
(2.17.1)
274+
275+
多多冰冰 (@duoduobingbing)
276+
277+
* Contributed #745: Add feature to include `standalone='yes'` in xml declaration
278+
(2.19.0)

release-notes/VERSION-2.x

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Project: jackson-dataformat-xml
44
=== Releases ===
55
------------------------------------------------------------------------
66

7-
2.19.0-rc (07-Apr-2025)
7+
#745: Add feature to include `standalone='yes'` in xml declaration
8+
(contributed by @duoduobingbing)
9+
10+
2.19.0-rc2 (07-Apr-2025)
811

912
#700: Unify testing structure/tools [JSTEP-10]
1013
(fix contributed by Joo Hyuk K)

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public enum Feature implements FormatFeature
5151
*/
5252
WRITE_XML_DECLARATION(false),
5353

54+
/**
55+
* Feature that controls whether XML declaration should include the standalone attribute
56+
* when generator is initialized (true) or not (false). Only honored when
57+
* {@link Feature#WRITE_XML_DECLARATION WRITE_XML_DECLARATION} is enabled
58+
*
59+
* @since 2.19
60+
*/
61+
WRITE_STANDALONE_YES_TO_XML_DECLARATION(false),
62+
5463
/**
5564
* Feature that controls whether output should be done as XML 1.1; if so,
5665
* certain aspects may differ from default (1.0) processing: for example,
@@ -297,15 +306,24 @@ public void initGenerator() throws IOException
297306
_initialized = true;
298307
try {
299308
boolean xmlDeclWritten;
300-
if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures)) {
301-
_xmlWriter.writeStartDocument("UTF-8", "1.1");
302-
xmlDeclWritten = true;
303-
} else if (Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {
304-
_xmlWriter.writeStartDocument("UTF-8", "1.0");
309+
310+
if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) ||
311+
Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {
312+
313+
String xmlVersion = Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) ? "1.1" : "1.0";
314+
String encoding = "UTF-8";
315+
316+
if (Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION.enabledIn(_formatFeatures)) {
317+
_xmlWriter.writeStartDocument(xmlVersion, encoding, true);
318+
} else {
319+
_xmlWriter.writeStartDocument(encoding, xmlVersion);
320+
}
321+
305322
xmlDeclWritten = true;
306323
} else {
307324
xmlDeclWritten = false;
308325
}
326+
309327
// as per [dataformat-xml#172], try adding indentation
310328
if (xmlDeclWritten && (_xmlPrettyPrinter != null)) {
311329
// ... but only if it is likely to succeed:

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestXmlDeclaration.java

+21
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,26 @@ public void testXml11Declaration() throws Exception
3232
String xml = mapper.writeValueAsString(new StringBean("abcd"));
3333
assertEquals(xml, "<?xml version='1.1' encoding='UTF-8'?><StringBean><text>abcd</text></StringBean>");
3434
}
35+
36+
@Test
37+
public void testXml11DeclarationWithStandalone() throws Exception
38+
{
39+
XmlMapper mapper = new XmlMapper();
40+
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
41+
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
42+
mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true);
43+
String xml = mapper.writeValueAsString(new StringBean("abcd"));
44+
assertEquals("<?xml version='1.1' encoding='UTF-8' standalone='yes'?><StringBean><text>abcd</text></StringBean>", xml);
45+
}
46+
47+
@Test
48+
public void testXml10DeclarationWithStandalone() throws Exception
49+
{
50+
XmlMapper mapper = new XmlMapper();
51+
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
52+
mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true);
53+
String xml = mapper.writeValueAsString(new StringBean("abcd"));
54+
assertEquals("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><StringBean><text>abcd</text></StringBean>", xml);
55+
}
3556

3657
}

0 commit comments

Comments
 (0)