author | wenzelm |
Mon, 24 Oct 2016 11:10:17 +0200 | |
changeset 64366 | e0ab4c0a5a93 |
parent 64358 | 15c90b744481 |
child 64370 | 865b39487b5d |
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 |
||
55618 | 10 |
|
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 |
|
57912 | 24 |
sealed abstract class Tree { override def toString: String = string_of_tree(this) } |
64354 | 25 |
type Body = List[Tree] |
26 |
case class Elem(markup: Markup, body: Body) extends Tree |
|
52890 | 27 |
{ |
28 |
def name: String = markup.name |
|
64358 | 29 |
def update_attributes(more_attributes: Attributes): Elem = |
30 |
if (more_attributes.isEmpty) this |
|
31 |
else Elem(markup.update_properties(more_attributes), body) |
|
52890 | 32 |
} |
29204 | 33 |
case class Text(content: String) extends Tree |
29203 | 34 |
|
64354 | 35 |
def elem(name: String, body: Body): XML.Elem = XML.Elem(Markup(name, Nil), body) |
36 |
def elem(name: String): XML.Elem = XML.Elem(Markup(name, Nil), Nil) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
37 |
|
29203 | 38 |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
39 |
/* wrapped elements */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
40 |
|
60215 | 41 |
val XML_ELEM = "xml_elem" |
42 |
val XML_NAME = "xml_name" |
|
43 |
val XML_BODY = "xml_body" |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
44 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
45 |
object Wrapped_Elem |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
46 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
47 |
def apply(markup: Markup, body1: Body, body2: Body): XML.Elem = |
61026 | 48 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, markup.name) :: markup.properties), |
49 |
XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
50 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
51 |
def unapply(tree: Tree): Option[(Markup, Body, Body)] = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
52 |
tree match { |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
53 |
case |
61026 | 54 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props), |
55 |
XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) => |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
56 |
Some(Markup(name, props), body1, body2) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
57 |
case _ => None |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
58 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
59 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
60 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
61 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
62 |
/* traverse text */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
63 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
64 |
def traverse_text[A](body: Body)(a: A)(op: (A, String) => A): A = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
65 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
66 |
def traverse(x: A, t: Tree): A = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
67 |
t match { |
61026 | 68 |
case XML.Wrapped_Elem(_, _, ts) => (x /: ts)(traverse) |
69 |
case XML.Elem(_, ts) => (x /: ts)(traverse) |
|
70 |
case XML.Text(s) => op(x, s) |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
71 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
72 |
(a /: body)(traverse) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
73 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
74 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
75 |
def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length } |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
76 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
77 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
78 |
/* text content */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
79 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
80 |
def content(body: Body): String = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
81 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
82 |
val text = new StringBuilder(text_length(body)) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
83 |
traverse_text(body)(()) { case (_, s) => text.append(s) } |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
84 |
text.toString |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
85 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
86 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
87 |
def content(tree: Tree): String = content(List(tree)) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
88 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
89 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
90 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
91 |
/** string representation **/ |
29203 | 92 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
93 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
94 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
95 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
96 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
97 |
def text(txt: String) { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
98 |
if (txt == null) s ++= txt |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
99 |
else { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
100 |
for (c <- txt.iterator) c match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
101 |
case '<' => s ++= "<" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
102 |
case '>' => s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
103 |
case '&' => s ++= "&" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
104 |
case '"' => s ++= """ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
105 |
case '\'' => s ++= "'" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
106 |
case _ => s += c |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
107 |
} |
34005 | 108 |
} |
29203 | 109 |
} |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
113 |
t match { |
61026 | 114 |
case XML.Elem(markup, Nil) => |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
115 |
s ++= "<"; elem(markup); s ++= "/>" |
61026 | 116 |
case XML.Elem(markup, ts) => |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
117 |
s ++= "<"; elem(markup); s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
118 |
ts.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
119 |
s ++= "</"; s ++= markup.name; s ++= ">" |
61026 | 120 |
case XML.Text(txt) => text(txt) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
121 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
122 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
123 |
s.toString |
29203 | 124 |
} |
125 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
126 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 127 |
|
128 |
||
44808 | 129 |
|
130 |
/** cache for partial sharing (weak table) **/ |
|
34108 | 131 |
|
43745 | 132 |
class Cache(initial_size: Int = 131071, max_string: Int = 100) |
34108 | 133 |
{ |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
134 |
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
|
135 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
136 |
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
|
137 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
138 |
val ref = table.get(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
139 |
if (ref == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
140 |
else { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
141 |
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
|
142 |
if (y == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
143 |
else Some(y) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
144 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
145 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
146 |
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
|
147 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
148 |
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
|
149 |
x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
150 |
} |
34108 | 151 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
152 |
private def trim_bytes(s: String): String = new String(s.toCharArray) |
38869 | 153 |
|
51663 | 154 |
private def cache_string(x: String): String = |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
155 |
if (x == "true") "true" |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
156 |
else if (x == "false") "false" |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
157 |
else if (x == "0.0") "0.0" |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
158 |
else if (Library.is_small_int(x)) Library.signed_string_of_int(Integer.parseInt(x)) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
159 |
else |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
160 |
lookup(x) match { |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
161 |
case Some(y) => y |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
162 |
case None => |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
163 |
val z = trim_bytes(x) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
164 |
if (z.length > max_string) z else store(z) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
165 |
} |
51663 | 166 |
private def cache_props(x: Properties.T): Properties.T = |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
167 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
168 |
else |
34133 | 169 |
lookup(x) match { |
170 |
case Some(y) => y |
|
51663 | 171 |
case None => store(x.map(p => (trim_bytes(p._1).intern, cache_string(p._2)))) |
34133 | 172 |
} |
51663 | 173 |
private def cache_markup(x: Markup): Markup = |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
174 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
175 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
176 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
177 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
178 |
case Markup(name, props) => |
51663 | 179 |
store(Markup(cache_string(name), cache_props(props))) |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
180 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
181 |
} |
51663 | 182 |
private def cache_tree(x: XML.Tree): XML.Tree = |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
183 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
184 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
185 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
186 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
187 |
case XML.Elem(markup, body) => |
51663 | 188 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
189 |
case XML.Text(text) => store(XML.Text(cache_string(text))) |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
190 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
191 |
} |
51663 | 192 |
private def cache_body(x: XML.Body): XML.Body = |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
193 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
194 |
else |
34133 | 195 |
lookup(x) match { |
196 |
case Some(y) => y |
|
51663 | 197 |
case None => x.map(cache_tree(_)) |
34133 | 198 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
199 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
200 |
// main methods |
51663 | 201 |
def string(x: String): String = synchronized { cache_string(x) } |
202 |
def props(x: Properties.T): Properties.T = synchronized { cache_props(x) } |
|
203 |
def markup(x: Markup): Markup = synchronized { cache_markup(x) } |
|
204 |
def tree(x: XML.Tree): XML.Tree = synchronized { cache_tree(x) } |
|
205 |
def body(x: XML.Body): XML.Body = synchronized { cache_body(x) } |
|
206 |
def elem(x: XML.Elem): XML.Elem = synchronized { cache_tree(x).asInstanceOf[XML.Elem] } |
|
34108 | 207 |
} |
208 |
||
209 |
||
43767 | 210 |
|
211 |
/** XML as data representation language **/ |
|
212 |
||
51987 | 213 |
abstract class Error(s: String) extends Exception(s) |
214 |
class XML_Atom(s: String) extends Error(s) |
|
215 |
class XML_Body(body: XML.Body) extends Error("") |
|
43767 | 216 |
|
217 |
object Encode |
|
218 |
{ |
|
219 |
type T[A] = A => XML.Body |
|
220 |
||
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 |
/* atomic values */ |
43767 | 223 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
224 |
def long_atom(i: Long): String = Library.signed_string_of_long(i) |
43767 | 225 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
226 |
def int_atom(i: Int): String = Library.signed_string_of_int(i) |
43767 | 227 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
228 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 229 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
230 |
def unit_atom(u: Unit) = "" |
43767 | 231 |
|
232 |
||
233 |
/* structural nodes */ |
|
234 |
||
235 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
236 |
||
43781 | 237 |
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
|
238 |
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
|
239 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
240 |
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
|
241 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 242 |
|
243 |
||
244 |
/* representation of standard types */ |
|
245 |
||
43780 | 246 |
val properties: T[Properties.T] = |
43767 | 247 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
248 |
||
249 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
250 |
||
251 |
val long: T[Long] = (x => string(long_atom(x))) |
|
252 |
||
253 |
val int: T[Int] = (x => string(int_atom(x))) |
|
254 |
||
255 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
256 |
||
257 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
258 |
||
259 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
260 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
261 |
||
262 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
263 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
264 |
||
265 |
def list[A](f: T[A]): T[List[A]] = |
|
266 |
(xs => xs.map((x: A) => node(f(x)))) |
|
267 |
||
268 |
def option[A](f: T[A]): T[Option[A]] = |
|
269 |
{ |
|
270 |
case None => Nil |
|
271 |
case Some(x) => List(node(f(x))) |
|
272 |
} |
|
273 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
274 |
def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] = |
43767 | 275 |
{ |
276 |
case x => |
|
277 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
278 |
List(tagged(tag, f(x))) |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
object Decode |
|
283 |
{ |
|
284 |
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
|
285 |
type V[A] = (List[String], XML.Body) => A |
43767 | 286 |
|
287 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
288 |
/* atomic values */ |
43767 | 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 long_atom(s: String): Long = |
43767 | 291 |
try { java.lang.Long.parseLong(s) } |
292 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
293 |
||
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 |
def int_atom(s: String): Int = |
43767 | 295 |
try { Integer.parseInt(s) } |
296 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
297 |
||
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 |
def bool_atom(s: String): Boolean = |
43767 | 299 |
if (s == "1") true |
300 |
else if (s == "0") false |
|
301 |
else throw new XML_Atom(s) |
|
302 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
303 |
def unit_atom(s: String): Unit = |
43767 | 304 |
if (s == "") () else throw new XML_Atom(s) |
305 |
||
306 |
||
307 |
/* structural nodes */ |
|
308 |
||
309 |
private def node(t: XML.Tree): XML.Body = |
|
310 |
t match { |
|
311 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
312 |
case _ => throw new XML_Body(List(t)) |
|
313 |
} |
|
314 |
||
43781 | 315 |
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
|
316 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
317 |
{ 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
|
318 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
319 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 320 |
t match { |
43781 | 321 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 322 |
case _ => throw new XML_Body(List(t)) |
323 |
} |
|
324 |
||
325 |
||
326 |
/* representation of standard types */ |
|
327 |
||
43780 | 328 |
val properties: T[Properties.T] = |
43767 | 329 |
{ |
330 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
331 |
case ts => throw new XML_Body(ts) |
|
332 |
} |
|
333 |
||
334 |
val string: T[String] = |
|
335 |
{ |
|
336 |
case Nil => "" |
|
337 |
case List(XML.Text(s)) => s |
|
338 |
case ts => throw new XML_Body(ts) |
|
339 |
} |
|
340 |
||
341 |
val long: T[Long] = (x => long_atom(string(x))) |
|
342 |
||
343 |
val int: T[Int] = (x => int_atom(string(x))) |
|
344 |
||
345 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
346 |
||
347 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
348 |
||
349 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
350 |
{ |
|
351 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
352 |
case ts => throw new XML_Body(ts) |
|
353 |
} |
|
354 |
||
355 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
356 |
{ |
|
357 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
358 |
case ts => throw new XML_Body(ts) |
|
359 |
} |
|
360 |
||
361 |
def list[A](f: T[A]): T[List[A]] = |
|
362 |
(ts => ts.map(t => f(node(t)))) |
|
363 |
||
364 |
def option[A](f: T[A]): T[Option[A]] = |
|
365 |
{ |
|
366 |
case Nil => None |
|
367 |
case List(t) => Some(f(node(t))) |
|
368 |
case ts => throw new XML_Body(ts) |
|
369 |
} |
|
370 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
371 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 372 |
{ |
373 |
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
|
374 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 375 |
val f = |
376 |
try { fs(tag) } |
|
377 |
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
|
378 |
f(xs, ts) |
43767 | 379 |
case ts => throw new XML_Body(ts) |
380 |
} |
|
381 |
} |
|
27931 | 382 |
} |