Skip to content

Commit f576a8b

Browse files
authored
Refactoring handling of XmlPrologDirective handling (#850)
1 parent b0c4e72 commit f576a8b

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/main/java/tools/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public class ToXmlGenerator
100100
*/
101101

102102
/**
103-
* Document Type Declaration to write, if any; {@code null} if none.
103+
* XML directives (DTD, Comments, PIs) to write, if any.
104104
*
105105
* @since 3.2
106106
*/
107-
protected DTD _dtd;
107+
protected List<XmlPrologDirective> _prologDirectives;
108108

109109
/*
110110
/**********************************************************************
@@ -260,8 +260,10 @@ public void initGenerator() throws JacksonException
260260
}
261261

262262
// 19-Apr-2026, tatu: [dataformat-xml#150] Allow outputting DTD
263-
if (_dtd != null) {
264-
_dtd.write(this, _xmlWriter);
263+
if (_prologDirectives != null) {
264+
for (XmlPrologDirective d : _prologDirectives) {
265+
d.write(this, _xmlWriter);
266+
}
265267
}
266268

267269
} catch (XMLStreamException e) {
@@ -275,12 +277,12 @@ public void initGenerator() throws JacksonException
275277
*
276278
* @since 3.2
277279
*/
278-
public void initConfig(DTD dtd)
280+
public void initProlog(List<XmlPrologDirective> directives)
279281
{
280282
if (_initialized) { // sanity check
281283
_reportError("Internal error: cannot call `initConfig()` after generator already initialized");
282284
}
283-
_dtd = dtd;
285+
_prologDirectives = directives;
284286
}
285287

286288
/*

src/main/java/tools/jackson/dataformat/xml/ser/XmlGeneratorInitializer.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package tools.jackson.dataformat.xml.ser;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import tools.jackson.core.JacksonException;
47
import tools.jackson.core.JsonGenerator;
8+
import tools.jackson.core.exc.StreamWriteException;
9+
510
import tools.jackson.databind.*;
611
import tools.jackson.databind.cfg.GeneratorInitializer;
712

@@ -22,12 +27,14 @@
2227
public class XmlGeneratorInitializer
2328
implements GeneratorInitializer
2429
{
25-
protected DTD _dtd;
30+
protected List<XmlPrologDirective> _directives;
31+
32+
protected boolean _hasDTD;
2633

2734
@Override
2835
public void initialize(SerializationConfig config, JsonGenerator g) throws JacksonException {
2936
if (g instanceof ToXmlGenerator xg) {
30-
xg.initConfig(_dtd);
37+
xg.initProlog(_directives);
3138
}
3239
}
3340

@@ -60,7 +67,18 @@ public XmlGeneratorInitializer addDTD(String rootName,
6067
* @return This initializer for call chaining
6168
*/
6269
public XmlGeneratorInitializer addDTD(DTD dtd) {
63-
_dtd = dtd;
70+
if (_hasDTD) {
71+
throw new StreamWriteException(null, "Cannot add another `DTD`, initializer already has one");
72+
}
73+
_hasDTD = true;
74+
return _add(dtd);
75+
}
76+
77+
protected XmlGeneratorInitializer _add(XmlPrologDirective d) {
78+
if (_directives == null) {
79+
_directives = new ArrayList<>();
80+
}
81+
_directives.add(d);
6482
return this;
6583
}
6684
}

src/main/java/tools/jackson/dataformat/xml/ser/XmlPrologDirective.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
public interface XmlPrologDirective
1414
{
1515
/**
16-
* Method to call to actually write out the directive.
16+
* Method to call to actually write out the directive using given
17+
* {@link XMLStreamWriter2}. {@link ToXmlGenerator} is only passed
18+
* in case access to configuration was needed.
19+
*
20+
* @param xmlGen Generator that called this method: MUST NOT call
21+
* its output methods, only to be used for configuration access
22+
* @param sw Writer to use for actual output of XML event
1723
*/
1824
public void write(ToXmlGenerator xmlGen, XMLStreamWriter2 sw)
1925
throws XMLStreamException;

0 commit comments

Comments
 (0)