Back to index

lightning-sunbird  0.9+nobinonly
txKey.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * Jonas Sicking.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jonas Sicking <jonas@sicking.cc>
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 
00039 #ifndef txKey_h__
00040 #define txKey_h__
00041 
00042 #include "nsDoubleHashtable.h"
00043 #include "txNodeSet.h"
00044 #include "txList.h"
00045 #include "txXSLTPatterns.h"
00046 #include "XMLUtils.h"
00047 
00048 class txPattern;
00049 class Expr;
00050 class txExecutionState;
00051 
00052 class txKeyValueHashKey
00053 {
00054 public:
00055     txKeyValueHashKey(const txExpandedName& aKeyName,
00056                       PRInt32 aDocumentIdentifier,
00057                       const nsAString& aKeyValue)
00058         : mKeyName(aKeyName),
00059           mKeyValue(aKeyValue),
00060           mDocumentIdentifier(aDocumentIdentifier)
00061     {
00062     }
00063 
00064     txExpandedName mKeyName;
00065     nsString mKeyValue;
00066     PRInt32 mDocumentIdentifier;
00067 };
00068 
00069 struct txKeyValueHashEntry : public PLDHashEntryHdr
00070 {
00071     txKeyValueHashEntry(const void* aKey)
00072         : mKey(*NS_STATIC_CAST(const txKeyValueHashKey*, aKey)),
00073           mNodeSet(new txNodeSet(nsnull))
00074     {
00075     }
00076 
00077     // @see nsDoubleHashtable.h
00078     const void* GetKey();
00079     PRBool MatchEntry(const void* aKey) const;
00080     static PLDHashNumber HashKey(const void* aKey);
00081     
00082     txKeyValueHashKey mKey;
00083     nsRefPtr<txNodeSet> mNodeSet;
00084 };
00085 
00086 DECL_DHASH_WRAPPER(txKeyValueHash, txKeyValueHashEntry, txKeyValueHashKey&)
00087 
00088 class txIndexedKeyHashKey
00089 {
00090 public:
00091     txIndexedKeyHashKey(txExpandedName aKeyName,
00092                         PRInt32 aDocumentIdentifier)
00093         : mKeyName(aKeyName),
00094           mDocumentIdentifier(aDocumentIdentifier)
00095     {
00096     }
00097 
00098     txExpandedName mKeyName;
00099     PRInt32 mDocumentIdentifier;
00100 };
00101 
00102 struct txIndexedKeyHashEntry : public PLDHashEntryHdr
00103 {
00104     txIndexedKeyHashEntry(const void* aKey)
00105         : mKey(*NS_STATIC_CAST(const txIndexedKeyHashKey*, aKey)),
00106           mIndexed(PR_FALSE)
00107     {
00108     }
00109 
00110     // @see nsDoubleHashtable.h
00111     const void* GetKey();
00112     PRBool MatchEntry(const void* aKey) const;
00113     static PLDHashNumber HashKey(const void* aKey);
00114 
00115     txIndexedKeyHashKey mKey;
00116     PRBool mIndexed;
00117 };
00118 
00119 DECL_DHASH_WRAPPER(txIndexedKeyHash, txIndexedKeyHashEntry,
00120                    txIndexedKeyHashKey&)
00121 
00122 
00126 class txXSLKey : public TxObject {
00127     
00128 public:
00129     txXSLKey(const txExpandedName& aName) : mName(aName)
00130     {
00131     }
00132     ~txXSLKey();
00133     
00140     PRBool addKey(nsAutoPtr<txPattern> aMatch, nsAutoPtr<Expr> aUse);
00141 
00148     nsresult indexDocument(const txXPathNode& aDocument,
00149                            txKeyValueHash& aKeyValueHash,
00150                            txExecutionState& aEs);
00151 
00152 private:
00161     nsresult indexTree(const txXPathNode& aNode, txKeyValueHashKey& aKey,
00162                        txKeyValueHash& aKeyValueHash, txExecutionState& aEs);
00163 
00172     nsresult testNode(const txXPathNode& aNode, txKeyValueHashKey& aKey,
00173                       txKeyValueHash& aKeyValueHash, txExecutionState& aEs);
00174 
00178     struct Key {
00179         nsAutoPtr<txPattern> matchPattern;
00180         nsAutoPtr<Expr> useExpr;
00181     };
00182 
00186     List mKeys;
00187     
00191     txExpandedName mName;
00192 };
00193 
00194 
00195 class txKeyHash
00196 {
00197 public:
00198     txKeyHash(const txExpandedNameMap& aKeys)
00199         : mKeys(aKeys)
00200     {
00201     }
00202     
00203     nsresult init();
00204 
00205     nsresult getKeyNodes(const txExpandedName& aKeyName,
00206                          const txXPathNode& aDocument,
00207                          const nsAString& aKeyValue,
00208                          PRBool aIndexIfNotFound,
00209                          txExecutionState& aEs,
00210                          txNodeSet** aResult);
00211 
00212 private:
00213     // Hash of all indexed key-values
00214     txKeyValueHash mKeyValues;
00215 
00216     // Hash showing which keys+documents has been indexed
00217     txIndexedKeyHash mIndexedKeys;
00218     
00219     // Map of txXSLKeys
00220     const txExpandedNameMap& mKeys;
00221     
00222     // Empty nodeset returned if no key is found
00223     nsRefPtr<txNodeSet> mEmptyNodeSet;
00224 };
00225 
00226 
00227 #endif //txKey_h__