src/Pure/General/xml.ML
author wenzelm
Fri, 18 Jun 2004 20:07:51 +0200
changeset 14969 3d9126cbf0e6
parent 14928 b8c1783c9101
child 15010 72fbe711e414
permissions -rw-r--r--
scalable string_of_tree; tuned;
wenzelm@12416
     1
(*  Title:      Pure/General/xml.ML
wenzelm@12416
     2
    ID:         $Id$
wenzelm@14728
     3
    Author:     David Aspinall, Stefan Berghofer and Markus Wenzel
wenzelm@12416
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@12416
     5
wenzelm@14969
     6
Basic support for XML.
wenzelm@12416
     7
*)
wenzelm@12416
     8
wenzelm@12416
     9
signature XML =
wenzelm@12416
    10
sig
wenzelm@14969
    11
  val header: string
wenzelm@14969
    12
  val text: string -> string
wenzelm@14969
    13
  val cdata: string -> string
wenzelm@14969
    14
  val element: string -> (string * string) list -> string list -> string
berghofe@13729
    15
  datatype tree =
berghofe@13729
    16
      Elem of string * (string * string) list * tree list
berghofe@13729
    17
    | Text of string
berghofe@13729
    18
  val string_of_tree: tree -> string
berghofe@14185
    19
  val parse_content: string list -> tree list * string list
berghofe@14185
    20
  val parse_elem: string list -> tree * string list
berghofe@14185
    21
  val parse_document: string list -> (string option * tree) * string list
wenzelm@14969
    22
  val tree_of_string: string -> tree
wenzelm@12416
    23
end;
wenzelm@12416
    24
wenzelm@12416
    25
structure XML: XML =
wenzelm@12416
    26
struct
wenzelm@12416
    27
wenzelm@14969
    28
(** string based representation (small scale) **)
wenzelm@14969
    29
wenzelm@14969
    30
val header = "<?xml version=\"1.0\"?>\n";
wenzelm@14969
    31
wenzelm@14969
    32
wenzelm@14969
    33
(* text and character data *)
wenzelm@14969
    34
wenzelm@14969
    35
fun decode "&lt;" = "<"
wenzelm@14969
    36
  | decode "&gt;" = ">"
wenzelm@14969
    37
  | decode "&amp;" = "&"
wenzelm@14969
    38
  | decode "&apos;" = "'"
wenzelm@14969
    39
  | decode "&quot;" = "\""
wenzelm@14969
    40
  | decode c = c;
wenzelm@12416
    41
wenzelm@12416
    42
fun encode "<" = "&lt;"
wenzelm@12416
    43
  | encode ">" = "&gt;"
wenzelm@12416
    44
  | encode "&" = "&amp;"
wenzelm@12416
    45
  | encode "'" = "&apos;"
wenzelm@12416
    46
  | encode "\"" = "&quot;"
wenzelm@12416
    47
  | encode c = c;
wenzelm@12416
    48
wenzelm@14928
    49
val text = Library.translate_string encode;
wenzelm@12416
    50
wenzelm@14969
    51
val cdata = enclose "<![CDATA[" "]]>";
wenzelm@12416
    52
wenzelm@14728
    53
wenzelm@12416
    54
(* elements *)
wenzelm@12416
    55
wenzelm@14728
    56
fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
wenzelm@12416
    57
wenzelm@12416
    58
fun element name atts cs =
wenzelm@12416
    59
  let val elem = space_implode " " (name :: map attribute atts) in
wenzelm@12416
    60
    if null cs then enclose "<" "/>" elem
wenzelm@12416
    61
    else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
wenzelm@12416
    62
  end;
wenzelm@12416
    63
wenzelm@14728
    64
wenzelm@12416
    65
wenzelm@14969
    66
(** explicit XML trees **)
berghofe@13729
    67
wenzelm@14969
    68
datatype tree =
wenzelm@14969
    69
    Elem of string * (string * string) list * tree list
wenzelm@14969
    70
  | Text of string;
wenzelm@14969
    71
wenzelm@14969
    72
fun string_of_tree tree =
wenzelm@14969
    73
  let
wenzelm@14969
    74
    fun string_of (Elem (name, atts, ts)) buf =
