1 /*
2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "XPathResult.h"
29
30 #if ENABLE(XPATH)
31
32 #include "Document.h"
33 #include "Node.h"
34 #include "ExceptionCode.h"
35 #include "XPathEvaluator.h"
36 #include "XPathException.h"
37
38 #if COMPILER(WINSCW)
39 #define BOOL_TO_VALUE_CAST (unsigned long)
40 #else
41 #define BOOL_TO_VALUE_CAST
42 #endif
43
44 namespace WebCore {
45
46 using namespace XPath;
47
XPathResult(Document * document,const Value & value)48 XPathResult::XPathResult(Document* document, const Value& value)
49 : m_value(value)
50 , m_nodeSetPosition(0)
51 , m_domTreeVersion(0)
52 {
53 switch (m_value.type()) {
54 case Value::BooleanValue:
55 m_resultType = BOOLEAN_TYPE;
56 return;
57 case Value::NumberValue:
58 m_resultType = NUMBER_TYPE;
59 return;
60 case Value::StringValue:
61 m_resultType = STRING_TYPE;
62 return;
63 case Value::NodeSetValue:
64 m_resultType = UNORDERED_NODE_ITERATOR_TYPE;
65 m_nodeSetPosition = 0;
66 m_nodeSet = m_value.toNodeSet();
67 m_document = document;
68 m_domTreeVersion = document->domTreeVersion();
69 return;
70 }
71 ASSERT_NOT_REACHED();
72 }
73
~XPathResult()74 XPathResult::~XPathResult()
75 {
76 }
77
convertTo(unsigned short type,ExceptionCode & ec)78 void XPathResult::convertTo(unsigned short type, ExceptionCode& ec)
79 {
80 switch (type) {
81 case ANY_TYPE:
82 break;
83 case NUMBER_TYPE:
84 m_resultType = type;
85 m_value = m_value.toNumber();
86 break;
87 case STRING_TYPE:
88 m_resultType = type;
89 m_value = m_value.toString();
90 break;
91 case BOOLEAN_TYPE:
92 m_resultType = type;
93 m_value = BOOL_TO_VALUE_CAST (m_value.toBoolean());
94 break;
95 case UNORDERED_NODE_ITERATOR_TYPE:
96 case UNORDERED_NODE_SNAPSHOT_TYPE:
97 case ANY_UNORDERED_NODE_TYPE:
98 case FIRST_ORDERED_NODE_TYPE: // This is correct - singleNodeValue() will take care of ordering.
99 if (!m_value.isNodeSet()) {
100 ec = XPathException::TYPE_ERR;
101 return;
102 }
103 m_resultType = type;
104 break;
105 case ORDERED_NODE_ITERATOR_TYPE:
106 if (!m_value.isNodeSet()) {
107 ec = XPathException::TYPE_ERR;
108 return;
109 }
110 m_nodeSet.sort();
111 m_resultType = type;
112 break;
113 case ORDERED_NODE_SNAPSHOT_TYPE:
114 if (!m_value.isNodeSet()) {
115 ec = XPathException::TYPE_ERR;
116 return;
117 }
118 m_value.toNodeSet().sort();
119 m_resultType = type;
120 break;
121 }
122 }
123
resultType() const124 unsigned short XPathResult::resultType() const
125 {
126 return m_resultType;
127 }
128
numberValue(ExceptionCode & ec) const129 double XPathResult::numberValue(ExceptionCode& ec) const
130 {
131 if (resultType() != NUMBER_TYPE) {
132 ec = XPathException::TYPE_ERR;
133 return 0.0;
134 }
135 return m_value.toNumber();
136 }
137
stringValue(ExceptionCode & ec) const138 String XPathResult::stringValue(ExceptionCode& ec) const
139 {
140 if (resultType() != STRING_TYPE) {
141 ec = XPathException::TYPE_ERR;
142 return String();
143 }
144 return m_value.toString();
145 }
146
booleanValue(ExceptionCode & ec) const147 bool XPathResult::booleanValue(ExceptionCode& ec) const
148 {
149 if (resultType() != BOOLEAN_TYPE) {
150 ec = XPathException::TYPE_ERR;
151 return false;
152 }
153 return m_value.toBoolean();
154 }
155
singleNodeValue(ExceptionCode & ec) const156 Node* XPathResult::singleNodeValue(ExceptionCode& ec) const
157 {
158 if (resultType() != ANY_UNORDERED_NODE_TYPE && resultType() != FIRST_ORDERED_NODE_TYPE) {
159 ec = XPathException::TYPE_ERR;
160 return 0;
161 }
162
163 const NodeSet& nodes = m_value.toNodeSet();
164 if (resultType() == FIRST_ORDERED_NODE_TYPE)
165 return nodes.firstNode();
166 else
167 return nodes.anyNode();
168 }
169
invalidIteratorState() const170 bool XPathResult::invalidIteratorState() const
171 {
172 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_NODE_ITERATOR_TYPE)
173 return false;
174
175 ASSERT(m_document);
176 return m_document->domTreeVersion() != m_domTreeVersion;
177 }
178
snapshotLength(ExceptionCode & ec) const179 unsigned long XPathResult::snapshotLength(ExceptionCode& ec) const
180 {
181 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_NODE_SNAPSHOT_TYPE) {
182 ec = XPathException::TYPE_ERR;
183 return 0;
184 }
185
186 return m_value.toNodeSet().size();
187 }
188
iterateNext(ExceptionCode & ec)189 Node* XPathResult::iterateNext(ExceptionCode& ec)
190 {
191 if (resultType() != UNORDERED_NODE_ITERATOR_TYPE && resultType() != ORDERED_NODE_ITERATOR_TYPE) {
192 ec = XPathException::TYPE_ERR;
193 return 0;
194 }
195
196 if (invalidIteratorState()) {
197 ec = INVALID_STATE_ERR;
198 return 0;
199 }
200
201 if (m_nodeSetPosition + 1 > m_nodeSet.size())
202 return 0;
203
204 Node* node = m_nodeSet[m_nodeSetPosition];
205
206 m_nodeSetPosition++;
207
208 return node;
209 }
210
snapshotItem(unsigned long index,ExceptionCode & ec)211 Node* XPathResult::snapshotItem(unsigned long index, ExceptionCode& ec)
212 {
213 if (resultType() != UNORDERED_NODE_SNAPSHOT_TYPE && resultType() != ORDERED_NODE_SNAPSHOT_TYPE) {
214 ec = XPathException::TYPE_ERR;
215 return 0;
216 }
217
218 const NodeSet& nodes = m_value.toNodeSet();
219 if (index >= nodes.size())
220 return 0;
221
222 return nodes[index];
223 }
224
225 }
226
227 #endif // ENABLE(XPATH)
228