author | wenzelm |
Mon, 05 Sep 2011 20:30:37 +0200 | |
changeset 44721 | ba478c3f7255 |
parent 44705 | 089fcaf94c00 |
child 44808 | 05b8997899a2 |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/xml.scala |
27931 | 2 |
Author: Makarius |
3 |
||
44698 | 4 |
Untyped XML trees and basic data representation. |
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 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
14 |
import scala.collection.mutable |
38446
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 |
||
43780 | 23 |
type Attributes = Properties.T |
27931 | 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 |
{ |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
91 |
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
|
92 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
93 |
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
|
94 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
95 |
val ref = table.get(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
96 |
if (ref == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
97 |
else { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
98 |
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
|
99 |
if (y == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
100 |
else Some(y) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
101 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
102 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
103 |
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
|
104 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
105 |
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
|
106 |
x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
107 |
} |
34108 | 108 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
109 |
private def trim_bytes(s: String): String = new String(s.toCharArray) |
38869 | 110 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
111 |
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
|
112 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
113 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
114 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
115 |
val z = trim_bytes(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
116 |
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
|
117 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
118 |
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
|
119 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
120 |
else |
34133 | 121 |
lookup(x) match { |
122 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
123 |
case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2)))) |
34133 | 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_markup(x: Markup): Markup = |
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 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
130 |
case Markup(name, props) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
131 |
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
|
132 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
133 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
134 |
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
|
135 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
136 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
137 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
138 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
139 |
case XML.Elem(markup, body) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
143 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
144 |
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
|
145 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
146 |
else |
34133 | 147 |
lookup(x) match { |
148 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
149 |
case None => x.map(_cache_tree(_)) |
34133 | 150 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
151 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
152 |
// main methods |
44705 | 153 |
def cache_string(x: String): String = synchronized { _cache_string(x) } |
154 |
def cache_markup(x: Markup): Markup = synchronized { _cache_markup(x) } |
|
155 |
def cache_tree(x: XML.Tree): XML.Tree = synchronized { _cache_tree(x) } |
|
156 |
def cache_body(x: XML.Body): XML.Body = synchronized { _cache_body(x) } |
|
34108 | 157 |
} |
158 |
||
159 |
||
43767 | 160 |
|
161 |
/** document object model (W3C DOM) **/ |
|
27948
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
wenzelm
parents:
27947
diff
changeset
|
162 |
|
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
|
163 |
def get_data(node: org.w3c.dom.Node): Option[XML.Tree] = |
38231 | 164 |
node.getUserData(Markup.Data.name) match { |
34047 | 165 |
case tree: XML.Tree => Some(tree) |
166 |
case _ => None |
|
167 |
} |
|
168 |
||
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
|
169 |
def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node = |
33953 | 170 |
{ |
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
|
171 |
def DOM(tr: Tree): org.w3c.dom.Node = tr match { |
38231 | 172 |
case Elem(Markup.Data, List(data, t)) => |
34046 | 173 |
val node = DOM(t) |
38231 | 174 |
node.setUserData(Markup.Data.name, data, null) |
34046 | 175 |
node |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
176 |
case Elem(Markup(name, atts), ts) => |
38231 | 177 |
if (name == Markup.Data.name) |
34046 | 178 |
error("Malformed data element: " + tr.toString) |
27947 | 179 |
val node = doc.createElement(name) |
180 |
for ((name, value) <- atts) node.setAttribute(name, value) |
|
27952 | 181 |
for (t <- ts) node.appendChild(DOM(t)) |
27947 | 182 |
node |
183 |
case Text(txt) => doc.createTextNode(txt) |
|
184 |
} |
|
33953 | 185 |
DOM(tree) |
186 |
} |
|
43767 | 187 |
|
188 |
||
189 |
||
190 |
/** XML as data representation language **/ |
|
191 |
||
192 |
class XML_Atom(s: String) extends Exception(s) |
|
193 |
class XML_Body(body: XML.Body) extends Exception |
|
194 |
||
195 |
object Encode |
|
196 |
{ |
|
197 |
type T[A] = A => XML.Body |
|
198 |
||
199 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
200 |
/* atomic values */ |
43767 | 201 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
202 |
def long_atom(i: Long): String = i.toString |
43767 | 203 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
204 |
def int_atom(i: Int): String = i.toString |
43767 | 205 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
206 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 207 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
208 |
def unit_atom(u: Unit) = "" |
43767 | 209 |
|
210 |
||
211 |
/* structural nodes */ |
|
212 |
||
213 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
214 |
||
43781 | 215 |
private def vector(xs: List[String]): XML.Attributes = |
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 |
xs.zipWithIndex.map(p => (int_atom(p._2), p._1)) |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
217 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
218 |
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
|
219 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 220 |
|
221 |
||
222 |
/* representation of standard types */ |
|
223 |
||
43780 | 224 |
val properties: T[Properties.T] = |
43767 | 225 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
226 |
||
227 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
228 |
||
229 |
val long: T[Long] = (x => string(long_atom(x))) |
|
230 |
||
231 |
val int: T[Int] = (x => string(int_atom(x))) |
|
232 |
||
233 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
234 |
||
235 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
236 |
||
237 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
238 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
239 |
||
240 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
241 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
242 |
||
243 |
def list[A](f: T[A]): T[List[A]] = |
|
244 |
(xs => xs.map((x: A) => node(f(x)))) |
|
245 |
||
246 |
def option[A](f: T[A]): T[Option[A]] = |
|
247 |
{ |
|
248 |
case None => Nil |
|
249 |
case Some(x) => List(node(f(x))) |
|
250 |
} |
|
251 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
252 |
def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] = |
43767 | 253 |
{ |
254 |
case x => |
|
255 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
256 |
List(tagged(tag, f(x))) |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
object Decode |
|
261 |
{ |
|
262 |
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
|
263 |
type V[A] = (List[String], XML.Body) => A |
43767 | 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 |
/* atomic values */ |
43767 | 267 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
268 |
def long_atom(s: String): Long = |
43767 | 269 |
try { java.lang.Long.parseLong(s) } |
270 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
271 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
272 |
def int_atom(s: String): Int = |
43767 | 273 |
try { Integer.parseInt(s) } |
274 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
275 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
276 |
def bool_atom(s: String): Boolean = |
43767 | 277 |
if (s == "1") true |
278 |
else if (s == "0") false |
|
279 |
else throw new XML_Atom(s) |
|
280 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
281 |
def unit_atom(s: String): Unit = |
43767 | 282 |
if (s == "") () else throw new XML_Atom(s) |
283 |
||
284 |
||
285 |
/* structural nodes */ |
|
286 |
||
287 |
private def node(t: XML.Tree): XML.Body = |
|
288 |
t match { |
|
289 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
290 |
case _ => throw new XML_Body(List(t)) |
|
291 |
} |
|
292 |
||
43781 | 293 |
private def vector(atts: XML.Attributes): List[String] = |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
294 |
{ |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
295 |
val xs = new mutable.ListBuffer[String] |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
296 |
var i = 0 |
43781 | 297 |
for ((a, x) <- atts) { |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
298 |
if (int_atom(a) == i) { xs += x; i = i + 1 } |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
299 |
else throw new XML_Atom(a) |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
300 |
} |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
301 |
xs.toList |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
302 |
} |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
303 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
304 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 305 |
t match { |
43781 | 306 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 307 |
case _ => throw new XML_Body(List(t)) |
308 |
} |
|
309 |
||
310 |
||
311 |
/* representation of standard types */ |
|
312 |
||
43780 | 313 |
val properties: T[Properties.T] = |
43767 | 314 |
{ |
315 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
316 |
case ts => throw new XML_Body(ts) |
|
317 |
} |
|
318 |
||
319 |
val string: T[String] = |
|
320 |
{ |
|
321 |
case Nil => "" |
|
322 |
case List(XML.Text(s)) => s |
|
323 |
case ts => throw new XML_Body(ts) |
|
324 |
} |
|
325 |
||
326 |
val long: T[Long] = (x => long_atom(string(x))) |
|
327 |
||
328 |
val int: T[Int] = (x => int_atom(string(x))) |
|
329 |
||
330 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
331 |
||
332 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
333 |
||
334 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
335 |
{ |
|
336 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
337 |
case ts => throw new XML_Body(ts) |
|
338 |
} |
|
339 |
||
340 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
341 |
{ |
|
342 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
343 |
case ts => throw new XML_Body(ts) |
|
344 |
} |
|
345 |
||
346 |
def list[A](f: T[A]): T[List[A]] = |
|
347 |
(ts => ts.map(t => f(node(t)))) |
|
348 |
||
349 |
def option[A](f: T[A]): T[Option[A]] = |
|
350 |
{ |
|
351 |
case Nil => None |
|
352 |
case List(t) => Some(f(node(t))) |
|
353 |
case ts => throw new XML_Body(ts) |
|
354 |
} |
|
355 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
356 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 357 |
{ |
358 |
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
|
359 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 360 |
val f = |
361 |
try { fs(tag) } |
|
362 |
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
|
363 |
f(xs, ts) |
43767 | 364 |
case ts => throw new XML_Body(ts) |
365 |
} |
|
366 |
} |
|
27931 | 367 |
} |