Skip to content

Commit f8a97f9

Browse files
authored
Changes wrt core#1480 (#777)
1 parent 855a3f6 commit f8a97f9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ public boolean inRoot() {
292292
return _streamWriteContext.inRoot();
293293
}
294294

295+
@Override // @since 3.0
296+
public XmlPrettyPrinter getPrettyPrinter() {
297+
return _xmlPrettyPrinter;
298+
}
299+
295300
/*
296301
/**********************************************************************
297302
/* Extended API, access to some internal components

src/test/java/tools/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package tools.jackson.dataformat.xml.ser;
22

3+
import java.io.StringWriter;
34
import java.util.*;
45

6+
import javax.xml.namespace.QName;
7+
58
import org.junit.jupiter.api.BeforeEach;
69
import org.junit.jupiter.api.Test;
710

811
import com.fasterxml.jackson.annotation.JsonInclude;
912
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1013

11-
import tools.jackson.databind.SerializationFeature;
14+
import tools.jackson.core.JsonGenerator;
1215

16+
import tools.jackson.databind.SerializationFeature;
1317
import tools.jackson.dataformat.xml.XmlMapper;
1418
import tools.jackson.dataformat.xml.XmlTestUtil;
1519
import tools.jackson.dataformat.xml.XmlWriteFeature;
@@ -147,7 +151,7 @@ public void testSimpleIntBean() throws Exception
147151
@Test
148152
public void testSimpleMap() throws Exception
149153
{
150-
Map<String,String> map = new HashMap<String,String>();
154+
Map<String,String> map = new HashMap<>();
151155
map.put("a", "b");
152156
String xml = _xmlMapper.writeValueAsString(map);
153157

@@ -285,4 +289,31 @@ public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine() throw
285289
+ "</Company>" + DEFAULT_NEW_LINE,
286290
xml);
287291
}
292+
293+
// [core#1480]
294+
@Test
295+
void accessToPrettyPrinter() {
296+
// By default, no indenting:
297+
XmlMapper noIndentMapper = newMapper();
298+
try (JsonGenerator g = noIndentMapper.createGenerator(new StringWriter())) {
299+
assertNull(g.getPrettyPrinter());
300+
_writeDoc(g);
301+
}
302+
303+
// But can enable
304+
XmlMapper indentingMapper = mapperBuilder()
305+
.enable(SerializationFeature.INDENT_OUTPUT)
306+
.build();
307+
try (JsonGenerator g = indentingMapper.createGenerator(new StringWriter())) {
308+
assertTrue(g.getPrettyPrinter() instanceof DefaultXmlPrettyPrinter);
309+
_writeDoc(g);
310+
}
311+
}
312+
313+
private void _writeDoc(JsonGenerator g) {
314+
((ToXmlGenerator) g).setNextName(new QName("abc"));
315+
g.writeStartObject();
316+
g.writeNumberProperty("value", 42);
317+
g.writeEndObject();
318+
}
288319
}

0 commit comments

Comments
 (0)