Skip to content

Commit 43dc3ef

Browse files
authored
Merge pull request #176 from shado23/threadlocal-parser
2 parents d360cb6 + 0aab600 commit 43dc3ef

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

shared/src/main/scala/scala/xml/factory/XMLLoader.scala

+11-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ trait XMLLoader[T <: Node] {
2727
import scala.xml.Source._
2828
def adapter: FactoryAdapter = new NoBindingFactoryAdapter()
2929

30-
/* Override this to use a different SAXParser. */
31-
def parser: SAXParser = {
32-
val f = SAXParserFactory.newInstance()
33-
f.setNamespaceAware(false)
34-
f.newSAXParser()
30+
private lazy val parserInstance = new ThreadLocal[SAXParser] {
31+
override def initialValue = {
32+
val parser = SAXParserFactory.newInstance()
33+
34+
parser.setNamespaceAware(false)
35+
parser.newSAXParser()
36+
}
3537
}
3638

39+
/* Override this to use a different SAXParser. */
40+
def parser: SAXParser = parserInstance.get
41+
3742
/**
3843
* Loads XML from the given InputSource, using the supplied parser.
3944
* The methods available in scala.xml.XML use the XML parser in the JDK.
@@ -62,4 +67,4 @@ trait XMLLoader[T <: Node] {
6267

6368
/** Loads XML from the given String. */
6469
def loadString(string: String): T = loadXML(fromString(string), parser)
65-
}
70+
}

0 commit comments

Comments
 (0)