The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | * sell copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | * IN THE SOFTWARE. */ |
| 20 | |
| 21 | // Contributors: Paul Hackenberger (unterminated entity handling in relaxed mode) |
| 22 | |
| 23 | package org.kxml2.io; |
| 24 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.InputStreamReader; |
| 28 | import java.io.Reader; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.Map; |
| 31 | import org.xmlpull.v1.XmlPullParser; |
| 32 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 34 | /** |
| 35 | * A pull based XML parser. |
| 36 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | public class KXmlParser implements XmlPullParser { |
| 38 | |
| 39 | private Object location; |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 40 | static final private String UNEXPECTED_EOF = "Unexpected EOF"; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | static final private String ILLEGAL_TYPE = "Wrong event type"; |
| 42 | static final private int LEGACY = 999; |
| 43 | static final private int XML_DECL = 998; |
| 44 | |
| 45 | // general |
| 46 | |
| 47 | private String version; |
| 48 | private Boolean standalone; |
| 49 | |
| 50 | private boolean processNsp; |
| 51 | private boolean relaxed; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 52 | private boolean keepNamespaceAttributes; |
| 53 | private Map<String, String> entityMap; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | private int depth; |
| 55 | private String[] elementStack = new String[16]; |
| 56 | private String[] nspStack = new String[8]; |
| 57 | private int[] nspCounts = new int[4]; |
| 58 | |
| 59 | // source |
| 60 | |
| 61 | private Reader reader; |
| 62 | private String encoding; |
| 63 | private char[] srcBuf; |
| 64 | |
| 65 | private int srcPos; |
| 66 | private int srcCount; |
| 67 | |
| 68 | private int line; |
| 69 | private int column; |
| 70 | |
| 71 | // txtbuffer |
| 72 | |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 73 | /** Target buffer for storing incoming text (including aggregated resolved entities) */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | private char[] txtBuf = new char[128]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 75 | /** Write position */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | private int txtPos; |
| 77 | |
| 78 | // Event-related |
| 79 | |
| 80 | private int type; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 81 | private boolean isWhitespace; |
| 82 | private String namespace; |
| 83 | private String prefix; |
| 84 | private String name; |
| 85 | |
| 86 | private boolean degenerated; |
| 87 | private int attributeCount; |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * The current element's attributes arranged in groups of 4: |
| 91 | * i + 0 = attribute namespace URI |
| 92 | * i + 1 = attribute namespace prefix |
| 93 | * i + 2 = attribute qualified name (may contain ":", as in "html:h1") |
| 94 | * i + 3 = attribute value |
| 95 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | private String[] attributes = new String[16]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 97 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 98 | private String error; |
| 99 | |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 100 | /** |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 101 | * A separate peek buffer seems simpler than managing wrap around in the first level read |
| 102 | * buffer |
| 103 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 104 | private int[] peek = new int[2]; |
| 105 | private int peekCount; |
| 106 | private boolean wasCR; |
| 107 | |
| 108 | private boolean unresolved; |
| 109 | private boolean token; |
| 110 | |
| 111 | public KXmlParser() { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 112 | srcBuf = new char[8192]; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 115 | /** |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 116 | * Retains namespace attributes like {@code xmlns="http://foo"} or {@code xmlns:foo="http:foo"} |
| 117 | * in pulled elements. Most applications will only be interested in the effective namespaces of |
| 118 | * their elements, so these attributes aren't useful. But for structure preserving wrappers like |
| 119 | * DOM, it is necessary to keep the namespace data around. |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 120 | */ |
| 121 | public void keepNamespaceAttributes() { |
| 122 | this.keepNamespaceAttributes = true; |
| 123 | } |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 124 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 125 | private boolean isProp(String n1, boolean prop, String n2) { |
| 126 | if (!n1.startsWith("http://xmlpull.org/v1/doc/")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | return false; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 128 | } |
| 129 | if (prop) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | return n1.substring(42).equals(n2); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 131 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 132 | return n1.substring(40).equals(n2); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 133 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 136 | private boolean adjustNsp() throws XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 137 | boolean any = false; |
| 138 | |
| 139 | for (int i = 0; i < attributeCount << 2; i += 4) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | String attrName = attributes[i + 2]; |
| 141 | int cut = attrName.indexOf(':'); |
| 142 | String prefix; |
| 143 | |
| 144 | if (cut != -1) { |
| 145 | prefix = attrName.substring(0, cut); |
| 146 | attrName = attrName.substring(cut + 1); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 147 | } else if (attrName.equals("xmlns")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 148 | prefix = attrName; |
| 149 | attrName = null; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 150 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 151 | continue; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 152 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 153 | |
| 154 | if (!prefix.equals("xmlns")) { |
| 155 | any = true; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 156 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | int j = (nspCounts[depth]++) << 1; |
| 158 | |
| 159 | nspStack = ensureCapacity(nspStack, j + 2); |
| 160 | nspStack[j] = attrName; |
| 161 | nspStack[j + 1] = attributes[i + 3]; |
| 162 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 163 | if (attrName != null && attributes[i + 3].isEmpty()) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | error("illegal empty namespace"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 165 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 167 | if (keepNamespaceAttributes) { |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 168 | // explicitly set the namespace for unprefixed attributes |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 169 | // such as xmlns="http://foo" |
| 170 | attributes[i] = "http://www.w3.org/2000/xmlns/"; |
| 171 | any = true; |
| 172 | } else { |
| 173 | System.arraycopy( |
| 174 | attributes, |
| 175 | i + 4, |
| 176 | attributes, |
| 177 | i, |
| 178 | ((--attributeCount) << 2) - i); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 179 | |
Jesse Wilson | 1ec94fe | 2010-02-19 09:22:21 -0800 | [diff] [blame] | 180 | i -= 4; |
| 181 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | if (any) { |
| 186 | for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) { |
| 187 | |
| 188 | String attrName = attributes[i + 2]; |
| 189 | int cut = attrName.indexOf(':'); |
| 190 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 191 | if (cut == 0 && !relaxed) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 192 | throw new RuntimeException( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 193 | "illegal attribute name: " + attrName + " at " + this); |
| 194 | } else if (cut != -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | String attrPrefix = attrName.substring(0, cut); |
| 196 | |
| 197 | attrName = attrName.substring(cut + 1); |
| 198 | |
| 199 | String attrNs = getNamespace(attrPrefix); |
| 200 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 201 | if (attrNs == null && !relaxed) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 202 | throw new RuntimeException( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 203 | "Undefined Prefix: " + attrPrefix + " in " + this); |
| 204 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 205 | |
| 206 | attributes[i] = attrNs; |
| 207 | attributes[i + 1] = attrPrefix; |
| 208 | attributes[i + 2] = attrName; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | int cut = name.indexOf(':'); |
| 214 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 215 | if (cut == 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 216 | error("illegal tag name: " + name); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 217 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 218 | |
| 219 | if (cut != -1) { |
| 220 | prefix = name.substring(0, cut); |
| 221 | name = name.substring(cut + 1); |
| 222 | } |
| 223 | |
| 224 | this.namespace = getNamespace(prefix); |
| 225 | |
| 226 | if (this.namespace == null) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 227 | if (prefix != null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 228 | error("undefined prefix: " + prefix); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 229 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 230 | this.namespace = NO_NAMESPACE; |
| 231 | } |
| 232 | |
| 233 | return any; |
| 234 | } |
| 235 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 236 | private String[] ensureCapacity(String[] arr, int required) { |
| 237 | if (arr.length >= required) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 238 | return arr; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 239 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 240 | String[] bigger = new String[required + 16]; |
| 241 | System.arraycopy(arr, 0, bigger, 0, arr.length); |
| 242 | return bigger; |
| 243 | } |
| 244 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 245 | private void error(String desc) throws XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 246 | if (relaxed) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 247 | if (error == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 248 | error = "ERR: " + desc; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 249 | } |
| 250 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 251 | exception(desc); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 252 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 255 | private void exception(String desc) throws XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 256 | throw new XmlPullParserException( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 257 | desc.length() < 100 ? desc : desc.substring(0, 100) + "\n", |
| 258 | this, |
| 259 | null); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 262 | /** |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 263 | * Common base for next() and nextToken(). Clears the state, except from txtPos and whitespace. |
| 264 | * Does not set the type variable. |
| 265 | */ |
| 266 | private void nextImpl() throws IOException, XmlPullParserException { |
| 267 | if (reader == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 268 | exception("No Input specified"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 269 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 270 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 271 | if (type == END_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 272 | depth--; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 273 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | |
| 275 | while (true) { |
| 276 | attributeCount = -1; |
| 277 | |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 278 | // degenerated needs to be handled before error because of possible |
| 279 | // processor expectations(!) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 280 | |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 281 | if (degenerated) { |
| 282 | degenerated = false; |
| 283 | type = END_TAG; |
| 284 | return; |
| 285 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 286 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 287 | if (error != null) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 288 | for (int i = 0; i < error.length(); i++) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 289 | push(error.charAt(i)); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 290 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 291 | error = null; |
| 292 | type = COMMENT; |
| 293 | return; |
| 294 | } |
| 295 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 296 | prefix = null; |
| 297 | name = null; |
| 298 | namespace = null; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 299 | |
| 300 | type = peekType(); |
| 301 | |
| 302 | switch (type) { |
| 303 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 304 | case ENTITY_REF: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 305 | pushEntity(); |
| 306 | return; |
| 307 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 308 | case START_TAG: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 309 | parseStartTag(false); |
| 310 | return; |
| 311 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 312 | case END_TAG: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 313 | parseEndTag(); |
| 314 | return; |
| 315 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 316 | case END_DOCUMENT: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 317 | return; |
| 318 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 319 | case TEXT: |
Elliott Hughes | 6bcf32a | 2009-11-16 21:23:11 -0800 | [diff] [blame] | 320 | pushText('<', !token, false); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 321 | if (depth == 0) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 322 | if (isWhitespace) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 323 | type = IGNORABLE_WHITESPACE; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 324 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 325 | } |
| 326 | return; |
| 327 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 328 | default: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 329 | type = parseLegacy(token); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 330 | if (type != XML_DECL) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 331 | return; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 332 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 337 | private int parseLegacy(boolean push) throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 338 | String req = ""; |
| 339 | int term; |
| 340 | int result; |
| 341 | int prev = 0; |
| 342 | |
| 343 | read(); // < |
| 344 | int c = read(); |
| 345 | |
| 346 | if (c == '?') { |
| 347 | if ((peek(0) == 'x' || peek(0) == 'X') |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 348 | && (peek(1) == 'm' || peek(1) == 'M')) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 349 | |
| 350 | if (push) { |
| 351 | push(peek(0)); |
| 352 | push(peek(1)); |
| 353 | } |
| 354 | read(); |
| 355 | read(); |
| 356 | |
| 357 | if ((peek(0) == 'l' || peek(0) == 'L') && peek(1) <= ' ') { |
| 358 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 359 | if (line != 1 || column > 4) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 360 | error("PI must not start with xml"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 361 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 362 | |
| 363 | parseStartTag(true); |
| 364 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 365 | if (attributeCount < 1 || !"version".equals(attributes[2])) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 366 | error("version expected"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 367 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 368 | |
| 369 | version = attributes[3]; |
| 370 | |
| 371 | int pos = 1; |
| 372 | |
| 373 | if (pos < attributeCount |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 374 | && "encoding".equals(attributes[2 + 4])) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 375 | encoding = attributes[3 + 4]; |
| 376 | pos++; |
| 377 | } |
| 378 | |
| 379 | if (pos < attributeCount |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 380 | && "standalone".equals(attributes[4 * pos + 2])) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 381 | String st = attributes[3 + 4 * pos]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 382 | if ("yes".equals(st)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 383 | standalone = new Boolean(true); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 384 | } else if ("no".equals(st)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 385 | standalone = new Boolean(false); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 386 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 387 | error("illegal standalone value: " + st); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 388 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 389 | pos++; |
| 390 | } |
| 391 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 392 | if (pos != attributeCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 393 | error("illegal xmldecl"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 394 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 395 | |
| 396 | isWhitespace = true; |
| 397 | txtPos = 0; |
| 398 | |
| 399 | return XML_DECL; |
| 400 | } |
| 401 | } |
| 402 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 403 | term = '?'; |
| 404 | result = PROCESSING_INSTRUCTION; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 405 | } else if (c == '!') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 406 | if (peek(0) == '-') { |
| 407 | result = COMMENT; |
| 408 | req = "--"; |
| 409 | term = '-'; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 410 | } else if (peek(0) == '[') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 411 | result = CDSECT; |
| 412 | req = "[CDATA["; |
| 413 | term = ']'; |
| 414 | push = true; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 415 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 416 | result = DOCDECL; |
| 417 | req = "DOCTYPE"; |
| 418 | term = -1; |
| 419 | } |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 420 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 421 | error("illegal: <" + c); |
| 422 | return COMMENT; |
| 423 | } |
| 424 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 425 | for (int i = 0; i < req.length(); i++) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 426 | read(req.charAt(i)); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 427 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 428 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 429 | if (result == DOCDECL) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 430 | parseDoctype(push); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 431 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 432 | while (true) { |
| 433 | c = read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 434 | if (c == -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 435 | error(UNEXPECTED_EOF); |
| 436 | return COMMENT; |
| 437 | } |
| 438 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 439 | if (push) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 440 | push(c); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 441 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 442 | |
| 443 | if ((term == '?' || c == term) |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 444 | && peek(0) == term |
| 445 | && peek(1) == '>') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 446 | break; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 447 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 448 | |
| 449 | prev = c; |
| 450 | } |
| 451 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 452 | if (term == '-' && prev == '-' && !relaxed) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 453 | error("illegal comment delimiter: --->"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 454 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 455 | |
| 456 | read(); |
| 457 | read(); |
| 458 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 459 | if (push && term != '?') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 460 | txtPos--; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 461 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 462 | |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 467 | /** |
| 468 | * precondition: <! consumed |
| 469 | */ |
| 470 | private void parseDoctype(boolean push) throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 471 | int nesting = 1; |
| 472 | boolean quoted = false; |
| 473 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 474 | while (true) { |
| 475 | int i = read(); |
| 476 | switch (i) { |
| 477 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 478 | case -1: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 479 | error(UNEXPECTED_EOF); |
| 480 | return; |
| 481 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 482 | case '\'': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 483 | quoted = !quoted; |
| 484 | break; |
| 485 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 486 | case '<': |
| 487 | if (!quoted) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 488 | nesting++; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 489 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 490 | break; |
| 491 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 492 | case '>': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 493 | if (!quoted) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 494 | if ((--nesting) == 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 495 | return; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 496 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 497 | } |
| 498 | break; |
| 499 | } |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 500 | if (push) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 501 | push(i); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 502 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 506 | /** |
| 507 | * precondition: </ consumed |
| 508 | */ |
| 509 | private void parseEndTag() throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 510 | read(); // '<' |
| 511 | read(); // '/' |
| 512 | name = readName(); |
| 513 | skip(); |
| 514 | read('>'); |
| 515 | |
| 516 | int sp = (depth - 1) << 2; |
| 517 | |
| 518 | if (depth == 0) { |
| 519 | error("element stack empty"); |
| 520 | type = COMMENT; |
| 521 | return; |
| 522 | } |
| 523 | |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 524 | if (!relaxed) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 525 | if (!name.equals(elementStack[sp + 3])) { |
| 526 | error("expected: /" + elementStack[sp + 3] + " read: " + name); |
| 527 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 528 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 529 | namespace = elementStack[sp]; |
| 530 | prefix = elementStack[sp + 1]; |
| 531 | name = elementStack[sp + 2]; |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 532 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 535 | private int peekType() throws IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 536 | switch (peek(0)) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 537 | case -1: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 538 | return END_DOCUMENT; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 539 | case '&': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 540 | return ENTITY_REF; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 541 | case '<': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 542 | switch (peek(1)) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 543 | case '/': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 544 | return END_TAG; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 545 | case '?': |
| 546 | case '!': |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 547 | return LEGACY; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 548 | default: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 549 | return START_TAG; |
| 550 | } |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 551 | default: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 552 | return TEXT; |
| 553 | } |
| 554 | } |
| 555 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 556 | private String get(int pos) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 557 | return new String(txtBuf, pos, txtPos - pos); |
| 558 | } |
| 559 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 560 | private void push(int c) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 561 | isWhitespace &= c <= ' '; |
| 562 | |
| 563 | if (txtPos == txtBuf.length) { |
| 564 | char[] bigger = new char[txtPos * 4 / 3 + 4]; |
| 565 | System.arraycopy(txtBuf, 0, bigger, 0, txtPos); |
| 566 | txtBuf = bigger; |
| 567 | } |
| 568 | |
| 569 | txtBuf[txtPos++] = (char) c; |
| 570 | } |
| 571 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 572 | /** |
| 573 | * Sets name and attributes |
| 574 | */ |
| 575 | private void parseStartTag(boolean xmldecl) throws IOException, XmlPullParserException { |
| 576 | if (!xmldecl) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 577 | read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 578 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 579 | name = readName(); |
| 580 | attributeCount = 0; |
| 581 | |
| 582 | while (true) { |
| 583 | skip(); |
| 584 | |
| 585 | int c = peek(0); |
| 586 | |
| 587 | if (xmldecl) { |
| 588 | if (c == '?') { |
| 589 | read(); |
| 590 | read('>'); |
| 591 | return; |
| 592 | } |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 593 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 594 | if (c == '/') { |
| 595 | degenerated = true; |
| 596 | read(); |
| 597 | skip(); |
| 598 | read('>'); |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | if (c == '>' && !xmldecl) { |
| 603 | read(); |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (c == -1) { |
| 609 | error(UNEXPECTED_EOF); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
| 613 | String attrName = readName(); |
| 614 | |
| 615 | if (attrName.length() == 0) { |
| 616 | error("attr name expected"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | |
| 620 | int i = (attributeCount++) << 2; |
| 621 | |
| 622 | attributes = ensureCapacity(attributes, i + 4); |
| 623 | |
| 624 | attributes[i++] = ""; |
| 625 | attributes[i++] = null; |
| 626 | attributes[i++] = attrName; |
| 627 | |
| 628 | skip(); |
| 629 | |
| 630 | if (peek(0) != '=') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 631 | if (!relaxed) { |
| 632 | error("Attr.value missing f. " + attrName); |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 633 | } |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 634 | attributes[i] = attrName; |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 635 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 636 | read('='); |
| 637 | skip(); |
| 638 | int delimiter = peek(0); |
| 639 | |
| 640 | if (delimiter != '\'' && delimiter != '"') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 641 | if (!relaxed) { |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 642 | error("attr value delimiter missing!"); |
| 643 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 644 | delimiter = ' '; |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 645 | } else { |
| 646 | read(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 647 | } |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 648 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 649 | int p = txtPos; |
Elliott Hughes | 6bcf32a | 2009-11-16 21:23:11 -0800 | [diff] [blame] | 650 | pushText(delimiter, true, true); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 651 | |
| 652 | attributes[i] = get(p); |
| 653 | txtPos = p; |
| 654 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 655 | if (delimiter != ' ') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 656 | read(); // skip endquote |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 657 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | int sp = depth++ << 2; |
| 662 | |
| 663 | elementStack = ensureCapacity(elementStack, sp + 4); |
| 664 | elementStack[sp + 3] = name; |
| 665 | |
| 666 | if (depth >= nspCounts.length) { |
| 667 | int[] bigger = new int[depth + 4]; |
| 668 | System.arraycopy(nspCounts, 0, bigger, 0, nspCounts.length); |
| 669 | nspCounts = bigger; |
| 670 | } |
| 671 | |
| 672 | nspCounts[depth] = nspCounts[depth - 1]; |
| 673 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 674 | if (processNsp) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 675 | adjustNsp(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 676 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 677 | namespace = ""; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 678 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 679 | |
| 680 | elementStack[sp] = namespace; |
| 681 | elementStack[sp + 1] = prefix; |
| 682 | elementStack[sp + 2] = name; |
| 683 | } |
| 684 | |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 685 | /** |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 686 | * result: isWhitespace; if the setName parameter is set, the name of the entity is stored in |
| 687 | * "name" |
| 688 | */ |
| 689 | private void pushEntity() throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 690 | push(read()); // & |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 691 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 692 | int pos = txtPos; |
| 693 | |
| 694 | while (true) { |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 695 | int c = peek(0); |
| 696 | if (c == ';') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 697 | read(); |
| 698 | break; |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 699 | } |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 700 | if (c < 128 && (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') |
| 701 | && c != '_' && c != '-' && c != '#') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 702 | if (!relaxed) { |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 703 | error("unterminated entity ref"); |
| 704 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 705 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 706 | return; |
| 707 | } |
| 708 | |
Elliott Hughes | b211e13 | 2009-11-09 16:06:42 -0800 | [diff] [blame] | 709 | push(read()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | String code = get(pos); |
| 713 | txtPos = pos - 1; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 714 | if (token && type == ENTITY_REF) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 715 | name = code; |
| 716 | } |
| 717 | |
| 718 | if (code.charAt(0) == '#') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 719 | int c = code.charAt(1) == 'x' |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 720 | ? Integer.parseInt(code.substring(2), 16) |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 721 | : Integer.parseInt(code.substring(1)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 722 | push(c); |
| 723 | return; |
| 724 | } |
| 725 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 726 | String result = entityMap.get(code); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 727 | |
| 728 | unresolved = result == null; |
| 729 | |
| 730 | if (unresolved) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 731 | if (!token) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 732 | error("unresolved: &" + code + ";"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 733 | } |
| 734 | } else { |
| 735 | for (int i = 0; i < result.length(); i++) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 736 | push(result.charAt(i)); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 737 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 741 | /** |
| 742 | * types: '<': parse to any token (for nextToken ()) '"': parse to quote ' ': parse to |
| 743 | * whitespace or '>' |
| 744 | */ |
| 745 | private void pushText(int delimiter, boolean resolveEntities, boolean inAttributeValue) |
| 746 | throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 747 | |
| 748 | int next = peek(0); |
| 749 | int cbrCount = 0; |
| 750 | |
| 751 | while (next != -1 && next != delimiter) { // covers eof, '<', '"' |
| 752 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 753 | if (delimiter == ' ' && (next <= ' ' || next == '>')) { |
| 754 | break; |
| 755 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 756 | |
| 757 | if (next == '&') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 758 | if (!resolveEntities) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 759 | break; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 760 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 761 | |
| 762 | pushEntity(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 763 | } else if (next == '<' && inAttributeValue) { |
Elliott Hughes | 6bcf32a | 2009-11-16 21:23:11 -0800 | [diff] [blame] | 764 | error("Illegal: \"<\" inside attribute value"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 765 | } else if (next == '\n' && type == START_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 766 | read(); |
| 767 | push(' '); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 768 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 769 | push(read()); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 770 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 771 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 772 | /* |
| 773 | * "]]>" is allowed in attribute values, but is not allowed in |
| 774 | * regular text between markup. |
| 775 | */ |
Elliott Hughes | 6bcf32a | 2009-11-16 21:23:11 -0800 | [diff] [blame] | 776 | final boolean allowCloseCdata = inAttributeValue; |
| 777 | if (!allowCloseCdata && (next == '>' && cbrCount >= 2 && delimiter != ']')) { |
| 778 | error("Illegal: \"]]>\" outside CDATA section"); |
| 779 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 780 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 781 | if (next == ']') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 782 | cbrCount++; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 783 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 784 | cbrCount = 0; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 785 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 786 | |
| 787 | next = peek(0); |
| 788 | } |
| 789 | } |
| 790 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 791 | private void read(char c) throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 792 | int a = read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 793 | if (a != c) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 794 | error("expected: '" + c + "' actual: '" + ((char) a) + "'"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 795 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 798 | private int read() throws IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 799 | int result; |
| 800 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 801 | if (peekCount == 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 802 | result = peek(0); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 803 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 804 | result = peek[0]; |
| 805 | peek[0] = peek[1]; |
| 806 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 807 | peekCount--; |
| 808 | |
| 809 | column++; |
| 810 | |
| 811 | if (result == '\n') { |
| 812 | |
| 813 | line++; |
| 814 | column = 1; |
| 815 | } |
| 816 | |
| 817 | return result; |
| 818 | } |
| 819 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 820 | /** |
| 821 | * Does never read more than needed |
| 822 | */ |
| 823 | private int peek(int pos) throws IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 824 | while (pos >= peekCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 825 | int nw; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 826 | if (srcBuf.length <= 1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 827 | nw = reader.read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 828 | } else if (srcPos < srcCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 829 | nw = srcBuf[srcPos++]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 830 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 831 | srcCount = reader.read(srcBuf, 0, srcBuf.length); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 832 | if (srcCount <= 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 833 | nw = -1; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 834 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 835 | nw = srcBuf[0]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 836 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 837 | |
| 838 | srcPos = 1; |
| 839 | } |
| 840 | |
| 841 | if (nw == '\r') { |
| 842 | wasCR = true; |
| 843 | peek[peekCount++] = '\n'; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 844 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 845 | if (nw == '\n') { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 846 | if (!wasCR) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 847 | peek[peekCount++] = '\n'; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 848 | } |
| 849 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 850 | peek[peekCount++] = nw; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 851 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 852 | |
| 853 | wasCR = false; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return peek[pos]; |
| 858 | } |
| 859 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 860 | private String readName() throws IOException, XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 861 | int pos = txtPos; |
| 862 | int c = peek(0); |
| 863 | if ((c < 'a' || c > 'z') |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 864 | && (c < 'A' || c > 'Z') |
| 865 | && c != '_' |
| 866 | && c != ':' |
| 867 | && c < 0x0c0 |
| 868 | && !relaxed) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 869 | error("name expected"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 870 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 871 | |
| 872 | do { |
| 873 | push(read()); |
| 874 | c = peek(0); |
| 875 | } |
| 876 | while ((c >= 'a' && c <= 'z') |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 877 | || (c >= 'A' && c <= 'Z') |
| 878 | || (c >= '0' && c <= '9') |
| 879 | || c == '_' |
| 880 | || c == '-' |
| 881 | || c == ':' |
| 882 | || c == '.' |
| 883 | || c >= 0x0b7); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 884 | |
| 885 | String result = get(pos); |
| 886 | txtPos = pos; |
| 887 | return result; |
| 888 | } |
| 889 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 890 | private void skip() throws IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 891 | while (true) { |
| 892 | int c = peek(0); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 893 | if (c > ' ' || c == -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 894 | break; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 895 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 896 | read(); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // public part starts here... |
| 901 | |
| 902 | public void setInput(Reader reader) throws XmlPullParserException { |
| 903 | this.reader = reader; |
| 904 | |
| 905 | line = 1; |
| 906 | column = 0; |
| 907 | type = START_DOCUMENT; |
| 908 | name = null; |
| 909 | namespace = null; |
| 910 | degenerated = false; |
| 911 | attributeCount = -1; |
| 912 | encoding = null; |
| 913 | version = null; |
| 914 | standalone = null; |
| 915 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 916 | if (reader == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 917 | return; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 918 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 919 | |
| 920 | srcPos = 0; |
| 921 | srcCount = 0; |
| 922 | peekCount = 0; |
| 923 | depth = 0; |
| 924 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 925 | entityMap = new HashMap<String, String>(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 926 | entityMap.put("amp", "&"); |
| 927 | entityMap.put("apos", "'"); |
| 928 | entityMap.put("gt", ">"); |
| 929 | entityMap.put("lt", "<"); |
| 930 | entityMap.put("quot", "\""); |
| 931 | } |
| 932 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 933 | public void setInput(InputStream is, String _enc) throws XmlPullParserException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 934 | srcPos = 0; |
| 935 | srcCount = 0; |
| 936 | String enc = _enc; |
| 937 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 938 | if (is == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 939 | throw new IllegalArgumentException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 940 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 941 | |
| 942 | try { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 943 | if (enc == null) { |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 944 | // read four bytes |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 945 | |
| 946 | int chk = 0; |
| 947 | |
| 948 | while (srcCount < 4) { |
| 949 | int i = is.read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 950 | if (i == -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 951 | break; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 952 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 953 | chk = (chk << 8) | i; |
| 954 | srcBuf[srcCount++] = (char) i; |
| 955 | } |
| 956 | |
| 957 | if (srcCount == 4) { |
| 958 | switch (chk) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 959 | case 0x00000FEFF: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 960 | enc = "UTF-32BE"; |
| 961 | srcCount = 0; |
| 962 | break; |
| 963 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 964 | case 0x0FFFE0000: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 965 | enc = "UTF-32LE"; |
| 966 | srcCount = 0; |
| 967 | break; |
| 968 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 969 | case 0x03c: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 970 | enc = "UTF-32BE"; |
| 971 | srcBuf[0] = '<'; |
| 972 | srcCount = 1; |
| 973 | break; |
| 974 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 975 | case 0x03c000000: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 976 | enc = "UTF-32LE"; |
| 977 | srcBuf[0] = '<'; |
| 978 | srcCount = 1; |
| 979 | break; |
| 980 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 981 | case 0x0003c003f: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 982 | enc = "UTF-16BE"; |
| 983 | srcBuf[0] = '<'; |
| 984 | srcBuf[1] = '?'; |
| 985 | srcCount = 2; |
| 986 | break; |
| 987 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 988 | case 0x03c003f00: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 989 | enc = "UTF-16LE"; |
| 990 | srcBuf[0] = '<'; |
| 991 | srcBuf[1] = '?'; |
| 992 | srcCount = 2; |
| 993 | break; |
| 994 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 995 | case 0x03c3f786d: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 996 | while (true) { |
| 997 | int i = is.read(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 998 | if (i == -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 999 | break; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1000 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1001 | srcBuf[srcCount++] = (char) i; |
| 1002 | if (i == '>') { |
| 1003 | String s = new String(srcBuf, 0, srcCount); |
| 1004 | int i0 = s.indexOf("encoding"); |
| 1005 | if (i0 != -1) { |
| 1006 | while (s.charAt(i0) != '"' |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1007 | && s.charAt(i0) != '\'') { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1008 | i0++; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1009 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1010 | char deli = s.charAt(i0++); |
| 1011 | int i1 = s.indexOf(deli, i0); |
| 1012 | enc = s.substring(i0, i1); |
| 1013 | } |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1018 | default: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1019 | if ((chk & 0x0ffff0000) == 0x0FEFF0000) { |
| 1020 | enc = "UTF-16BE"; |
| 1021 | srcBuf[0] = |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1022 | (char) ((srcBuf[2] << 8) | srcBuf[3]); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1023 | srcCount = 1; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1024 | } else if ((chk & 0x0ffff0000) == 0x0fffe0000) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1025 | enc = "UTF-16LE"; |
| 1026 | srcBuf[0] = |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1027 | (char) ((srcBuf[3] << 8) | srcBuf[2]); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1028 | srcCount = 1; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1029 | } else if ((chk & 0x0ffffff00) == 0x0EFBBBF00) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1030 | enc = "UTF-8"; |
| 1031 | srcBuf[0] = srcBuf[3]; |
| 1032 | srcCount = 1; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1038 | if (enc == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1039 | enc = "UTF-8"; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1040 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1041 | |
| 1042 | int sc = srcCount; |
| 1043 | setInput(new InputStreamReader(is, enc)); |
| 1044 | encoding = _enc; |
| 1045 | srcCount = sc; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1046 | } catch (Exception e) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1047 | throw new XmlPullParserException( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1048 | "Invalid stream or encoding: " + e.toString(), |
| 1049 | this, |
| 1050 | e); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | public boolean getFeature(String feature) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1055 | if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1056 | return processNsp; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1057 | } else if (isProp(feature, false, "relaxed")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1058 | return relaxed; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1059 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1060 | return false; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1061 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | public String getInputEncoding() { |
| 1065 | return encoding; |
| 1066 | } |
| 1067 | |
| 1068 | public void defineEntityReplacementText(String entity, String value) |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1069 | throws XmlPullParserException { |
| 1070 | if (entityMap == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1071 | throw new RuntimeException("entity replacement text must be defined after setInput!"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1072 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1073 | entityMap.put(entity, value); |
| 1074 | } |
| 1075 | |
| 1076 | public Object getProperty(String property) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1077 | if (isProp(property, true, "xmldecl-version")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1078 | return version; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1079 | } |
| 1080 | if (isProp(property, true, "xmldecl-standalone")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1081 | return standalone; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1082 | } |
| 1083 | if (isProp(property, true, "location")) { |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 1084 | return location != null ? location : reader.toString(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1085 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1086 | return null; |
| 1087 | } |
| 1088 | |
| 1089 | public int getNamespaceCount(int depth) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1090 | if (depth > this.depth) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1091 | throw new IndexOutOfBoundsException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1092 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1093 | return nspCounts[depth]; |
| 1094 | } |
| 1095 | |
| 1096 | public String getNamespacePrefix(int pos) { |
| 1097 | return nspStack[pos << 1]; |
| 1098 | } |
| 1099 | |
| 1100 | public String getNamespaceUri(int pos) { |
| 1101 | return nspStack[(pos << 1) + 1]; |
| 1102 | } |
| 1103 | |
| 1104 | public String getNamespace(String prefix) { |
| 1105 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1106 | if ("xml".equals(prefix)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1107 | return "http://www.w3.org/XML/1998/namespace"; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1108 | } |
| 1109 | if ("xmlns".equals(prefix)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1110 | return "http://www.w3.org/2000/xmlns/"; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1111 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1112 | |
| 1113 | for (int i = (getNamespaceCount(depth) << 1) - 2; i >= 0; i -= 2) { |
| 1114 | if (prefix == null) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1115 | if (nspStack[i] == null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1116 | return nspStack[i + 1]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1117 | } |
| 1118 | } else if (prefix.equals(nspStack[i])) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1119 | return nspStack[i + 1]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1120 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1121 | } |
| 1122 | return null; |
| 1123 | } |
| 1124 | |
| 1125 | public int getDepth() { |
| 1126 | return depth; |
| 1127 | } |
| 1128 | |
| 1129 | public String getPositionDescription() { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1130 | StringBuilder buf = new StringBuilder(type < TYPES.length ? TYPES[type] : "unknown"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1131 | buf.append(' '); |
| 1132 | |
| 1133 | if (type == START_TAG || type == END_TAG) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1134 | if (degenerated) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1135 | buf.append("(empty) "); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1136 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1137 | buf.append('<'); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1138 | if (type == END_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1139 | buf.append('/'); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1140 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1141 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1142 | if (prefix != null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1143 | buf.append("{" + namespace + "}" + prefix + ":"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1144 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1145 | buf.append(name); |
| 1146 | |
| 1147 | int cnt = attributeCount << 2; |
| 1148 | for (int i = 0; i < cnt; i += 4) { |
| 1149 | buf.append(' '); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1150 | if (attributes[i + 1] != null) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1151 | buf.append( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1152 | "{" + attributes[i] + "}" + attributes[i + 1] + ":"); |
| 1153 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1154 | buf.append(attributes[i + 2] + "='" + attributes[i + 3] + "'"); |
| 1155 | } |
| 1156 | |
| 1157 | buf.append('>'); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1158 | } else if (type == IGNORABLE_WHITESPACE) { |
| 1159 | ; |
| 1160 | } else if (type != TEXT) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1161 | buf.append(getText()); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1162 | } else if (isWhitespace) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1163 | buf.append("(whitespace)"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1164 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1165 | String text = getText(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1166 | if (text.length() > 16) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1167 | text = text.substring(0, 16) + "..."; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1168 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1169 | buf.append(text); |
| 1170 | } |
| 1171 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1172 | buf.append("@" + line + ":" + column); |
| 1173 | if (location != null) { |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 1174 | buf.append(" in "); |
| 1175 | buf.append(location); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1176 | } else if (reader != null) { |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 1177 | buf.append(" in "); |
| 1178 | buf.append(reader.toString()); |
| 1179 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1180 | return buf.toString(); |
| 1181 | } |
| 1182 | |
| 1183 | public int getLineNumber() { |
| 1184 | return line; |
| 1185 | } |
| 1186 | |
| 1187 | public int getColumnNumber() { |
| 1188 | return column; |
| 1189 | } |
| 1190 | |
| 1191 | public boolean isWhitespace() throws XmlPullParserException { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1192 | if (type != TEXT && type != IGNORABLE_WHITESPACE && type != CDSECT) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1193 | exception(ILLEGAL_TYPE); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1194 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1195 | return isWhitespace; |
| 1196 | } |
| 1197 | |
| 1198 | public String getText() { |
| 1199 | return type < TEXT |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1200 | || (type == ENTITY_REF && unresolved) ? null : get(0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | public char[] getTextCharacters(int[] poslen) { |
| 1204 | if (type >= TEXT) { |
| 1205 | if (type == ENTITY_REF) { |
| 1206 | poslen[0] = 0; |
| 1207 | poslen[1] = name.length(); |
| 1208 | return name.toCharArray(); |
| 1209 | } |
| 1210 | poslen[0] = 0; |
| 1211 | poslen[1] = txtPos; |
| 1212 | return txtBuf; |
| 1213 | } |
| 1214 | |
| 1215 | poslen[0] = -1; |
| 1216 | poslen[1] = -1; |
| 1217 | return null; |
| 1218 | } |
| 1219 | |
| 1220 | public String getNamespace() { |
| 1221 | return namespace; |
| 1222 | } |
| 1223 | |
| 1224 | public String getName() { |
| 1225 | return name; |
| 1226 | } |
| 1227 | |
| 1228 | public String getPrefix() { |
| 1229 | return prefix; |
| 1230 | } |
| 1231 | |
| 1232 | public boolean isEmptyElementTag() throws XmlPullParserException { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1233 | if (type != START_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1234 | exception(ILLEGAL_TYPE); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1235 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1236 | return degenerated; |
| 1237 | } |
| 1238 | |
| 1239 | public int getAttributeCount() { |
| 1240 | return attributeCount; |
| 1241 | } |
| 1242 | |
| 1243 | public String getAttributeType(int index) { |
| 1244 | return "CDATA"; |
| 1245 | } |
| 1246 | |
| 1247 | public boolean isAttributeDefault(int index) { |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | public String getAttributeNamespace(int index) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1252 | if (index >= attributeCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1253 | throw new IndexOutOfBoundsException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1254 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1255 | return attributes[index << 2]; |
| 1256 | } |
| 1257 | |
| 1258 | public String getAttributeName(int index) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1259 | if (index >= attributeCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1260 | throw new IndexOutOfBoundsException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1261 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1262 | return attributes[(index << 2) + 2]; |
| 1263 | } |
| 1264 | |
| 1265 | public String getAttributePrefix(int index) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1266 | if (index >= attributeCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1267 | throw new IndexOutOfBoundsException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1268 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1269 | return attributes[(index << 2) + 1]; |
| 1270 | } |
| 1271 | |
| 1272 | public String getAttributeValue(int index) { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1273 | if (index >= attributeCount) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1274 | throw new IndexOutOfBoundsException(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1275 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1276 | return attributes[(index << 2) + 3]; |
| 1277 | } |
| 1278 | |
| 1279 | public String getAttributeValue(String namespace, String name) { |
| 1280 | |
| 1281 | for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) { |
| 1282 | if (attributes[i + 2].equals(name) |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1283 | && (namespace == null || attributes[i].equals(namespace))) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1284 | return attributes[i + 3]; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1285 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | return null; |
| 1289 | } |
| 1290 | |
| 1291 | public int getEventType() throws XmlPullParserException { |
| 1292 | return type; |
| 1293 | } |
| 1294 | |
| 1295 | public int next() throws XmlPullParserException, IOException { |
| 1296 | |
| 1297 | txtPos = 0; |
| 1298 | isWhitespace = true; |
| 1299 | int minType = 9999; |
| 1300 | token = false; |
| 1301 | |
| 1302 | do { |
| 1303 | nextImpl(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1304 | if (type < minType) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1305 | minType = type; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1306 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1307 | // if (curr <= TEXT) type = curr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1308 | } |
| 1309 | while (minType > ENTITY_REF // ignorable |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1310 | || (minType >= TEXT && peekType() >= TEXT)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1311 | |
| 1312 | type = minType; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1313 | if (type > TEXT) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1314 | type = TEXT; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1315 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1316 | |
| 1317 | return type; |
| 1318 | } |
| 1319 | |
| 1320 | public int nextToken() throws XmlPullParserException, IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1321 | isWhitespace = true; |
| 1322 | txtPos = 0; |
| 1323 | |
| 1324 | token = true; |
| 1325 | nextImpl(); |
| 1326 | return type; |
| 1327 | } |
| 1328 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1329 | // utility methods to make XML parsing easier ... |
| 1330 | |
| 1331 | public int nextTag() throws XmlPullParserException, IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1332 | next(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1333 | if (type == TEXT && isWhitespace) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1334 | next(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1335 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1336 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1337 | if (type != END_TAG && type != START_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1338 | exception("unexpected type"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1339 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1340 | |
| 1341 | return type; |
| 1342 | } |
| 1343 | |
| 1344 | public void require(int type, String namespace, String name) |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1345 | throws XmlPullParserException, IOException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1346 | |
| 1347 | if (type != this.type |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1348 | || (namespace != null && !namespace.equals(getNamespace())) |
| 1349 | || (name != null && !name.equals(getName()))) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1350 | exception( |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1351 | "expected: " + TYPES[type] + " {" + namespace + "}" + name); |
| 1352 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | public String nextText() throws XmlPullParserException, IOException { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1356 | if (type != START_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1357 | exception("precondition: START_TAG"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1358 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1359 | |
| 1360 | next(); |
| 1361 | |
| 1362 | String result; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1363 | if (type == TEXT) { |
| 1364 | result = getText(); |
| 1365 | next(); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1366 | } else { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1367 | result = ""; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1368 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1369 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1370 | if (type != END_TAG) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1371 | exception("END_TAG expected"); |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1372 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1373 | |
| 1374 | return result; |
| 1375 | } |
| 1376 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1377 | public void setFeature(String feature, boolean value) throws XmlPullParserException { |
| 1378 | if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1379 | processNsp = value; |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1380 | } else if (isProp(feature, false, "relaxed")) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1381 | relaxed = value; |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 1382 | } else { |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1383 | exception("unsupported feature: " + feature); |
Elliott Hughes | d21d78f | 2010-05-13 11:32:57 -0700 | [diff] [blame] | 1384 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Jesse Wilson | ccd79e2 | 2010-11-04 14:02:20 -0700 | [diff] [blame^] | 1387 | public void setProperty(String property, Object value) throws XmlPullParserException { |
| 1388 | if (isProp(property, true, "location")) { |
| 1389 | location = value; |
| 1390 | } else { |
| 1391 | throw new XmlPullParserException("unsupported property: " + property); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | } |