| author | wenzelm |
| Fri, 18 Dec 2009 12:28:50 +0100 | |
| changeset 34119 | ae92efb48784 |
| parent 34117 | 1eb8d8e3e40a |
| child 34133 | 17554065f3be |
| 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)] = |
|
126 |
lookup(x) match {
|
|
127 |
case Some(y) => y |
|
128 |
case None => store(x.map(p => (cache_string(p._1), cache_string(p._2)))) |
|
129 |
} |
|
| 34117 | 130 |
def cache_tree(x: XML.Tree): XML.Tree = |
| 34108 | 131 |
lookup(x) match {
|
132 |
case Some(y) => y |
|
133 |
case None => |
|
134 |
x match {
|
|
135 |
case XML.Elem(name, props, body) => |
|
| 34117 | 136 |
store(XML.Elem(cache_string(name), cache_props(props), cache_trees(body))) |
| 34108 | 137 |
case XML.Text(text) => XML.Text(cache_string(text)) |
138 |
} |
|
139 |
} |
|
| 34117 | 140 |
def cache_trees(x: List[XML.Tree]): List[XML.Tree] = |
| 34108 | 141 |
lookup(x) match {
|
142 |
case Some(y) => y |
|
| 34117 | 143 |
case None => x.map(cache_tree(_)) |
| 34108 | 144 |
} |
145 |
} |
|
146 |
||
147 |
||
| 33953 | 148 |
/* document object model (W3C DOM) */ |
|
27948
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
wenzelm
parents:
27947
diff
changeset
|
149 |
|
| 34047 | 150 |
def get_data(node: Node): Option[XML.Tree] = |
151 |
node.getUserData(Markup.DATA) match {
|
|
152 |
case tree: XML.Tree => Some(tree) |
|
153 |
case _ => None |
|
154 |
} |
|
155 |
||
| 33953 | 156 |
def document_node(doc: Document, tree: Tree): Node = |
157 |
{
|
|
| 27952 | 158 |
def DOM(tr: Tree): Node = tr match {
|
| 34046 | 159 |
case Elem(Markup.DATA, Nil, List(data, t)) => |
160 |
val node = DOM(t) |
|
161 |
node.setUserData(Markup.DATA, data, null) |
|
162 |
node |
|
163 |
case Elem(name, atts, ts) => |
|
164 |
if (name == Markup.DATA) |
|
165 |
error("Malformed data element: " + tr.toString)
|
|
| 27947 | 166 |
val node = doc.createElement(name) |
167 |
for ((name, value) <- atts) node.setAttribute(name, value) |
|
| 27952 | 168 |
for (t <- ts) node.appendChild(DOM(t)) |
| 27947 | 169 |
node |
170 |
case Text(txt) => doc.createTextNode(txt) |
|
171 |
} |
|
| 33953 | 172 |
DOM(tree) |
173 |
} |
|
| 27931 | 174 |
} |