KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.namespace.NamespaceContext JavaDoc;
25 import javax.xml.stream.XMLStreamConstants;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamReader;
28 import javax.xml.stream.util.StreamReaderDelegate;
29
30 public class ExtendedXMLStreamReader extends StreamReaderDelegate {
31
32     private SimpleNamespaceContext context = new SimpleNamespaceContext();
33     
34     public ExtendedXMLStreamReader(XMLStreamReader delegate) {
35         super(delegate);
36     }
37
38     public NamespaceContext JavaDoc getNamespaceContext() {
39         return context;
40     }
41     
42     public int nextTag() throws XMLStreamException {
43         int eventType = next();
44          while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
45
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
46              // skip whitespace
47
|| eventType == XMLStreamConstants.SPACE
48              || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
49              || eventType == XMLStreamConstants.COMMENT
50              ) {
51              eventType = next();
52          }
53          if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
54             throw new XMLStreamException("expected start or end tag", getLocation());
55          }
56          return eventType;
57     }
58
59     public int next() throws XMLStreamException {
60         int next = super.next();
61         if (next == START_ELEMENT) {
62             context = new SimpleNamespaceContext(context, getNamespaces());
63         } else if (next == END_ELEMENT) {
64             context = context.getParent();
65         }
66         return next;
67     }
68
69     private Map JavaDoc getNamespaces() {
70         Map JavaDoc ns = new HashMap JavaDoc();
71         for (int i = 0; i < getNamespaceCount(); i++) {
72             ns.put(getNamespacePrefix(i), getNamespaceURI(i));
73         }
74         return ns;
75     }
76     
77     public static class SimpleNamespaceContext implements ExtendedNamespaceContext {
78
79         private SimpleNamespaceContext parent;
80         private Map JavaDoc namespaces;
81         
82         public SimpleNamespaceContext() {
83             namespaces = new HashMap JavaDoc();
84         }
85
86         public SimpleNamespaceContext(SimpleNamespaceContext parent, Map JavaDoc namespaces) {
87             this.parent = parent;
88             this.namespaces = namespaces;
89         }
90
91         public SimpleNamespaceContext getParent() {
92             return parent;
93         }
94         
95         public Iterator JavaDoc getPrefixes() {
96             HashSet JavaDoc prefixes = new HashSet JavaDoc();
97             for (SimpleNamespaceContext context = this; context != null; context = context.parent) {
98                 prefixes.addAll(context.namespaces.keySet());
99             }
100             return prefixes.iterator();
101         }
102
103         public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
104             String JavaDoc uri = (String JavaDoc) namespaces.get(prefix);
105             if (uri == null && parent != null) {
106                 uri = parent.getNamespaceURI(prefix);
107             }
108             return uri;
109         }
110
111         public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
112             for (SimpleNamespaceContext context = this; context != null; context = context.parent) {
113                 for (Iterator JavaDoc it = context.namespaces.keySet().iterator(); it.hasNext();) {
114                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
115                     if (entry.getValue().equals(namespaceURI)) {
116                         return (String JavaDoc) entry.getKey();
117                     }
118                 }
119             }
120             return null;
121         }
122
123         public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
124             HashSet JavaDoc prefixes = new HashSet JavaDoc();
125             for (SimpleNamespaceContext context = this; context != null; context = context.parent) {
126                 for (Iterator JavaDoc it = context.namespaces.keySet().iterator(); it.hasNext();) {
127                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
128                     if (entry.getValue().equals(namespaceURI)) {
129                         prefixes.add(entry.getKey());
130                     }
131                 }
132             }
133             return prefixes.iterator();
134         }
135         
136     }
137
138
139 }
140
Popular Tags