Code

limit the smallest exponent in transforms; anything smaller is written as 0
[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     sval = other.sval;\r
66     ival = other.ival;\r
67     dval = other.dval;\r
68 }\r
69 \r
70 \r
71 /**\r
72  *\r
73  */\r
74 StackItem::~StackItem()\r
75 {\r
76 }\r
77 \r
78 \r
79 //########################################################################\r
80 //# X P A T H    S T A C K\r
81 //########################################################################\r
82 \r
83 /**\r
84  *\r
85  */\r
86 Stack::Stack()\r
87 {\r
88     size = 0;\r
89 }\r
90 \r
91 \r
92 /**\r
93  *\r
94  */\r
95 Stack::Stack(const Stack &other)\r
96 {\r
97     assign(other);\r
98 }\r
99 \r
100 \r
101 /**\r
102  *\r
103  */\r
104 Stack::~Stack()\r
105 {\r
106 }\r
107 \r
108 \r
109 /**\r
110  *\r
111  */\r
112 void Stack::assign(const Stack &other)\r
113 {\r
114     root = other.root;\r
115     nodeList = other.nodeList;\r
116     size = other.size;\r
117     for (int i=0 ; i<size ; i++)\r
118         items[i] = other.items[i];\r
119 }\r
120 \r
121 \r
122 /**\r
123  *\r
124  */\r
125 void Stack::reset()\r
126 {\r
127     root = NULL;\r
128     NodeList n; /*no "clear" in api*/\r
129     nodeList = n;\r
130     size = 0;\r
131 }\r
132 \r
133 \r
134 \r
135 \r
136 /**\r
137  *\r
138  */\r
139 void Stack::push(StackItem &item)\r
140 {\r
141     if (size>=STACK_SIZE)\r
142         {\r
143         return;\r
144         }\r
145     items[size++] = item;\r
146 }\r
147 \r
148 /**\r
149  *\r
150  */\r
151 StackItem Stack::pop()\r
152 {\r
153     if (size<1)\r
154         {\r
155         StackItem item;\r
156         return item;\r
157         }\r
158     return items[--size];\r
159 }\r
160 \r
161 /**\r
162  * Set the root node\r
163  */\r
164 void Stack::setRootNode(const Node *node)\r
165 {\r
166     root = (Node *)node;\r
167 }\r
168 \r
169 \r
170 /**\r
171  * Get the current node list;\r
172  */\r
173 NodeList &Stack::getNodeList()\r
174 {\r
175     return nodeList;\r
176 }\r
177 \r
178 \r
179 //########################################################################\r
180 //# T O K E N    L I S T\r
181 //########################################################################\r
182 \r
183 /**\r
184  *\r
185  */\r
186 TokenList::TokenList()\r
187 {\r
188 }\r
189 \r
190 \r
191 /**\r
192  *\r
193  */\r
194 TokenList::~TokenList()\r
195 {\r
196     clear();\r
197 }\r
198 \r
199 /**\r
200  *\r
201  */\r
202 void TokenList::clear()\r
203 {\r
204     std::vector<Token *>::iterator iter;\r
205     for (iter = tokens.begin() ; iter!= tokens.end() ; iter++)\r
206         {\r
207         delete (*iter);\r
208         }\r
209     tokens.clear();\r
210 }\r
211 \r
212 /**\r
213  *\r
214  */\r
215 void TokenList::add(Token *tok)\r
216 {\r
217     tokens.push_back(tok);\r
218 }\r
219 \r
220 /**\r
221  *  This method "executes" a list of Tokens in the context of a DOM root\r
222  *  Node, returning a list of Nodes that match the xpath expression.\r
223  */\r
224 NodeList TokenList::execute(const Node *root)\r
225 {\r
226     NodeList list;\r
227 \r
228     if (!root)\r
229         return list;\r
230 \r
231     Stack stack;\r
232     stack.setRootNode(root);\r
233 \r
234     //### Execute the token list\r
235     std::vector<Token *>::iterator iter;\r
236     for (iter = tokens.begin() ; iter != tokens.end() ; iter++)\r
237         {\r
238         Token *tok = *iter;\r
239         tok->execute(stack);\r
240         }\r
241 \r
242     list = stack.getNodeList();\r
243 \r
244     return list;\r
245 }\r
246 \r
247 \r
248 /**\r
249  *\r
250  */\r
251 void TokenList::dump()\r
252 {\r
253     std::vector<Token *>::iterator iter;\r
254     printf("############# TOKENS\n");\r
255     for (iter = tokens.begin() ; iter != tokens.end() ; iter++)\r
256         {\r
257         Token *tok = *iter;\r
258         tok->dump();\r
259         }\r
260 }\r
261 \r
262 \r
263 \r
264 \r
265 \r
266 } // namespace xpath\r
267 } // namespace dom\r
268 } // namespace w3c\r
269 } // namespace org\r
270 //########################################################################\r
271 //# E N D    O F    F I L E\r
272 //########################################################################\r
273 \r
274 \r
275 \r