1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import com.sun.xml.internal.fastinfoset._
import com.sun.xml.internal.fastinfoset.sax._
import scala.xml.parsing.NoBindingFactoryAdapter
import scala.xml._
// Note - this is in pending because com.sun.xml.etc is not standard,
// and I don't have time to extract a smaller test.
object Test {
def main(args: Array[String]) {
val node = <test/>
val bytes = new ByteArrayOutputStream
val serializer = new SAXDocumentSerializer()
serializer.setOutputStream(bytes)
serializer.startDocument()
serialize(node, serializer)
serializer.endDocument()
println(parse(new ByteArrayInputStream(bytes.toByteArray)))
}
def serialize(node: Node, serializer: SAXDocumentSerializer) {
node match {
case _ : ProcInstr | _ : Comment | _ : EntityRef =>
case x : Atom[_] =>
val chars = x.text.toCharArray
serializer.characters(chars, 0, chars.length)
case _ : Elem =>
serializer.startElement("", node.label.toLowerCase, node.label.toLowerCase, attributes(node.attributes))
for (m <- node.child) serialize(m, serializer)
serializer.endElement("", node.label.toLowerCase, node.label.toLowerCase)
}
}
def parse(str: ByteArrayInputStream) = {
val parser = new SAXDocumentParser
val fac = new NoBindingFactoryAdapter
parser.setContentHandler(fac)
try {
parser.parse(str)
} catch {
case x: Exception =>
x.printStackTrace
}
fac.rootElem
}
def attributes(d: MetaData) = {
val attrs = new AttributesHolder
if (d != null) {
for (attr <- d) {
val sb = new StringBuilder()
Utility.sequenceToXML(attr.value, TopScope, sb, true)
attrs.addAttribute(new QualifiedName("", "", attr.key.toLowerCase), sb.toString)
}
}
attrs
}
}
|