author | wenzelm |
Mon, 08 Nov 2021 12:45:35 +0100 | |
changeset 74731 | 161e84e6b40a |
parent 74683 | c8327efc7af1 |
child 74785 | 4671d29feb00 |
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 |
||
55618 | 9 |
|
29203 | 10 |
object XML |
11 |
{ |
|
43767 | 12 |
/** XML trees **/ |
13 |
||
27947 | 14 |
/* datatype representation */ |
15 |
||
65753 | 16 |
type Attribute = Properties.Entry |
43780 | 17 |
type Attributes = Properties.T |
27931 | 18 |
|
57912 | 19 |
sealed abstract class Tree { override def toString: String = string_of_tree(this) } |
64354 | 20 |
type Body = List[Tree] |
21 |
case class Elem(markup: Markup, body: Body) extends Tree |
|
52890 | 22 |
{ |
73032 | 23 |
private lazy val hash: Int = (markup, body).hashCode() |
24 |
override def hashCode(): Int = hash |
|
25 |
||
52890 | 26 |
def name: String = markup.name |
65753 | 27 |
|
64358 | 28 |
def update_attributes(more_attributes: Attributes): Elem = |
29 |
if (more_attributes.isEmpty) this |
|
30 |
else Elem(markup.update_properties(more_attributes), body) |
|
65753 | 31 |
|
65772 | 32 |
def + (att: Attribute): Elem = Elem(markup + att, body) |
52890 | 33 |
} |
29204 | 34 |
case class Text(content: String) extends Tree |
73032 | 35 |
{ |
36 |
private lazy val hash: Int = content.hashCode() |
|
37 |
override def hashCode(): Int = hash |
|
38 |
} |
|
29203 | 39 |
|
66196 | 40 |
def elem(markup: Markup): XML.Elem = XML.Elem(markup, Nil) |
64354 | 41 |
def elem(name: String, body: Body): XML.Elem = XML.Elem(Markup(name, Nil), body) |
42 |
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
|
43 |
|
73028 | 44 |
val no_text: Text = Text("") |
69867 | 45 |
val newline: Text = Text("\n") |
46 |
||
29203 | 47 |
|
69805 | 48 |
/* name space */ |
49 |
||
50 |
object Namespace |
|
51 |
{ |
|
52 |
def apply(prefix: String, target: String): Namespace = |
|
53 |
new Namespace(prefix, target) |
|
54 |
} |
|
55 |
||
56 |
final class Namespace private(prefix: String, target: String) |
|
57 |
{ |
|
58 |
def apply(name: String): String = prefix + ":" + name |
|
59 |
val attribute: XML.Attribute = ("xmlns:" + prefix, target) |
|
60 |
||
61 |
override def toString: String = attribute.toString |
|
62 |
} |
|
63 |
||
64 |
||
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
|
65 |
/* 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
|
66 |
|
60215 | 67 |
val XML_ELEM = "xml_elem" |
68 |
val XML_NAME = "xml_name" |
|
69 |
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
|
70 |
|
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 |
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
|
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 apply(markup: Markup, body1: Body, body2: Body): XML.Elem = |
61026 | 74 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, markup.name) :: markup.properties), |
75 |
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
|
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 |
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
|
78 |
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
|
79 |
case |
61026 | 80 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props), |
81 |
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
|
82 |
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
|
83 |
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
|
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 |
} |
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 |
|
67818
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
87 |
object Root_Elem |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
88 |
{ |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
89 |
def apply(body: Body): XML.Elem = XML.Elem(Markup(XML_ELEM, Nil), body) |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
90 |
def unapply(tree: Tree): Option[Body] = |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
91 |
tree match { |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
92 |
case XML.Elem(Markup(XML_ELEM, Nil), body) => Some(body) |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
93 |
case _ => None |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
94 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
95 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
96 |
|
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
|
97 |
|
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
|
98 |
/* 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
|
99 |
|
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
|
100 |
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
|
101 |
{ |
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
|
102 |
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
|
103 |
t match { |
73359 | 104 |
case XML.Wrapped_Elem(_, _, ts) => ts.foldLeft(x)(traverse) |
105 |
case XML.Elem(_, ts) => ts.foldLeft(x)(traverse) |
|
61026 | 106 |
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
|
107 |
} |
73359 | 108 |
body.foldLeft(a)(traverse) |
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
|
109 |
} |
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
|
110 |
|
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
|
111 |
def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length } |
74683
c8327efc7af1
clarified signature: more direct XML.symbol_length;
wenzelm
parents:
73528
diff
changeset
|
112 |
def symbol_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + Symbol.length(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
|
113 |
|
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
|
114 |
|
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
|
115 |
/* 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
|
116 |
|
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
|
117 |
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
|
118 |
{ |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
} |
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
|
123 |
|
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
|
124 |
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
|
125 |
|
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
|
126 |
|
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
|
127 |
|
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
|
128 |
/** string representation **/ |
29203 | 129 |
|
69804 | 130 |
val header: String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
131 |
||
73340 | 132 |
def output_char(s: StringBuilder, c: Char, permissive: Boolean = false): Unit = |
65990 | 133 |
{ |
134 |
c match { |
|
135 |
case '<' => s ++= "<" |
|
136 |
case '>' => s ++= ">" |
|
137 |
case '&' => s ++= "&" |
|
73203 | 138 |
case '"' if !permissive => s ++= """ |
139 |
case '\'' if !permissive => s ++= "'" |
|
65990 | 140 |
case _ => s += c |
141 |
} |
|
142 |
} |
|
143 |
||
73340 | 144 |
def output_string(s: StringBuilder, str: String, permissive: Boolean = false): Unit = |
65990 | 145 |
{ |
146 |
if (str == null) s ++= str |
|
73203 | 147 |
else str.iterator.foreach(output_char(s, _, permissive = permissive)) |
65990 | 148 |
} |
149 |
||
73340 | 150 |
def output_elem(s: StringBuilder, markup: Markup, end: Boolean = false): Unit = |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
151 |
{ |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
152 |
s += '<' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
153 |
s ++= markup.name |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
154 |
for ((a, b) <- markup.properties) { |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
155 |
s += ' ' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
156 |
s ++= a |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
157 |
s += '=' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
158 |
s += '"' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
159 |
output_string(s, b) |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
160 |
s += '"' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
161 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
162 |
if (end) s += '/' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
163 |
s += '>' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
164 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
165 |
|
73340 | 166 |
def output_elem_end(s: StringBuilder, name: String): Unit = |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
167 |
{ |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
168 |
s += '<' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
169 |
s += '/' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
170 |
s ++= name |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
171 |
s += '>' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
172 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
173 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
174 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
175 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
176 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
177 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
178 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
179 |
t match { |
61026 | 180 |
case XML.Elem(markup, Nil) => |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
181 |
output_elem(s, markup, end = true) |
61026 | 182 |
case XML.Elem(markup, ts) => |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
183 |
output_elem(s, markup) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
184 |
ts.foreach(tree) |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
185 |
output_elem_end(s, markup.name) |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
186 |
case XML.Text(txt) => output_string(s, txt) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
187 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
188 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
189 |
s.toString |
29203 | 190 |
} |
191 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
192 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 193 |
|
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73359
diff
changeset
|
194 |
def text(s: String): String = string_of_tree(XML.Text(s)) |
27941 | 195 |
|
44808 | 196 |
|
68265 | 197 |
/** cache **/ |
34108 | 198 |
|
73024 | 199 |
object Cache |
200 |
{ |
|
201 |
def make( |
|
74731
161e84e6b40a
just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents:
74683
diff
changeset
|
202 |
xz: XZ.Cache = XZ.Cache.make(), |
161e84e6b40a
just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents:
74683
diff
changeset
|
203 |
max_string: Int = isabelle.Cache.default_max_string, |
73024 | 204 |
initial_size: Int = isabelle.Cache.default_initial_size): Cache = |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
205 |
new Cache(xz, max_string, initial_size) |
68169 | 206 |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
207 |
val none: Cache = make(XZ.Cache.none, max_string = 0) |
73024 | 208 |
} |
209 |
||
74731
161e84e6b40a
just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents:
74683
diff
changeset
|
210 |
class Cache(val xz: XZ.Cache, max_string: Int, initial_size: Int) |
73024 | 211 |
extends isabelle.Cache(max_string, initial_size) |
34108 | 212 |
{ |
68265 | 213 |
protected 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
|
214 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
215 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
216 |
else |
34133 | 217 |
lookup(x) match { |
218 |
case Some(y) => y |
|
65903 | 219 |
case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2)))) |
34133 | 220 |
} |
68265 | 221 |
} |
222 |
||
223 |
protected def cache_markup(x: Markup): Markup = |
|
224 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
225 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
226 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
227 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
228 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
229 |
case Markup(name, props) => |
51663 | 230 |
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
|
231 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
232 |
} |
68265 | 233 |
} |
234 |
||
235 |
protected def cache_tree(x: XML.Tree): XML.Tree = |
|
236 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
237 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
238 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
239 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
240 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
241 |
case XML.Elem(markup, body) => |
51663 | 242 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
243 |
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
|
244 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
245 |
} |
68265 | 246 |
} |
247 |
||
248 |
protected def cache_body(x: XML.Body): XML.Body = |
|
249 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
250 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
251 |
else |
34133 | 252 |
lookup(x) match { |
253 |
case Some(y) => y |
|
71601 | 254 |
case None => x.map(cache_tree) |
34133 | 255 |
} |
68265 | 256 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
257 |
|
73030 | 258 |
// support hash-consing |
259 |
def tree0(x: XML.Tree): XML.Tree = |
|
260 |
if (no_cache) x else synchronized { lookup(x) getOrElse store(x) } |
|
261 |
||
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
262 |
// main methods |
73024 | 263 |
def props(x: Properties.T): Properties.T = |
264 |
if (no_cache) x else synchronized { cache_props(x) } |
|
265 |
def markup(x: Markup): Markup = |
|
266 |
if (no_cache) x else synchronized { cache_markup(x) } |
|
267 |
def tree(x: XML.Tree): XML.Tree = |
|
268 |
if (no_cache) x else synchronized { cache_tree(x) } |
|
269 |
def body(x: XML.Body): XML.Body = |
|
270 |
if (no_cache) x else synchronized { cache_body(x) } |
|
271 |
def elem(x: XML.Elem): XML.Elem = |
|
272 |
if (no_cache) x else synchronized { cache_tree(x).asInstanceOf[XML.Elem] } |
|
34108 | 273 |
} |
274 |
||
275 |
||
43767 | 276 |
|
277 |
/** XML as data representation language **/ |
|
278 |
||
51987 | 279 |
abstract class Error(s: String) extends Exception(s) |
280 |
class XML_Atom(s: String) extends Error(s) |
|
281 |
class XML_Body(body: XML.Body) extends Error("") |
|
43767 | 282 |
|
283 |
object Encode |
|
284 |
{ |
|
285 |
type T[A] = A => XML.Body |
|
65334 | 286 |
type V[A] = PartialFunction[A, (List[String], XML.Body)] |
70828 | 287 |
type P[A] = PartialFunction[A, List[String]] |
43767 | 288 |
|
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 |
/* atomic values */ |
43767 | 291 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
292 |
def long_atom(i: Long): String = Library.signed_string_of_long(i) |
43767 | 293 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
294 |
def int_atom(i: Int): String = Library.signed_string_of_int(i) |
43767 | 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(b: Boolean): String = if (b) "1" else "0" |
43767 | 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 unit_atom(u: Unit) = "" |
43767 | 299 |
|
300 |
||
301 |
/* structural nodes */ |
|
302 |
||
303 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
304 |
||
43781 | 305 |
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
|
306 |
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
|
307 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
308 |
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
|
309 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 310 |
|
311 |
||
312 |
/* representation of standard types */ |
|
313 |
||
65333 | 314 |
val tree: T[XML.Tree] = (t => List(t)) |
315 |
||
43780 | 316 |
val properties: T[Properties.T] = |
43767 | 317 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
318 |
||
319 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
320 |
||
321 |
val long: T[Long] = (x => string(long_atom(x))) |
|
322 |
||
323 |
val int: T[Int] = (x => string(int_atom(x))) |
|
324 |
||
325 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
326 |
||
327 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
328 |
||
329 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
330 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
331 |
||
332 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
333 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
334 |
||
335 |
def list[A](f: T[A]): T[List[A]] = |
|
336 |
(xs => xs.map((x: A) => node(f(x)))) |
|
337 |
||
338 |
def option[A](f: T[A]): T[Option[A]] = |
|
339 |
{ |
|
340 |
case None => Nil |
|
341 |
case Some(x) => List(node(f(x))) |
|
342 |
} |
|
343 |
||
65334 | 344 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 345 |
{ |
346 |
case x => |
|
347 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
348 |
List(tagged(tag, f(x))) |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
object Decode |
|
353 |
{ |
|
354 |
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
|
355 |
type V[A] = (List[String], XML.Body) => A |
70828 | 356 |
type P[A] = PartialFunction[List[String], A] |
43767 | 357 |
|
358 |
||
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 |
/* atomic values */ |
43767 | 360 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
361 |
def long_atom(s: String): Long = |
43767 | 362 |
try { java.lang.Long.parseLong(s) } |
363 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
364 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
365 |
def int_atom(s: String): Int = |
43767 | 366 |
try { Integer.parseInt(s) } |
367 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
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 bool_atom(s: String): Boolean = |
43767 | 370 |
if (s == "1") true |
371 |
else if (s == "0") false |
|
372 |
else throw new XML_Atom(s) |
|
373 |
||
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 |
def unit_atom(s: String): Unit = |
43767 | 375 |
if (s == "") () else throw new XML_Atom(s) |
376 |
||
377 |
||
378 |
/* structural nodes */ |
|
379 |
||
380 |
private def node(t: XML.Tree): XML.Body = |
|
381 |
t match { |
|
382 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
383 |
case _ => throw new XML_Body(List(t)) |
|
384 |
} |
|
385 |
||
43781 | 386 |
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
|
387 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
388 |
{ 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
|
389 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
390 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 391 |
t match { |
43781 | 392 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 393 |
case _ => throw new XML_Body(List(t)) |
394 |
} |
|
395 |
||
396 |
||
397 |
/* representation of standard types */ |
|
398 |
||
65333 | 399 |
val tree: T[XML.Tree] = |
400 |
{ |
|
401 |
case List(t) => t |
|
402 |
case ts => throw new XML_Body(ts) |
|
403 |
} |
|
404 |
||
43780 | 405 |
val properties: T[Properties.T] = |
43767 | 406 |
{ |
407 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
408 |
case ts => throw new XML_Body(ts) |
|
409 |
} |
|
410 |
||
411 |
val string: T[String] = |
|
412 |
{ |
|
413 |
case Nil => "" |
|
414 |
case List(XML.Text(s)) => s |
|
415 |
case ts => throw new XML_Body(ts) |
|
416 |
} |
|
417 |
||
418 |
val long: T[Long] = (x => long_atom(string(x))) |
|
419 |
||
420 |
val int: T[Int] = (x => int_atom(string(x))) |
|
421 |
||
422 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
423 |
||
424 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
425 |
||
426 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
427 |
{ |
|
428 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
429 |
case ts => throw new XML_Body(ts) |
|
430 |
} |
|
431 |
||
432 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
433 |
{ |
|
434 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
435 |
case ts => throw new XML_Body(ts) |
|
436 |
} |
|
437 |
||
438 |
def list[A](f: T[A]): T[List[A]] = |
|
439 |
(ts => ts.map(t => f(node(t)))) |
|
440 |
||
441 |
def option[A](f: T[A]): T[Option[A]] = |
|
442 |
{ |
|
443 |
case Nil => None |
|
444 |
case List(t) => Some(f(node(t))) |
|
445 |
case ts => throw new XML_Body(ts) |
|
446 |
} |
|
447 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
448 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 449 |
{ |
450 |
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
|
451 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 452 |
val f = |
453 |
try { fs(tag) } |
|
454 |
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
|
455 |
f(xs, ts) |
43767 | 456 |
case ts => throw new XML_Body(ts) |
457 |
} |
|
458 |
} |
|
27931 | 459 |
} |