~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/transformiix/source/xpath/nsXPath1Scheme.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Heikki Toivonen <heikki@netscape.com> (original author)
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
/**
 
40
 * This file implements the xpath1 XPointer scheme, and the xmlns scheme
 
41
 * as well but only for xpath1.
 
42
 *
 
43
 * http://www.simonstl.com/ietf/draft-stlaurent-xpath-frag-00.html
 
44
 */
 
45
 
 
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"
 
58
#include "nsString.h"
 
59
 
 
60
#include "nsContentCID.h"
 
61
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
 
62
 
 
63
/**
 
64
 * nsXPath1SchemeNSResolver
 
65
 *
 
66
 * This will effectively give us xmlns scheme support.
 
67
 */
 
68
class nsXPath1SchemeNSResolver : public nsIDOMXPathNSResolver
 
69
{
 
70
public:
 
71
  nsXPath1SchemeNSResolver(nsIXPointerSchemeContext *aContext)
 
72
    : mContext(aContext)
 
73
  {
 
74
  }
 
75
  
 
76
  virtual ~nsXPath1SchemeNSResolver()
 
77
  {
 
78
  }
 
79
 
 
80
  NS_DECL_ISUPPORTS
 
81
  NS_DECL_NSIDOMXPATHNSRESOLVER
 
82
 
 
83
private:
 
84
  nsCOMPtr<nsIXPointerSchemeContext> mContext;
 
85
};
 
86
 
 
87
NS_IMPL_ISUPPORTS1(nsXPath1SchemeNSResolver, nsIDOMXPathNSResolver)
 
88
 
 
89
//DOMString          lookupNamespaceURI(in DOMString prefix);
 
90
NS_IMETHODIMP
 
91
nsXPath1SchemeNSResolver::LookupNamespaceURI(const nsAString &aPrefix,
 
92
                                             nsAString &aURI)
 
93
{
 
94
  aURI.Truncate();
 
95
 
 
96
  // This method will be called each time the XPath engine encounters
 
97
  // a prefix.
 
98
  
 
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...
 
104
 
 
105
  if (!mContext) {
 
106
    return NS_OK;
 
107
  }
 
108
 
 
109
  NS_NAMED_LITERAL_STRING(xmlns, "xmlns");
 
110
 
 
111
  PRUint32 count;
 
112
  mContext->GetCount(&count);
 
113
  PRUint32 i;
 
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));
 
121
        return NS_OK;
 
122
      }
 
123
    }
 
124
  }
 
125
 
 
126
  SetDOMStringToNull(aURI);
 
127
 
 
128
  return NS_OK;
 
129
}
 
130
 
 
131
// nsXPath1SchemeProcessor
 
132
nsXPath1SchemeProcessor::nsXPath1SchemeProcessor()
 
133
{
 
134
}
 
135
 
 
136
nsXPath1SchemeProcessor::~nsXPath1SchemeProcessor()
 
137
{
 
138
}
 
139
 
 
140
NS_IMPL_ISUPPORTS1(nsXPath1SchemeProcessor, nsIXPointerSchemeProcessor)
 
141
 
 
142
/**
 
143
 * Evaluate.
 
144
 *
 
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.
 
149
 */
 
150
NS_IMETHODIMP
 
151
nsXPath1SchemeProcessor::Evaluate(nsIDOMDocument *aDocument,
 
152
                         nsIXPointerSchemeContext *aContext,
 
153
                         const nsAString &aData,
 
154
                         nsIXPointerResult **aResult)
 
155
{
 
156
  NS_ENSURE_ARG_POINTER(aDocument);
 
157
  NS_ENSURE_ARG_POINTER(aContext);
 
158
  NS_ENSURE_ARG_POINTER(aResult);
 
159
  *aResult = nsnull;
 
160
 
 
161
  // Resolve expression
 
162
  nsCOMPtr<nsIDOMXPathNSResolver> nsresolver(new nsXPath1SchemeNSResolver(aContext));
 
163
  if (!nsresolver) {
 
164
    return NS_ERROR_OUT_OF_MEMORY;
 
165
  }
 
166
  nsRefPtr<nsXPathEvaluator> e(new nsXPathEvaluator());
 
167
  if (!e) {
 
168
    return NS_ERROR_OUT_OF_MEMORY;
 
169
  }
 
170
  nsCOMPtr<nsIDOMXPathResult> result;
 
171
  nsresult rv = e->Evaluate(aData,
 
172
                            aDocument,
 
173
                            nsresolver,
 
174
                            nsIDOMXPathResult::ORDERED_NODE_ITERATOR_TYPE,
 
175
                            nsnull,
 
176
                            getter_AddRefs(result));
 
177
  if (NS_FAILED(rv)) {
 
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.
 
183
      rv = NS_OK;
 
184
    }
 
185
    return rv;
 
186
  }
 
187
 
 
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));
 
192
  if (NS_FAILED(rv)) {
 
193
    return rv;
 
194
  }
 
195
 
 
196
  nsCOMPtr<nsIModifyableXPointerResult> privatePointerResult(do_QueryInterface(xpointerResult));
 
197
  if (!privatePointerResult) {
 
198
    return NS_ERROR_FAILURE;
 
199
  }
 
200
 
 
201
  nsCOMPtr<nsIDOMNode> node;
 
202
  rv = result->IterateNext(getter_AddRefs(node));
 
203
  if (NS_FAILED(rv)) {
 
204
    return rv;
 
205
  }
 
206
 
 
207
  // Fill in return result
 
208
  while (node) {
 
209
    nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID, &rv));
 
210
    if (NS_FAILED(rv))
 
211
      break;
 
212
 
 
213
    rv = range->SelectNode(node);
 
214
    if (NS_FAILED(rv))
 
215
      break;
 
216
 
 
217
    rv = privatePointerResult->AppendRange(range);
 
218
    if (NS_FAILED(rv))
 
219
      break;
 
220
 
 
221
    rv = result->IterateNext(getter_AddRefs(node));
 
222
    if (NS_FAILED(rv))
 
223
      break;
 
224
  }
 
225
 
 
226
  PRUint32 count;
 
227
  xpointerResult->GetLength(&count);
 
228
  if (NS_SUCCEEDED(rv) && (count > 0)) {
 
229
    *aResult = xpointerResult;
 
230
    NS_ADDREF(*aResult);
 
231
  }
 
232
 
 
233
  return rv;
 
234
}