Skip to content

Commit 06caa02

Browse files
committed
Refactoring post #150
1 parent 1e413c6 commit 06caa02

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package tools.jackson.dataformat.xml.ser;
22

3+
import javax.xml.stream.XMLStreamException;
4+
5+
import org.codehaus.stax2.XMLStreamWriter2;
6+
37
/**
48
* Value container to represent XML Document Type Declaration,
59
* to be written using {@link XmlGeneratorInitializer}.
@@ -8,7 +12,9 @@
812
*/
913
public record DTD(String rootName,
1014
String systemId, String publicId,
11-
String internalSubset) {
15+
String internalSubset)
16+
implements XmlPrologDirective
17+
{
1218
public DTD {
1319
rootName = _nonEmptyNonNull("rootName", rootName);
1420
systemId = _emptyToNull(systemId);
@@ -27,4 +33,11 @@ static String _nonEmptyNonNull(String prop, String str) {
2733
}
2834
return str;
2935
}
36+
37+
@Override
38+
public void write(ToXmlGenerator xmlGen, XMLStreamWriter2 xmlWriter)
39+
throws XMLStreamException
40+
{
41+
xmlWriter.writeDTD(rootName(), systemId(), publicId(), internalSubset());
42+
}
3043
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ public void initGenerator() throws JacksonException
261261

262262
// 19-Apr-2026, tatu: [dataformat-xml#150] Allow outputting DTD
263263
if (_dtd != null) {
264-
_xmlWriter.writeDTD(_dtd.rootName(), _dtd.systemId(), _dtd.publicId(),
265-
_dtd.internalSubset());
264+
_dtd.write(this, _xmlWriter);
266265
}
267266

268267
} catch (XMLStreamException e) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tools.jackson.dataformat.xml.ser;
2+
3+
import javax.xml.stream.XMLStreamException;
4+
5+
import org.codehaus.stax2.XMLStreamWriter2;
6+
7+
/**
8+
* Common API for XML nodes -- Comments, DTDs, Processing Instructions -- to
9+
* be written <b>before</b> actual XML Document (root node).
10+
*
11+
* @since 3.2
12+
*/
13+
public interface XmlPrologDirective
14+
{
15+
/**
16+
* Method to call to actually write out the directive.
17+
*/
18+
public void write(ToXmlGenerator xmlGen, XMLStreamWriter2 sw)
19+
throws XMLStreamException;
20+
}

0 commit comments

Comments
 (0)