Skip to content

Commit 2ecbbcf

Browse files
authored
Fixes #631: add XmlMapper overloads for parser/generator creation from Stax entities (#632)
1 parent e284043 commit 2ecbbcf

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project: jackson-dataformat-xml
99
#618: `ArrayIndexOutOfBoundsException` thrown for invalid ending XML string
1010
when using JDK default Stax XML parser
1111
(reported by Arthur C)
12+
#631: Add `XmlMapper.createGenerator(XMLStreamWriter)` and
13+
`XmlMapper.createParser(XMLStreamReader)` overloads
1214
* Upgrade Woodstox to 6.6.0 (latest at the time)
1315

1416
2.16.1 (24-Dec-2023)

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyp
268268
*/
269269
@Deprecated
270270
protected void setXMLTextElementName(String name) {
271-
((XmlFactory) _jsonFactory).setXMLTextElementName(name);
271+
getFactory().setXMLTextElementName(name);
272272
}
273273

274274
/**
@@ -292,7 +292,7 @@ public XmlMapper setDefaultUseWrapper(boolean state) {
292292
* @since 2.14
293293
*/
294294
public void setXmlNameProcessor(XmlNameProcessor processor) {
295-
((XmlFactory)_jsonFactory).setXmlNameProcessor(processor);
295+
getFactory().setXmlNameProcessor(processor);
296296
}
297297

298298
/*
@@ -342,6 +342,26 @@ public ObjectMapper disable(FromXmlParser.Feature f) {
342342
/**********************************************************
343343
*/
344344

345+
/**
346+
* Overloaded variant that allows constructing {@link FromXmlParser}
347+
* for given Stax {@link XMLStreamReader}.
348+
*
349+
* @since 2.17
350+
*/
351+
public FromXmlParser createParser(XMLStreamReader r) throws IOException {
352+
return getFactory().createParser(r);
353+
}
354+
355+
/**
356+
* Overloaded variant that allows constructing {@link ToXmlGenerator}
357+
* for given Stax {@link XMLStreamWriter}.
358+
*
359+
* @since 2.17
360+
*/
361+
public ToXmlGenerator createGenerator(XMLStreamWriter w) throws IOException {
362+
return getFactory().createGenerator(w);
363+
}
364+
345365
/**
346366
* Method for reading a single XML value from given XML-specific input
347367
* source; useful for incremental data-binding, combining traversal using
@@ -374,7 +394,7 @@ public <T> T readValue(XMLStreamReader r, TypeReference<T> valueTypeRef) throws
374394
@SuppressWarnings("resource")
375395
public <T> T readValue(XMLStreamReader r, JavaType valueType) throws IOException
376396
{
377-
FromXmlParser p = getFactory().createParser(r);
397+
FromXmlParser p = createParser(r);
378398
return super.readValue(p, valueType);
379399
}
380400

@@ -385,9 +405,9 @@ public <T> T readValue(XMLStreamReader r, JavaType valueType) throws IOException
385405
*
386406
* @since 2.4
387407
*/
388-
public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
408+
public void writeValue(XMLStreamWriter w, Object value) throws IOException {
389409
@SuppressWarnings("resource")
390-
ToXmlGenerator g = getFactory().createGenerator(w0);
410+
ToXmlGenerator g = createGenerator(w);
391411
super.writeValue(g, value);
392412
// NOTE: above call should do flush(); and we should NOT close here.
393413
// Finally, 'g' has no buffers to release.

src/test/java/com/fasterxml/jackson/dataformat/xml/incr/IncrementalWritingTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.io.*;
44

5+
import javax.xml.stream.XMLOutputFactory;
56
import javax.xml.stream.XMLStreamWriter;
67

78
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
89
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
10+
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
911

1012
public class IncrementalWritingTest extends XmlTestBase
1113
{
@@ -31,4 +33,29 @@ public void testSimple() throws Exception
3133
+"<NameBean age=\"17\"><first>Growl</first><last>Tiger</last></NameBean></root>",
3234
xml);
3335
}
36+
37+
// @since 2.17
38+
public void testWriteUsingXMLStreamWriter() throws Exception
39+
{
40+
XMLOutputFactory staxF = MAPPER.getFactory().getXMLOutputFactory();
41+
final Point p = new Point(1, 2);
42+
43+
// Serialize first using convenience method
44+
try (StringWriter w = new StringWriter()) {
45+
XMLStreamWriter sw = staxF.createXMLStreamWriter(w);
46+
MAPPER.writeValue(sw, p);
47+
assertEquals("<Point><x>1</x><y>2</y></Point>", w.toString());
48+
}
49+
50+
// and then by explicit XMLStreamWriter
51+
try (StringWriter w = new StringWriter()) {
52+
XMLStreamWriter sw = staxF.createXMLStreamWriter(w);
53+
sw.writeStartDocument("US-ASCII", "1.1");
54+
try (ToXmlGenerator g = MAPPER.createGenerator(sw)) {
55+
MAPPER.writeValue(g, p);
56+
assertEquals("<?xml version='1.1' encoding='US-ASCII'?>"
57+
+"<Point><x>1</x><y>2</y></Point>", w.toString());
58+
}
59+
}
60+
}
3461
}

src/test/java/com/fasterxml/jackson/dataformat/xml/incr/PartialReadTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import javax.xml.stream.*;
55

6+
import com.fasterxml.jackson.core.JsonParser;
67
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
78
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
89

@@ -43,4 +44,27 @@ public void testSimpleRead() throws Exception
4344

4445
sr.close();
4546
}
47+
48+
// @since 2.17
49+
public void testReadUsingXMLStreamReader() throws Exception
50+
{
51+
final String DOC = "<Point><x>1</x><y>2</y></Point>";
52+
53+
XMLInputFactory staxF = MAPPER.getFactory().getXMLInputFactory();
54+
55+
// First read using XmlMapper convenience method
56+
XMLStreamReader sr = staxF.createXMLStreamReader(new StringReader(DOC));
57+
Point p = MAPPER.readValue(sr, Point.class);
58+
assertEquals(1, p.x);
59+
assertEquals(2, p.y);
60+
sr.close();
61+
62+
// Then read using XmlFactory parser factory method
63+
sr = staxF.createXMLStreamReader(new StringReader(DOC));
64+
try (JsonParser jp = MAPPER.createParser(sr)) {
65+
p = MAPPER.readValue(jp, Point.class);
66+
assertEquals(1, p.x);
67+
assertEquals(2, p.y);
68+
}
69+
}
4670
}

0 commit comments

Comments
 (0)