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