print_xml.ml 4.58 KB
Newer Older
1
2
3
4
5
(* Print XML documents, using PXP *)

open Pxp_aux
open Pxp_types
open Value
6
open Ident
7
module U = Encodings.Utf8
8

9
let exn_print_xml = CDuceExn (Pair (
10
				Atom (Atoms.V.mk_ascii "Invalid_argument"),
11
				string_latin1 "print_xml"))
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let blank = U.mk " "
let true_literal = U.mk "true"
let false_literal = U.mk "false"

  (* @raise exn_print_xml in case of failure. Rationale: schema printing is
   * the last attempt to print a value, others have already failed *)
let rec schema_value ?(recurs=true) ~wds v =
  match v with
  | Record _ as v ->
      (try
        wds (Schema_builtin.string_of_time_type (Value.get_fields v))
      with Schema_builtin.Schema_builtin_error _ -> raise exn_print_xml)
  | Integer i -> wds (U.mk (Intervals.V.to_string i))
  | v when Value.equal v Value.vtrue -> wds true_literal
  | v when Value.equal v Value.vfalse -> wds false_literal
  | Pair _ as v when recurs -> schema_values ~wds v
  | String_utf8 _ as v -> wds (fst (get_string_utf8 v))
  | _ -> raise exn_print_xml

and schema_values ~wds v =
  match v with
  | Pair (hd, Atom a) when a = Sequence.nil_atom ->
      schema_value ~recurs:false ~wds hd
  | Pair (hd, tl) ->
      schema_value ~recurs:false ~wds hd;
      wds blank;
      schema_values ~wds tl
  | _ -> raise exn_print_xml

42
let string_of_xml ~utf8 ns_table v = 
43
  let to_enc = if utf8 then `Enc_utf8 else `Enc_iso88591 in
44
45

  let buffer = Buffer.create 127 in
46
  let printer = Ns.Printer.printer ns_table in
47
48
49

  let wms = 
    write_markup_string
50
      ~from_enc:`Enc_utf8 
51
52
53
      ~to_enc
      (`Out_buffer buffer)

54
  and wds s =
55
    write_data_string
56
      ~from_enc:`Enc_utf8
57
58
      ~to_enc
      (`Out_buffer buffer)
59
      (U.get_str s)
60
  in
61
62
63
64
65
66
67
68
69
  let write_att (n,v) =
    wms (" " ^ (Ns.Printer.attr printer n) ^ "=\""); wds v; wms "\"" in
  let write_xmlns (pr,ns) =
    let pr = U.get_str pr in
    if pr = "" then wms " xmlns"
    else (wms " xmlns:"; wms pr);
    wms "=\"";
    wds (Ns.value ns);
    wms "\"" in
70

71
72
73
74
75
76
77
78
79
80
81
82
  let element_start n xmlns attrs = 
    wms ("<" ^ (Ns.Printer.tag printer n)); 
    List.iter write_xmlns xmlns;
    List.iter write_att attrs; 
    wms ">"
  and empty_element n xmlns attrs = 
    wms ("<" ^ (Ns.Printer.tag printer n)); 
    List.iter write_xmlns xmlns;
    List.iter write_att attrs; 
    wms "/>"
  and element_end n = 
    wms ("</" ^ (Ns.Printer.attr printer n) ^ ">")
83
  and document_start () = 
84
(*    wms ("<?xml version='1.0' encoding='" ^
85
	 Netconversion.string_of_encoding to_enc ^
86
	 "'?>\n") *)
87
    ()
88
  in
89

90
91
92
93
94
  let rec register_elt = function
    | Xml (Atom tag, Record attrs, content) ->
	List.iter
	  (fun (n,_) -> Ns.Printer.register_attr printer (LabelPool.value n))
	  (LabelMap.get attrs);
95
	Ns.Printer.register_tag printer (Atoms.V.value tag);
96
97
98
99
100
101
	register_content content
    | _ -> ()
  and register_content = function
    | String_utf8 (_,_,_,q)
    | String_latin1 (_,_,_,q) -> register_content q
    | Pair (x, q) -> register_elt x; register_content q
102
    | Concat (x,y) -> register_content x; register_content y
103
104
105
106
107
    | _ -> () 
  in
  register_elt v;

  let rec print_elt xmlns = function
108
    | Xml (Atom tag, Record attrs, content) ->
109
	let tag = Atoms.V.value tag in
110
111
	let attrs = LabelMap.mapi_to_list 
		      (fun n v -> 
112
113
114
115
116
117
118
119
120
121
122
123
                         if is_str v then begin
                           let (s,q) = get_string_utf8 v in
                           match q with
                             | Atom a when a = Sequence.nil_atom -> 
                                 (LabelPool.value n), s
                             | _ -> raise exn_print_xml
                         end else begin
                           let buf = Buffer.create 20 in
                           let wds s = Buffer.add_string buf (U.get_str s) in
                           schema_value ~wds v;
                           (LabelPool.value n, U.mk (Buffer.contents buf))
                         end
124
		      ) attrs in
125
	(match content with
126
	  | Atom a when a = Sequence.nil_atom -> empty_element tag xmlns attrs
127
	  | _ ->
128
	      element_start tag xmlns attrs;
129
130
	      print_content content;
	      element_end tag)
131
    | _ -> raise exn_print_xml
132
133
134
135
  and print_content v =
    let (s,q) = get_string_utf8 v in
    wds s;
    match q with
136
      | Pair (Xml _ as x, q) -> print_elt [] x; print_content q
137
      | Atom a when a = Sequence.nil_atom -> ()
138
      | v -> schema_value ~wds v
139
140
  in
  document_start ();
141
  print_elt (Ns.Printer.prefixes printer) v;
142
  Buffer.contents buffer
143

144
let print_xml ~utf8 ns_table s =
145
  try   
146
    let s = string_of_xml ~utf8 ns_table s in
147
148
149
150
151
    if utf8 then string_utf8 (U.mk s) else string_latin1 s
  with exn ->
    raise
      (Location.Generic (Pxp_types.string_of_exn exn))