/[svn]/runtime/load_xml.ml
ViewVC logotype

Contents of /runtime/load_xml.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 233 - (show annotations)
Tue Jul 10 17:17:31 2007 UTC (5 years, 10 months ago) by abate
File size: 2843 byte(s)
[r2003-03-10 22:35:20 by cvscast] De nouveau rapides, les records

Original author: cvscast
Date: 2003-03-10 22:35:21+00:00
1 (* Loading XML documents *)
2
3 (*TODO: close the file ! *)
4
5 open Pxp_yacc
6 open Pxp_lexer_types
7 open Pxp_types
8 open Value
9 open Ident
10
11 let is_ws s =
12 let rec check i =
13 (i < 0) ||
14 (match s.[i] with
15 | ' ' | '\t' | '\n' | '\r' -> check (i - 1)
16 | _ -> false) in
17 check (String.length s - 1)
18
19
20 let string s q =
21 String (0,String.length s,s,q)
22
23 let attrib att =
24 let att = List.map (fun (l,v) -> LabelPool.mk l, string v nil) att in
25 LabelMap.from_list (fun _ _ -> assert false) att
26
27 let elem tag att child =
28 Xml (Atom (Atoms.mk tag), Pair (Record (attrib att), child))
29
30 let load_xml_aux s =
31 let config = { default_config with
32 store_element_positions = false;
33 drop_ignorable_whitespace = true
34 }
35 in
36 let mgr = create_entity_manager config (from_file s) in
37 let next_event =
38 create_pull_parser config (`Entry_document[]) mgr in
39 let curr = ref E_end_of_stream in
40 let get () =
41 match next_event () with
42 | Some (E_error exn) -> failwith (Pxp_types.string_of_exn exn)
43 | Some E_end_of_stream -> failwith "Unexpected end of XML stream"
44 | Some x -> curr := x
45 | None -> () in
46
47 let txt = Buffer.create 1024 in
48
49 let rec parse_elt name att =
50 let elt = elem name att (parse_seq ()) in
51 (match !curr with
52 | E_end_tag (_,_) -> get ()
53 | _ -> failwith "Expect end_tag");
54 elt
55
56
57 and dump_txt q =
58 let data = Buffer.contents txt in
59 Buffer.clear txt;
60 if (is_ws data) then q () else string data (q ())
61
62 and parse_seq () =
63 match !curr with
64 | E_start_tag (name,att,_) ->
65 get ();
66 dump_txt (fun () ->
67 let e1 = parse_elt name att in
68 let rest = parse_seq () in
69 Pair (e1, rest)
70 )
71 | E_char_data data ->
72 get();
73 Buffer.add_string txt data;
74 parse_seq ()
75 | E_end_tag (_,_) ->
76 dump_txt (fun () -> nil)
77 | _ -> failwith "Expect start_tag, char_data, or end_tag"
78
79 and parse_doc () =
80 match !curr with
81 | E_start_tag (name,att,_) -> get (); parse_elt name att
82 | _ -> get (); parse_doc ()
83 in
84 get ();
85 parse_doc ()
86
87
88 let load_xml s =
89 Location.protect_op "load_xml";
90 try load_xml_aux s
91 with exn ->
92 raise
93 (Location.Generic (Pxp_types.string_of_exn exn))
94
95
96 let load_html s =
97 let rec val_of_doc q = function
98 | Nethtml.Data data ->
99 if (is_ws data) then q else string data q
100 | Nethtml.Element (tag, att, child) ->
101 Pair (elem tag att (val_of_docs child), q)
102 and val_of_docs = function
103 | [] -> nil
104 | h::t -> val_of_doc (val_of_docs t) h
105 in
106
107 Location.protect_op "load_xml";
108 let ic = open_in s in
109 let doc = Nethtml.parse_document
110 ~dtd:Nethtml.relaxed_html40_dtd
111 (Lexing.from_channel ic) in
112 let doc = Nethtml.decode ~subst:(fun _ -> "???") doc in
113 close_in ic;
114 val_of_docs doc

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5