author | wenzelm |
Sat, 30 Jan 2021 17:06:13 +0100 | |
changeset 73202 | 8a17c7bf530a |
parent 73032 | 72b13af7f266 |
child 73203 | 9c10b4fa17b5 |
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 { |
61026 | 104 |
case XML.Wrapped_Elem(_, _, ts) => (x /: ts)(traverse) |
105 |
case XML.Elem(_, ts) => (x /: ts)(traverse) |
|
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 |
} |
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
|
108 |
(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
|
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 } |
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
|
112 |
|
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 |
/* 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
|
115 |
|
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 |
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
|
117 |
{ |
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 |
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
|
119 |
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
|
120 |
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
|
121 |
} |
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 |
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
|
124 |
|
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 |
/** string representation **/ |
29203 | 128 |
|
69804 | 129 |
val header: String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
130 |
||
73202 | 131 |
def output_char(s: StringBuilder, c: Char) |
65990 | 132 |
{ |
133 |
c match { |
|
134 |
case '<' => s ++= "<" |
|
135 |
case '>' => s ++= ">" |
|
136 |
case '&' => s ++= "&" |
|
137 |
case '"' => s ++= """ |
|
138 |
case '\'' => s ++= "'" |
|
139 |
case _ => s += c |
|
140 |
} |
|
141 |
} |
|
142 |
||
73202 | 143 |
def output_string(s: StringBuilder, str: String) |
65990 | 144 |
{ |
145 |
if (str == null) s ++= str |
|
73202 | 146 |
else str.iterator.foreach(c => output_char(s, c)) |
65990 | 147 |
} |
148 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
149 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
150 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
151 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
152 |
|
73202 | 153 |
def text(txt: String) { output_string(s, txt) } |
65991 | 154 |
def elem(markup: Markup) |
155 |
{ |
|
156 |
s ++= markup.name |
|
157 |
for ((a, b) <- markup.properties) { |
|
158 |
s += ' '; s ++= a; s += '='; s += '"'; text(b); s += '"' |
|
159 |
} |
|
160 |
} |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
161 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
162 |
t match { |
61026 | 163 |
case XML.Elem(markup, Nil) => |
65991 | 164 |
s += '<'; elem(markup); s ++= "/>" |
61026 | 165 |
case XML.Elem(markup, ts) => |
65991 | 166 |
s += '<'; elem(markup); s += '>' |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
167 |
ts.foreach(tree) |
65991 | 168 |
s ++= "</"; s ++= markup.name; s += '>' |
61026 | 169 |
case XML.Text(txt) => text(txt) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
170 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
171 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
172 |
s.toString |
29203 | 173 |
} |
174 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
175 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 176 |
|
177 |
||
44808 | 178 |
|
68265 | 179 |
/** cache **/ |
34108 | 180 |
|
73024 | 181 |
object Cache |
182 |
{ |
|
183 |
def make( |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
184 |
xz: XZ.Cache = XZ.Cache.make(), |
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
185 |
max_string: Int = isabelle.Cache.default_max_string, |
73024 | 186 |
initial_size: Int = isabelle.Cache.default_initial_size): Cache = |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
187 |
new Cache(xz, max_string, initial_size) |
68169 | 188 |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
189 |
val none: Cache = make(XZ.Cache.none, max_string = 0) |
73024 | 190 |
} |
191 |
||
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
192 |
class Cache private[XML](val xz: XZ.Cache, max_string: Int, initial_size: Int) |
73024 | 193 |
extends isabelle.Cache(max_string, initial_size) |
34108 | 194 |
{ |
68265 | 195 |
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
|
196 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
197 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
198 |
else |
34133 | 199 |
lookup(x) match { |
200 |
case Some(y) => y |
|
65903 | 201 |
case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2)))) |
34133 | 202 |
} |
68265 | 203 |
} |
204 |
||
205 |
protected def cache_markup(x: Markup): Markup = |
|
206 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
207 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
208 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
209 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
210 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
211 |
case Markup(name, props) => |
51663 | 212 |
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
|
213 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
214 |
} |
68265 | 215 |
} |
216 |
||
217 |
protected def cache_tree(x: XML.Tree): XML.Tree = |
|
218 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
219 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
220 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
221 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
222 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
223 |
case XML.Elem(markup, body) => |
51663 | 224 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
225 |
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
|
226 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
227 |
} |
68265 | 228 |
} |
229 |
||
230 |
protected def cache_body(x: XML.Body): XML.Body = |
|
231 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
232 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
233 |
else |
34133 | 234 |
lookup(x) match { |
235 |
case Some(y) => y |
|
71601 | 236 |
case None => x.map(cache_tree) |
34133 | 237 |
} |
68265 | 238 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
239 |
|
73030 | 240 |
// support hash-consing |
241 |
def tree0(x: XML.Tree): XML.Tree = |
|
242 |
if (no_cache) x else synchronized { lookup(x) getOrElse store(x) } |
|
243 |
||
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
244 |
// main methods |
73024 | 245 |
def props(x: Properties.T): Properties.T = |
246 |
if (no_cache) x else synchronized { cache_props(x) } |
|
247 |
def markup(x: Markup): Markup = |
|
248 |
if (no_cache) x else synchronized { cache_markup(x) } |
|
249 |
def tree(x: XML.Tree): XML.Tree = |
|
250 |
if (no_cache) x else synchronized { cache_tree(x) } |
|
251 |
def body(x: XML.Body): XML.Body = |
|
252 |
if (no_cache) x else synchronized { cache_body(x) } |
|
253 |
def elem(x: XML.Elem): XML.Elem = |
|
254 |
if (no_cache) x else synchronized { cache_tree(x).asInstanceOf[XML.Elem] } |
|
34108 | 255 |
} |
256 |
||
257 |
||
43767 | 258 |
|
259 |
/** XML as data representation language **/ |
|
260 |
||
51987 | 261 |
abstract class Error(s: String) extends Exception(s) |
262 |
class XML_Atom(s: String) extends Error(s) |
|
263 |
class XML_Body(body: XML.Body) extends Error("") |
|
43767 | 264 |
|
265 |
object Encode |
|
266 |
{ |
|
267 |
type T[A] = A => XML.Body |
|
65334 | 268 |
type V[A] = PartialFunction[A, (List[String], XML.Body)] |
70828 | 269 |
type P[A] = PartialFunction[A, List[String]] |
43767 | 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 |
/* atomic values */ |
43767 | 273 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
274 |
def long_atom(i: Long): String = Library.signed_string_of_long(i) |
43767 | 275 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
276 |
def int_atom(i: Int): String = Library.signed_string_of_int(i) |
43767 | 277 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
278 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 279 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
280 |
def unit_atom(u: Unit) = "" |
43767 | 281 |
|
282 |
||
283 |
/* structural nodes */ |
|
284 |
||
285 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
286 |
||
43781 | 287 |
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
|
288 |
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
|
289 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
290 |
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
|
291 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 292 |
|
293 |
||
294 |
/* representation of standard types */ |
|
295 |
||
65333 | 296 |
val tree: T[XML.Tree] = (t => List(t)) |
297 |
||
43780 | 298 |
val properties: T[Properties.T] = |
43767 | 299 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
300 |
||
301 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
302 |
||
303 |
val long: T[Long] = (x => string(long_atom(x))) |
|
304 |
||
305 |
val int: T[Int] = (x => string(int_atom(x))) |
|
306 |
||
307 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
308 |
||
309 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
310 |
||
311 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
312 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
313 |
||
314 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
315 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
316 |
||
317 |
def list[A](f: T[A]): T[List[A]] = |
|
318 |
(xs => xs.map((x: A) => node(f(x)))) |
|
319 |
||
320 |
def option[A](f: T[A]): T[Option[A]] = |
|
321 |
{ |
|
322 |
case None => Nil |
|
323 |
case Some(x) => List(node(f(x))) |
|
324 |
} |
|
325 |
||
65334 | 326 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 327 |
{ |
328 |
case x => |
|
329 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
330 |
List(tagged(tag, f(x))) |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
object Decode |
|
335 |
{ |
|
336 |
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
|
337 |
type V[A] = (List[String], XML.Body) => A |
70828 | 338 |
type P[A] = PartialFunction[List[String], A] |
43767 | 339 |
|
340 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
341 |
/* atomic values */ |
43767 | 342 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
343 |
def long_atom(s: String): Long = |
43767 | 344 |
try { java.lang.Long.parseLong(s) } |
345 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
346 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
347 |
def int_atom(s: String): Int = |
43767 | 348 |
try { Integer.parseInt(s) } |
349 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
350 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
351 |
def bool_atom(s: String): Boolean = |
43767 | 352 |
if (s == "1") true |
353 |
else if (s == "0") false |
|
354 |
else throw new XML_Atom(s) |
|
355 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
356 |
def unit_atom(s: String): Unit = |
43767 | 357 |
if (s == "") () else throw new XML_Atom(s) |
358 |
||
359 |
||
360 |
/* structural nodes */ |
|
361 |
||
362 |
private def node(t: XML.Tree): XML.Body = |
|
363 |
t match { |
|
364 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
365 |
case _ => throw new XML_Body(List(t)) |
|
366 |
} |
|
367 |
||
43781 | 368 |
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
|
369 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
370 |
{ 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
|
371 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
372 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 373 |
t match { |
43781 | 374 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 375 |
case _ => throw new XML_Body(List(t)) |
376 |
} |
|
377 |
||
378 |
||
379 |
/* representation of standard types */ |
|
380 |
||
65333 | 381 |
val tree: T[XML.Tree] = |
382 |
{ |
|
383 |
case List(t) => t |
|
384 |
case ts => throw new XML_Body(ts) |
|
385 |
} |
|
386 |
||
43780 | 387 |
val properties: T[Properties.T] = |
43767 | 388 |
{ |
389 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
390 |
case ts => throw new XML_Body(ts) |
|
391 |
} |
|
392 |
||
393 |
val string: T[String] = |
|
394 |
{ |
|
395 |
case Nil => "" |
|
396 |
case List(XML.Text(s)) => s |
|
397 |
case ts => throw new XML_Body(ts) |
|
398 |
} |
|
399 |
||
400 |
val long: T[Long] = (x => long_atom(string(x))) |
|
401 |
||
402 |
val int: T[Int] = (x => int_atom(string(x))) |
|
403 |
||
404 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
405 |
||
406 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
407 |
||
408 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
409 |
{ |
|
410 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
411 |
case ts => throw new XML_Body(ts) |
|
412 |
} |
|
413 |
||
414 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
415 |
{ |
|
416 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
417 |
case ts => throw new XML_Body(ts) |
|
418 |
} |
|
419 |
||
420 |
def list[A](f: T[A]): T[List[A]] = |
|
421 |
(ts => ts.map(t => f(node(t)))) |
|
422 |
||
423 |
def option[A](f: T[A]): T[Option[A]] = |
|
424 |
{ |
|
425 |
case Nil => None |
|
426 |
case List(t) => Some(f(node(t))) |
|
427 |
case ts => throw new XML_Body(ts) |
|
428 |
} |
|
429 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
430 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 431 |
{ |
432 |
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
|
433 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 434 |
val f = |
435 |
try { fs(tag) } |
|
436 |
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
|
437 |
f(xs, ts) |
43767 | 438 |
case ts => throw new XML_Body(ts) |
439 |
} |
|
440 |
} |
|
27931 | 441 |
} |