Code

bed02294333e2d6144caa3fa13e05e5325e483e0
[inkscape.git] / src / dom / xpathtoken.cpp
1 /**\r
2  * Phoebe DOM Implementation.\r
3  *\r
4  * This is a C++ approximation of the W3C DOM model, which follows\r
5  * fairly closely the specifications in the various .idl files, copies of\r
6  * which are provided for reference.  Most important is this one:\r
7  *\r
8  * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html\r
9  *\r
10  * Authors:\r
11  *   Bob Jamison\r
12  *\r
13  * Copyright (C) 2006 Bob Jamison\r
14  *\r
15  *  This library is free software; you can redistribute it and/or\r
16  *  modify it under the terms of the GNU Lesser General Public\r
17  *  License as published by the Free Software Foundation; either\r
18  *  version 2.1 of the License, or (at your option) any later version.\r
19  *\r
20  *  This library is distributed in the hope that it will be useful,\r
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
23  *  Lesser General Public License for more details.\r
24  *\r
25  *  You should have received a copy of the GNU Lesser General Public\r
26  *  License along with this library; if not, write to the Free Software\r
27  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
28  */\r
29 \r
30 \r
31 #include "xpathtoken.h"\r
32 #include <stdio.h>\r
33 \r
34 \r
35 \r
36 namespace org\r
37 {\r
38 namespace w3c\r
39 {\r
40 namespace dom\r
41 {\r
42 namespace xpath\r
43 {\r
44 \r
45 \r
46 //########################################################################\r
47 //# X P A T H    S T A C K    I T E M\r
48 //########################################################################\r
49 \r
50 /**\r
51  *\r
52  */\r
53 StackItem::StackItem()\r
54 {\r
55     ival = 0L;\r
56     dval = 0.0;\r
57 }\r
58 \r
59 \r
60 /**\r
61  *\r
62  */\r
63 StackItem::StackItem(const StackItem &other)\r
64 {\r
65     assign(other);\r
66 }\r
67 \r
68 \r
69 /**\r
70  *\r
71  */\r
72 StackItem::~StackItem()\r
73 {\r
74 }\r
75 \r
76 \r
77 /**\r
78  *\r
79  */\r
80 StackItem &StackItem::operator=(const StackItem &other)\r
81 {\r
82     assign(other);\r
83     return *this;\r
84 }\r
85 \r
86 /**\r
87  *\r
88  */\r
89 void StackItem::assign(const StackItem &other)\r
90 {\r
91     sval = other.sval;\r
92     ival = other.ival;\r
93     dval = other.dval;\r
94 }\r
95 \r
96 \r
97 //########################################################################\r
98 //# X P A T H    S T A C K\r
99 //########################################################################\r
100 \r
101 /**\r
102  *\r
103  */\r
104 Stack::Stack()\r
105 {\r
106     size = 0;\r
107 }\r
108 \r
109 \r
110 /**\r
111  *\r
112  */\r
113 Stack::Stack(const Stack &other)\r
114 {\r
115     assign(other);\r
116 }\r
117 \r
118 \r
119 /**\r
120  *\r
121  */\r
122 Stack::~Stack()\r
123 {\r
124 }\r
125 \r
126 \r
127 /**\r
128  *\r
129  */\r
130 void Stack::assign(const Stack &other)\r
131 {\r
132     root = other.root;\r
133     nodeList = other.nodeList;\r
134     size = other.size;\r
135     for (int i=0 ; i<size ; i++)\r
136         items[i] = other.items[i];\r
137 }\r
138 \r
139 \r
140 /**\r
141  *\r
142  */\r
143 void Stack::reset()\r
144 {\r
145     root = NULL;\r
146     NodeList n; /*no "clear" in api*/\r
147     nodeList = n;\r
148     size = 0;\r
149 }\r
150 \r
151 \r
152 \r
153 \r
154 /**\r
155  *\r
156  */\r
157 void Stack::push(StackItem &item)\r
158 {\r
159     if (size>=STACK_SIZE)\r
160         {\r
161         return;\r
162         }\r
163     items[size++] = item;\r
164 }\r
165 \r
166 /**\r
167  *\r
168  */\r
169 StackItem Stack::pop()\r
170 {\r
171     if (size<1)\r
172         {\r
173         StackItem item;\r
174         return item;\r
175         }\r
176     return items[--size];\r
177 }\r
178 \r
179 /**\r
180  * Set the root node\r
181  */\r
182 void Stack::setRootNode(const Node *node)\r
183 {\r
184     root = (Node *)node;\r
185 }\r
186 \r
187 \r
188 /**\r
189  * Get the current node list;\r
190  */\r
191 NodeList &Stack::getNodeList()\r
192 {\r
193     return nodeList;\r
194 }\r
195 \r
196 \r
197 //########################################################################\r
198 //# T O K E N    L I S T\r
199 //########################################################################\r
200 \r
201 /**\r
202  *\r
203  */\r
204 TokenList::TokenList()\r
205 {\r
206 }\r
207 \r
208 \r
209 /**\r
210  *\r
211  */\r
212 TokenList::TokenList(const TokenList &other)\r
213 {\r
214     assign(other);\r
215 }\r
216 \r
217 /**\r
218  *\r
219  */\r
220 TokenList &TokenList::operator=(const TokenList &other)\r
221 {\r
222     assign(other);\r
223     return *this;\r
224 }\r
225 \r
226 /**\r
227  *\r
228  */\r
229 void TokenList::assign(const TokenList &other)\r
230 {\r
231     tokens = other.tokens;\r
232 }\r
233 \r
234 /**\r
235  *\r
236  */\r
237 TokenList::~TokenList()\r
238 {\r
239     clear();\r
240 }\r
241 \r
242 /**\r
243  *\r
244  */\r
245 void TokenList::clear()\r
246 {\r
247     std::vector<Token *>::iterator iter;\r
248     for (iter = tokens.begin() ; iter!= tokens.end() ; iter++)\r
249         {\r
250         delete (*iter);\r
251         }\r
252     tokens.clear();\r
253 }\r
254 \r
255 /**\r
256  *\r
257  */\r
258 void TokenList::add(Token *tok)\r
259 {\r
260     tokens.push_back(tok);\r
261 }\r
262 \r
263 /**\r
264  *  This method "executes" a list of Tokens in the context of a DOM root\r
265  *  Node, returning a list of Nodes that match the xpath expression.\r
266  */\r
267 NodeList TokenList::execute(const Node *root)\r
268 {\r
269     NodeList list;\r
270 \r
271     if (!root)\r
272         return list;\r
273 \r
274     Stack stack;\r
275     stack.setRootNode(root);\r
276 \r
277     //### Execute the token list\r
278     std::vector<Token *>::iterator iter;\r
279     for (iter = tokens.begin() ; iter != tokens.end() ; iter++)\r
280         {\r
281         Token *tok = *iter;\r
282         tok->execute(stack);\r
283         }\r
284 \r
285     list = stack.getNodeList();\r
286 \r
287     return list;\r
288 }\r
289 \r
290 \r
291 /**\r
292  *\r
293  */\r
294 void TokenList::dump()\r
295 {\r
296     std::vector<Token *>::iterator iter;\r
297     printf("############# TOKENS\n");\r
298     for (iter = tokens.begin() ; iter != tokens.end() ; iter++)\r
299         {\r
300         Token *tok = *iter;\r
301         tok->dump();\r
302         }\r
303 }\r
304 \r
305 \r
306 \r
307 \r
308 \r
309 } // namespace xpath\r
310 } // namespace dom\r
311 } // namespace w3c\r
312 } // namespace org\r
313 //########################################################################\r
314 //# E N D    O F    F I L E\r
315 //########################################################################\r
316 \r
317 \r
318 \r