9ef78ce6b25e7c74b3e0a708c689ffda3997b68a
[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-04 18:40:39 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
45 (** Signatures for trees *)
46
47 module type S =
48 sig
49   type node
50   (** The type of a tree node *)
51
52   type t
53   (** The type of trees *)
54
55   val size : t -> int
56   (** Return the number of nodes *)
57
58   val nil : node
59   (** Nil node, denoting the first/second child of a leaf or the parent of
60       the root *)
61
62   val dummy : node
63   (** Dummy node that is guaranteed to never occur in any tree *)
64
65   val load_xml_file : in_channel -> t
66   (** Takes a file descriptor and returns the XML data stored in the
67       corresponding file. Start at the current position in the file
68       descriptor (which is not necessarily the begining of file)
69   *)
70
71   val load_xml_string : string -> t
72   (** Loads XML data stored in a string *)
73
74   val print_xml : out_channel -> t -> node -> unit
75   (** Outputs the tree as an XML document on the given output_channel *)
76
77   val root : t -> node
78   (** Returns the root of the tree *)
79
80   val first_child : t -> node -> node
81   (** [first_child t n] returns the first child of node [n] in tree [t].
82       Returns [nil] if [n] is a leaf. Returns [nil] if [n == nil].
83   *)
84
85   val next_sibling : t -> node -> node
86   (** [next_sibling t n] returns the next_sibling of node [n] in tree [t].
87       Returns [nil] if [n] is the last child of a node.
88       Returns [nil] if [n == nil].
89   *)
90
91   val parent : t -> node -> node
92   (** [next_sibling t n] returns the parent of node [n] in tree [t].
93       Returns [nil] if [n] is the root of the tree.
94       Returns [nil] if [n == nil].
95   *)
96
97   val tag : t -> node -> QName.t
98   (** Returns the label of a given node *)
99
100   val data : t -> node -> string
101   (** Returns the character data associated with a node.
102       The only node having character data are those whose label is
103       QName.text, QName.cdata_section or QName.comment
104   *)
105
106   val kind : t -> node -> NodeKind.t
107   (** Returns the kind of the given node *)
108
109   val preorder : t -> node -> int
110   (** Returns the position of a node in pre-order in the tree. The
111     root has preorder 0. [nil] has pre-order [-1].
112   *)
113
114   val print_node : Format.formatter -> node -> unit
115 end