2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.ide.eclipse.adt.internal.editors.layout;
19 import com.android.layoutlib.api.IXmlPullParser;
21 import org.xmlpull.v1.XmlPullParserException;
23 import java.io.InputStream;
24 import java.io.Reader;
27 * Base implementation of an {@link IXmlPullParser} for cases where the parser is not sitting
28 * on top of an actual XML file.
29 * <p/>It's designed to work on layout files, and will most likely not work on other resource
32 public abstract class BasePullParser implements IXmlPullParser {
34 protected int mParsingState = START_DOCUMENT;
36 public BasePullParser() {
39 // --- new methods to override ---
41 public abstract void onNextFromStartDocument();
42 public abstract void onNextFromStartTag();
43 public abstract void onNextFromEndTag();
45 // --- basic implementation of IXmlPullParser ---
47 public void setFeature(String name, boolean state) throws XmlPullParserException {
48 if (FEATURE_PROCESS_NAMESPACES.equals(name) && state) {
51 if (FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name) && state) {
54 throw new XmlPullParserException("Unsupported feature: " + name);
57 public boolean getFeature(String name) {
58 if (FEATURE_PROCESS_NAMESPACES.equals(name)) {
61 if (FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
67 public void setProperty(String name, Object value) throws XmlPullParserException {
68 throw new XmlPullParserException("setProperty() not supported");
71 public Object getProperty(String name) {
75 public void setInput(Reader in) throws XmlPullParserException {
76 throw new XmlPullParserException("setInput() not supported");
79 public void setInput(InputStream inputStream, String inputEncoding)
80 throws XmlPullParserException {
81 throw new XmlPullParserException("setInput() not supported");
84 public void defineEntityReplacementText(String entityName, String replacementText)
85 throws XmlPullParserException {
86 throw new XmlPullParserException("defineEntityReplacementText() not supported");
89 public String getNamespacePrefix(int pos) throws XmlPullParserException {
90 throw new XmlPullParserException("getNamespacePrefix() not supported");
93 public String getInputEncoding() {
97 public String getNamespace(String prefix) {
98 throw new RuntimeException("getNamespace() not supported");
101 public int getNamespaceCount(int depth) throws XmlPullParserException {
102 throw new XmlPullParserException("getNamespaceCount() not supported");
105 public String getNamespaceUri(int pos) throws XmlPullParserException {
106 throw new XmlPullParserException("getNamespaceUri() not supported");
109 public int getColumnNumber() {
113 public int getLineNumber() {
117 public String getAttributeType(int arg0) {
121 public int getEventType() {
122 return mParsingState;
125 public String getText() {
129 public char[] getTextCharacters(int[] arg0) {
133 public boolean isAttributeDefault(int arg0) {
137 public boolean isWhitespace() {
141 public int next() throws XmlPullParserException {
142 switch (mParsingState) {
144 throw new XmlPullParserException("Nothing after the end");
146 onNextFromStartDocument();
149 onNextFromStartTag();
163 case IGNORABLE_WHITESPACE:
166 case PROCESSING_INSTRUCTION:
177 return mParsingState;
180 public int nextTag() throws XmlPullParserException {
181 int eventType = next();
182 if (eventType != START_TAG && eventType != END_TAG) {
183 throw new XmlPullParserException("expected start or end tag", this, null);
188 public String nextText() throws XmlPullParserException {
189 if (getEventType() != START_TAG) {
190 throw new XmlPullParserException("parser must be on START_TAG to read next text", this,
193 int eventType = next();
194 if (eventType == TEXT) {
195 String result = getText();
197 if (eventType != END_TAG) {
198 throw new XmlPullParserException(
199 "event TEXT it must be immediately followed by END_TAG", this, null);
202 } else if (eventType == END_TAG) {
205 throw new XmlPullParserException("parser must be on START_TAG or TEXT to read text",
210 public int nextToken() throws XmlPullParserException {
214 public void require(int type, String namespace, String name) throws XmlPullParserException {
215 if (type != getEventType() || (namespace != null && !namespace.equals(getNamespace()))
216 || (name != null && !name.equals(getName())))
217 throw new XmlPullParserException("expected " + TYPES[type] + getPositionDescription());