View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import javax.xml.namespace.QName;
19  import com.bea.xml.stream.util.EmptyIterator;
20  import com.bea.xml.stream.util.ElementTypeNames;
21  import javax.xml.stream.events.XMLEvent;
22  import javax.xml.stream.XMLStreamReader;
23  import javax.xml.stream.XMLStreamException;
24  import java.util.List;
25  
26  /***
27   * <p> Creates a SubReader over a node of a document </p>
28   */
29  
30  public class SubReader extends ReaderDelegate {
31    private int depth=0;
32    private boolean open=true;
33    public SubReader(XMLStreamReader reader) 
34      throws XMLStreamException
35    {
36      super(reader);
37      if (!reader.isStartElement())
38        throw new XMLStreamException("Unable to instantiate a subReader "+
39                                     "because the underlying reader "+
40                                     "was not on a start element.");
41      open = true;
42      depth++;
43    }
44  
45    public int next() 
46      throws XMLStreamException 
47    {
48      if (depth <= 0) open = false;
49      int type = super.next();
50      if (isStartElement()) depth++;
51      if (isEndElement()) {
52        depth--;
53      }
54      return type;
55    }
56  
57    public int nextElement() 
58      throws XMLStreamException 
59    {
60      next();
61      while (hasNext() && !isStartElement() && !isEndElement()) next(); 
62      return super.getEventType();
63    }
64  
65    public boolean hasNext() 
66      throws XMLStreamException
67    {
68      if (!open) return false;
69      return super.hasNext();
70    }
71  
72    public boolean moveToStartElement() 
73      throws XMLStreamException
74    {
75      if (isStartElement()) return true;
76      while(hasNext()) {
77        if (isStartElement()) return true;
78        else
79          next();
80      }
81      return false;
82    }
83  
84    public boolean moveToStartElement(String localName) 
85      throws XMLStreamException
86    {
87      if (localName == null) return false;
88      while( moveToStartElement() ) {
89        if (localName.equals(getLocalName())) return true;
90        if (!hasNext()) return false;
91        next();
92      }
93      return false;
94    }
95  
96    public boolean moveToStartElement(String localName, String namespaceUri) 
97      throws XMLStreamException
98    {
99      if (localName == null || namespaceUri == null) return false;
100     while(moveToStartElement(localName)) {
101       if(namespaceUri.equals(getNamespaceURI())) return true;
102       if (!hasNext()) return false;
103       next();
104     }
105     return false;
106   }
107 
108   public boolean moveToEndElement() 
109     throws XMLStreamException
110   {
111     if (isEndElement()) return true;
112     while (hasNext()) {
113       if (isEndElement()) return true;
114       else
115         next();
116     }
117     return false;
118   }
119 
120   public boolean moveToEndElement(String localName) 
121     throws XMLStreamException
122   {
123     if (localName == null) return false;
124     while( moveToEndElement() ) {
125       if (localName.equals(getLocalName())) return true;
126       if (!hasNext()) return false;
127       next();
128     }
129     return false;
130   }
131 
132   public boolean moveToEndElement(String localName, String namespaceUri) 
133     throws XMLStreamException
134   {
135     if (localName == null || namespaceUri == null) return false;
136     while(moveToEndElement(localName)) {
137       if(namespaceUri.equals(getNamespaceURI())) return true;
138       if (!hasNext()) return false;
139       next();
140     }
141     return false;
142   }
143   
144   public static void print(XMLStreamReader r, int depth) throws XMLStreamException {
145     System.out.print("["+depth+"]Sub: "+ElementTypeNames.getEventTypeString(r.getEventType()));
146     if(r.hasName()) System.out.println("->"+r.getLocalName());
147     else if(r.hasText()) System.out.println("->["+r.getText()+"]");
148     else System.out.println();
149   }
150 
151   public static void sub(XMLStreamReader r, int depth) throws Exception {
152     while (r.hasNext()) {
153       print(r,depth);
154       r.next();
155     }
156   } 
157 
158   public static void main(String args[]) throws Exception {
159     MXParser r = new MXParser();
160     r.setInput(new java.io.FileReader(args[0]));
161     r.moveToStartElement(); r.next();
162     while(r.moveToStartElement()) {
163       System.out.println("SE->"+r.getName());
164       XMLStreamReader subr = r.subReader();
165       sub(subr,1);
166     }
167   }
168 }
169 
170 
171 
172 
173 
174 
175