Skip to content

Commit

Permalink
Fixes #631: add XmlMapper overloads for parser/generator creation fro…
Browse files Browse the repository at this point in the history
…m Stax entities (#632)
  • Loading branch information
cowtowncoder authored Jan 30, 2024
1 parent e284043 commit 2ecbbcf
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project: jackson-dataformat-xml
#618: `ArrayIndexOutOfBoundsException` thrown for invalid ending XML string
when using JDK default Stax XML parser
(reported by Arthur C)
#631: Add `XmlMapper.createGenerator(XMLStreamWriter)` and
`XmlMapper.createParser(XMLStreamReader)` overloads
* Upgrade Woodstox to 6.6.0 (latest at the time)

2.16.1 (24-Dec-2023)
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyp
*/
@Deprecated
protected void setXMLTextElementName(String name) {
((XmlFactory) _jsonFactory).setXMLTextElementName(name);
getFactory().setXMLTextElementName(name);
}

/**
Expand All @@ -292,7 +292,7 @@ public XmlMapper setDefaultUseWrapper(boolean state) {
* @since 2.14
*/
public void setXmlNameProcessor(XmlNameProcessor processor) {
((XmlFactory)_jsonFactory).setXmlNameProcessor(processor);
getFactory().setXmlNameProcessor(processor);
}

/*
Expand Down Expand Up @@ -342,6 +342,26 @@ public ObjectMapper disable(FromXmlParser.Feature f) {
/**********************************************************
*/

/**
* Overloaded variant that allows constructing {@link FromXmlParser}
* for given Stax {@link XMLStreamReader}.
*
* @since 2.17
*/
public FromXmlParser createParser(XMLStreamReader r) throws IOException {
return getFactory().createParser(r);
}

/**
* Overloaded variant that allows constructing {@link ToXmlGenerator}
* for given Stax {@link XMLStreamWriter}.
*
* @since 2.17
*/
public ToXmlGenerator createGenerator(XMLStreamWriter w) throws IOException {
return getFactory().createGenerator(w);
}

/**
* Method for reading a single XML value from given XML-specific input
* source; useful for incremental data-binding, combining traversal using
Expand Down Expand Up @@ -374,7 +394,7 @@ public <T> T readValue(XMLStreamReader r, TypeReference<T> valueTypeRef) throws
@SuppressWarnings("resource")
public <T> T readValue(XMLStreamReader r, JavaType valueType) throws IOException
{
FromXmlParser p = getFactory().createParser(r);
FromXmlParser p = createParser(r);
return super.readValue(p, valueType);
}

Expand All @@ -385,9 +405,9 @@ public <T> T readValue(XMLStreamReader r, JavaType valueType) throws IOException
*
* @since 2.4
*/
public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
public void writeValue(XMLStreamWriter w, Object value) throws IOException {
@SuppressWarnings("resource")
ToXmlGenerator g = getFactory().createGenerator(w0);
ToXmlGenerator g = createGenerator(w);
super.writeValue(g, value);
// NOTE: above call should do flush(); and we should NOT close here.
// Finally, 'g' has no buffers to release.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.io.*;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

public class IncrementalWritingTest extends XmlTestBase
{
Expand All @@ -31,4 +33,29 @@ public void testSimple() throws Exception
+"<NameBean age=\"17\"><first>Growl</first><last>Tiger</last></NameBean></root>",
xml);
}

// @since 2.17
public void testWriteUsingXMLStreamWriter() throws Exception
{
XMLOutputFactory staxF = MAPPER.getFactory().getXMLOutputFactory();
final Point p = new Point(1, 2);

// Serialize first using convenience method
try (StringWriter w = new StringWriter()) {
XMLStreamWriter sw = staxF.createXMLStreamWriter(w);
MAPPER.writeValue(sw, p);
assertEquals("<Point><x>1</x><y>2</y></Point>", w.toString());
}

// and then by explicit XMLStreamWriter
try (StringWriter w = new StringWriter()) {
XMLStreamWriter sw = staxF.createXMLStreamWriter(w);
sw.writeStartDocument("US-ASCII", "1.1");
try (ToXmlGenerator g = MAPPER.createGenerator(sw)) {
MAPPER.writeValue(g, p);
assertEquals("<?xml version='1.1' encoding='US-ASCII'?>"
+"<Point><x>1</x><y>2</y></Point>", w.toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import javax.xml.stream.*;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

Expand Down Expand Up @@ -43,4 +44,27 @@ public void testSimpleRead() throws Exception

sr.close();
}

// @since 2.17
public void testReadUsingXMLStreamReader() throws Exception
{
final String DOC = "<Point><x>1</x><y>2</y></Point>";

XMLInputFactory staxF = MAPPER.getFactory().getXMLInputFactory();

// First read using XmlMapper convenience method
XMLStreamReader sr = staxF.createXMLStreamReader(new StringReader(DOC));
Point p = MAPPER.readValue(sr, Point.class);
assertEquals(1, p.x);
assertEquals(2, p.y);
sr.close();

// Then read using XmlFactory parser factory method
sr = staxF.createXMLStreamReader(new StringReader(DOC));
try (JsonParser jp = MAPPER.createParser(sr)) {
p = MAPPER.readValue(jp, Point.class);
assertEquals(1, p.x);
assertEquals(2, p.y);
}
}
}

0 comments on commit 2ecbbcf

Please sign in to comment.