author | wenzelm |
Thu, 03 Apr 2008 21:23:37 +0200 | |
changeset 26546 | ba4cdf92c7c4 |
parent 26539 | a0754be538ab |
child 26551 | da1cd11d8a25 |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: Pure/General/xml.ML |
24264 | 2 |
ID: $Id$ |
3 |
Author: David Aspinall, Stefan Berghofer and Markus Wenzel |
|
4 |
||
5 |
Basic support for XML. |
|
6 |
*) |
|
7 |
||
8 |
signature XML = |
|
9 |
sig |
|
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
10 |
type attributes = Markup.property list |
24264 | 11 |
datatype tree = |
12 |
Elem of string * attributes * tree list |
|
13 |
| Text of string |
|
14 |
| Output of output |
|
26546 | 15 |
val add_content: tree -> Buffer.T -> Buffer.T |
16 |
val detect: string -> bool |
|
17 |
val header: string |
|
18 |
val text: string -> string |
|
19 |
val element: string -> attributes -> string list -> string |
|
20 |
val output_markup: Markup.T -> output * output |
|
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
21 |
val string_of: tree -> string |
26546 | 22 |
val output: tree -> TextIO.outstream -> unit |
24264 | 23 |
val parse_string : string -> string option |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
24 |
val parse_comment_whspc: string list -> unit * string list |
26546 | 25 |
val parse_element: string list -> tree * string list |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
26 |
val parse: string -> tree |
24264 | 27 |
end; |
28 |
||
29 |
structure XML: XML = |
|
30 |
struct |
|
31 |
||
26546 | 32 |
(** XML trees **) |
33 |
||
34 |
type attributes = Markup.property list; |
|
35 |
||
36 |
datatype tree = |
|
37 |
Elem of string * attributes * tree list |
|
38 |
| Text of string |
|
39 |
| Output of output; (* FIXME !? *) |
|
40 |
||
41 |
fun add_content (Elem (_, _, ts)) = fold add_content ts |
|
42 |
| add_content (Text s) = Buffer.add s |
|
43 |
| add_content (Output _) = I; (* FIXME !? *) |
|
44 |
||
45 |
||
24264 | 46 |
|
26525 | 47 |
(** string representation **) |
48 |
||
49 |
val detect = String.isPrefix "<?xml"; |
|
24264 | 50 |
val header = "<?xml version=\"1.0\"?>\n"; |
51 |
||
52 |
||
26546 | 53 |
(* escaped text *) |
24264 | 54 |
|
55 |
fun decode "<" = "<" |
|
56 |
| decode ">" = ">" |
|
57 |
| decode "&" = "&" |
|
58 |
| decode "'" = "'" |
|
59 |
| decode """ = "\"" |
|
60 |
| decode c = c; |
|
61 |
||
62 |
fun encode "<" = "<" |
|
63 |
| encode ">" = ">" |
|
64 |
| encode "&" = "&" |
|
65 |
| encode "'" = "'" |
|
66 |
| encode "\"" = """ |
|
67 |
| encode c = c; |
|
68 |
||
25838 | 69 |
val text = translate_string encode; |
24264 | 70 |
|
71 |
||
72 |
(* elements *) |
|
73 |
||
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
74 |
fun elem name atts = |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
75 |
space_implode " " (name :: map (fn (a, x) => a ^ " = \"" ^ text x ^ "\"") atts); |
24264 | 76 |
|
26525 | 77 |
fun element name atts body = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
78 |
let val b = implode body in |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
79 |
if b = "" then enclose "<" "/>" (elem name atts) |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
80 |
else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name |
24264 | 81 |
end; |
82 |
||
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
83 |
fun output_markup (name, atts) = |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
84 |
(enclose "<" ">" (elem name atts), |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
85 |
enclose "</" ">" name); |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
86 |
|
24264 | 87 |
|
26546 | 88 |
(* output *) |
24264 | 89 |
|
26546 | 90 |
fun buffer_of tree = |
24264 | 91 |
let |
26546 | 92 |
fun traverse (Elem (name, atts, [])) = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
93 |
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>" |
26546 | 94 |
| traverse (Elem (name, atts, ts)) = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
95 |
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #> |
26546 | 96 |
fold traverse ts #> |
26525 | 97 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">" |
26546 | 98 |
| traverse (Text s) = Buffer.add (text s) |
99 |
| traverse (Output s) = Buffer.add s; |
|
100 |
in Buffer.empty |> traverse tree end; |
|
24264 | 101 |
|
26546 | 102 |
val string_of = Buffer.content o buffer_of; |
103 |
val output = Buffer.output o buffer_of; |
|
25838 | 104 |
|
24264 | 105 |
|
106 |
||
26546 | 107 |
(** XML parsing (slow) **) |
108 |
||
109 |
local |
|
24264 | 110 |
|
111 |
fun err s (xs, _) = |
|
112 |
"XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs); |
|
113 |
||
114 |
val scan_whspc = Scan.many Symbol.is_blank; |
|
115 |
||
116 |
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode; |
|
117 |
||
118 |
val parse_chars = Scan.repeat1 (Scan.unless ((* scan_whspc -- *)$$ "<") |
|
119 |
(scan_special || Scan.one Symbol.is_regular)) >> implode; |
|
120 |
||
121 |
val parse_cdata = Scan.this_string "<![CDATA[" |-- |
|
122 |
(Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.is_regular)) >> |
|
123 |
implode) --| Scan.this_string "]]>"; |
|
124 |
||
125 |
val parse_att = |
|
126 |
Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc -- |
|
127 |
(($$ "\"" || $$ "'") :|-- (fn s => (Scan.repeat (Scan.unless ($$ s) |
|
128 |
(scan_special || Scan.one Symbol.is_regular)) >> implode) --| $$ s)); |
|
129 |
||
130 |
val parse_comment = Scan.this_string "<!--" -- |
|
131 |
Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.is_regular)) -- |
|
132 |
Scan.this_string "-->"; |
|
133 |
||
134 |
val parse_pi = Scan.this_string "<?" |-- |
|
135 |
Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.is_regular)) --| |
|
136 |
Scan.this_string "?>"; |
|
137 |
||
26546 | 138 |
in |
139 |
||
140 |
val parse_string = Scan.read Symbol.stopper parse_chars o explode; |
|
141 |
||
24264 | 142 |
fun parse_content xs = |
143 |
((Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] -- |
|
144 |
(Scan.repeat ((* scan_whspc |-- *) |
|
26546 | 145 |
( parse_element >> single |
24264 | 146 |
|| parse_cdata >> (single o Text) |
147 |
|| parse_pi >> K [] |
|
148 |
|| parse_comment >> K []) -- |
|
149 |
Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] |
|
150 |
>> op @) >> flat) >> op @)(* --| scan_whspc*)) xs |
|
151 |
||
26546 | 152 |
and parse_element xs = |
24264 | 153 |
($$ "<" |-- Symbol.scan_id -- |
154 |
Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) => |
|
155 |
!! (err "Expected > or />") |
|
156 |
(Scan.this_string "/>" >> K [] |
|
157 |
|| $$ ">" |-- parse_content --| |
|
158 |
!! (err ("Expected </" ^ s ^ ">")) |
|
159 |
(Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >> |
|
160 |
(fn ((s, atts), ts) => Elem (s, atts, ts))) xs; |
|
161 |
||
26546 | 162 |
val parse_comment_whspc = |
163 |
(scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K()))); |
|
24264 | 164 |
|
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
165 |
fun parse s = |
24264 | 166 |
(case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element") |
26546 | 167 |
(scan_whspc |-- parse_element --| scan_whspc))) (Symbol.explode s) of |
24264 | 168 |
(x, []) => x |
169 |
| (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys)); |
|
170 |
||
171 |
end; |
|
26546 | 172 |
|
173 |
end; |