wenzelm@14969
    75
        let val buf' =
wenzelm@14969
    76
          buf |> Buffer.add "<"
wenzelm@14969
    77
          |> fold Buffer.add (separate " " (name :: map attribute atts))
wenzelm@14969
    78
        in
wenzelm@14969
    79
          if null ts then
wenzelm@14969
    80
            buf' |> Buffer.add "/>"
wenzelm@14969
    81
          else
wenzelm@14969
    82
            buf' |> Buffer.add ">"
wenzelm@14969
    83
            |> fold string_of ts
wenzelm@14969
    84
            |> Buffer.add "</" |> Buffer.add name |> Buffer.add ">"
wenzelm@14969
    85
        end
wenzelm@14969
    86
      | string_of (Text s) buf = Buffer.add (text s) buf;
wenzelm@14969
    87
  in Buffer.content (string_of tree Buffer.empty) end;
wenzelm@14969
    88
wenzelm@14969
    89
wenzelm@14969
    90
wenzelm@14969
    91
(** XML parsing **)
berghofe@13729
    92
wenzelm@14728
    93
fun err s (xs, _) =
wenzelm@14728
    94
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
berghofe@13729
    95
wenzelm@14728
    96
val scan_whspc = Scan.any Symbol.is_blank;
berghofe@13729
    97
berghofe@13729
    98
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
berghofe@13729
    99
berghofe@14185
   100
val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<")
berghofe@14185
   101
  (scan_special || Scan.one Symbol.not_eof)) >> implode;
berghofe@13729
   102
wenzelm@14910
   103
val parse_cdata = Scan.this_string "<![CDATA[" |--
wenzelm@14910
   104
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >>
wenzelm@14910
   105
    implode) --| Scan.this_string "]]>";
berghofe@13729
   106
berghofe@14185
   107
val parse_att =
berghofe@14865
   108
  Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
berghofe@14865
   109
  (($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s)
berghofe@14865
   110
    (scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd);
aspinall@14863
   111
wenzelm@14910
   112
val parse_comment = Scan.this_string "<!--" --
wenzelm@14910
   113
  Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) --
wenzelm@14910
   114
  Scan.this_string "-->";
berghofe@14185
   115
wenzelm@14910
   116
val parse_pi = Scan.this_string "<?" |--
wenzelm@14910
   117
  Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --|
wenzelm@14910
   118
  Scan.this_string "?>";
berghofe@14185
   119
berghofe@14185
   120
fun parse_content xs =
berghofe@14185
   121
  ((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] --
berghofe@14185
   122
    (Scan.repeat (scan_whspc |--
berghofe@14185
   123
       (   parse_elem >> single
berghofe@14185
   124
        || parse_cdata >> (single o Text)
berghofe@14185
   125
        || parse_pi >> K []
berghofe@14185
   126
        || parse_comment >> K []) --
berghofe@14185
   127
       Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) []
berghofe@14185
   128
         >> op @) >> flat) >> op @) --| scan_whspc) xs
berghofe@14185
   129
berghofe@14185
   130
and parse_elem xs =
berghofe@14185
   131
  ($$ "<" |-- Symbol.scan_id --
berghofe@14865
   132
    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
berghofe@14185
   133
      !! (err "Expected > or />")
wenzelm@14910
   134
        (Scan.this_string "/>" >> K []
berghofe@14185
   135
         || $$ ">" |-- parse_content --|
berghofe@14185
   136
            !! (err ("Expected </" ^ s ^ ">"))
wenzelm@14910
   137
              (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
berghofe@13729
   138
    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
berghofe@13729
   139
berghofe@14185
   140
val parse_document =
wenzelm@14910
   141
  Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
berghofe@14185
   142
    (Scan.repeat (Scan.unless ($$ ">")
berghofe@14185
   143
      (Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) --
berghofe@14185
   144
  parse_elem;
berghofe@14185
   145
berghofe@13729
   146
fun tree_of_string s =
berghofe@14185
   147
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
berghofe@14185
   148
      (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
wenzelm@14728
   149
    (x, []) => x
wenzelm@14728
   150
  | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys));
berghofe@13729
   151
wenzelm@12416
   152
end;