KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * This implementation comes from the XFire project
21  * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
22  */

23 import java.util.ArrayList JavaDoc;
24
25 import javax.xml.namespace.NamespaceContext JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.stream.XMLStreamException;
28
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.CDATASection JavaDoc;
31 import org.w3c.dom.Comment JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.EntityReference JavaDoc;
35 import org.w3c.dom.NamedNodeMap JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38
39 public class W3CDOMStreamReader extends DOMStreamReader {
40     private Node JavaDoc content;
41
42     private Document JavaDoc document;
43
44     private W3CNamespaceContext context;
45
46     /**
47      * @param element
48      */

49     public W3CDOMStreamReader(Element JavaDoc element) {
50         super(new ElementFrame(element, null));
51
52         this.document = element.getOwnerDocument();
53     }
54
55     /**
56      * Get the document associated with this stream.
57      *
58      * @return
59      */

60     public Document JavaDoc getDocument() {
61         return document;
62     }
63
64     /**
65      * Find name spaces declaration in atrributes and move them to separate
66      * collection.
67      */

68     protected void newFrame(ElementFrame frame) {
69         Element JavaDoc element = getCurrentElement();
70         frame.uris = new ArrayList JavaDoc();
71         frame.prefixes = new ArrayList JavaDoc();
72         frame.attributes = new ArrayList JavaDoc();
73
74         if (context == null)
75             context = new W3CNamespaceContext();
76
77         context.setElement(element);
78
79         NamedNodeMap JavaDoc nodes = element.getAttributes();
80
81         String JavaDoc nsURI = element.getNamespaceURI();
82         String JavaDoc ePrefix = element.getPrefix();
83         if (ePrefix == null) {
84             ePrefix = "";
85         }
86
87         for (int i = 0; i < nodes.getLength(); i++) {
88             Node JavaDoc node = nodes.item(i);
89             String JavaDoc prefix = node.getPrefix();
90             String JavaDoc localName = node.getLocalName();
91             String JavaDoc value = node.getNodeValue();
92             String JavaDoc name = node.getNodeName();
93             String JavaDoc uri = node.getNamespaceURI();
94
95             if (prefix == null)
96                 prefix = "";
97
98             if (name != null && name.equals("xmlns")) {
99                 frame.uris.add(value);
100                 frame.prefixes.add("");
101             } else if (prefix.length() > 0 && prefix.equals("xmlns")) {
102                 frame.uris.add(value);
103                 frame.prefixes.add(localName);
104             } else if (name.startsWith("xmlns:")) {
105                 prefix = name.substring(6);
106                 frame.uris.add(value);
107                 frame.prefixes.add(prefix);
108             } else {
109                 frame.attributes.add(node);
110             }
111         }
112     }
113
114     protected void endElement() {
115         super.endElement();
116     }
117
118     Element JavaDoc getCurrentElement() {
119         return (Element JavaDoc) getCurrentFrame().element;
120     }
121
122     protected ElementFrame getChildFrame(int currentChild) {
123         return new ElementFrame(getCurrentElement().getChildNodes().item(currentChild), getCurrentFrame());
124     }
125
126     protected int getChildCount() {
127         return getCurrentElement().getChildNodes().getLength();
128     }
129
130     protected int moveToChild(int currentChild) {
131         this.content = getCurrentElement().getChildNodes().item(currentChild);
132
133         if (content instanceof Text JavaDoc)
134             return CHARACTERS;
135         else if (content instanceof Element JavaDoc)
136             return START_ELEMENT;
137         else if (content instanceof CDATASection JavaDoc)
138             return CDATA;
139         else if (content instanceof Comment JavaDoc)
140             return CHARACTERS;
141         else if (content instanceof EntityReference JavaDoc)
142             return ENTITY_REFERENCE;
143
144         throw new IllegalStateException JavaDoc();
145     }
146
147     public String JavaDoc getElementText() throws XMLStreamException {
148         return getText();
149     }
150
151     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
152         ElementFrame frame = getCurrentFrame();
153
154         while (null != frame) {
155             int index = frame.prefixes.indexOf(prefix);
156             if (index != -1) {
157                 return (String JavaDoc) frame.uris.get(index);
158             }
159
160             frame = frame.parent;
161         }
162
163         return null;
164     }
165
166     public String JavaDoc getAttributeValue(String JavaDoc ns, String JavaDoc local) {
167         if (ns == null || ns.equals(""))
168             return getCurrentElement().getAttribute(local);
169         else
170             return getCurrentElement().getAttributeNS(ns, local);
171     }
172
173     public int getAttributeCount() {
174         return getCurrentFrame().attributes.size();
175     }
176
177     Attr JavaDoc getAttribute(int i) {
178         return (Attr JavaDoc) getCurrentFrame().attributes.get(i);
179     }
180
181     private String JavaDoc getLocalName(Attr JavaDoc attr) {
182
183         String JavaDoc name = attr.getLocalName();
184         if (name == null) {
185             name = attr.getNodeName();
186         }
187         return name;
188     }
189
190     public QName JavaDoc getAttributeName(int i) {
191         Attr JavaDoc at = getAttribute(i);
192
193         String JavaDoc prefix = at.getPrefix();
194         String JavaDoc ln = getLocalName(at);
195         // at.getNodeName();
196
String JavaDoc ns = at.getNamespaceURI();
197
198         if (prefix == null) {
199             return new QName JavaDoc(ns, ln);
200         } else {
201             return new QName JavaDoc(ns, ln, prefix);
202         }
203     }
204
205     public String JavaDoc getAttributeNamespace(int i) {
206         return getAttribute(i).getNamespaceURI();
207     }
208
209     public String JavaDoc getAttributeLocalName(int i) {
210         Attr JavaDoc attr = getAttribute(i);
211         String JavaDoc name = getLocalName(attr);
212         return name;
213     }
214
215     public String JavaDoc getAttributePrefix(int i) {
216         return getAttribute(i).getPrefix();
217     }
218
219     public String JavaDoc getAttributeType(int i) {
220         return toStaxType(getAttribute(i).getNodeType());
221     }
222
223     public static String JavaDoc toStaxType(short jdom) {
224         switch (jdom) {
225         default:
226             return null;
227         }
228     }
229
230     public String JavaDoc getAttributeValue(int i) {
231         return getAttribute(i).getValue();
232     }
233
234     public boolean isAttributeSpecified(int i) {
235         return getAttribute(i).getValue() != null;
236     }
237
238     public int getNamespaceCount() {
239         return getCurrentFrame().prefixes.size();
240     }
241
242     public String JavaDoc getNamespacePrefix(int i) {
243         return (String JavaDoc) getCurrentFrame().prefixes.get(i);
244     }
245
246     public String JavaDoc getNamespaceURI(int i) {
247         return (String JavaDoc) getCurrentFrame().uris.get(i);
248     }
249
250     public NamespaceContext JavaDoc getNamespaceContext() {
251         return context;
252     }
253
254     public String JavaDoc getText() {
255         Node JavaDoc node = getCurrentElement().getChildNodes().item(getCurrentFrame().currentChild);
256         return node.getNodeValue();
257     }
258
259     public char[] getTextCharacters() {
260         return getText().toCharArray();
261     }
262
263     public int getTextStart() {
264         return 0;
265     }
266
267     public int getTextLength() {
268         return getText().length();
269     }
270
271     public String JavaDoc getEncoding() {
272         return null;
273     }
274
275     public QName JavaDoc getName() {
276         Element JavaDoc el = getCurrentElement();
277
278         String JavaDoc prefix = getPrefix();
279         String JavaDoc ln = getLocalName();
280
281         if (prefix == null) {
282             return new QName JavaDoc(el.getNamespaceURI(), ln);
283         } else {
284             return new QName JavaDoc(el.getNamespaceURI(), ln, prefix);
285         }
286     }
287
288     public String JavaDoc getLocalName() {
289         String JavaDoc name = getCurrentElement().getLocalName();
290         // When the element has no namespaces, null is returned
291
if (name == null) {
292             name = getCurrentElement().getNodeName();
293         }
294         return name;
295     }
296
297     public String JavaDoc getNamespaceURI() {
298         return getCurrentElement().getNamespaceURI();
299     }
300
301     public String JavaDoc getPrefix() {
302         String JavaDoc prefix = getCurrentElement().getPrefix();
303         if (prefix == null) {
304             prefix = "";
305         }
306         return prefix;
307     }
308
309     public String JavaDoc getPITarget() {
310         throw new UnsupportedOperationException JavaDoc();
311     }
312
313     public String JavaDoc getPIData() {
314         throw new UnsupportedOperationException JavaDoc();
315     }
316 }
317
Popular Tags