@@ -14,9 +14,9 @@ package scala
1414package xml
1515package factory
1616
17- import org .xml .sax .{SAXNotRecognizedException , SAXNotSupportedException , XMLReader }
17+ import org .xml .sax .XMLReader
18+ import scala .xml .Source
1819import javax .xml .parsers .SAXParserFactory
19- import parsing .{FactoryAdapter , NoBindingFactoryAdapter }
2020import java .io .{File , FileDescriptor , InputStream , Reader }
2121import java .net .URL
2222
@@ -25,9 +25,6 @@ import java.net.URL
2525 * created by "def parser" or the reader created by "def reader".
2626 */
2727trait XMLLoader [T <: Node ] {
28- import scala .xml .Source ._
29- def adapter : FactoryAdapter = new NoBindingFactoryAdapter ()
30-
3128 private def setSafeDefaults (parserFactory : SAXParserFactory ): Unit = {
3229 parserFactory.setFeature(" http://javax.xml.XMLConstants/feature/secure-processing" , true )
3330 parserFactory.setFeature(" http://apache.org/xml/features/nonvalidating/load-external-dtd" , false )
@@ -54,69 +51,49 @@ trait XMLLoader[T <: Node] {
5451 def reader : XMLReader = parser.getXMLReader
5552
5653 /**
57- * Loads XML from the given InputSource, using the supplied parser.
54+ * Loads XML from the given InputSource, using the supplied parser or reader .
5855 * The methods available in scala.xml.XML use the XML parser in the JDK
5956 * (unless another parser is present on the classpath).
6057 */
61- def loadXML (inputSource : InputSource , parser : SAXParser ): T = loadXML(inputSource, parser.getXMLReader)
62-
63- def loadXMLNodes (inputSource : InputSource , parser : SAXParser ): Seq [Node ] = loadXMLNodes(inputSource, parser.getXMLReader)
64-
65- private def loadXML (inputSource : InputSource , reader : XMLReader ): T = {
66- val result : FactoryAdapter = parse(inputSource, reader)
67- result.rootElem.asInstanceOf [T ]
68- }
69-
70- private def loadXMLNodes (inputSource : InputSource , reader : XMLReader ): Seq [Node ] = {
71- val result : FactoryAdapter = parse(inputSource, reader)
72- result.prolog ++ (result.rootElem :: result.epilogue)
73- }
74-
75- private def parse (inputSource : InputSource , xmlReader : XMLReader ): FactoryAdapter = {
76- if (inputSource == null ) throw new IllegalArgumentException (" InputSource cannot be null" )
77-
78- val result : FactoryAdapter = adapter
79-
80- xmlReader.setContentHandler(result)
81- xmlReader.setDTDHandler(result)
82- /* Do not overwrite pre-configured EntityResolver. */
83- if (xmlReader.getEntityResolver == null ) xmlReader.setEntityResolver(result)
84- /* Do not overwrite pre-configured ErrorHandler. */
85- if (xmlReader.getErrorHandler == null ) xmlReader.setErrorHandler(result)
86-
87- try {
88- xmlReader.setProperty(" http://xml.org/sax/properties/lexical-handler" , result)
89- } catch {
90- case _ : SAXNotRecognizedException =>
91- case _ : SAXNotSupportedException =>
92- }
93-
94- result.scopeStack = TopScope :: result.scopeStack
95- xmlReader.parse(inputSource)
96- result.scopeStack = result.scopeStack.tail
97-
98- result
99- }
100-
101- /** Loads XML. */
102- def load (inputSource : InputSource ): T = loadXML(inputSource, reader)
103- def loadFile (fileName : String ): T = load(fromFile(fileName))
104- def loadFile (file : File ): T = load(fromFile(file))
105- def load (url : URL ): T = load(fromUrl(url))
106- def load (sysId : String ): T = load(fromSysId(sysId))
107- def loadFile (fileDescriptor : FileDescriptor ): T = load(fromFile(fileDescriptor))
108- def load (inputStream : InputStream ): T = load(fromInputStream(inputStream))
109- def load (reader : Reader ): T = load(fromReader(reader))
110- def loadString (string : String ): T = load(fromString(string))
58+ private def getDocElem (document : Document ): T = document.docElem.asInstanceOf [T ]
59+
60+ def loadXML (inputSource : InputSource , parser : SAXParser ): T = getDocElem(loadDocument(inputSource, parser))
61+ def loadXMLNodes (inputSource : InputSource , parser : SAXParser ): Seq [Node ] = loadDocument(inputSource, parser).children
62+
63+ private def loadDocument (inputSource : InputSource , parser : SAXParser ): Document = adapter.loadDocument(inputSource, parser)
64+ private def loadDocument (inputSource : InputSource , reader : XMLReader ): Document = adapter.loadDocument(inputSource, reader)
65+ def adapter : parsing.FactoryAdapter = new parsing.NoBindingFactoryAdapter ()
66+
67+ /** Loads XML Document. */
68+ def loadDocument (source : InputSource ): Document = loadDocument(source, reader)
69+ def loadFileDocument (fileName : String ): Document = loadDocument(Source .fromFile(fileName))
70+ def loadFileDocument (file : File ): Document = loadDocument(Source .fromFile(file))
71+ def loadDocument (url : URL ): Document = loadDocument(Source .fromUrl(url))
72+ def loadDocument (sysId : String ): Document = loadDocument(Source .fromSysId(sysId))
73+ def loadFileDocument (fileDescriptor : FileDescriptor ): Document = loadDocument(Source .fromFile(fileDescriptor))
74+ def loadDocument (inputStream : InputStream ): Document = loadDocument(Source .fromInputStream(inputStream))
75+ def loadDocument (reader : Reader ): Document = loadDocument(Source .fromReader(reader))
76+ def loadStringDocument (string : String ): Document = loadDocument(Source .fromString(string))
77+
78+ /** Loads XML element. */
79+ def load (inputSource : InputSource ): T = getDocElem(loadDocument(inputSource))
80+ def loadFile (fileName : String ): T = getDocElem(loadFileDocument(fileName))
81+ def loadFile (file : File ): T = getDocElem(loadFileDocument(file))
82+ def load (url : URL ): T = getDocElem(loadDocument(url))
83+ def load (sysId : String ): T = getDocElem(loadDocument(sysId))
84+ def loadFile (fileDescriptor : FileDescriptor ): T = getDocElem(loadFileDocument(fileDescriptor))
85+ def load (inputStream : InputStream ): T = getDocElem(loadDocument(inputStream))
86+ def load (reader : Reader ): T = getDocElem(loadDocument(reader))
87+ def loadString (string : String ): T = getDocElem(loadStringDocument(string))
11188
11289 /** Load XML nodes, including comments and processing instructions that precede and follow the root element. */
113- def loadNodes (inputSource : InputSource ): Seq [Node ] = loadXMLNodes (inputSource, reader)
114- def loadFileNodes (fileName : String ): Seq [Node ] = loadNodes(fromFile( fileName))
115- def loadFileNodes (file : File ): Seq [Node ] = loadNodes(fromFile( file))
116- def loadNodes (url : URL ): Seq [Node ] = loadNodes(fromUrl( url))
117- def loadNodes (sysId : String ): Seq [Node ] = loadNodes(fromSysId( sysId))
118- def loadFileNodes (fileDescriptor : FileDescriptor ): Seq [Node ] = loadNodes(fromFile( fileDescriptor))
119- def loadNodes (inputStream : InputStream ): Seq [Node ] = loadNodes(fromInputStream( inputStream))
120- def loadNodes (reader : Reader ): Seq [Node ] = loadNodes(fromReader( reader))
121- def loadStringNodes (string : String ): Seq [Node ] = loadNodes(fromString( string))
90+ def loadNodes (inputSource : InputSource ): Seq [Node ] = loadDocument (inputSource).children
91+ def loadFileNodes (fileName : String ): Seq [Node ] = loadFileDocument( fileName).children
92+ def loadFileNodes (file : File ): Seq [Node ] = loadFileDocument( file).children
93+ def loadNodes (url : URL ): Seq [Node ] = loadDocument( url).children
94+ def loadNodes (sysId : String ): Seq [Node ] = loadDocument( sysId).children
95+ def loadFileNodes (fileDescriptor : FileDescriptor ): Seq [Node ] = loadFileDocument( fileDescriptor).children
96+ def loadNodes (inputStream : InputStream ): Seq [Node ] = loadDocument( inputStream).children
97+ def loadNodes (reader : Reader ): Seq [Node ] = loadDocument( reader).children
98+ def loadStringNodes (string : String ): Seq [Node ] = loadStringDocument( string).children
12299}
0 commit comments