KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > StaxReader


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.converters.ErrorWriter;
4 import com.thoughtworks.xstream.io.StreamException;
5
6 import javax.xml.namespace.QName JavaDoc;
7 import javax.xml.stream.XMLStreamConstants;
8 import javax.xml.stream.XMLStreamException;
9 import javax.xml.stream.XMLStreamReader;
10
11 /**
12  * A reader using the StAX API.
13  *
14  * @author James Strachan
15  * @version $Revision: 578 $
16  */

17 public class StaxReader extends AbstractPullReader {
18
19     private final QNameMap qnameMap;
20     private final XMLStreamReader in;
21
22     public StaxReader(QNameMap qnameMap, XMLStreamReader in) {
23         this.qnameMap = qnameMap;
24         this.in = in;
25         moveDown();
26     }
27
28     protected int pullNextEvent() {
29         try {
30             switch(in.next()) {
31                 case XMLStreamConstants.START_DOCUMENT:
32                 case XMLStreamConstants.START_ELEMENT:
33                     return START_NODE;
34                 case XMLStreamConstants.END_DOCUMENT:
35                 case XMLStreamConstants.END_ELEMENT:
36                     return END_NODE;
37                 case XMLStreamConstants.CHARACTERS:
38                     return TEXT;
39                 case XMLStreamConstants.COMMENT:
40                     return COMMENT;
41                 default:
42                     return OTHER;
43             }
44         } catch (XMLStreamException e) {
45             throw new StreamException(e);
46         }
47     }
48
49     protected String JavaDoc pullElementName() {
50         // let the QNameMap handle any mapping of QNames to Java class names
51
QName JavaDoc qname = in.getName();
52         return qnameMap.getJavaClassName(qname);
53     }
54
55     protected String JavaDoc pullText() {
56         return in.getText();
57     }
58
59     public String JavaDoc getAttribute(String JavaDoc name) {
60         return in.getAttributeValue(null, name);
61     }
62
63     public String JavaDoc getAttribute(int index) {
64         return in.getAttributeValue(index);
65     }
66
67     public int getAttributeCount() {
68         return in.getAttributeCount();
69     }
70
71     public String JavaDoc getAttributeName(int index) {
72         return in.getAttributeLocalName(index);
73     }
74
75     public void appendErrors(ErrorWriter errorWriter) {
76         errorWriter.add("line number", String.valueOf(in.getLocation().getLineNumber()));
77     }
78
79     public void close() {
80         try {
81             in.close();
82         } catch (XMLStreamException e) {
83             throw new StreamException(e);
84         }
85     }
86
87 }
Popular Tags