Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public class ToXmlGenerator
*/

/**
* Document Type Declaration to write, if any; {@code null} if none.
* XML directives (DTD, Comments, PIs) to write, if any.
*
* @since 3.2
*/
protected DTD _dtd;
protected List<XmlPrologDirective> _prologDirectives;

/*
/**********************************************************************
Expand Down Expand Up @@ -260,8 +260,10 @@ public void initGenerator() throws JacksonException
}

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

} catch (XMLStreamException e) {
Expand All @@ -275,12 +277,12 @@ public void initGenerator() throws JacksonException
*
* @since 3.2
*/
public void initConfig(DTD dtd)
public void initProlog(List<XmlPrologDirective> directives)
{
if (_initialized) { // sanity check
_reportError("Internal error: cannot call `initConfig()` after generator already initialized");
}
_dtd = dtd;
_prologDirectives = directives;
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package tools.jackson.dataformat.xml.ser;

import java.util.ArrayList;
import java.util.List;

import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonGenerator;
import tools.jackson.core.exc.StreamWriteException;

import tools.jackson.databind.*;
import tools.jackson.databind.cfg.GeneratorInitializer;

Expand All @@ -22,12 +27,14 @@
public class XmlGeneratorInitializer
implements GeneratorInitializer
{
protected DTD _dtd;
protected List<XmlPrologDirective> _directives;

protected boolean _hasDTD;

@Override
public void initialize(SerializationConfig config, JsonGenerator g) throws JacksonException {
if (g instanceof ToXmlGenerator xg) {
xg.initConfig(_dtd);
xg.initProlog(_directives);
}
}

Expand Down Expand Up @@ -60,7 +67,18 @@ public XmlGeneratorInitializer addDTD(String rootName,
* @return This initializer for call chaining
*/
public XmlGeneratorInitializer addDTD(DTD dtd) {
_dtd = dtd;
if (_hasDTD) {
throw new StreamWriteException(null, "Cannot add another `DTD`, initializer already has one");
}
_hasDTD = true;
return _add(dtd);
}

protected XmlGeneratorInitializer _add(XmlPrologDirective d) {
if (_directives == null) {
_directives = new ArrayList<>();
}
_directives.add(d);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
public interface XmlPrologDirective
{
/**
* Method to call to actually write out the directive.
* Method to call to actually write out the directive using given
* {@link XMLStreamWriter2}. {@link ToXmlGenerator} is only passed
* in case access to configuration was needed.
*
* @param xmlGen Generator that called this method: MUST NOT call
* its output methods, only to be used for configuration access
* @param sw Writer to use for actual output of XML event
*/
public void write(ToXmlGenerator xmlGen, XMLStreamWriter2 sw)
throws XMLStreamException;
Expand Down