From f3db6b5f5c0747bbf811a0b6a196b8dfcc5d864a Mon Sep 17 00:00:00 2001 From: "vidur%netscape.com" Date: Fri, 14 Sep 2001 20:54:09 +0000 Subject: [PATCH] Adding DOM utility classes. This could get moved to a more general location if we continue to add functionality that is common to multiple modules. For now, it's a schema/wsdl-only thing. This is not yet part of the build. --- .../webservices/schema/src/nsDOMUtils.h | 133 ++++++++++++++++++ extensions/xmlextras/schema/src/nsDOMUtils.h | 133 ++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 extensions/webservices/schema/src/nsDOMUtils.h create mode 100644 extensions/xmlextras/schema/src/nsDOMUtils.h diff --git a/extensions/webservices/schema/src/nsDOMUtils.h b/extensions/webservices/schema/src/nsDOMUtils.h new file mode 100644 index 00000000000..54f13277e01 --- /dev/null +++ b/extensions/webservices/schema/src/nsDOMUtils.h @@ -0,0 +1,133 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Vidur Apparao (original author) + */ + +#ifndef _nsDOMUtils_h__ +#define _nsDOMUtils_h__ + +// content includes +#include "nsIContent.h" +#include "nsINameSpaceManager.h" +#include "nsINodeInfo.h" +#include "nsIDOMElement.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" + +// string includes +#include "nsString.h" +#include "nsReadableUtils.h" + +// collection includes +#include "nsVoidArray.h" + +//////////////////////////////////////////////////////////// +// +// nsChildElementIterator +// +//////////////////////////////////////////////////////////// + +class nsChildElementIterator { +private: + nsCOMPtr mNodeList; + PRUint32 mLength; + PRUint32 mIndex; + nsString mNamespace; + +public: + nsChildElementIterator(nsIDOMElement* aParent) : + mIndex(0), mLength(0) + { + SetElement(aParent); + } + + nsChildElementIterator(nsIDOMElement* aParent, + const nsAReadableString& aNamespace) : + mIndex(0), mLength(0), mNamespace(aNamespace) + { + SetElement(aParent); + } + + ~nsChildElementIterator() {} + + void SetElement(nsIDOMElement* aParent) + { + aParent->GetChildNodes(getter_AddRefs(mNodeList)); + if (mNodeList) { + mNodeList->GetLength(&mLength); + } + } + + PRUint32 GetCurrentIndex() { return mIndex; } + + void Reset(PRUint32 aIndex=0) { mIndex = aIndex; } + + PRBool HasChildNodes() { return mLength; } + + nsresult GetNextChild(nsIDOMElement** aChildElement, + nsIAtom** aElementName) + { + *aChildElement = nsnull; + + if (!mNodeList) { + return NS_ERROR_FAILURE; + } + + nsCOMPtr child; + while (mIndex < mLength) { + mNodeList->Item(mIndex++, getter_AddRefs(child)); + nsCOMPtr childElement(do_QueryInterface(child)); + if (!childElement) { + continue; + } + + // Confirm that the element is an element of the specified namespace + if (!mNamespace.IsEmpty()) { + nsAutoString namespaceURI; + childElement->GetNamespaceURI(namespaceURI); + if (!namespaceURI.Equals(mNamespace)) { + continue; + } + } + + nsCOMPtr content(do_QueryInterface(childElement)); + NS_ASSERTION(content, "Element is not content"); + if (!content) { + return NS_ERROR_FAILURE; + } + + nsCOMPtr nodeInfo; + content->GetNodeInfo(*getter_AddRefs(nodeInfo)); + if (!nodeInfo) { + return NS_ERROR_FAILURE; + } + + nodeInfo->GetNameAtom(*aElementName); + *aChildElement = childElement; + NS_ADDREF(*aChildElement); + break; + } + + return NS_OK; + } +}; + +#endif // _nsDOMUtils_h__ diff --git a/extensions/xmlextras/schema/src/nsDOMUtils.h b/extensions/xmlextras/schema/src/nsDOMUtils.h new file mode 100644 index 00000000000..54f13277e01 --- /dev/null +++ b/extensions/xmlextras/schema/src/nsDOMUtils.h @@ -0,0 +1,133 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Vidur Apparao (original author) + */ + +#ifndef _nsDOMUtils_h__ +#define _nsDOMUtils_h__ + +// content includes +#include "nsIContent.h" +#include "nsINameSpaceManager.h" +#include "nsINodeInfo.h" +#include "nsIDOMElement.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" + +// string includes +#include "nsString.h" +#include "nsReadableUtils.h" + +// collection includes +#include "nsVoidArray.h" + +//////////////////////////////////////////////////////////// +// +// nsChildElementIterator +// +//////////////////////////////////////////////////////////// + +class nsChildElementIterator { +private: + nsCOMPtr mNodeList; + PRUint32 mLength; + PRUint32 mIndex; + nsString mNamespace; + +public: + nsChildElementIterator(nsIDOMElement* aParent) : + mIndex(0), mLength(0) + { + SetElement(aParent); + } + + nsChildElementIterator(nsIDOMElement* aParent, + const nsAReadableString& aNamespace) : + mIndex(0), mLength(0), mNamespace(aNamespace) + { + SetElement(aParent); + } + + ~nsChildElementIterator() {} + + void SetElement(nsIDOMElement* aParent) + { + aParent->GetChildNodes(getter_AddRefs(mNodeList)); + if (mNodeList) { + mNodeList->GetLength(&mLength); + } + } + + PRUint32 GetCurrentIndex() { return mIndex; } + + void Reset(PRUint32 aIndex=0) { mIndex = aIndex; } + + PRBool HasChildNodes() { return mLength; } + + nsresult GetNextChild(nsIDOMElement** aChildElement, + nsIAtom** aElementName) + { + *aChildElement = nsnull; + + if (!mNodeList) { + return NS_ERROR_FAILURE; + } + + nsCOMPtr child; + while (mIndex < mLength) { + mNodeList->Item(mIndex++, getter_AddRefs(child)); + nsCOMPtr childElement(do_QueryInterface(child)); + if (!childElement) { + continue; + } + + // Confirm that the element is an element of the specified namespace + if (!mNamespace.IsEmpty()) { + nsAutoString namespaceURI; + childElement->GetNamespaceURI(namespaceURI); + if (!namespaceURI.Equals(mNamespace)) { + continue; + } + } + + nsCOMPtr content(do_QueryInterface(childElement)); + NS_ASSERTION(content, "Element is not content"); + if (!content) { + return NS_ERROR_FAILURE; + } + + nsCOMPtr nodeInfo; + content->GetNodeInfo(*getter_AddRefs(nodeInfo)); + if (!nodeInfo) { + return NS_ERROR_FAILURE; + } + + nodeInfo->GetNameAtom(*aElementName); + *aChildElement = childElement; + NS_ADDREF(*aChildElement); + break; + } + + return NS_OK; + } +}; + +#endif // _nsDOMUtils_h__