1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 2003
20
* the Initial Developer. All Rights Reserved.
23
* Heikki Toivonen <heikki@netscape.com> (original author)
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
40
* This file implements the xpath1 XPointer scheme, and the xmlns scheme
41
* as well but only for xpath1.
43
* http://www.simonstl.com/ietf/draft-stlaurent-xpath-frag-00.html
46
#include "nsXPath1Scheme.h"
47
#include "nsXPathEvaluator.h"
48
#include "nsXPathException.h"
49
#include "nsDOMError.h"
50
#include "nsXPathResult.h"
51
#include "nsIDOMNode.h"
52
#include "nsIDOMDocument.h"
53
#include "nsIDOMXPathNSResolver.h"
54
#include "nsIDOMRange.h"
55
#include "nsDOMString.h"
56
#include "nsIModifyableXPointer.h"
57
#include "nsAutoPtr.h"
60
#include "nsContentCID.h"
61
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
64
* nsXPath1SchemeNSResolver
66
* This will effectively give us xmlns scheme support.
68
class nsXPath1SchemeNSResolver : public nsIDOMXPathNSResolver
71
nsXPath1SchemeNSResolver(nsIXPointerSchemeContext *aContext)
76
virtual ~nsXPath1SchemeNSResolver()
81
NS_DECL_NSIDOMXPATHNSRESOLVER
84
nsCOMPtr<nsIXPointerSchemeContext> mContext;
87
NS_IMPL_ISUPPORTS1(nsXPath1SchemeNSResolver, nsIDOMXPathNSResolver)
89
//DOMString lookupNamespaceURI(in DOMString prefix);
91
nsXPath1SchemeNSResolver::LookupNamespaceURI(const nsAString &aPrefix,
96
// This method will be called each time the XPath engine encounters
99
// We could cache the extracted prefix + URI pairs in a hash table,
100
// and do a lookup from that first. But typical XPointers only have
101
// a few scheme + data pairs (and only some of those will be xmlns
102
// schemes), and typical XPath expressions only have a few prefixes
103
// as well, so we'll see if we can manage without...
109
NS_NAMED_LITERAL_STRING(xmlns, "xmlns");
112
mContext->GetCount(&count);
114
for (i = 0; i < count; ++i) {
115
nsAutoString scheme, data;
116
mContext->GetSchemeData(i, scheme, data);
117
if (scheme.Equals(xmlns)) {
118
PRInt32 sep = data.FindChar('=');
119
if (sep > 0 && aPrefix.Equals(Substring(data, 0, sep))) {
120
aURI.Assign(Substring(data, sep + 1, data.Length() - sep - 1));
126
SetDOMStringToNull(aURI);
131
// nsXPath1SchemeProcessor
132
nsXPath1SchemeProcessor::nsXPath1SchemeProcessor()
136
nsXPath1SchemeProcessor::~nsXPath1SchemeProcessor()
140
NS_IMPL_ISUPPORTS1(nsXPath1SchemeProcessor, nsIXPointerSchemeProcessor)
145
* @param aDocument The document in which to resolve the XPointer.
146
* @param aContext The XPointer context in which to process aData.
147
* @param aData The data in the scheme that needs to be resolved.
148
* @return The result of the evaluation.
151
nsXPath1SchemeProcessor::Evaluate(nsIDOMDocument *aDocument,
152
nsIXPointerSchemeContext *aContext,
153
const nsAString &aData,
154
nsIXPointerResult **aResult)
156
NS_ENSURE_ARG_POINTER(aDocument);
157
NS_ENSURE_ARG_POINTER(aContext);
158
NS_ENSURE_ARG_POINTER(aResult);
161
// Resolve expression
162
nsCOMPtr<nsIDOMXPathNSResolver> nsresolver(new nsXPath1SchemeNSResolver(aContext));
164
return NS_ERROR_OUT_OF_MEMORY;
166
nsRefPtr<nsXPathEvaluator> e(new nsXPathEvaluator());
168
return NS_ERROR_OUT_OF_MEMORY;
170
nsCOMPtr<nsIDOMXPathResult> result;
171
nsresult rv = e->Evaluate(aData,
174
nsIDOMXPathResult::ORDERED_NODE_ITERATOR_TYPE,
176
getter_AddRefs(result));
178
if ((rv == NS_ERROR_DOM_INVALID_EXPRESSION_ERR) ||
179
(rv == NS_ERROR_DOM_NAMESPACE_ERR) ||
180
(rv == NS_ERROR_DOM_TYPE_ERR)) {
181
// These errors are benign, change them to NS_OK so that
182
// we will not terminate the processor.
188
// Create return result
189
// XXX perf: just store the XPathResult and resolve as XPointerResult on demand
190
nsCOMPtr<nsIXPointerResult> xpointerResult(
191
do_CreateInstance("@mozilla.org/xmlextras/xpointerresult;1", &rv));
196
nsCOMPtr<nsIModifyableXPointerResult> privatePointerResult(do_QueryInterface(xpointerResult));
197
if (!privatePointerResult) {
198
return NS_ERROR_FAILURE;
201
nsCOMPtr<nsIDOMNode> node;
202
rv = result->IterateNext(getter_AddRefs(node));
207
// Fill in return result
209
nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID, &rv));
213
rv = range->SelectNode(node);
217
rv = privatePointerResult->AppendRange(range);
221
rv = result->IterateNext(getter_AddRefs(node));
227
xpointerResult->GetLength(&count);
228
if (NS_SUCCEEDED(rv) && (count > 0)) {
229
*aResult = xpointerResult;