View Javadoc
1   /*
2    * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.org.apache.xerces.internal.util;
27  
28  //java imports
29  import java.util.Iterator ;
30  import java.util.NoSuchElementException;
31  
32  //xerces imports
33  import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl ;
34  
35  /**
36   *
37   * @author  Neeraj Bajaj, Sun Microsystems
38   */
39  
40  /**
41   * Its better to extend the functionality of existing XMLAttributesImpl and also make it of type Iterator.
42   * We can directly  give an object of type iterator from StartElement event. We should also have
43   * Attribute object of type javax.xml.stream.Attribute internally. It would avoid the need of creating
44   * new javax.xml.stream.Attribute object at the later stage.
45   *
46   * Should we change XMLAttributes interface to implement Iteraotr ? I think its better avoid touching XNI as
47   * much as possible. - NB.
48   */
49  
50  public class XMLAttributesIteratorImpl extends XMLAttributesImpl implements Iterator {
51  
52      //pointer to current position.
53      protected int fCurrent = 0 ;
54  
55      protected XMLAttributesImpl.Attribute fLastReturnedItem ;
56  
57      /** Creates a new instance of XMLAttributesIteratorImpl */
58      public XMLAttributesIteratorImpl() {
59      }
60  
61      public boolean hasNext() {
62          return fCurrent < getLength() ? true : false ;
63      }//hasNext()
64  
65      public Object next() {
66          if(hasNext()){
67              // should this be of type javax.xml.stream.Attribute ?
68              return fLastReturnedItem = fAttributes[fCurrent++] ;
69          }
70          else{
71              throw new NoSuchElementException() ;
72          }
73      }//next
74  
75      public void remove() {
76          //make sure that only last returned item can be removed.
77          if(fLastReturnedItem == fAttributes[fCurrent - 1]){
78              //remove the attribute at current index and lower the current position by 1.
79              removeAttributeAt(fCurrent--) ;
80          }
81          else {
82              //either the next method has been called yet, or the remove method has already been called
83              //after the last call to the next method.
84              throw new IllegalStateException();
85          }
86      }//remove
87  
88      public void removeAllAttributes() {
89          super.removeAllAttributes() ;
90          fCurrent = 0 ;
91      }
92      /** xxx: should we be doing this way ? Attribute event defines so many functions which doesn't make any sense
93       *for Attribute.
94       *
95       */
96      /*
97      class AttributeImpl extends com.sun.org.apache.xerces.internal.util.XMLAttributesImpl.Attribute implements javax.xml.stream.events.Attribute{
98  
99      }
100      */
101 
102 } //XMLAttributesIteratorImpl