Skip to content

Change FromXmlParser to reduce mutability #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 12 additions & 33 deletions src/main/java/tools/jackson/dataformat/xml/XmlFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public class XmlFactory
/**********************************************************************
*/

// !!! 09-Jan-2018, tatu: make final ASAP
protected String _cfgNameForTextElement;
protected final String _cfgNameForTextElement;

protected final XmlNameProcessor _nameProcessor;

Expand Down Expand Up @@ -93,7 +92,7 @@ public XmlFactory(XMLInputFactory xmlIn) {
public XmlFactory(XMLInputFactory xmlIn, XMLOutputFactory xmlOut) {
this(DEFAULT_XML_READ_FEATURE_FLAGS, DEFAULT_XML_WRITE_FEATURE_FLAGS,
xmlIn, xmlOut, XmlNameProcessors.newPassthroughProcessor(),
null);
FromXmlParser.DEFAULT_UNNAMED_TEXT_PROPERTY);
}

protected XmlFactory(int xpFeatures, int xgFeatures,
Expand Down Expand Up @@ -435,15 +434,11 @@ public FromXmlParser createParser(ObjectReadContext readCtxt,
}

// false -> not managed
FromXmlParser xp = new FromXmlParser(readCtxt,
return new FromXmlParser(readCtxt,
_createContext(_createContentReference(sr), false),
readCtxt.getStreamReadFeatures(_streamReadFeatures),
readCtxt.getFormatReadFeatures(_formatReadFeatures),
sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
sr, _nameProcessor, _cfgNameForTextElement);
}

/**
Expand Down Expand Up @@ -480,14 +475,10 @@ protected FromXmlParser _createParser(ObjectReadContext readCtxt, IOContext ioCt
return StaxUtil.throwAsReadException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(readCtxt, ioCtxt,
return new FromXmlParser(readCtxt, ioCtxt,
readCtxt.getStreamReadFeatures(_streamReadFeatures),
readCtxt.getFormatReadFeatures(_formatReadFeatures),
sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
sr, _nameProcessor, _cfgNameForTextElement);
}

@Override
Expand All @@ -501,14 +492,10 @@ protected FromXmlParser _createParser(ObjectReadContext readCtxt, IOContext ioCt
return StaxUtil.throwAsReadException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(readCtxt, ioCtxt,
return new FromXmlParser(readCtxt, ioCtxt,
readCtxt.getStreamReadFeatures(_streamReadFeatures),
readCtxt.getFormatReadFeatures(_formatReadFeatures),
sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
sr, _nameProcessor, _cfgNameForTextElement);
}

@Override
Expand All @@ -531,14 +518,10 @@ protected FromXmlParser _createParser(ObjectReadContext readCtxt, IOContext ioCt
return StaxUtil.throwAsReadException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(readCtxt, ioCtxt,
return new FromXmlParser(readCtxt, ioCtxt,
readCtxt.getStreamReadFeatures(_streamReadFeatures),
readCtxt.getFormatReadFeatures(_formatReadFeatures),
sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
sr, _nameProcessor, _cfgNameForTextElement);
}

@Override
Expand All @@ -558,14 +541,10 @@ protected FromXmlParser _createParser(ObjectReadContext readCtxt, IOContext ioCt
return StaxUtil.throwAsReadException(e, null);
}
sr = _initializeXmlReader(sr);
FromXmlParser xp = new FromXmlParser(readCtxt, ioCtxt,
return new FromXmlParser(readCtxt, ioCtxt,
readCtxt.getStreamReadFeatures(_streamReadFeatures),
readCtxt.getFormatReadFeatures(_formatReadFeatures),
sr, _nameProcessor);
if (_cfgNameForTextElement != null) {
xp.setXMLTextElementName(_cfgNameForTextElement);
}
return xp;
sr, _nameProcessor, _cfgNameForTextElement);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tools.jackson.core.StreamReadConstraints;
import tools.jackson.core.StreamWriteConstraints;
import tools.jackson.core.base.DecorableTSFactory.DecorableTSFBuilder;
import tools.jackson.dataformat.xml.deser.FromXmlParser;

/**
* {@link tools.jackson.core.TSFBuilder}
Expand Down Expand Up @@ -50,7 +51,7 @@ public class XmlFactoryBuilder extends DecorableTSFBuilder<XmlFactory, XmlFactor
*<p>
* Name used for pseudo-property used for returning XML Text value (which does
* not have actual element name to use). Defaults to empty String, but
* may be changed for interoperability reasons: JAXB, for example, uses
* may be changed for inter-operability reasons: JAXB, for example, uses
* "value" as name.
*/
protected String _nameForTextElement;
Expand Down Expand Up @@ -82,6 +83,7 @@ protected XmlFactoryBuilder() {
XmlFactory.DEFAULT_XML_WRITE_FEATURE_FLAGS);
_classLoaderForStax = null;
_nameProcessor = XmlNameProcessors.newPassthroughProcessor();
_nameForTextElement = FromXmlParser.DEFAULT_UNNAMED_TEXT_PROPERTY;
}

public XmlFactoryBuilder(XmlFactory base) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class FromXmlParser
* may be changed for inter-operability reasons: JAXB, for example, uses
* "value" as name.
*/
protected String _cfgNameForTextElement = DEFAULT_UNNAMED_TEXT_PROPERTY;
protected final String _cfgNameForTextElement;

/*
/**********************************************************************
Expand All @@ -74,7 +74,7 @@ public class FromXmlParser
* {@link XmlReadFeature}s
* are enabled.
*/
protected int _formatFeatures;
protected final int _formatFeatures;

/*
/**********************************************************************
Expand Down Expand Up @@ -158,13 +158,15 @@ public class FromXmlParser
public FromXmlParser(ObjectReadContext readCtxt, IOContext ioCtxt,
int parserFeatures, int xmlFeatures,
XMLStreamReader xmlReader,
XmlNameProcessor nameProcessor)
XmlNameProcessor nameProcessor,
String nameForTextElement)
{
super(readCtxt, ioCtxt, parserFeatures);
_formatFeatures = xmlFeatures;
_streamReadContext = XmlReadContext.createRootContext(-1, -1);
_xmlTokens = new XmlTokenStream(xmlReader, ioCtxt.contentReference(),
_formatFeatures, nameProcessor);
_cfgNameForTextElement = nameForTextElement;

final int firstToken;
try {
Expand Down Expand Up @@ -208,10 +210,6 @@ public Version version() {
return PackageVersion.VERSION;
}

public void setXMLTextElementName(String name) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the thing remove as unnecessary, specifically... :)

_cfgNameForTextElement = name;
}

@Override // since 3.0
public XMLStreamReader2 streamReadInputSource() {
return _xmlTokens.getXmlReader();
Expand Down