# HG changeset patch # User wenzelm # Date 1087582071 -7200 # Node ID 3d9126cbf0e6c5fd54a4430ebedc88a6912afb6a # Parent 9db3d2be8cdf053c50cb258003195417aa010216 scalable string_of_tree; tuned; diff -r 9db3d2be8cdf -r 3d9126cbf0e6 src/Pure/General/xml.ML --- a/src/Pure/General/xml.ML Fri Jun 18 20:07:42 2004 +0200 +++ b/src/Pure/General/xml.ML Fri Jun 18 20:07:51 2004 +0200 @@ -3,29 +3,41 @@ Author: David Aspinall, Stefan Berghofer and Markus Wenzel License: GPL (GNU GENERAL PUBLIC LICENSE) -Basic support for XML input and output. +Basic support for XML. *) signature XML = sig + val header: string + val text: string -> string + val cdata: string -> string + val element: string -> (string * string) list -> string list -> string datatype tree = Elem of string * (string * string) list * tree list | Text of string - val element: string -> (string * string) list -> string list -> string - val text: string -> string - val cdata: string -> string - val header: string val string_of_tree: tree -> string - val tree_of_string: string -> tree val parse_content: string list -> tree list * string list val parse_elem: string list -> tree * string list val parse_document: string list -> (string option * tree) * string list + val tree_of_string: string -> tree end; structure XML: XML = struct -(* character data *) +(** string based representation (small scale) **) + +val header = "\n"; + + +(* text and character data *) + +fun decode "<" = "<" + | decode ">" = ">" + | decode "&" = "&" + | decode "'" = "'" + | decode """ = "\"" + | decode c = c; fun encode "<" = "<" | encode ">" = ">" @@ -34,27 +46,13 @@ | encode "\"" = """ | encode c = c; -fun decode "<" = "<" - | decode ">" = ">" - | decode "&" = "&" - | decode "'" = "'" - | decode """ = "\"" - | decode c = c; - val text = Library.translate_string encode; -val cdata_open = "" - -fun cdata s = cdata_open ^ s ^ cdata_close; +val cdata = enclose ""; (* elements *) -datatype tree = - Elem of string * (string * string) list * tree list - | Text of string; - fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\""; fun element name atts cs = @@ -63,14 +61,34 @@ else enclose "<" ">" elem ^ implode cs ^ enclose "" name end; -fun string_of_tree (Elem (name, atts, ts)) = - element name atts (map string_of_tree ts) - | string_of_tree (Text s) = s; - -val header = "\n"; -(* parser *) +(** explicit XML trees **) + +datatype tree = + Elem of string * (string * string) list * tree list + | Text of string; + +fun string_of_tree tree = + let + fun string_of (Elem (name, atts, ts)) buf = + let val buf' = + buf |> Buffer.add "<" + |> fold Buffer.add (separate " " (name :: map attribute atts)) + in + if null ts then + buf' |> Buffer.add "/>" + else + buf' |> Buffer.add ">" + |> fold string_of ts + |> Buffer.add " Buffer.add name |> Buffer.add ">" + end + | string_of (Text s) buf = Buffer.add (text s) buf; + in Buffer.content (string_of tree Buffer.empty) end; + + + +(** XML parsing **) fun err s (xs, _) = "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);