author | wenzelm |
Tue, 12 Jul 2011 11:16:56 +0200 | |
changeset 43768 | d52ab827d62b |
parent 43767 | e0219ef7f84c |
child 43778 | ce9189450447 |
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 |
||
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
38869
diff
changeset
|
9 |
import java.lang.System |
34108 | 10 |
import java.util.WeakHashMap |
11 |
import java.lang.ref.WeakReference |
|
12 |
import javax.xml.parsers.DocumentBuilderFactory |
|
13 |
||
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
14 |
import scala.actors.Actor._ |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
15 |
|
27947 | 16 |
|
29203 | 17 |
object XML |
18 |
{ |
|
43767 | 19 |
/** XML trees **/ |
20 |
||
27947 | 21 |
/* datatype representation */ |
22 |
||
27931 | 23 |
type Attributes = List[(String, String)] |
24 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
25 |
sealed abstract class Tree { override def toString = string_of_tree(this) } |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
26 |
case class Elem(markup: Markup, body: List[Tree]) extends Tree |
29204 | 27 |
case class Text(content: String) extends Tree |
29203 | 28 |
|
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
29 |
def elem(name: String, body: List[Tree]) = Elem(Markup(name, Nil), body) |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
30 |
def elem(name: String) = Elem(Markup(name, Nil), Nil) |
33999 | 31 |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
32 |
type Body = List[Tree] |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
33 |
|
29203 | 34 |
|
35 |
/* string representation */ |
|
36 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
37 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
38 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
39 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
40 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
41 |
def text(txt: String) { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
42 |
if (txt == null) s ++= txt |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
43 |
else { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
44 |
for (c <- txt.iterator) c match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
45 |
case '<' => s ++= "<" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
46 |
case '>' => s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
47 |
case '&' => s ++= "&" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
48 |
case '"' => s ++= """ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
49 |
case '\'' => s ++= "'" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
50 |
case _ => s += c |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
51 |
} |
34005 | 52 |
} |
29203 | 53 |
} |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
54 |
def attrib(p: (String, String)) { s ++= " "; s ++= p._1; s ++= "=\""; text(p._2); s ++= "\"" } |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
55 |
def elem(markup: Markup) { s ++= markup.name; markup.properties.foreach(attrib) } |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
56 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
57 |
t match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
58 |
case Elem(markup, Nil) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
59 |
s ++= "<"; elem(markup); s ++= "/>" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
60 |
case Elem(markup, ts) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
61 |
s ++= "<"; elem(markup); s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
62 |
ts.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
63 |
s ++= "</"; s ++= markup.name; s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
64 |
case Text(txt) => text(txt) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
65 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
66 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
67 |
s.toString |
29203 | 68 |
} |
69 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
70 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 71 |
|
72 |
||
38484
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
wenzelm
parents:
38446
diff
changeset
|
73 |
/* text content */ |
27941 | 74 |
|
38484
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
wenzelm
parents:
38446
diff
changeset
|
75 |
def content_stream(tree: Tree): Stream[String] = |
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
wenzelm
parents:
38446
diff
changeset
|
76 |
tree match { |
43747 | 77 |
case Elem(_, body) => content_stream(body) |
38484
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
wenzelm
parents:
38446
diff
changeset
|
78 |
case Text(content) => Stream(content) |
27941 | 79 |
} |
43747 | 80 |
def content_stream(body: Body): Stream[String] = |
81 |
body.toStream.flatten(content_stream(_)) |
|
27941 | 82 |
|
38484
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
wenzelm
parents:
38446
diff
changeset
|
83 |
def content(tree: Tree): Iterator[String] = content_stream(tree).iterator |
43747 | 84 |
def content(body: Body): Iterator[String] = content_stream(body).iterator |
27941 | 85 |
|
27947 | 86 |
|
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
87 |
/* pipe-lined cache for partial sharing */ |
34108 | 88 |
|
43745 | 89 |
class Cache(initial_size: Int = 131071, max_string: Int = 100) |
34108 | 90 |
{ |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
91 |
private val cache_actor = actor |
34108 | 92 |
{ |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
93 |
val table = new WeakHashMap[Any, WeakReference[Any]](initial_size) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
94 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
95 |
def lookup[A](x: A): Option[A] = |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
96 |
{ |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
97 |
val ref = table.get(x) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
98 |
if (ref == null) None |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
99 |
else { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
100 |
val y = ref.asInstanceOf[WeakReference[A]].get |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
101 |
if (y == null) None |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
102 |
else Some(y) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
103 |
} |
34108 | 104 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
105 |
def store[A](x: A): A = |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
106 |
{ |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
107 |
table.put(x, new WeakReference[Any](x)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
108 |
x |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
109 |
} |
34108 | 110 |
|
38869 | 111 |
def trim_bytes(s: String): String = new String(s.toCharArray) |
112 |
||
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
113 |
def cache_string(x: String): String = |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
114 |
lookup(x) match { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
115 |
case Some(y) => y |
43745 | 116 |
case None => |
117 |
val z = trim_bytes(x) |
|
118 |
if (z.length > max_string) z else store(z) |
|
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
119 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
120 |
def cache_props(x: List[(String, String)]): List[(String, String)] = |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
121 |
if (x.isEmpty) x |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
122 |
else |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
123 |
lookup(x) match { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
124 |
case Some(y) => y |
38869 | 125 |
case None => store(x.map(p => (trim_bytes(p._1).intern, cache_string(p._2)))) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
126 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
127 |
def cache_markup(x: Markup): Markup = |
34133 | 128 |
lookup(x) match { |
129 |
case Some(y) => y |
|
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
130 |
case None => |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
131 |
x match { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
132 |
case Markup(name, props) => |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
133 |
store(Markup(cache_string(name), cache_props(props))) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
134 |
} |
34133 | 135 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
136 |
def cache_tree(x: XML.Tree): XML.Tree = |
34133 | 137 |
lookup(x) match { |
138 |
case Some(y) => y |
|
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
139 |
case None => |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
140 |
x match { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
141 |
case XML.Elem(markup, body) => |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
142 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
143 |
case XML.Text(text) => store(XML.Text(cache_string(text))) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
144 |
} |
34133 | 145 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
146 |
def cache_body(x: XML.Body): XML.Body = |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
147 |
if (x.isEmpty) x |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
148 |
else |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
149 |
lookup(x) match { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
150 |
case Some(y) => y |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
151 |
case None => x.map(cache_tree(_)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
152 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
153 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
154 |
// main loop |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
155 |
loop { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
156 |
react { |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
157 |
case Cache_String(x, f) => f(cache_string(x)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
158 |
case Cache_Markup(x, f) => f(cache_markup(x)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
159 |
case Cache_Tree(x, f) => f(cache_tree(x)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
160 |
case Cache_Body(x, f) => f(cache_body(x)) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
161 |
case bad => System.err.println("XML.cache_actor: ignoring bad input " + bad) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
162 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
163 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
164 |
} |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
165 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
166 |
private case class Cache_String(x: String, f: String => Unit) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
167 |
private case class Cache_Markup(x: Markup, f: Markup => Unit) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
168 |
private case class Cache_Tree(x: XML.Tree, f: XML.Tree => Unit) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
169 |
private case class Cache_Body(x: XML.Body, f: XML.Body => Unit) |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
170 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
171 |
// main methods |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
172 |
def cache_string(x: String)(f: String => Unit) { cache_actor ! Cache_String(x, f) } |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
173 |
def cache_markup(x: Markup)(f: Markup => Unit) { cache_actor ! Cache_Markup(x, f) } |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
174 |
def cache_tree(x: XML.Tree)(f: XML.Tree => Unit) { cache_actor ! Cache_Tree(x, f) } |
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
175 |
def cache_body(x: XML.Body)(f: XML.Body => Unit) { cache_actor ! Cache_Body(x, f) } |
34108 | 176 |
} |
177 |
||
178 |
||
43767 | 179 |
|
180 |
/** document object model (W3C DOM) **/ |
|
27948
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
wenzelm
parents:
27947
diff
changeset
|
181 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34133
diff
changeset
|
182 |
def get_data(node: org.w3c.dom.Node): Option[XML.Tree] = |
38231 | 183 |
node.getUserData(Markup.Data.name) match { |
34047 | 184 |
case tree: XML.Tree => Some(tree) |
185 |
case _ => None |
|
186 |
} |
|
187 |
||
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34133
diff
changeset
|
188 |
def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node = |
33953 | 189 |
{ |
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34133
diff
changeset
|
190 |
def DOM(tr: Tree): org.w3c.dom.Node = tr match { |
38231 | 191 |
case Elem(Markup.Data, List(data, t)) => |
34046 | 192 |
val node = DOM(t) |
38231 | 193 |
node.setUserData(Markup.Data.name, data, null) |
34046 | 194 |
node |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
195 |
case Elem(Markup(name, atts), ts) => |
38231 | 196 |
if (name == Markup.Data.name) |
34046 | 197 |
error("Malformed data element: " + tr.toString) |
27947 | 198 |
val node = doc.createElement(name) |
199 |
for ((name, value) <- atts) node.setAttribute(name, value) |
|
27952 | 200 |
for (t <- ts) node.appendChild(DOM(t)) |
27947 | 201 |
node |
202 |
case Text(txt) => doc.createTextNode(txt) |
|
203 |
} |
|
33953 | 204 |
DOM(tree) |
205 |
} |
|
43767 | 206 |
|
207 |
||
208 |
||
209 |
/** XML as data representation language **/ |
|
210 |
||
211 |
class XML_Atom(s: String) extends Exception(s) |
|
212 |
class XML_Body(body: XML.Body) extends Exception |
|
213 |
||
214 |
object Encode |
|
215 |
{ |
|
216 |
type T[A] = A => XML.Body |
|
217 |
||
218 |
||
219 |
/* basic values */ |
|
220 |
||
221 |
private def long_atom(i: Long): String = i.toString |
|
222 |
||
223 |
private def int_atom(i: Int): String = i.toString |
|
224 |
||
225 |
private def bool_atom(b: Boolean): String = if (b) "1" else "0" |
|
226 |
||
227 |
private def unit_atom(u: Unit) = "" |
|
228 |
||
229 |
||
230 |
/* structural nodes */ |
|
231 |
||
232 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
233 |
||
234 |
private def tagged(tag: Int, ts: XML.Body): XML.Tree = |
|
235 |
XML.Elem(Markup(int_atom(tag), Nil), ts) |
|
236 |
||
237 |
||
238 |
/* representation of standard types */ |
|
239 |
||
240 |
val properties: T[List[(String, String)]] = |
|
241 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
|
242 |
||
243 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
244 |
||
245 |
val long: T[Long] = (x => string(long_atom(x))) |
|
246 |
||
247 |
val int: T[Int] = (x => string(int_atom(x))) |
|
248 |
||
249 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
250 |
||
251 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
252 |
||
253 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
254 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
255 |
||
256 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
257 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
258 |
||
259 |
def list[A](f: T[A]): T[List[A]] = |
|
260 |
(xs => xs.map((x: A) => node(f(x)))) |
|
261 |
||
262 |
def option[A](f: T[A]): T[Option[A]] = |
|
263 |
{ |
|
264 |
case None => Nil |
|
265 |
case Some(x) => List(node(f(x))) |
|
266 |
} |
|
267 |
||
268 |
def variant[A](fs: List[PartialFunction[A, XML.Body]]): T[A] = |
|
269 |
{ |
|
270 |
case x => |
|
271 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
272 |
List(tagged(tag, f(x))) |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
object Decode |
|
277 |
{ |
|
278 |
type T[A] = XML.Body => A |
|
279 |
||
280 |
||
281 |
/* basic values */ |
|
282 |
||
283 |
private def long_atom(s: String): Long = |
|
284 |
try { java.lang.Long.parseLong(s) } |
|
285 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
286 |
||
287 |
private def int_atom(s: String): Int = |
|
288 |
try { Integer.parseInt(s) } |
|
289 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
290 |
||
291 |
private def bool_atom(s: String): Boolean = |
|
292 |
if (s == "1") true |
|
293 |
else if (s == "0") false |
|
294 |
else throw new XML_Atom(s) |
|
295 |
||
296 |
private def unit_atom(s: String): Unit = |
|
297 |
if (s == "") () else throw new XML_Atom(s) |
|
298 |
||
299 |
||
300 |
/* structural nodes */ |
|
301 |
||
302 |
private def node(t: XML.Tree): XML.Body = |
|
303 |
t match { |
|
304 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
305 |
case _ => throw new XML_Body(List(t)) |
|
306 |
} |
|
307 |
||
308 |
private def tagged(t: XML.Tree): (Int, XML.Body) = |
|
309 |
t match { |
|
310 |
case XML.Elem(Markup(s, Nil), ts) => (int_atom(s), ts) |
|
311 |
case _ => throw new XML_Body(List(t)) |
|
312 |
} |
|
313 |
||
314 |
||
315 |
/* representation of standard types */ |
|
316 |
||
317 |
val properties: T[List[(String, String)]] = |
|
318 |
{ |
|
319 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
320 |
case ts => throw new XML_Body(ts) |
|
321 |
} |
|
322 |
||
323 |
val string: T[String] = |
|
324 |
{ |
|
325 |
case Nil => "" |
|
326 |
case List(XML.Text(s)) => s |
|
327 |
case ts => throw new XML_Body(ts) |
|
328 |
} |
|
329 |
||
330 |
val long: T[Long] = (x => long_atom(string(x))) |
|
331 |
||
332 |
val int: T[Int] = (x => int_atom(string(x))) |
|
333 |
||
334 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
335 |
||
336 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
337 |
||
338 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
339 |
{ |
|
340 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
341 |
case ts => throw new XML_Body(ts) |
|
342 |
} |
|
343 |
||
344 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
345 |
{ |
|
346 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
347 |
case ts => throw new XML_Body(ts) |
|
348 |
} |
|
349 |
||
350 |
def list[A](f: T[A]): T[List[A]] = |
|
351 |
(ts => ts.map(t => f(node(t)))) |
|
352 |
||
353 |
def option[A](f: T[A]): T[Option[A]] = |
|
354 |
{ |
|
355 |
case Nil => None |
|
356 |
case List(t) => Some(f(node(t))) |
|
357 |
case ts => throw new XML_Body(ts) |
|
358 |
} |
|
359 |
||
360 |
def variant[A](fs: List[T[A]]): T[A] = |
|
361 |
{ |
|
362 |
case List(t) => |
|
363 |
val (tag, ts) = tagged(t) |
|
43768 | 364 |
val f = |
365 |
try { fs(tag) } |
|
366 |
catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) } |
|
367 |
f(ts) |
|
43767 | 368 |
case ts => throw new XML_Body(ts) |
369 |
} |
|
370 |
} |
|
27931 | 371 |
} |