@@ -106,6 +106,14 @@ public class ToXmlGenerator
106106 */
107107 protected List <XmlPrologDirective > _prologDirectives ;
108108
109+ /**
110+ * Whether linefeed ("pretty-printing") enabled between directives
111+ * in Document prolog.
112+ *
113+ * @since 3.2
114+ */
115+ protected boolean _lfBetweenPrologDirectives ;
116+
109117 /*
110118 /**********************************************************************
111119 /* Logical output state
@@ -218,6 +226,22 @@ public ToXmlGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt,
218226 /**********************************************************************
219227 */
220228
229+ /**
230+ * Method called by {@link XmlGeneratorInitializer} to inject
231+ * necessary configuration.
232+ *
233+ * @since 3.2
234+ */
235+ public void initProlog (boolean lfBetweenPrologDirectives ,
236+ List <XmlPrologDirective > directives )
237+ {
238+ if (_initialized ) { // sanity check
239+ _reportError ("Internal error: cannot call `initConfig()` after generator already initialized" );
240+ }
241+ _lfBetweenPrologDirectives = lfBetweenPrologDirectives ;
242+ _prologDirectives = directives ;
243+ }
244+
221245 /**
222246 * Method called by {@link XmlSerializationContext} before writing any output,
223247 * to optionally output XML declaration and other before-root-element
@@ -230,29 +254,21 @@ public void initGenerator() throws JacksonException
230254 }
231255 _initialized = true ;
232256 try {
233- boolean xmlDeclWritten ;
234-
235- if (XmlWriteFeature .WRITE_XML_1_1 .enabledIn (_formatFeatures )
236- || XmlWriteFeature .WRITE_XML_DECLARATION .enabledIn (_formatFeatures )) {
257+ final boolean xml11Decl = XmlWriteFeature .WRITE_XML_1_1 .enabledIn (_formatFeatures );
258+ if (xml11Decl || XmlWriteFeature .WRITE_XML_DECLARATION .enabledIn (_formatFeatures )) {
237259
238- String xmlVersion = XmlWriteFeature . WRITE_XML_1_1 . enabledIn ( _formatFeatures ) ? "1.1" : "1.0" ;
260+ String xmlVersion = xml11Decl ? "1.1" : "1.0" ;
239261 String encoding = "UTF-8" ;
240262
241263 if (XmlWriteFeature .WRITE_STANDALONE_YES_TO_XML_DECLARATION .enabledIn (_formatFeatures )) {
242264 _xmlWriter .writeStartDocument (xmlVersion , encoding , true );
243265 } else {
244266 _xmlWriter .writeStartDocument (encoding , xmlVersion );
245267 }
246- xmlDeclWritten = true ;
247- } else {
248- xmlDeclWritten = false ;
249- }
250-
251- // as per [dataformat-xml#172], try adding indentation
252- if (xmlDeclWritten && (_xmlPrettyPrinter != null )) {
253- // ... but only if it is likely to succeed:
254- if (!_stax2Emulation ) {
255- _xmlPrettyPrinter .writePrologLinefeed (_xmlWriter );
268+ // 20-Apr-2026, tatu: for legacy path, only output prolog lf when pretty-printing
269+ // OR _lfBetweenPrologDirectives passed by initializer
270+ if (_lfBetweenPrologDirectives || _xmlPrettyPrinter != null ) {
271+ _prologLinefeed ();
256272 }
257273 }
258274 if (XmlWriteFeature .AUTO_DETECT_XSI_TYPE .enabledIn (_formatFeatures )) {
@@ -263,6 +279,10 @@ public void initGenerator() throws JacksonException
263279 if (_prologDirectives != null ) {
264280 for (XmlPrologDirective d : _prologDirectives ) {
265281 d .write (this , _xmlWriter );
282+ // Add linefeed separators b/w directives
283+ if (_lfBetweenPrologDirectives ) {
284+ _prologLinefeed ();
285+ }
266286 }
267287 }
268288
@@ -271,18 +291,16 @@ public void initGenerator() throws JacksonException
271291 }
272292 }
273293
274- /**
275- * Method called by {@link XmlGeneratorInitializer} to inject
276- * necessary configuration.
277- *
278- * @since 3.2
279- */
280- public void initProlog (List <XmlPrologDirective > directives )
294+ // @since 3.2
295+ private void _prologLinefeed () throws XMLStreamException
281296 {
282- if (_initialized ) { // sanity check
283- _reportError ("Internal error: cannot call `initConfig()` after generator already initialized" );
297+ if (!_stax2Emulation ) {
298+ if (_xmlPrettyPrinter != null ) {
299+ _xmlPrettyPrinter .writePrologLinefeed (_xmlWriter );
300+ } else {
301+ _xmlWriter .writeSpace ("\n " );
302+ }
284303 }
285- _prologDirectives = directives ;
286304 }
287305
288306 /*
0 commit comments