e06a11e5ff8d6c1a855e82d7f3eb262988515cec
[tatoo.git] / src / tree.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-04-22 16:29:52 CEST by Kim Nguyen>
18 *)
19
20 (** The different kind of XML nodes and utility functions *)
21
22 module NodeKind =
23   struct
24     type t =
25         Document | Element | Text | Comment | Attribute | ProcessingInstruction | Node
26
27     let to_string =
28       function
29     Document -> "document"
30     | Element -> "element"
31     | Attribute -> "attribute"
32     | Text -> "text"
33     | Comment -> "comment"
34     | ProcessingInstruction -> "processing-instruction"
35     | Node -> "node"
36
37     let print ppf k = Format.fprintf ppf "%s" (to_string k)
38
39
40     let is_a k1 k2 =
41       k1 == Node || k2 == Node || k1 == k2
42 end
43
44 (** Signatures for trees *)
45
46 exception Parse_error of string
47
48 module type S =
49 sig
50   type node
51   (** The type of a tree node *)
52
53   type t
54   (** The type of trees *)
55
56   val size : t -> int
57   (** Return the number of nodes *)
58
59   val nil : node
60   (** Nil node, denoting the first/second child of a leaf or the parent of
61       the root *)
62
63   val dummy : node
64   (** Dummy node that is guaranteed to never occur in any tree *)
65
66   val load_xml_file : in_channel -> t
67   (** Takes a file descriptor and returns the XML data stored in the
68       corresponding file. Start at the current position in the file
69       descriptor (which is not necessarily the begining of file)
70   *)
71
72   val load_xml_string : string -> t
73   (** Loads XML data stored in a string *)
74
75   val print_xml : out_channel -> t -> node -> unit
76   (** Outputs the tree as an XML document on the given output_channel *)
77
78   val root : t -> node
79   (** Returns the root of the tree *)
80
81   val first_child : t -> node -> node
82   (** [first_child t n] returns the first child of node [n] in tree [t].
83       Returns [nil] if [n] is a leaf. Returns [nil] if [n == nil].
84   *)
85
86   val next_sibling : t -> node -> node
87   (** [next_sibling t n] returns the next_sibling of node [n] in tree [t].
88       Returns [nil] if [n] is the last child of a node.
89       Returns [nil] if [n == nil].
90   *)
91
92   val parent : t -> node -> node
93   (** [next_sibling t n] returns the parent of node [n] in tree [t].
94       Returns [nil] if [n] is the root of the tree.
95       Returns [nil] if [n == nil].
96   *)
97
98   val tag : t -> node -> QName.t
99   (** Returns the label of a given node *)
100
101   val data : t -> node -> string
102   (** Returns the character data associated with a node.
103       The only node having character data are those whose label is
104       QName.text, QName.cdata_section or QName.comment
105   *)
106
107   val kind : t -> node -> NodeKind.t
108   (** Returns the kind of the given node *)
109
110   val preorder : t -> node -> int
111   (** Returns the position of a node in pre-order in the tree. The
112     root has preorder 0. [nil] has pre-order [-1].
113   *)
114
115   val print_node : Format.formatter -> node -> unit
116 end