author | wenzelm |
Thu, 20 Sep 2012 10:43:04 +0200 | |
changeset 49465 | 76ecbc7f3683 |
parent 49417 | c5a8592fb5d3 |
child 49466 | 99ed1f422635 |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/xml.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
27931 | 3 |
Author: Makarius |
4 |
||
44698 | 5 |
Untyped XML trees and basic data representation. |
27931 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
38869
diff
changeset
|
10 |
import java.lang.System |
34108 | 11 |
import java.util.WeakHashMap |
12 |
import java.lang.ref.WeakReference |
|
13 |
import javax.xml.parsers.DocumentBuilderFactory |
|
14 |
||
27947 | 15 |
|
29203 | 16 |
object XML |
17 |
{ |
|
43767 | 18 |
/** XML trees **/ |
19 |
||
27947 | 20 |
/* datatype representation */ |
21 |
||
43780 | 22 |
type Attributes = Properties.T |
27931 | 23 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
24 |
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
|
25 |
case class Elem(markup: Markup, body: List[Tree]) extends Tree |
29204 | 26 |
case class Text(content: String) extends Tree |
29203 | 27 |
|
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
28 |
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
|
29 |
def elem(name: String) = Elem(Markup(name, Nil), Nil) |
33999 | 30 |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
31 |
type Body = List[Tree] |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
32 |
|
29203 | 33 |
|
34 |
/* string representation */ |
|
35 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
36 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
37 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
38 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
39 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
40 |
def text(txt: String) { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
41 |
if (txt == null) s ++= txt |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
42 |
else { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
43 |
for (c <- txt.iterator) c match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
44 |
case '<' => s ++= "<" |
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 += c |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
50 |
} |
34005 | 51 |
} |
29203 | 52 |
} |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
56 |
t match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
57 |
case Elem(markup, Nil) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
58 |
s ++= "<"; elem(markup); s ++= "/>" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
59 |
case Elem(markup, ts) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
60 |
s ++= "<"; elem(markup); s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
61 |
ts.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
62 |
s ++= "</"; s ++= markup.name; s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
63 |
case Text(txt) => text(txt) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
64 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
65 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
66 |
s.toString |
29203 | 67 |
} |
68 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
69 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 70 |
|
71 |
||
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
72 |
/* content -- text and markup */ |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
73 |
|
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
74 |
private def make_content(body: Body, record_markup: Boolean): (String, Markup_Tree) = |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
75 |
{ |
49465 | 76 |
val text = new StringBuilder |
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
77 |
var markup_tree = Markup_Tree.empty |
27941 | 78 |
|
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
79 |
def traverse(tree: Tree): Unit = |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
80 |
tree match { |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
81 |
case Elem(markup, trees) => |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
82 |
val offset = text.length |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
83 |
trees.foreach(traverse) |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
84 |
val end_offset = text.length |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
85 |
if (record_markup) |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
86 |
markup_tree += |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
87 |
isabelle.Text.Info(isabelle.Text.Range(offset, end_offset), Elem(markup, Nil)) |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
88 |
case Text(s) => text.append(s) |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
89 |
} |
27941 | 90 |
|
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
91 |
body.foreach(traverse) |
49417 | 92 |
(text.toString, markup_tree.reverse_markup) |
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
93 |
} |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
94 |
|
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
95 |
def content_markup(body: Body): (String, Markup_Tree) = make_content(body, true) |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
96 |
def content(body: Body): String = make_content(body, false)._1 |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
97 |
def content(tree: Tree): String = make_content(List(tree), false)._1 |
27941 | 98 |
|
27947 | 99 |
|
44808 | 100 |
|
101 |
/** cache for partial sharing (weak table) **/ |
|
34108 | 102 |
|
43745 | 103 |
class Cache(initial_size: Int = 131071, max_string: Int = 100) |
34108 | 104 |
{ |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
105 |
private var table = new WeakHashMap[Any, WeakReference[Any]](initial_size) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
106 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
107 |
private def lookup[A](x: A): Option[A] = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
108 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
109 |
val ref = table.get(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
110 |
if (ref == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
111 |
else { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
112 |
val y = ref.asInstanceOf[WeakReference[A]].get |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
113 |
if (y == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
114 |
else Some(y) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
115 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
116 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
117 |
private def store[A](x: A): A = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
118 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
119 |
table.put(x, new WeakReference[Any](x)) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
120 |
x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
121 |
} |
34108 | 122 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
123 |
private def trim_bytes(s: String): String = new String(s.toCharArray) |
38869 | 124 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
125 |
private def _cache_string(x: String): String = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
126 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
127 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
128 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
129 |
val z = trim_bytes(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
130 |
if (z.length > max_string) z else store(z) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
131 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
132 |
private def _cache_props(x: Properties.T): Properties.T = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
133 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
134 |
else |
34133 | 135 |
lookup(x) match { |
136 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
137 |
case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2)))) |
34133 | 138 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
139 |
private def _cache_markup(x: Markup): Markup = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
140 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
141 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
142 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
143 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
144 |
case Markup(name, props) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
145 |
store(Markup(_cache_string(name), _cache_props(props))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
146 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
147 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
148 |
private def _cache_tree(x: XML.Tree): XML.Tree = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
149 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
150 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
151 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
152 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
153 |
case XML.Elem(markup, body) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
154 |
store(XML.Elem(_cache_markup(markup), _cache_body(body))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
155 |
case XML.Text(text) => store(XML.Text(_cache_string(text))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
156 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
157 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
158 |
private def _cache_body(x: XML.Body): XML.Body = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
159 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
160 |
else |
34133 | 161 |
lookup(x) match { |
162 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
163 |
case None => x.map(_cache_tree(_)) |
34133 | 164 |
} |
38446
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 |
// main methods |
44705 | 167 |
def cache_string(x: String): String = synchronized { _cache_string(x) } |
168 |
def cache_markup(x: Markup): Markup = synchronized { _cache_markup(x) } |
|
169 |
def cache_tree(x: XML.Tree): XML.Tree = synchronized { _cache_tree(x) } |
|
170 |
def cache_body(x: XML.Body): XML.Body = synchronized { _cache_body(x) } |
|
34108 | 171 |
} |
172 |
||
173 |
||
43767 | 174 |
|
175 |
/** document object model (W3C DOM) **/ |
|
27948
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
wenzelm
parents:
27947
diff
changeset
|
176 |
|
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
|
177 |
def get_data(node: org.w3c.dom.Node): Option[XML.Tree] = |
38231 | 178 |
node.getUserData(Markup.Data.name) match { |
34047 | 179 |
case tree: XML.Tree => Some(tree) |
180 |
case _ => None |
|
181 |
} |
|
182 |
||
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
|
183 |
def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node = |
33953 | 184 |
{ |
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
|
185 |
def DOM(tr: Tree): org.w3c.dom.Node = tr match { |
38231 | 186 |
case Elem(Markup.Data, List(data, t)) => |
34046 | 187 |
val node = DOM(t) |
38231 | 188 |
node.setUserData(Markup.Data.name, data, null) |
34046 | 189 |
node |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
190 |
case Elem(Markup(name, atts), ts) => |
38231 | 191 |
if (name == Markup.Data.name) |
34046 | 192 |
error("Malformed data element: " + tr.toString) |
27947 | 193 |
val node = doc.createElement(name) |
194 |
for ((name, value) <- atts) node.setAttribute(name, value) |
|
27952 | 195 |
for (t <- ts) node.appendChild(DOM(t)) |
27947 | 196 |
node |
197 |
case Text(txt) => doc.createTextNode(txt) |
|
198 |
} |
|
33953 | 199 |
DOM(tree) |
200 |
} |
|
43767 | 201 |
|
202 |
||
203 |
||
204 |
/** XML as data representation language **/ |
|
205 |
||
206 |
class XML_Atom(s: String) extends Exception(s) |
|
207 |
class XML_Body(body: XML.Body) extends Exception |
|
208 |
||
209 |
object Encode |
|
210 |
{ |
|
211 |
type T[A] = A => XML.Body |
|
212 |
||
213 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
214 |
/* atomic values */ |
43767 | 215 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
216 |
def long_atom(i: Long): String = i.toString |
43767 | 217 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
218 |
def int_atom(i: Int): String = i.toString |
43767 | 219 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
220 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 221 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
222 |
def unit_atom(u: Unit) = "" |
43767 | 223 |
|
224 |
||
225 |
/* structural nodes */ |
|
226 |
||
227 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
228 |
||
43781 | 229 |
private def vector(xs: List[String]): XML.Attributes = |
46839
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
230 |
xs.zipWithIndex.map({ case (x, i) => (int_atom(i), x) }) |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
231 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
232 |
private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree = |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
233 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 234 |
|
235 |
||
236 |
/* representation of standard types */ |
|
237 |
||
43780 | 238 |
val properties: T[Properties.T] = |
43767 | 239 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
240 |
||
241 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
242 |
||
243 |
val long: T[Long] = (x => string(long_atom(x))) |
|
244 |
||
245 |
val int: T[Int] = (x => string(int_atom(x))) |
|
246 |
||
247 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
248 |
||
249 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
250 |
||
251 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
252 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
253 |
||
254 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
255 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
256 |
||
257 |
def list[A](f: T[A]): T[List[A]] = |
|
258 |
(xs => xs.map((x: A) => node(f(x)))) |
|
259 |
||
260 |
def option[A](f: T[A]): T[Option[A]] = |
|
261 |
{ |
|
262 |
case None => Nil |
|
263 |
case Some(x) => List(node(f(x))) |
|
264 |
} |
|
265 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
266 |
def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] = |
43767 | 267 |
{ |
268 |
case x => |
|
269 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
270 |
List(tagged(tag, f(x))) |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
object Decode |
|
275 |
{ |
|
276 |
type T[A] = XML.Body => A |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
277 |
type V[A] = (List[String], XML.Body) => A |
43767 | 278 |
|
279 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
280 |
/* atomic values */ |
43767 | 281 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
282 |
def long_atom(s: String): Long = |
43767 | 283 |
try { java.lang.Long.parseLong(s) } |
284 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
285 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
286 |
def int_atom(s: String): Int = |
43767 | 287 |
try { Integer.parseInt(s) } |
288 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
289 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
290 |
def bool_atom(s: String): Boolean = |
43767 | 291 |
if (s == "1") true |
292 |
else if (s == "0") false |
|
293 |
else throw new XML_Atom(s) |
|
294 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
295 |
def unit_atom(s: String): Unit = |
43767 | 296 |
if (s == "") () else throw new XML_Atom(s) |
297 |
||
298 |
||
299 |
/* structural nodes */ |
|
300 |
||
301 |
private def node(t: XML.Tree): XML.Body = |
|
302 |
t match { |
|
303 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
304 |
case _ => throw new XML_Body(List(t)) |
|
305 |
} |
|
306 |
||
43781 | 307 |
private def vector(atts: XML.Attributes): List[String] = |
46839
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
308 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
309 |
{ case ((a, x), i) => if (int_atom(a) == i) x else throw new XML_Atom(a) }).toList |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
310 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
311 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 312 |
t match { |
43781 | 313 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 314 |
case _ => throw new XML_Body(List(t)) |
315 |
} |
|
316 |
||
317 |
||
318 |
/* representation of standard types */ |
|
319 |
||
43780 | 320 |
val properties: T[Properties.T] = |
43767 | 321 |
{ |
322 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
323 |
case ts => throw new XML_Body(ts) |
|
324 |
} |
|
325 |
||
326 |
val string: T[String] = |
|
327 |
{ |
|
328 |
case Nil => "" |
|
329 |
case List(XML.Text(s)) => s |
|
330 |
case ts => throw new XML_Body(ts) |
|
331 |
} |
|
332 |
||
333 |
val long: T[Long] = (x => long_atom(string(x))) |
|
334 |
||
335 |
val int: T[Int] = (x => int_atom(string(x))) |
|
336 |
||
337 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
338 |
||
339 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
340 |
||
341 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
342 |
{ |
|
343 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
344 |
case ts => throw new XML_Body(ts) |
|
345 |
} |
|
346 |
||
347 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
348 |
{ |
|
349 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
350 |
case ts => throw new XML_Body(ts) |
|
351 |
} |
|
352 |
||
353 |
def list[A](f: T[A]): T[List[A]] = |
|
354 |
(ts => ts.map(t => f(node(t)))) |
|
355 |
||
356 |
def option[A](f: T[A]): T[Option[A]] = |
|
357 |
{ |
|
358 |
case Nil => None |
|
359 |
case List(t) => Some(f(node(t))) |
|
360 |
case ts => throw new XML_Body(ts) |
|
361 |
} |
|
362 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
363 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 364 |
{ |
365 |
case List(t) => |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
366 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 367 |
val f = |
368 |
try { fs(tag) } |
|
369 |
catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) } |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
370 |
f(xs, ts) |
43767 | 371 |
case ts => throw new XML_Body(ts) |
372 |
} |
|
373 |
} |
|
27931 | 374 |
} |