src/Pure/General/xml.ML
author aspinall
Tue, 28 Sep 2004 10:44:51 +0200
changeset 15211 5f54721547a7
parent 15207 a383b0a412b0
child 15216 2fac1f11b7f6
permissions -rw-r--r--
Add text_charref to encode a string using character references
     1 (*  Title:      Pure/General/xml.ML
     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
    10   val header: string
    11   val text: string -> string
    12   val text_charref: string -> string
    13   val cdata: string -> string
    14   val element: string -> (string * string) list -> string list -> string
    15   datatype tree =
    16       Elem of string * (string * string) list * tree list
    17     | Text of string
    18   val string_of_tree: tree -> string
    19   val parse_content: string list -> tree list * string list
    20   val parse_elem: string list -> tree * string list
    21   val parse_document: string list -> (string option * tree) * string list
    22   val scan_comment_whspc : string list -> unit * string list
    23   val tree_of_string: string -> tree
    24 end;
    25 
    26 structure XML: XML =
    27 struct
    28 
    29 (** string based representation (small scale) **)
    30 
    31 val header = "<?xml version=\"1.0\"?>\n";
    32 
    33 
    34 (* text and character data *)
    35 
    36 fun decode "&lt;" = "<"
    37   | decode "&gt;" = ">"
    38   | decode "&amp;" = "&"
    39   | decode "&apos;" = "'"
    40   | decode "&quot;" = "\""
    41   | decode c = c;
    42 
    43 fun encode "<" = "&lt;"
    44   | encode ">" = "&gt;"
    45   | encode "&" = "&amp;"
    46   | encode "'" = "&apos;"
    47   | encode "\"" = "&quot;"
    48   | encode c = c;
    49 
    50 fun encode_charref c = "&#"^Int.toString (Char.ord c)^ ";"
    51 
    52 val text = Library.translate_string encode
    53 
    54 val text_charref = implode o (map encode_charref) o String.explode
    55 
    56 val cdata = enclose "<![CDATA[" "]]>\n"
    57 
    58 
    59 (* elements *)
    60 
    61 fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
    62 
    63 fun element name atts cs =
    64   let val elem = space_implode " " (name :: map attribute atts) in
    65     if null cs then enclose "<" "/>" elem
    66     else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
    67   end;
    68 
    69 
    70 
    71 
    72 (** explicit XML trees **)
    73 
    74 datatype tree =
    75     Elem of string * (string * string) list * tree list
    76   | Text of string;
    77 
    78 fun string_of_tree tree =
    79   let
    80     fun string_of (Elem (name, atts, ts)) buf =
    81         let val buf' =
    82           buf |> Buffer.add "<"
    83           |> fold Buffer.add (separate " " (name :: map attribute atts))
    84         in
    85           if null ts then
    86             buf' |> Buffer.add "/>"
    87           else
    88             buf' |> Buffer.add ">"
    89             |> fold string_of ts
    90             |> Buffer.add "</" |> Buffer.add name |> Buffer.add ">"
    91         end
    92       | string_of (Text s) buf = Buffer.add (text s) buf;
    93   in Buffer.content (string_of tree Buffer.empty) end;
    94 
    95 
    96 
    97 (** XML parsing **)
    98 
    99 fun err s (xs, _) =
   100   "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
   101 
   102 val scan_whspc = Scan.any Symbol.is_blank;
   103 
   104 val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
   105 
   106 val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<")
   107   (scan_special || Scan.one Symbol.not_eof)) >> implode;
   108 
   109 val parse_cdata = Scan.this_string "<![CDATA[" |--
   110   (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >>
   111     implode) --| Scan.this_string "]]>";
   112 
   113 val parse_att =
   114   Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
   115   (($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s)
   116     (scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd);
   117 
   118 val parse_comment = Scan.this_string "<!--" --
   119   Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) --
   120   Scan.this_string "-->";
   121 
   122 val scan_comment_whspc = 
   123     (scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K())));
   124 
   125 val parse_pi = Scan.this_string "<?" |--
   126   Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --|
   127   Scan.this_string "?>";
   128 
   129 fun parse_content xs =
   130   ((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] --
   131     (Scan.repeat (scan_whspc |--
   132        (   parse_elem >> single
   133         || parse_cdata >> (single o Text)
   134         || parse_pi >> K []
   135         || parse_comment >> K []) --
   136        Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) []
   137          >> op @) >> flat) >> op @) --| scan_whspc) xs
   138 
   139 and parse_elem xs =
   140   ($$ "<" |-- Symbol.scan_id --
   141     Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
   142       !! (err "Expected > or />")
   143         (Scan.this_string "/>" >> K []
   144          || $$ ">" |-- parse_content --|
   145             !! (err ("Expected </" ^ s ^ ">"))
   146               (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
   147     (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
   148 
   149 val parse_document =
   150   Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
   151     (Scan.repeat (Scan.unless ($$ ">")
   152       (Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) --
   153   parse_elem;
   154 
   155 fun tree_of_string s =
   156   (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
   157       (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
   158     (x, []) => x
   159   | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys));
   160 
   161 end;