@@ -43,24 +43,47 @@ abstract class FactoryAdapter extends DefaultHandler2 with factory.XMLLoader[Nod
43
43
// reference to the XMLReader that parses the document; this is used to query
44
44
// features (e.g., 'is-standalone') and properties (e.g., document-xml-version) -
45
45
// see http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html
46
- private var xmlReader : Option [XMLReader ] = None
46
+ private [ this ] var xmlReader : Option [XMLReader ] = None
47
47
48
- private var dtdBuilder : Option [DtdBuilder ] = None
48
+ private [ this ] var dtdBuilder : Option [DtdBuilder ] = None
49
49
private def inDtd : Boolean = dtdBuilder.isDefined && ! dtdBuilder.get.isDone
50
50
51
- private var document : Option [Document ] = None
52
- private var baseURI : Option [String ] = None
53
- private var xmlEncoding : Option [String ] = None
54
51
55
- private var prefixMappings : List [(String , String )] = List .empty
52
+ /**
53
+ * The [[Document ]] built during [[endDocument ]]. This allows using [[FactoryAdapter ]] (or [[NoBindingFactoryAdapter ]])
54
+ * for converting other XML representations accepting an [[org.xml.sax.ContentHandler ]] to scala.xml.
55
+ *
56
+ * {{{
57
+ * // using org.jdom:jdom2:2.0.6.1
58
+ *
59
+ * import scala.xml._
60
+ * import org.jdom2._
61
+ * import org.jdom2.output._
62
+ *
63
+ * def jd2s(d: Document): Node = {
64
+ * val out = new SAXOutputter
65
+ * val h = new parsing.NoBindingFactoryAdapter
66
+ * out.setContentHandler(h)
67
+ * out.output(d)
68
+ * h.document.docElem
69
+ * }
70
+ * }}}
71
+ */
72
+ def document : Document = _document
73
+
74
+ private [this ] var _document : Document = null
75
+ private [this ] var baseURI : Option [String ] = None
76
+ private [this ] var xmlEncoding : Option [String ] = None
77
+
78
+ private [this ] var prefixMappings : List [(String , String )] = List .empty
56
79
57
80
// TODO all the variables should be private, but - binary compatibility...
58
81
var prolog : List [Node ] = List .empty
59
82
var rootElem : Node = _
60
83
var epilogue : List [Node ] = List .empty
61
84
62
85
val buffer : StringBuilder = new StringBuilder ()
63
- private var inCDATA : Boolean = false
86
+ private [ this ] var inCDATA : Boolean = false
64
87
65
88
/** List of attributes
66
89
*
@@ -140,7 +163,7 @@ abstract class FactoryAdapter extends DefaultHandler2 with factory.XMLLoader[Nod
140
163
this .xmlReader = Some (xmlReader)
141
164
xmlReader.parse(inputSource)
142
165
143
- document.get
166
+ _document
144
167
}
145
168
146
169
// abstract methods
@@ -213,7 +236,7 @@ abstract class FactoryAdapter extends DefaultHandler2 with factory.XMLLoader[Nod
213
236
epilogue = hStack.init.reverse
214
237
215
238
val document = new Document
216
- this .document = Some ( document)
239
+ this ._document = document
217
240
document.children = prolog ++ rootElem ++ epilogue
218
241
document.docElem = rootElem
219
242
document.dtd = dtdBuilder.map(_.dtd).orNull
@@ -222,15 +245,15 @@ abstract class FactoryAdapter extends DefaultHandler2 with factory.XMLLoader[Nod
222
245
223
246
document.version =
224
247
try {
225
- Option ( xmlReader.get .getProperty(" http://xml.org/sax/properties/document-xml-version" ).asInstanceOf [String ])
248
+ xmlReader.map(_ .getProperty(" http://xml.org/sax/properties/document-xml-version" ).asInstanceOf [String ])
226
249
} catch {
227
250
case _ : SAXNotRecognizedException => None
228
251
case _ : SAXNotSupportedException => None
229
252
}
230
253
231
254
document.standAlone =
232
255
try {
233
- Some ( xmlReader.get .getFeature(" http://xml.org/sax/features/is-standalone" ))
256
+ xmlReader.map(_ .getFeature(" http://xml.org/sax/features/is-standalone" ))
234
257
} catch {
235
258
case _ : SAXNotRecognizedException => None
236
259
case _ : SAXNotSupportedException => None
0 commit comments