Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
39 changes: 30 additions & 9 deletions src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,16 @@ public JsonParser createParser(String content) throws IOException {
/**********************************************************
*/

/**
* Overriding this method makes it easy to extend and customize the ToXmlGenerator.
*
* @since 2.20
*/
public ToXmlGenerator createGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures, ObjectCodec codec,
XMLStreamWriter sw, XmlNameProcessor nameProcessor) {
return new ToXmlGenerator(ctxt, stdFeatures, xmlFeatures, codec, sw, nameProcessor);
}

@Override
public ToXmlGenerator createGenerator(OutputStream out) throws IOException {
return createGenerator(out, JsonEncoding.UTF8);
Expand All @@ -513,7 +523,7 @@ public ToXmlGenerator createGenerator(OutputStream out, JsonEncoding enc) throws
// false -> we won't manage the stream unless explicitly directed to
final IOContext ctxt = _createContext(_createContentReference(out), false);
ctxt.setEncoding(enc);
return new ToXmlGenerator(ctxt,
return this.createGenerator(ctxt,
_generatorFeatures, _xmlGeneratorFeatures,
_objectCodec, _createXmlWriter(ctxt, out), _nameProcessor);
}
Expand All @@ -522,7 +532,7 @@ public ToXmlGenerator createGenerator(OutputStream out, JsonEncoding enc) throws
public ToXmlGenerator createGenerator(Writer out) throws IOException
{
final IOContext ctxt = _createContext(_createContentReference(out), false);
return new ToXmlGenerator(ctxt,
return this.createGenerator(ctxt,
_generatorFeatures, _xmlGeneratorFeatures,
_objectCodec, _createXmlWriter(ctxt, out), _nameProcessor);
}
Expand All @@ -535,7 +545,7 @@ public ToXmlGenerator createGenerator(File f, JsonEncoding enc) throws IOExcepti
// true -> yes, we have to manage the stream since we created it
final IOContext ctxt = _createContext(_createContentReference(out), true);
ctxt.setEncoding(enc);
return new ToXmlGenerator(ctxt, _generatorFeatures, _xmlGeneratorFeatures,
return this.createGenerator(ctxt, _generatorFeatures, _xmlGeneratorFeatures,
_objectCodec, _createXmlWriter(ctxt, out), _nameProcessor);
}

Expand All @@ -559,14 +569,25 @@ public FromXmlParser createParser(XMLStreamReader sr) throws IOException
}

// false -> not managed
FromXmlParser xp = new FromXmlParser(_createContext(_createContentReference(sr), false),
FromXmlParser xp = this.createParser(_createContext(_createContentReference(sr), false),
_parserFeatures, _xmlParserFeatures, _objectCodec, sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
}

/**
* Creates and returns a new instance of {@link FromXmlParser} configured with the provided parameters.
* If you need to extend or customize the FromXmlParser, you can simply override this method.
*
* @since 2.20
*/
protected FromXmlParser createParser(IOContext ctxt, int genericParserFeatures, int xmlFeatures, ObjectCodec codec,
XMLStreamReader xmlReader, XmlNameProcessor tagProcessor) throws IOException {
return new FromXmlParser(ctxt, genericParserFeatures, xmlFeatures, codec, xmlReader, tagProcessor);
}

/**
* Factory method that wraps given {@link XMLStreamWriter}, usually to allow
* incremental serialization to compose large output by serializing a sequence
Expand All @@ -578,7 +599,7 @@ public ToXmlGenerator createGenerator(XMLStreamWriter sw) throws IOException
{
sw = _initializeXmlWriter(sw);
IOContext ctxt = _createContext(_createContentReference(sw), false);
return new ToXmlGenerator(ctxt, _generatorFeatures, _xmlGeneratorFeatures,
return this.createGenerator(ctxt, _generatorFeatures, _xmlGeneratorFeatures,
_objectCodec, sw, _nameProcessor);
}

Expand All @@ -598,7 +619,7 @@ protected FromXmlParser _createParser(InputStream in, IOContext ctxt) throws IOE
return StaxUtil.throwAsParseException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(ctxt, _parserFeatures, _xmlParserFeatures,
FromXmlParser xp = this.createParser(ctxt, _parserFeatures, _xmlParserFeatures,
_objectCodec, sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
Expand All @@ -616,7 +637,7 @@ protected FromXmlParser _createParser(Reader r, IOContext ctxt) throws IOExcepti
return StaxUtil.throwAsParseException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(ctxt, _parserFeatures, _xmlParserFeatures,
FromXmlParser xp = this.createParser(ctxt, _parserFeatures, _xmlParserFeatures,
_objectCodec, sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
Expand All @@ -643,7 +664,7 @@ protected FromXmlParser _createParser(char[] data, int offset, int len, IOContex
return StaxUtil.throwAsParseException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(ctxt, _parserFeatures, _xmlParserFeatures,
FromXmlParser xp = this.createParser(ctxt, _parserFeatures, _xmlParserFeatures,
_objectCodec, sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
Expand Down Expand Up @@ -677,7 +698,7 @@ protected FromXmlParser _createParser(byte[] data, int offset, int len, IOContex
return StaxUtil.throwAsParseException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(ctxt, _parserFeatures, _xmlParserFeatures,
FromXmlParser xp = this.createParser(ctxt, _parserFeatures, _xmlParserFeatures,
_objectCodec, sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;
import java.util.Set;

import javax.xml.XMLConstants;
Expand Down Expand Up @@ -274,17 +275,29 @@ private Feature(boolean defaultState) {
*/

public FromXmlParser(IOContext ctxt, int genericParserFeatures, int xmlFeatures,
ObjectCodec codec, XMLStreamReader xmlReader, XmlNameProcessor tagProcessor)
throws IOException
{
ObjectCodec codec, XMLStreamReader xmlReader, XmlNameProcessor tagProcessor) throws IOException {
this(ctxt, genericParserFeatures, codec, new XmlTokenStream(xmlReader, ctxt.contentReference(), xmlFeatures, tagProcessor));
}

/**
* Constructs a new {@link FromXmlParser} instance using the provided XML token stream.
* This constructor initializes the parser with the given I/O context, parser features,
* and object codec for deserializing XML content into Java objects.
*
* @since 2.20
* @param ctxt I/O context used for handling low-level I/O operations and buffering
* @param genericParserFeatures set of bitmasked parser features to control parsing behavior
* @param codec object codec used for converting between JSON-like structures and Java objects
* @param xmlTokenStream the pre-processed XML token stream to parse from
* @throws IOException if an I/O error occurs during initialization or parsing setup
*/
public FromXmlParser(IOContext ctxt, int genericParserFeatures, ObjectCodec codec, XmlTokenStream xmlTokenStream) throws IOException {
super(genericParserFeatures, ctxt.streamReadConstraints());
_formatFeatures = xmlFeatures;
_ioContext = ctxt;
_objectCodec = codec;
_parsingContext = XmlReadContext.createRootContext(-1, -1);
_xmlTokens = new XmlTokenStream(xmlReader, ctxt.contentReference(),
_formatFeatures, tagProcessor);

_xmlTokens = Objects.requireNonNull(xmlTokenStream, "xmlTokenStream cannot be null");
_formatFeatures = xmlTokenStream.getFormatFeatures();
final int firstToken;
try {
firstToken = _xmlTokens.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ protected void setFormatFeatures(int f) {
_cfgProcessXsiType = FromXmlParser.Feature.AUTO_DETECT_XSI_TYPE.enabledIn(f);
}

/**
* get FormatFeatures
*
* @return data
* @since 2.20
*/
public int getFormatFeatures() {
return _formatFeatures;
}

/*
/**********************************************************************
/* Public API
Expand Down