author | wenzelm |
Fri, 18 Dec 2009 16:52:36 +0100 | |
changeset 34133 | 17554065f3be |
parent 34119 | ae92efb48784 |
child 34871 | e596a0b71f3c |
permissions | -rw-r--r-- |
27931 | 1 |
/* Title: Pure/General/xml.scala |
2 |
Author: Makarius |
|
3 |
||
27947 | 4 |
Simple XML tree values. |
27931 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
34108 | 9 |
import java.util.WeakHashMap |
10 |
import java.lang.ref.WeakReference |
|
11 |
import javax.xml.parsers.DocumentBuilderFactory |
|
12 |
||
27947 | 13 |
import org.w3c.dom.{Node, Document} |
14 |
||
15 |
||
29203 | 16 |
object XML |
17 |
{ |
|
27947 | 18 |
/* datatype representation */ |
19 |
||
27931 | 20 |
type Attributes = List[(String, String)] |
21 |
||
34046 | 22 |
sealed abstract class Tree { |
29204 | 23 |
override def toString = { |
24 |
val s = new StringBuilder |
|
25 |
append_tree(this, s) |
|
26 |
s.toString |
|
27 |
} |
|
29203 | 28 |
} |
29204 | 29 |
case class Elem(name: String, attributes: Attributes, body: List[Tree]) extends Tree |
30 |
case class Text(content: String) extends Tree |
|
29203 | 31 |
|
33999 | 32 |
def elem(name: String, body: List[Tree]) = Elem(name, Nil, body) |
33 |
def elem(name: String) = Elem(name, Nil, Nil) |
|
34 |
||
29203 | 35 |
|
36 |
/* string representation */ |
|
37 |
||
38 |
private def append_text(text: String, s: StringBuilder) { |
|
34005 | 39 |
if (text == null) s ++ text |
40 |
else { |
|
41 |
for (c <- text.elements) c match { |
|
42 |
case '<' => s ++ "<" |
|
43 |
case '>' => s ++ ">" |
|
44 |
case '&' => s ++ "&" |
|
45 |
case '"' => s ++ """ |
|
46 |
case '\'' => s ++ "'" |
|
47 |
case _ => s + c |
|
48 |
} |
|
29203 | 49 |
} |
50 |
} |
|
51 |
||
52 |
private def append_elem(name: String, atts: Attributes, s: StringBuilder) { |
|
34005 | 53 |
s ++ name |
29203 | 54 |
for ((a, x) <- atts) { |
34005 | 55 |
s ++ " "; s ++ a; s ++ "=\""; append_text(x, s); s ++ "\"" |
29203 | 56 |
} |
57 |
} |
|
58 |
||
29204 | 59 |
private def append_tree(tree: Tree, s: StringBuilder) { |
29203 | 60 |
tree match { |
61 |
case Elem(name, atts, Nil) => |
|
34005 | 62 |
s ++ "<"; append_elem(name, atts, s); s ++ "/>" |
29203 | 63 |
case Elem(name, atts, ts) => |
34005 | 64 |
s ++ "<"; append_elem(name, atts, s); s ++ ">" |
29203 | 65 |
for (t <- ts) append_tree(t, s) |
34005 | 66 |
s ++ "</"; s ++ name; s ++ ">" |
29203 | 67 |
case Text(text) => append_text(text, s) |
68 |
} |
|
69 |
} |
|
27941 | 70 |
|
71 |
||
27942 | 72 |
/* iterate over content */ |
27941 | 73 |
|
74 |
private type State = Option[(String, List[Tree])] |
|
75 |
||
76 |
private def get_next(tree: Tree): State = tree match { |
|
77 |
case Elem(_, _, body) => get_nexts(body) |
|
78 |
case Text(content) => Some(content, Nil) |
|
79 |
} |
|
80 |
private def get_nexts(trees: List[Tree]): State = trees match { |
|
81 |
case Nil => None |
|
82 |
case t :: ts => get_next(t) match { |
|
83 |
case None => get_nexts(ts) |
|
28007 | 84 |
case Some((s, r)) => Some((s, r ++ ts)) |
27941 | 85 |
} |
86 |
} |
|
87 |
||
88 |
def content(tree: Tree) = new Iterator[String] { |
|
89 |
private var state = get_next(tree) |
|
90 |
def hasNext() = state.isDefined |
|
91 |
def next() = state match { |
|
92 |
case Some((s, rest)) => { state = get_nexts(rest); s } |
|
93 |
case None => throw new NoSuchElementException("next on empty iterator") |
|
94 |
} |
|
95 |
} |
|
96 |
||
27947 | 97 |
|
34108 | 98 |
/* cache for partial sharing -- NOT THREAD SAFE */ |
99 |
||
100 |
class Cache(initial_size: Int) |
|
101 |
{ |
|
102 |
private val table = new WeakHashMap[Any, WeakReference[Any]](initial_size) |
|
103 |
||
104 |
private def lookup[A](x: A): Option[A] = |
|
105 |
{ |
|
106 |
val ref = table.get(x) |
|
107 |
if (ref == null) None |
|
108 |
else { |
|
109 |
val y = ref.asInstanceOf[WeakReference[A]].get |
|
110 |
if (y == null) None |
|
111 |
else Some(y) |
|
112 |
} |
|
113 |
} |
|
114 |
private def store[A](x: A): A = |
|
115 |
{ |
|
116 |
table.put(x, new WeakReference[Any](x)) |
|
117 |
x |
|
118 |
} |
|
119 |
||
120 |
def cache_string(x: String): String = |
|
121 |
lookup(x) match { |
|
122 |
case Some(y) => y |
|
123 |
case None => store(x) |
|
124 |
} |
|
125 |
def cache_props(x: List[(String, String)]): List[(String, String)] = |
|
34133 | 126 |
if (x.isEmpty) x |
127 |
else |
|
128 |
lookup(x) match { |
|
129 |
case Some(y) => y |
|
130 |
case None => store(x.map(p => (cache_string(p._1), cache_string(p._2)))) |
|
131 |
} |
|
34117 | 132 |
def cache_tree(x: XML.Tree): XML.Tree = |
34108 | 133 |
lookup(x) match { |
134 |
case Some(y) => y |
|
135 |
case None => |
|
136 |
x match { |
|
137 |
case XML.Elem(name, props, body) => |
|
34117 | 138 |
store(XML.Elem(cache_string(name), cache_props(props), cache_trees(body))) |
34108 | 139 |
case XML.Text(text) => XML.Text(cache_string(text)) |
140 |
} |
|
141 |
} |
|
34117 | 142 |
def cache_trees(x: List[XML.Tree]): List[XML.Tree] = |
34133 | 143 |
if (x.isEmpty) x |
144 |
else |
|
145 |
lookup(x) match { |
|
146 |
case Some(y) => y |
|
147 |
case None => x.map(cache_tree(_)) |
|
148 |
} |
|
34108 | 149 |
} |
150 |
||
151 |
||
33953 | 152 |
/* document object model (W3C DOM) */ |
27948
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
wenzelm
parents:
27947
diff
changeset
|
153 |
|
34047 | 154 |
def get_data(node: Node): Option[XML.Tree] = |
155 |
node.getUserData(Markup.DATA) match { |
|
156 |
case tree: XML.Tree => Some(tree) |
|
157 |
case _ => None |
|
158 |
} |
|
159 |
||
33953 | 160 |
def document_node(doc: Document, tree: Tree): Node = |
161 |
{ |
|
27952 | 162 |
def DOM(tr: Tree): Node = tr match { |
34046 | 163 |
case Elem(Markup.DATA, Nil, List(data, t)) => |
164 |
val node = DOM(t) |
|
165 |
node.setUserData(Markup.DATA, data, null) |
|
166 |
node |
|
167 |
case Elem(name, atts, ts) => |
|
168 |
if (name == Markup.DATA) |
|
169 |
error("Malformed data element: " + tr.toString) |
|
27947 | 170 |
val node = doc.createElement(name) |
171 |
for ((name, value) <- atts) node.setAttribute(name, value) |
|
27952 | 172 |
for (t <- ts) node.appendChild(DOM(t)) |
27947 | 173 |
node |
174 |
case Text(txt) => doc.createTextNode(txt) |
|
175 |
} |
|
33953 | 176 |
DOM(tree) |
177 |
} |
|
27931 | 178 |
} |