Skip to content
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

Call reset on reusable SAXParser instance #742

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,14 @@ class XMLTestJVM {
@UnitTest
def checkThatErrorHandlerIsNotOverwritten(): Unit = {
var gotAnError: Boolean = false
XML.reader.setErrorHandler(new org.xml.sax.ErrorHandler {
val reader = XML.reader
reader.setErrorHandler(new org.xml.sax.ErrorHandler {
override def warning(e: SAXParseException): Unit = gotAnError = true
override def error(e: SAXParseException): Unit = gotAnError = true
override def fatalError(e: SAXParseException): Unit = gotAnError = true
})
try {
XML.loadString("<a>")
XML.adapter.loadDocument(Source.fromString("<a>"), reader)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love to make XMLLoader.adapter, NoBindingFactoryAdapter and FactoryAdapter private at some point (and remove some of them altogether), and add XMLLoader.loadDocument(inputSource: InputSource, reader: XMLReader)...

} catch {
case _: org.xml.sax.SAXParseException =>
}
Expand Down
6 changes: 5 additions & 1 deletion shared/src/main/scala/scala/xml/factory/XMLLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ trait XMLLoader[T <: Node] {
}

/* Override this to use a different SAXParser. */
def parser: SAXParser = parserInstance.get
def parser: SAXParser = {
val p = parserInstance.get
p.reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

Auto-reset on get leads to surprising changes in behavior, e.g. non-persistence of the settings on XMLLoader.reader, as witnessed by the changes to the test above.
Better place for auto-reset seems to be FactoryAdapter.loadDocument(), but it will then apply to all readers, not just the default one...
Better still would be - in my opinion - not to reuse parsers, readers and factory adapters and not to use thread-locals.

Copy link
Member Author

Choose a reason for hiding this comment

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

So revert #176?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not going to campaign for reverting it, but I am not a fan ;)

p
}

/* Override this to use a different XMLReader. */
def reader: XMLReader = parser.getXMLReader
Expand Down