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

« back to all changes in this revision

Viewing changes to mozilla/extensions/webservices/schema/src/nsDOMUtils.h

  • 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
/*
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is Mozilla.
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications.  Portions created by Netscape Communications are
 
17
 * Copyright (C) 2001 by Netscape Communications.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): 
 
21
 *   Vidur Apparao <vidur@netscape.com> (original author)
 
22
 */
 
23
 
 
24
#ifndef _nsDOMUtils_h__
 
25
#define _nsDOMUtils_h__
 
26
 
 
27
// content includes
 
28
#include "nsIContent.h"
 
29
#include "nsINodeInfo.h"
 
30
#include "nsIDOMElement.h"
 
31
#include "nsIDOMNode.h"
 
32
#include "nsIDOMNodeList.h"
 
33
 
 
34
// string includes
 
35
#include "nsString.h"
 
36
#include "nsReadableUtils.h"
 
37
 
 
38
// collection includes
 
39
#include "nsVoidArray.h"
 
40
 
 
41
////////////////////////////////////////////////////////////
 
42
//
 
43
// nsChildElementIterator 
 
44
//
 
45
////////////////////////////////////////////////////////////
 
46
 
 
47
class nsChildElementIterator {
 
48
private:
 
49
  nsCOMPtr<nsIDOMNodeList> mNodeList;
 
50
  PRUint32 mIndex;
 
51
  PRUint32 mLength;
 
52
  nsString mNamespace;
 
53
  const char** mNamespaceArray;
 
54
  PRUint32 mNumNamespaces;
 
55
 
 
56
public:
 
57
  nsChildElementIterator(nsIDOMElement* aParent) :
 
58
    mIndex(0), mLength(0), mNumNamespaces(0)
 
59
  {
 
60
    SetElement(aParent);
 
61
  }    
 
62
  
 
63
  nsChildElementIterator(nsIDOMElement* aParent,
 
64
                         const nsAString& aNamespace) :
 
65
    mIndex(0), mLength(0), mNamespace(aNamespace), mNumNamespaces(0)
 
66
  {
 
67
    SetElement(aParent);
 
68
  }
 
69
 
 
70
  nsChildElementIterator(nsIDOMElement* aParent,
 
71
                         const char** aNamespaceArray,
 
72
                         PRUint32 aNumNamespaces) :
 
73
    mIndex(0), mLength(0), mNamespaceArray(aNamespaceArray), 
 
74
    mNumNamespaces(aNumNamespaces)
 
75
  {
 
76
    SetElement(aParent);    
 
77
  }
 
78
 
 
79
  ~nsChildElementIterator() {}
 
80
 
 
81
  void SetElement(nsIDOMElement* aParent) 
 
82
  {
 
83
    aParent->GetChildNodes(getter_AddRefs(mNodeList));
 
84
    if (mNodeList) {
 
85
      mNodeList->GetLength(&mLength);
 
86
    }    
 
87
  }
 
88
 
 
89
  PRUint32 GetCurrentIndex() { return mIndex; }
 
90
 
 
91
  void Reset(PRUint32 aIndex=0) { mIndex = aIndex; }
 
92
 
 
93
  PRBool HasChildNodes() { return mLength; }
 
94
 
 
95
  nsresult GetNextChild(nsIDOMElement** aChildElement,
 
96
                        nsIAtom** aElementName)
 
97
  {
 
98
    *aChildElement = nsnull;
 
99
    
 
100
    if (!mNodeList) {
 
101
      return NS_ERROR_FAILURE;
 
102
    }
 
103
 
 
104
    nsCOMPtr<nsIDOMNode> child; 
 
105
    while (mIndex < mLength) {
 
106
      mNodeList->Item(mIndex++, getter_AddRefs(child));
 
107
      nsCOMPtr<nsIDOMElement> childElement(do_QueryInterface(child));
 
108
      if (!childElement) {
 
109
        continue;
 
110
      }
 
111
      
 
112
      // Confirm that the element is an element of the specified namespace
 
113
      nsAutoString namespaceURI;           
 
114
      childElement->GetNamespaceURI(namespaceURI);  
 
115
      if (!mNamespace.IsEmpty()) {
 
116
        if (!namespaceURI.Equals(mNamespace)) {
 
117
          continue;
 
118
        }
 
119
      }
 
120
      else if (mNumNamespaces) {
 
121
        PRUint32 i;
 
122
        for (i = 0; i < mNumNamespaces; i++) {
 
123
          if (!namespaceURI.Equals(NS_ConvertASCIItoUCS2(mNamespaceArray[i]))) {
 
124
            continue;
 
125
          }
 
126
        }
 
127
      }
 
128
      
 
129
      nsCOMPtr<nsIContent> content(do_QueryInterface(childElement));
 
130
      NS_ASSERTION(content, "Element is not content");
 
131
      if (!content) {
 
132
        return NS_ERROR_FAILURE;
 
133
      }
 
134
      
 
135
      nsINodeInfo *nodeInfo = content->GetNodeInfo();
 
136
      if (!nodeInfo) {
 
137
        return NS_ERROR_FAILURE;
 
138
      }
 
139
 
 
140
      NS_ADDREF(*aElementName = nodeInfo->NameAtom());
 
141
 
 
142
      *aChildElement = childElement;
 
143
      NS_ADDREF(*aChildElement);
 
144
      break;
 
145
    }
 
146
    
 
147
    return NS_OK;
 
148
  }
 
149
};
 
150
 
 
151
#endif // _nsDOMUtils_h__