Back to index

lightning-sunbird  0.9+nobinonly
nsXPath1Scheme.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Heikki Toivonen <heikki@netscape.com> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00046 #include "nsXPath1Scheme.h"
00047 #include "nsXPathEvaluator.h"
00048 #include "nsXPathException.h"
00049 #include "nsDOMError.h"
00050 #include "nsXPathResult.h"
00051 #include "nsIDOMNode.h"
00052 #include "nsIDOMDocument.h"
00053 #include "nsIDOMXPathNSResolver.h"
00054 #include "nsIDOMRange.h"
00055 #include "nsDOMString.h"
00056 #include "nsIModifyableXPointer.h"
00057 #include "nsAutoPtr.h"
00058 #include "nsString.h"
00059 
00060 #include "nsContentCID.h"
00061 static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
00062 
00068 class nsXPath1SchemeNSResolver : public nsIDOMXPathNSResolver
00069 {
00070 public:
00071   nsXPath1SchemeNSResolver(nsIXPointerSchemeContext *aContext)
00072     : mContext(aContext)
00073   {
00074   }
00075   
00076   virtual ~nsXPath1SchemeNSResolver()
00077   {
00078   }
00079 
00080   NS_DECL_ISUPPORTS
00081   NS_DECL_NSIDOMXPATHNSRESOLVER
00082 
00083 private:
00084   nsCOMPtr<nsIXPointerSchemeContext> mContext;
00085 };
00086 
00087 NS_IMPL_ISUPPORTS1(nsXPath1SchemeNSResolver, nsIDOMXPathNSResolver)
00088 
00089 //DOMString          lookupNamespaceURI(in DOMString prefix);
00090 NS_IMETHODIMP
00091 nsXPath1SchemeNSResolver::LookupNamespaceURI(const nsAString &aPrefix,
00092                                              nsAString &aURI)
00093 {
00094   aURI.Truncate();
00095 
00096   // This method will be called each time the XPath engine encounters
00097   // a prefix.
00098   
00099   // We could cache the extracted prefix + URI pairs in a hash table,
00100   // and do a lookup from that first. But typical XPointers only have
00101   // a few scheme + data pairs (and only some of those will be xmlns
00102   // schemes), and typical XPath expressions only have a few prefixes
00103   // as well, so we'll see if we can manage without...
00104 
00105   if (!mContext) {
00106     return NS_OK;
00107   }
00108 
00109   NS_NAMED_LITERAL_STRING(xmlns, "xmlns");
00110 
00111   PRUint32 count;
00112   mContext->GetCount(&count);
00113   PRUint32 i;
00114   for (i = 0; i < count; ++i) {
00115     nsAutoString scheme, data;
00116     mContext->GetSchemeData(i, scheme, data);
00117     if (scheme.Equals(xmlns)) {
00118       PRInt32 sep = data.FindChar('=');
00119       if (sep > 0 && aPrefix.Equals(Substring(data, 0, sep))) {
00120         aURI.Assign(Substring(data, sep + 1, data.Length() - sep - 1));
00121         return NS_OK;
00122       }
00123     }
00124   }
00125 
00126   SetDOMStringToNull(aURI);
00127 
00128   return NS_OK;
00129 }
00130 
00131 // nsXPath1SchemeProcessor
00132 nsXPath1SchemeProcessor::nsXPath1SchemeProcessor()
00133 {
00134 }
00135 
00136 nsXPath1SchemeProcessor::~nsXPath1SchemeProcessor()
00137 {
00138 }
00139 
00140 NS_IMPL_ISUPPORTS1(nsXPath1SchemeProcessor, nsIXPointerSchemeProcessor)
00141 
00142 
00150 NS_IMETHODIMP
00151 nsXPath1SchemeProcessor::Evaluate(nsIDOMDocument *aDocument,
00152                          nsIXPointerSchemeContext *aContext,
00153                          const nsAString &aData,
00154                          nsIXPointerResult **aResult)
00155 {
00156   NS_ENSURE_ARG_POINTER(aDocument);
00157   NS_ENSURE_ARG_POINTER(aContext);
00158   NS_ENSURE_ARG_POINTER(aResult);
00159   *aResult = nsnull;
00160  
00161   // Resolve expression
00162   nsCOMPtr<nsIDOMXPathNSResolver> nsresolver(new nsXPath1SchemeNSResolver(aContext));
00163   if (!nsresolver) {
00164     return NS_ERROR_OUT_OF_MEMORY;
00165   }
00166   nsRefPtr<nsXPathEvaluator> e(new nsXPathEvaluator());
00167   if (!e) {
00168     return NS_ERROR_OUT_OF_MEMORY;
00169   }
00170   nsCOMPtr<nsIDOMXPathResult> result;
00171   nsresult rv = e->Evaluate(aData,
00172                             aDocument,
00173                             nsresolver,
00174                             nsIDOMXPathResult::ORDERED_NODE_ITERATOR_TYPE,
00175                             nsnull,
00176                             getter_AddRefs(result));
00177   if (NS_FAILED(rv)) {
00178     if ((rv == NS_ERROR_DOM_INVALID_EXPRESSION_ERR) ||
00179         (rv == NS_ERROR_DOM_NAMESPACE_ERR) ||
00180         (rv == NS_ERROR_DOM_TYPE_ERR)) {
00181       // These errors are benign, change them to NS_OK so that
00182       // we will not terminate the processor.
00183       rv = NS_OK;
00184     }
00185     return rv;
00186   }
00187 
00188   // Create return result
00189   // XXX perf: just store the XPathResult and resolve as XPointerResult on demand
00190   nsCOMPtr<nsIXPointerResult> xpointerResult(
00191     do_CreateInstance("@mozilla.org/xmlextras/xpointerresult;1", &rv));
00192   if (NS_FAILED(rv)) {
00193     return rv;
00194   }
00195 
00196   nsCOMPtr<nsIModifyableXPointerResult> privatePointerResult(do_QueryInterface(xpointerResult));
00197   if (!privatePointerResult) {
00198     return NS_ERROR_FAILURE;
00199   }
00200 
00201   nsCOMPtr<nsIDOMNode> node;
00202   rv = result->IterateNext(getter_AddRefs(node));
00203   if (NS_FAILED(rv)) {
00204     return rv;
00205   }
00206 
00207   // Fill in return result
00208   while (node) {
00209     nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID, &rv));
00210     if (NS_FAILED(rv))
00211       break;
00212 
00213     rv = range->SelectNode(node);
00214     if (NS_FAILED(rv))
00215       break;
00216 
00217     rv = privatePointerResult->AppendRange(range);
00218     if (NS_FAILED(rv))
00219       break;
00220 
00221     rv = result->IterateNext(getter_AddRefs(node));
00222     if (NS_FAILED(rv))
00223       break;
00224   }
00225 
00226   PRUint32 count;
00227   xpointerResult->GetLength(&count);
00228   if (NS_SUCCEEDED(rv) && (count > 0)) {
00229     *aResult = xpointerResult;
00230     NS_ADDREF(*aResult);
00231   }
00232 
00233   return rv;
00234 }