KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > FragmentStreamReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.xml.namespace.NamespaceContext JavaDoc;
24 import javax.xml.stream.XMLStreamConstants;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamReader;
27 import javax.xml.stream.util.StreamReaderDelegate;
28
29 public class FragmentStreamReader extends StreamReaderDelegate implements XMLStreamReader {
30
31     private static final int STATE_START_DOC = 0;
32     private static final int STATE_FIRST_ELEM = 1;
33     private static final int STATE_FIRST_RUN = 2;
34     private static final int STATE_RUN = 3;
35     private static final int STATE_END_DOC = 4;
36     
37     private int depth;
38     private int state = STATE_START_DOC;
39     private int event = START_DOCUMENT;
40     private List JavaDoc rootPrefixes;
41     
42     public FragmentStreamReader(XMLStreamReader parent) {
43         super(parent);
44         rootPrefixes = new ArrayList JavaDoc();
45         NamespaceContext JavaDoc ctx = getParent().getNamespaceContext();
46         if (ctx instanceof ExtendedNamespaceContext) {
47             Iterator JavaDoc it = ((ExtendedNamespaceContext) ctx).getPrefixes();
48             while (it.hasNext()) {
49                 String JavaDoc prefix = (String JavaDoc) it.next();
50                 rootPrefixes.add(prefix);
51             }
52         }
53     }
54     
55     public int getEventType() {
56         return event;
57     }
58
59     public int next() throws XMLStreamException {
60         switch (state) {
61         case STATE_START_DOC:
62             state = STATE_FIRST_ELEM;
63             event = START_DOCUMENT;
64             break;
65         case STATE_FIRST_ELEM:
66             state = STATE_FIRST_RUN;
67             depth++;
68             event = START_ELEMENT;
69             break;
70         case STATE_FIRST_RUN:
71             state = STATE_RUN;
72             // do not break
73
case STATE_RUN:
74             event = getParent().next();
75             if (event == START_ELEMENT) {
76                 depth++;
77             } else if (event == END_ELEMENT) {
78                 depth--;
79                 if (depth == 0) {
80                     state = STATE_END_DOC;
81                 }
82             }
83             break;
84         case STATE_END_DOC:
85             event = END_DOCUMENT;
86             break;
87         default:
88             throw new IllegalStateException JavaDoc();
89         }
90         return event;
91     }
92
93     public int nextTag() throws XMLStreamException {
94         int eventType = next();
95         while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
96
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace
97
|| eventType == XMLStreamConstants.SPACE
98             || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
99             || eventType == XMLStreamConstants.COMMENT) {
100             eventType = next();
101          }
102          if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
103              throw new XMLStreamException("expected start or end tag", getLocation());
104          }
105          return eventType;
106     }
107     
108     public int getNamespaceCount() {
109         if (state == STATE_FIRST_RUN) {
110             return rootPrefixes.size();
111         } else {
112             return getParent().getNamespaceCount();
113         }
114     }
115     
116     public String JavaDoc getNamespacePrefix(int i) {
117         if (state == STATE_FIRST_RUN) {
118             return (String JavaDoc) rootPrefixes.get(i);
119         } else {
120             return getParent().getNamespacePrefix(i);
121         }
122     }
123     
124     public String JavaDoc getNamespaceURI(int i) {
125         if (state == STATE_FIRST_RUN) {
126             return getParent().getNamespaceContext().getNamespaceURI((String JavaDoc) rootPrefixes.get(i));
127         } else {
128             return getParent().getNamespaceURI(i);
129         }
130     }
131     
132     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
133         if (state == STATE_FIRST_RUN) {
134             return getParent().getNamespaceContext().getNamespaceURI(prefix);
135         } else {
136             return getParent().getNamespaceURI(prefix);
137         }
138     }
139     
140 }
141
Popular Tags