Back to index

lightning-sunbird  0.9+nobinonly
nsDOMUtils.h
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 Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vidur Apparao <vidur@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 
00039 #ifndef _nsDOMUtils_h__
00040 #define _nsDOMUtils_h__
00041 
00042 // content includes
00043 #include "nsIContent.h"
00044 #include "nsINodeInfo.h"
00045 #include "nsIDOMElement.h"
00046 #include "nsIDOMNode.h"
00047 #include "nsIDOMNodeList.h"
00048 
00049 // string includes
00050 #include "nsString.h"
00051 #include "nsReadableUtils.h"
00052 
00053 // collection includes
00054 #include "nsVoidArray.h"
00055 
00057 //
00058 // nsChildElementIterator 
00059 //
00061 
00062 class nsChildElementIterator {
00063 private:
00064   nsCOMPtr<nsIDOMNodeList> mNodeList;
00065   PRUint32 mIndex;
00066   PRUint32 mLength;
00067   nsString mNamespace;
00068   const char** mNamespaceArray;
00069   PRUint32 mNumNamespaces;
00070 
00071 public:
00072   nsChildElementIterator(nsIDOMElement* aParent) :
00073     mIndex(0), mLength(0), mNumNamespaces(0)
00074   {
00075     SetElement(aParent);
00076   }    
00077   
00078   nsChildElementIterator(nsIDOMElement* aParent,
00079                          const nsAString& aNamespace) :
00080     mIndex(0), mLength(0), mNamespace(aNamespace), mNumNamespaces(0)
00081   {
00082     SetElement(aParent);
00083   }
00084 
00085   nsChildElementIterator(nsIDOMElement* aParent,
00086                          const char** aNamespaceArray,
00087                          PRUint32 aNumNamespaces) :
00088     mIndex(0), mLength(0), mNamespaceArray(aNamespaceArray), 
00089     mNumNamespaces(aNumNamespaces)
00090   {
00091     SetElement(aParent);    
00092   }
00093 
00094   ~nsChildElementIterator() {}
00095 
00096   void SetElement(nsIDOMElement* aParent) 
00097   {
00098     aParent->GetChildNodes(getter_AddRefs(mNodeList));
00099     if (mNodeList) {
00100       mNodeList->GetLength(&mLength);
00101       // if we change the element, reset the index
00102       mIndex = 0;
00103     }    
00104   }
00105 
00106   PRUint32 GetCurrentIndex() { return mIndex; }
00107 
00108   void Reset(PRUint32 aIndex=0) { mIndex = aIndex; }
00109 
00110   PRBool HasChildNodes() { return mLength; }
00111 
00112   nsresult GetNextChild(nsIDOMElement** aChildElement,
00113                         nsIAtom** aElementName)
00114   {
00115     *aChildElement = nsnull;
00116     
00117     if (!mNodeList) {
00118       return NS_ERROR_FAILURE;
00119     }
00120 
00121     nsCOMPtr<nsIDOMNode> child; 
00122     while (mIndex < mLength) {
00123       mNodeList->Item(mIndex++, getter_AddRefs(child));
00124       nsCOMPtr<nsIDOMElement> childElement(do_QueryInterface(child));
00125       if (!childElement) {
00126         continue;
00127       }
00128       
00129       // Confirm that the element is an element of the specified namespace
00130       nsAutoString namespaceURI;           
00131       childElement->GetNamespaceURI(namespaceURI);  
00132       if (!mNamespace.IsEmpty()) {
00133         if (!namespaceURI.Equals(mNamespace)) {
00134           continue;
00135         }
00136       }
00137       else if (mNumNamespaces) {
00138         PRUint32 i;
00139         for (i = 0; i < mNumNamespaces; i++) {
00140           if (!namespaceURI.Equals(NS_ConvertASCIItoUCS2(mNamespaceArray[i]))) {
00141             continue;
00142           }
00143         }
00144       }
00145       
00146       nsCOMPtr<nsIContent> content(do_QueryInterface(childElement));
00147       NS_ASSERTION(content, "Element is not content");
00148       if (!content) {
00149         return NS_ERROR_FAILURE;
00150       }
00151       
00152       nsINodeInfo *nodeInfo = content->GetNodeInfo();
00153       if (!nodeInfo) {
00154         return NS_ERROR_FAILURE;
00155       }
00156 
00157       NS_ADDREF(*aElementName = nodeInfo->NameAtom());
00158 
00159       *aChildElement = childElement;
00160       NS_ADDREF(*aChildElement);
00161       break;
00162     }
00163     
00164     return NS_OK;
00165   }
00166 };
00167 
00168 #endif // _nsDOMUtils_h__