• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
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.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout;
18 
19 import com.android.layoutlib.api.IXmlPullParser;
20 
21 import org.xmlpull.v1.XmlPullParserException;
22 
23 import java.io.InputStream;
24 import java.io.Reader;
25 
26 /**
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
30  * files.
31  */
32 public abstract class BasePullParser implements IXmlPullParser {
33 
34     protected int mParsingState = START_DOCUMENT;
35 
BasePullParser()36     public BasePullParser() {
37     }
38 
39     // --- new methods to override ---
40 
onNextFromStartDocument()41     public abstract void onNextFromStartDocument();
onNextFromStartTag()42     public abstract void onNextFromStartTag();
onNextFromEndTag()43     public abstract void onNextFromEndTag();
44 
45     // --- basic implementation of IXmlPullParser ---
46 
setFeature(String name, boolean state)47     public void setFeature(String name, boolean state) throws XmlPullParserException {
48         if (FEATURE_PROCESS_NAMESPACES.equals(name) && state) {
49             return;
50         }
51         if (FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name) && state) {
52             return;
53         }
54         throw new XmlPullParserException("Unsupported feature: " + name);
55     }
56 
getFeature(String name)57     public boolean getFeature(String name) {
58         if (FEATURE_PROCESS_NAMESPACES.equals(name)) {
59             return true;
60         }
61         if (FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
62             return true;
63         }
64         return false;
65     }
66 
setProperty(String name, Object value)67     public void setProperty(String name, Object value) throws XmlPullParserException {
68         throw new XmlPullParserException("setProperty() not supported");
69     }
70 
getProperty(String name)71     public Object getProperty(String name) {
72         return null;
73     }
74 
setInput(Reader in)75     public void setInput(Reader in) throws XmlPullParserException {
76         throw new XmlPullParserException("setInput() not supported");
77     }
78 
setInput(InputStream inputStream, String inputEncoding)79     public void setInput(InputStream inputStream, String inputEncoding)
80             throws XmlPullParserException {
81         throw new XmlPullParserException("setInput() not supported");
82     }
83 
defineEntityReplacementText(String entityName, String replacementText)84     public void defineEntityReplacementText(String entityName, String replacementText)
85             throws XmlPullParserException {
86         throw new XmlPullParserException("defineEntityReplacementText() not supported");
87     }
88 
getNamespacePrefix(int pos)89     public String getNamespacePrefix(int pos) throws XmlPullParserException {
90         throw new XmlPullParserException("getNamespacePrefix() not supported");
91     }
92 
getInputEncoding()93     public String getInputEncoding() {
94         return null;
95     }
96 
getNamespace(String prefix)97     public String getNamespace(String prefix) {
98         throw new RuntimeException("getNamespace() not supported");
99     }
100 
getNamespaceCount(int depth)101     public int getNamespaceCount(int depth) throws XmlPullParserException {
102         throw new XmlPullParserException("getNamespaceCount() not supported");
103     }
104 
getNamespaceUri(int pos)105     public String getNamespaceUri(int pos) throws XmlPullParserException {
106         throw new XmlPullParserException("getNamespaceUri() not supported");
107     }
108 
getColumnNumber()109     public int getColumnNumber() {
110         return -1;
111     }
112 
getLineNumber()113     public int getLineNumber() {
114         return -1;
115     }
116 
getAttributeType(int arg0)117     public String getAttributeType(int arg0) {
118         return "CDATA";
119     }
120 
getEventType()121     public int getEventType() {
122         return mParsingState;
123     }
124 
getText()125     public String getText() {
126         return null;
127     }
128 
getTextCharacters(int[] arg0)129     public char[] getTextCharacters(int[] arg0) {
130         return null;
131     }
132 
isAttributeDefault(int arg0)133     public boolean isAttributeDefault(int arg0) {
134         return false;
135     }
136 
isWhitespace()137     public boolean isWhitespace() {
138         return false;
139     }
140 
next()141     public int next() throws XmlPullParserException {
142         switch (mParsingState) {
143             case END_DOCUMENT:
144                 throw new XmlPullParserException("Nothing after the end");
145             case START_DOCUMENT:
146                 onNextFromStartDocument();
147                 break;
148             case START_TAG:
149                 onNextFromStartTag();
150                 break;
151             case END_TAG:
152                 onNextFromEndTag();
153                 break;
154             case TEXT:
155                 // not used
156                 break;
157             case CDSECT:
158                 // not used
159                 break;
160             case ENTITY_REF:
161                 // not used
162                 break;
163             case IGNORABLE_WHITESPACE:
164                 // not used
165                 break;
166             case PROCESSING_INSTRUCTION:
167                 // not used
168                 break;
169             case COMMENT:
170                 // not used
171                 break;
172             case DOCDECL:
173                 // not used
174                 break;
175         }
176 
177         return mParsingState;
178     }
179 
nextTag()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);
184         }
185         return eventType;
186     }
187 
nextText()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,
191                     null);
192         }
193         int eventType = next();
194         if (eventType == TEXT) {
195             String result = getText();
196             eventType = next();
197             if (eventType != END_TAG) {
198                 throw new XmlPullParserException(
199                         "event TEXT it must be immediately followed by END_TAG", this, null);
200             }
201             return result;
202         } else if (eventType == END_TAG) {
203             return "";
204         } else {
205             throw new XmlPullParserException("parser must be on START_TAG or TEXT to read text",
206                     this, null);
207         }
208     }
209 
nextToken()210     public int nextToken() throws XmlPullParserException {
211         return next();
212     }
213 
require(int type, String namespace, String name)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());
218     }
219 }
220