| author | blanchet | 
| Fri, 22 Apr 2011 00:00:05 +0200 | |
| changeset 42451 | a75fcd103cbb | 
| parent 38869 | 7634e3f10576 | 
| child 43520 | cec9b95fa35d | 
| permissions | -rw-r--r-- | 
| 27931 | 1  | 
/* Title: Pure/General/xml.scala  | 
2  | 
Author: Makarius  | 
|
3  | 
||
| 27947 | 4  | 
Simple XML tree values.  | 
| 27931 | 5  | 
*/  | 
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
| 34108 | 9  | 
import java.util.WeakHashMap  | 
10  | 
import java.lang.ref.WeakReference  | 
|
11  | 
import javax.xml.parsers.DocumentBuilderFactory  | 
|
12  | 
||
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
13  | 
import scala.actors.Actor._  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
14  | 
|
| 27947 | 15  | 
|
| 29203 | 16  | 
object XML  | 
17  | 
{
 | 
|
| 27947 | 18  | 
/* datatype representation */  | 
19  | 
||
| 27931 | 20  | 
type Attributes = List[(String, String)]  | 
21  | 
||
| 
38268
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
22  | 
  sealed abstract class Tree { override def toString = string_of_tree(this) }
 | 
| 
38230
 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 
wenzelm 
parents: 
36817 
diff
changeset
 | 
23  | 
case class Elem(markup: Markup, body: List[Tree]) extends Tree  | 
| 29204 | 24  | 
case class Text(content: String) extends Tree  | 
| 29203 | 25  | 
|
| 
38230
 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 
wenzelm 
parents: 
36817 
diff
changeset
 | 
26  | 
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
 | 
27  | 
def elem(name: String) = Elem(Markup(name, Nil), Nil)  | 
| 33999 | 28  | 
|
| 
38267
 
e50c283dd125
type XML.Body as basic data representation language (Scala version);
 
wenzelm 
parents: 
38263 
diff
changeset
 | 
29  | 
type Body = List[Tree]  | 
| 
 
e50c283dd125
type XML.Body as basic data representation language (Scala version);
 
wenzelm 
parents: 
38263 
diff
changeset
 | 
30  | 
|
| 29203 | 31  | 
|
32  | 
/* string representation */  | 
|
33  | 
||
| 
38268
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
34  | 
def string_of_body(body: Body): String =  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
35  | 
  {
 | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
36  | 
val s = new StringBuilder  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
37  | 
|
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
38  | 
    def text(txt: String) {
 | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
39  | 
if (txt == null) s ++= txt  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
40  | 
      else {
 | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
41  | 
        for (c <- txt.iterator) c match {
 | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
42  | 
case '<' => s ++= "<"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
43  | 
case '>' => s ++= ">"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
44  | 
case '&' => s ++= "&"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
45  | 
case '"' => s ++= """  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
46  | 
case '\'' => s ++= "'"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
47  | 
case _ => s += c  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
48  | 
}  | 
| 34005 | 49  | 
}  | 
| 29203 | 50  | 
}  | 
| 
38268
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
51  | 
    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
 | 
52  | 
    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
 | 
53  | 
def tree(t: Tree): Unit =  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
54  | 
      t match {
 | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
55  | 
case Elem(markup, Nil) =>  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
56  | 
s ++= "<"; elem(markup); s ++= "/>"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
57  | 
case Elem(markup, ts) =>  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
58  | 
s ++= "<"; elem(markup); s ++= ">"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
59  | 
ts.foreach(tree)  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
60  | 
s ++= "</"; s ++= markup.name; s ++= ">"  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
61  | 
case Text(txt) => text(txt)  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
62  | 
}  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
63  | 
body.foreach(tree)  | 
| 
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
64  | 
s.toString  | 
| 29203 | 65  | 
}  | 
66  | 
||
| 
38268
 
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
 
wenzelm 
parents: 
38267 
diff
changeset
 | 
67  | 
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))  | 
| 27941 | 68  | 
|
69  | 
||
| 
38484
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
70  | 
/* text content */  | 
| 27941 | 71  | 
|
| 
38484
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
72  | 
def content_stream(tree: Tree): Stream[String] =  | 
| 
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
73  | 
    tree match {
 | 
| 
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
74  | 
case Elem(_, body) => body.toStream.flatten(content_stream(_))  | 
| 
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
75  | 
case Text(content) => Stream(content)  | 
| 27941 | 76  | 
}  | 
77  | 
||
| 
38484
 
9c1fde4e2487
tuned XML.content: Stream based iteration is supposed to be declarative *and* efficient;
 
wenzelm 
parents: 
38446 
diff
changeset
 | 
78  | 
def content(tree: Tree): Iterator[String] = content_stream(tree).iterator  | 
| 27941 | 79  | 
|
| 27947 | 80  | 
|
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
81  | 
/* pipe-lined cache for partial sharing */  | 
| 34108 | 82  | 
|
83  | 
class Cache(initial_size: Int)  | 
|
84  | 
  {
 | 
|
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
85  | 
private val cache_actor = actor  | 
| 34108 | 86  | 
    {
 | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
87  | 
val table = new WeakHashMap[Any, WeakReference[Any]](initial_size)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
88  | 
|
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
89  | 
def lookup[A](x: A): Option[A] =  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
90  | 
      {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
91  | 
val ref = table.get(x)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
92  | 
if (ref == null) None  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
93  | 
        else {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
94  | 
val y = ref.asInstanceOf[WeakReference[A]].get  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
95  | 
if (y == null) None  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
96  | 
else Some(y)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
97  | 
}  | 
| 34108 | 98  | 
}  | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
99  | 
def store[A](x: A): A =  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
100  | 
      {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
101  | 
table.put(x, new WeakReference[Any](x))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
102  | 
x  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
103  | 
}  | 
| 34108 | 104  | 
|
| 38869 | 105  | 
def trim_bytes(s: String): String = new String(s.toCharArray)  | 
106  | 
||
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
107  | 
def cache_string(x: String): String =  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
108  | 
        lookup(x) match {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
109  | 
case Some(y) => y  | 
| 38869 | 110  | 
case None => store(trim_bytes(x))  | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
111  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
112  | 
def cache_props(x: List[(String, String)]): List[(String, String)] =  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
113  | 
if (x.isEmpty) x  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
114  | 
else  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
115  | 
          lookup(x) match {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
116  | 
case Some(y) => y  | 
| 38869 | 117  | 
case None => store(x.map(p => (trim_bytes(p._1).intern, cache_string(p._2))))  | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
118  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
119  | 
def cache_markup(x: Markup): Markup =  | 
| 34133 | 120  | 
        lookup(x) match {
 | 
121  | 
case Some(y) => y  | 
|
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
122  | 
case None =>  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
123  | 
            x match {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
124  | 
case Markup(name, props) =>  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
125  | 
store(Markup(cache_string(name), cache_props(props)))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
126  | 
}  | 
| 34133 | 127  | 
}  | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
128  | 
def cache_tree(x: XML.Tree): XML.Tree =  | 
| 34133 | 129  | 
        lookup(x) match {
 | 
130  | 
case Some(y) => y  | 
|
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
131  | 
case None =>  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
132  | 
            x match {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
133  | 
case XML.Elem(markup, body) =>  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
134  | 
store(XML.Elem(cache_markup(markup), cache_body(body)))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
135  | 
case XML.Text(text) => store(XML.Text(cache_string(text)))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
136  | 
}  | 
| 34133 | 137  | 
}  | 
| 
38446
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
138  | 
def cache_body(x: XML.Body): XML.Body =  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
139  | 
if (x.isEmpty) x  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
140  | 
else  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
141  | 
          lookup(x) match {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
142  | 
case Some(y) => y  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
143  | 
case None => x.map(cache_tree(_))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
144  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
145  | 
|
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
146  | 
// main loop  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
147  | 
      loop {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
148  | 
        react {
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
149  | 
case Cache_String(x, f) => f(cache_string(x))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
150  | 
case Cache_Markup(x, f) => f(cache_markup(x))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
151  | 
case Cache_Tree(x, f) => f(cache_tree(x))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
152  | 
case Cache_Body(x, f) => f(cache_body(x))  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
153  | 
          case bad => System.err.println("XML.cache_actor: ignoring bad input " + bad)
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
154  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
155  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
156  | 
}  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
157  | 
|
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
158  | 
private case class Cache_String(x: String, f: String => Unit)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
159  | 
private case class Cache_Markup(x: Markup, f: Markup => Unit)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
160  | 
private case class Cache_Tree(x: XML.Tree, f: XML.Tree => Unit)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
161  | 
private case class Cache_Body(x: XML.Body, f: XML.Body => Unit)  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
162  | 
|
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
163  | 
// main methods  | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
164  | 
    def cache_string(x: String)(f: String => Unit) { cache_actor ! Cache_String(x, f) }
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
165  | 
    def cache_markup(x: Markup)(f: Markup => Unit) { cache_actor ! Cache_Markup(x, f) }
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
166  | 
    def cache_tree(x: XML.Tree)(f: XML.Tree => Unit) { cache_actor ! Cache_Tree(x, f) }
 | 
| 
 
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
 
wenzelm 
parents: 
38268 
diff
changeset
 | 
167  | 
    def cache_body(x: XML.Body)(f: XML.Body => Unit) { cache_actor ! Cache_Body(x, f) }
 | 
| 34108 | 168  | 
}  | 
169  | 
||
170  | 
||
| 33953 | 171  | 
/* document object model (W3C DOM) */  | 
| 
27948
 
2638b611d3ce
renamed DOM to document, add xml version and optional stylesheets;
 
wenzelm 
parents: 
27947 
diff
changeset
 | 
172  | 
|
| 
34871
 
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
 
wenzelm 
parents: 
34133 
diff
changeset
 | 
173  | 
def get_data(node: org.w3c.dom.Node): Option[XML.Tree] =  | 
| 38231 | 174  | 
    node.getUserData(Markup.Data.name) match {
 | 
| 34047 | 175  | 
case tree: XML.Tree => Some(tree)  | 
176  | 
case _ => None  | 
|
177  | 
}  | 
|
178  | 
||
| 
34871
 
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
 
wenzelm 
parents: 
34133 
diff
changeset
 | 
179  | 
def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node =  | 
| 33953 | 180  | 
  {
 | 
| 
34871
 
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
 
wenzelm 
parents: 
34133 
diff
changeset
 | 
181  | 
    def DOM(tr: Tree): org.w3c.dom.Node = tr match {
 | 
| 38231 | 182  | 
case Elem(Markup.Data, List(data, t)) =>  | 
| 34046 | 183  | 
val node = DOM(t)  | 
| 38231 | 184  | 
node.setUserData(Markup.Data.name, data, null)  | 
| 34046 | 185  | 
node  | 
| 
38230
 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 
wenzelm 
parents: 
36817 
diff
changeset
 | 
186  | 
case Elem(Markup(name, atts), ts) =>  | 
| 38231 | 187  | 
if (name == Markup.Data.name)  | 
| 34046 | 188  | 
          error("Malformed data element: " + tr.toString)
 | 
| 27947 | 189  | 
val node = doc.createElement(name)  | 
190  | 
for ((name, value) <- atts) node.setAttribute(name, value)  | 
|
| 27952 | 191  | 
for (t <- ts) node.appendChild(DOM(t))  | 
| 27947 | 192  | 
node  | 
193  | 
case Text(txt) => doc.createTextNode(txt)  | 
|
194  | 
}  | 
|
| 33953 | 195  | 
DOM(tree)  | 
196  | 
}  | 
|
| 27931 | 197  | 
}  |