Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2012 Kohei Yoshida
4 : *
5 : * Permission is hereby granted, free of charge, to any person
6 : * obtaining a copy of this software and associated documentation
7 : * files (the "Software"), to deal in the Software without
8 : * restriction, including without limitation the rights to use,
9 : * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : * copies of the Software, and to permit persons to whom the
11 : * Software is furnished to do so, subject to the following
12 : * conditions:
13 : *
14 : * The above copyright notice and this permission notice shall be
15 : * included in all copies or substantial portions of the Software.
16 : *
17 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 : * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 : * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 : * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 : * OTHER DEALINGS IN THE SOFTWARE.
25 : *
26 : ************************************************************************/
27 :
28 : #ifndef __ORCUS_XML_STRUCTURE_TREE_HPP__
29 : #define __ORCUS_XML_STRUCTURE_TREE_HPP__
30 :
31 : #include "env.hpp"
32 : #include "types.hpp"
33 :
34 : #include <ostream>
35 :
36 : namespace orcus {
37 :
38 : class xmlns_context;
39 : struct xml_structure_tree_impl;
40 :
41 : /**
42 : * Tree representing the structure of elements in XML content. Recurring
43 : * elements under the same parent are represented by a single element
44 : * instance. This tree only includes elements; no attributes and content
45 : * nodes appear in this tree.
46 : */
47 : class ORCUS_DLLPUBLIC xml_structure_tree
48 : {
49 : xml_structure_tree(const xml_structure_tree&); // disabled;
50 : xml_structure_tree& operator= (const xml_structure_tree&); // disabled
51 :
52 : public:
53 :
54 0 : struct entity_name
55 : {
56 : xmlns_id_t ns;
57 : pstring name;
58 :
59 : entity_name();
60 : entity_name(xmlns_id_t _ns, const pstring& _name);
61 :
62 : bool operator< (const entity_name& r) const;
63 : bool operator== (const entity_name& r) const;
64 :
65 : struct hash
66 : {
67 : size_t operator ()(const entity_name& val) const;
68 : };
69 : };
70 :
71 : typedef std::vector<entity_name> entity_names_type;
72 :
73 : struct element
74 : {
75 : entity_name name;
76 : bool repeat;
77 :
78 : element();
79 : element(const entity_name& _name, bool _repeat);
80 : };
81 :
82 : struct walker_impl;
83 :
84 : /**
85 : * This class allows client to traverse the tree.
86 : */
87 : class walker
88 : {
89 : friend class xml_structure_tree;
90 : walker_impl* mp_impl;
91 :
92 : walker(); // disabled
93 : walker(const xml_structure_tree_impl& parent_impl);
94 : public:
95 :
96 : static size_t index_not_found;
97 :
98 : walker(const walker& r);
99 : ~walker();
100 : walker& operator= (const walker& r);
101 :
102 : /**
103 : * Set current position to the root element, and return the root
104 : * element.
105 : *
106 : * @return root element.
107 : */
108 : element root();
109 :
110 : /**
111 : * Descend into specified child element.
112 : *
113 : * @param ns namespace of child element
114 : * @param name name of child element
115 : *
116 : * @return child element
117 : */
118 : element descend(const entity_name& name);
119 :
120 : /**
121 : * Move up to the parent element.
122 : */
123 : element ascend();
124 :
125 : /**
126 : * Get a list of names of all child elements at current element
127 : * position. The list of names is in order of appearance.
128 : *
129 : * @param names list of child element names in order of appearance.
130 : */
131 : void get_children(entity_names_type& names);
132 :
133 : /**
134 : * Get a list of names of all attributes that belong to current
135 : * element. The list of names is in order of appearance.
136 : *
137 : * @param names list of attribute names in order of appearance.
138 : */
139 : void get_attributes(entity_names_type& names);
140 :
141 : /**
142 : * Get a numerical, 0-based index of given XML namespace.
143 : *
144 : * @param ns XML namespace ID.
145 : *
146 : * @return numeric, 0-based index of XML namespace if found, or
147 : * <code>xml_structure_tree::walker::index_not_found</code> if
148 : * the namespace is not found in this structure.
149 : */
150 : size_t get_xmlns_index(xmlns_id_t ns) const;
151 :
152 : std::string get_xmlns_short_name(xmlns_id_t ns) const;
153 : };
154 :
155 : xml_structure_tree(xmlns_context& xmlns_cxt);
156 : ~xml_structure_tree();
157 :
158 : void parse(const char* p, size_t n);
159 :
160 : void dump_compact(std::ostream& os) const;
161 :
162 : walker get_walker() const;
163 :
164 : private:
165 : xml_structure_tree_impl* mp_impl;
166 : };
167 :
168 : }
169 :
170 :
171 :
172 : #endif
|