package ezxmlm

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ezxmlm.ml

1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(*
 * Copyright (c) 2013 Anil Madhavapeddy <anil@recoil.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *)

type node = ('a Xmlm.frag as 'a) Xmlm.frag
type nodes = node list

let from_input i =
  try
    let el tag children = `El (tag, children) in
    let data d = `Data d in
    Xmlm.input_doc_tree ~el ~data i
  with Xmlm.Error((line,col),err) ->
    let err = Xmlm.error_message err in
    raise (Failure (Printf.sprintf "[line %d, col %d] %s" line col err))

let from_channel chan =
  let i = Xmlm.make_input (`Channel chan) in
  let (dtd,doc) = from_input i in
  (dtd, [doc])

let from_string buf =
  let i = Xmlm.make_input (`String (0,buf)) in
  let (dtd,doc) = from_input i in
  (dtd, [doc])
  
let to_output o t = 
  let frag = function
  | `El (tag, childs) -> `El (tag, childs) 
  | `Data d -> `Data d in
  Xmlm.output_doc_tree frag o t

let write_document mode ?(decl=false) dtd doc =
  let o = Xmlm.make_output ~decl mode in
  match doc with
  | [] -> ()
  | hd::tl ->
     to_output o (dtd, hd);
     List.iter (fun t -> to_output o (None, t)) tl

let to_channel chan ?(decl=false) dtd doc =
  write_document (`Channel chan) ~decl dtd doc

let to_string ?(decl=false) ?dtd doc =
  let buf = Buffer.create 512 in
  write_document (`Buffer buf) ~decl dtd doc;
  Buffer.contents buf

let pp fmt doc = Format.pp_print_string fmt (to_string doc)

let make_tag tag (attrs,nodes) : node =
  `El ((("",tag),attrs),nodes)

let mem_attr k v attrs =
  try List.assoc ("",k) attrs = v
  with Not_found -> false

let get_attr k attrs =
  List.assoc ("",k) attrs

let rec filter_map ~tag ~f i =
  List.concat (
    List.map (
      function
      | `El (((_,t),attr),c) when t=tag -> f attr c
      | `El (p,c) -> [`El (p, (filter_map ~tag ~f c))]
      | `Data x -> [`Data x]
    ) i
  )

let rec filter_iter ~tag ~f i =
  List.iter (
    function
    | `El (((_,t),attr),c) when t=tag -> f attr c
    | `El (_,c) -> filter_iter ~tag ~f c
    | `Data _ -> ()
  ) i

let filter_attrs attr value (al:(Xmlm.attribute list * nodes) list) =
  List.filter (fun (attrs, _nodes) ->
    try List.assoc ("",attr) attrs = value
    with Not_found -> false
  ) al

let filter_attr attr value al =
  match filter_attrs attr value al with
  | [] -> raise Not_found
  | hd :: _ -> hd

let hd nodes =
  List.hd nodes

let tl =
  function
  | [] -> []
  | _::tl -> tl

exception Tag_not_found of string

let members_with_attr tag nodes =
  let r = List.fold_left (fun a b ->
    match b with
    | `El (((_,t),attr),c) when t=tag -> (attr,c) :: a
    | _ -> a
  ) [] nodes in
  List.rev r

let member_with_attr tag nodes =
  match members_with_attr tag nodes with
  | [] -> raise (Tag_not_found tag)
  | hd::_ -> hd
  
let has_member tag nodes =
    match members_with_attr tag nodes with
  | [] -> false
  | _hd::_ -> true

let members tag nodes =
  List.map snd (members_with_attr tag nodes)

let member tag nodes =
  snd (member_with_attr tag nodes)

let pick_tags tag cl v nodes =
  members_with_attr tag nodes
  |> filter_attrs cl v
  |> List.map (make_tag tag)

let pick_tag tag cl v nodes =
  members_with_attr tag nodes
  |> filter_attr cl v
  |> make_tag tag

let data_to_string nodes =
  let buf = Buffer.create 512 in
  List.iter (function
    | `Data x -> Buffer.add_string buf x
    | _ -> ()
  ) nodes;
  Buffer.contents buf