001: /*
002: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 08. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import java.io.BufferedReader;
015: import java.io.IOException;
016: import java.io.Reader;
017:
018: import org.xmlpull.mxp1.MXParser;
019: import org.xmlpull.v1.XmlPullParser;
020: import org.xmlpull.v1.XmlPullParserException;
021:
022: import com.thoughtworks.xstream.converters.ErrorWriter;
023: import com.thoughtworks.xstream.io.StreamException;
024:
025: /**
026: * XStream reader that pulls directly from the stream using the XmlPullParser API.
027: *
028: * @author Joe Walnes
029: */
030: public class XppReader extends AbstractPullReader {
031:
032: private final XmlPullParser parser;
033: private final BufferedReader reader;
034:
035: public XppReader(Reader reader) {
036: this (reader, new XmlFriendlyReplacer());
037: }
038:
039: /**
040: * @since 1.2
041: */
042: public XppReader(Reader reader, XmlFriendlyReplacer replacer) {
043: super (replacer);
044: try {
045: parser = createParser();
046: this .reader = new BufferedReader(reader);
047: parser.setInput(this .reader);
048: moveDown();
049: } catch (XmlPullParserException e) {
050: throw new StreamException(e);
051: }
052: }
053:
054: /**
055: * To use another implementation of org.xmlpull.v1.XmlPullParser, override this method.
056: */
057: protected XmlPullParser createParser() {
058: return new MXParser();
059: }
060:
061: protected int pullNextEvent() {
062: try {
063: switch (parser.next()) {
064: case XmlPullParser.START_DOCUMENT:
065: case XmlPullParser.START_TAG:
066: return START_NODE;
067: case XmlPullParser.END_DOCUMENT:
068: case XmlPullParser.END_TAG:
069: return END_NODE;
070: case XmlPullParser.TEXT:
071: return TEXT;
072: case XmlPullParser.COMMENT:
073: return COMMENT;
074: default:
075: return OTHER;
076: }
077: } catch (XmlPullParserException e) {
078: throw new StreamException(e);
079: } catch (IOException e) {
080: throw new StreamException(e);
081: }
082: }
083:
084: protected String pullElementName() {
085: return parser.getName();
086: }
087:
088: protected String pullText() {
089: return parser.getText();
090: }
091:
092: public String getAttribute(String name) {
093: return parser.getAttributeValue(null, name);
094: }
095:
096: public String getAttribute(int index) {
097: return parser.getAttributeValue(index);
098: }
099:
100: public int getAttributeCount() {
101: return parser.getAttributeCount();
102: }
103:
104: public String getAttributeName(int index) {
105: return unescapeXmlName(parser.getAttributeName(index));
106: }
107:
108: public void appendErrors(ErrorWriter errorWriter) {
109: errorWriter.add("line number", String.valueOf(parser
110: .getLineNumber()));
111: }
112:
113: public void close() {
114: try {
115: reader.close();
116: } catch (IOException e) {
117: throw new StreamException(e);
118: }
119: }
120:
121: }
|