├── .gitignore
├── LICENSE
├── README.md
├── ccl_abx
├── README.md
├── TEST FILES
│ ├── test-basic.xml
│ ├── test-basic.xml.abx
│ ├── test-typed-attribute.xml
│ ├── test-typed-attribute.xml.abx
│ ├── test_interned_strings.xml
│ └── test_interned_strings.xml.abx
└── ccl_abx.py
└── makeabx
├── LICENSE
└── src
├── META-INF
└── MANIFEST.MF
├── com
├── android
│ └── org
│ │ └── kxml2
│ │ └── io
│ │ └── KXmlParser.java
└── ccl
│ └── abxmaker
│ ├── BinaryXmlSerializer.java
│ ├── Converter.java
│ ├── FastDataOutput.java
│ ├── FastXmlSerializer.java
│ ├── Main.java
│ └── TypedXmlSerializer.java
├── libcore
└── internal
│ └── StringPool.java
└── org
└── xmlpull
└── v1
├── XmlPullParser.java
├── XmlPullParserException.java
├── XmlPullParserFactory.java
└── XmlSerializer.java
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | __pycache__/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 CCL Solutions Group
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android-bits
2 | Various Android tools, utilities and modules
3 |
--------------------------------------------------------------------------------
/ccl_abx/README.md:
--------------------------------------------------------------------------------
1 | # ccl_abx
2 | `ccl_abx` is a Python module for reading Android Binary XML (ABX) files and converting them back to XML for processing. The module can also be executed at the command line to convert ABX files there.
3 |
4 | ## Command line usage
5 | To convert an ABX file at the command line:
6 |
7 | `ccl_abx.py file_path_here.xml`
8 |
9 | The converted data will be outputted to `STDOUT`, so if you want to save the file you can redirect the output:
10 |
11 | `ccl_abx.py file_path_here.xml > processed_file_path.xml`
12 |
13 | If the file being converted has multiple roots (such as the `settings_secure.xml` file) you can add the `-mr` switch to account for these multple roots:
14 |
15 | `ccl_abx.py file_path_here.xml -mr`
16 |
17 | ## Using the module in your scripts
18 | Use of the module is fairly straight-forward, a minimal example which reads a file provided at the command line might look like:
19 |
20 | ```python
21 | import sys
22 | import pathlib
23 | import ccl_abx
24 |
25 | input_path = pathlib.Path(sys.argv[1])
26 | with input_path.open("rb") as f:
27 | reader = ccl_abx.AbxReader(f) # Pass a binary file-like object into the AbxReader constructor
28 | doc = reader.read() # Call the reader object's read() function which returns an ElementTree Element which is the root of the document
29 |
30 | ```
31 |
32 | ## Known issues
33 | Our testing so far suggests that the module will round-trip an ABX created from a known XML file except for
34 | * Ignorable whitespace (e.g layout whitespace around tags) will be discarded
35 | * Elements with mixed content (text *and* child elements) will cause an exception to be raised - it doesn't appear that the methods that create ABX files in Android will create this structure though, so it appears to be a non-issue for now.
36 | * If the code which *encodes* the ABX file attempts to convert numerical attributes to floating-point numbers then the usual caveats around floating-point accuracy apply
37 |
38 | We have provided some test files so that you can confirm the behaviour of the module, and the `makeabx` Java application found in this repo can be used to generate further test files if needed.
39 |
--------------------------------------------------------------------------------
/ccl_abx/TEST FILES/test-basic.xml:
--------------------------------------------------------------------------------
1 |
51 | * The high-level design of the wire protocol is to directly serialize the event 52 | * stream, while efficiently and compactly writing strongly-typed primitives 53 | * delivered through the {@link TypedXmlSerializer} interface. 54 | *
55 | * Each serialized event is a single byte where the lower half is a normal 56 | * {@link XmlPullParser} token and the upper half is an optional data type 57 | * signal, such as {@link #TYPE_INT}. 58 | *
59 | * This serializer has some specific limitations: 60 | *
36 | * Benchmarks have demonstrated this class is 2x more efficient than using a
37 | * {@link DataOutputStream} with a {@link BufferedOutputStream}.
38 | */
39 | public class FastDataOutput implements DataOutput, Flushable, Closeable {
40 | private static final int MAX_UNSIGNED_SHORT = 65_535;
41 |
42 | //private final VMRuntime mRuntime;
43 | private final OutputStream mOut;
44 |
45 | private final byte[] mBuffer;
46 | //private final long mBufferPtr;
47 | private final int mBufferCap;
48 |
49 | private int mBufferPos;
50 |
51 | /**
52 | * Values that have been "interned" by {@link #writeInternedUTF(String)}.
53 | */
54 | private HashMap
150 | * Canonicalization is implemented by writing each unique string value once
151 | * the first time it appears, and then writing a lightweight {@code short}
152 | * reference when that string is written again in the future.
153 | *
154 | *
155 | */
156 | public void writeInternedUTF( String s) throws IOException {
157 | Short ref = mStringRefs.get(s);
158 | if (ref != null) {
159 | writeShort(ref);
160 | } else {
161 | writeShort(MAX_UNSIGNED_SHORT);
162 | writeUTF(s);
163 |
164 | // We can only safely intern when we have remaining values; if we're
165 | // full we at least sent the string value above
166 | ref = (short) mStringRefs.size();
167 | if (ref < MAX_UNSIGNED_SHORT) {
168 | mStringRefs.put(s, ref);
169 | }
170 | }
171 | }
172 |
173 | @Override
174 | public void writeBoolean(boolean v) throws IOException {
175 | writeByte(v ? 1 : 0);
176 | }
177 |
178 | @Override
179 | public void writeByte(int v) throws IOException {
180 | if (mBufferCap - mBufferPos < 1) drain();
181 | mBuffer[mBufferPos++] = (byte) ((v >> 0) & 0xff);
182 | }
183 |
184 | @Override
185 | public void writeShort(int v) throws IOException {
186 | if (mBufferCap - mBufferPos < 2) drain();
187 | mBuffer[mBufferPos++] = (byte) ((v >> 8) & 0xff);
188 | mBuffer[mBufferPos++] = (byte) ((v >> 0) & 0xff);
189 | }
190 |
191 | @Override
192 | public void writeChar(int v) throws IOException {
193 | writeShort((short) v);
194 | }
195 |
196 | @Override
197 | public void writeInt(int v) throws IOException {
198 | if (mBufferCap - mBufferPos < 4) drain();
199 | mBuffer[mBufferPos++] = (byte) ((v >> 24) & 0xff);
200 | mBuffer[mBufferPos++] = (byte) ((v >> 16) & 0xff);
201 | mBuffer[mBufferPos++] = (byte) ((v >> 8) & 0xff);
202 | mBuffer[mBufferPos++] = (byte) ((v >> 0) & 0xff);
203 | }
204 |
205 | @Override
206 | public void writeLong(long v) throws IOException {
207 | if (mBufferCap - mBufferPos < 8) drain();
208 | int i = (int) (v >> 32);
209 | mBuffer[mBufferPos++] = (byte) ((i >> 24) & 0xff);
210 | mBuffer[mBufferPos++] = (byte) ((i >> 16) & 0xff);
211 | mBuffer[mBufferPos++] = (byte) ((i >> 8) & 0xff);
212 | mBuffer[mBufferPos++] = (byte) ((i >> 0) & 0xff);
213 | i = (int) v;
214 | mBuffer[mBufferPos++] = (byte) ((i >> 24) & 0xff);
215 | mBuffer[mBufferPos++] = (byte) ((i >> 16) & 0xff);
216 | mBuffer[mBufferPos++] = (byte) ((i >> 8) & 0xff);
217 | mBuffer[mBufferPos++] = (byte) ((i >> 0) & 0xff);
218 | }
219 |
220 | @Override
221 | public void writeFloat(float v) throws IOException {
222 | writeInt(Float.floatToIntBits(v));
223 | }
224 |
225 | @Override
226 | public void writeDouble(double v) throws IOException {
227 | writeLong(Double.doubleToLongBits(v));
228 | }
229 |
230 | @Override
231 | public void writeBytes(String s) throws IOException {
232 | // Callers should use writeUTF()
233 | throw new UnsupportedOperationException();
234 | }
235 |
236 | @Override
237 | public void writeChars(String s) throws IOException {
238 | // Callers should use writeUTF()
239 | throw new UnsupportedOperationException();
240 | }
241 | }
242 |
243 |
244 |
--------------------------------------------------------------------------------
/makeabx/src/com/ccl/abxmaker/FastXmlSerializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Modified version of FastXmlSerializer
3 | * Original License:
4 | *
5 | * Copyright (C) 2006 The Android Open Source Project
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 |
20 |
21 | package com.ccl.abxmaker;
22 |
23 |
24 | //import android.compat.annotation.UnsupportedAppUsage;
25 |
26 | import org.xmlpull.v1.XmlSerializer;
27 |
28 | import java.io.IOException;
29 | import java.io.OutputStream;
30 | import java.io.OutputStreamWriter;
31 | import java.io.UnsupportedEncodingException;
32 | import java.io.Writer;
33 | import java.nio.ByteBuffer;
34 | import java.nio.CharBuffer;
35 | import java.nio.charset.Charset;
36 | import java.nio.charset.CharsetEncoder;
37 | import java.nio.charset.CoderResult;
38 | import java.nio.charset.CodingErrorAction;
39 | import java.nio.charset.IllegalCharsetNameException;
40 | import java.nio.charset.UnsupportedCharsetException;
41 |
42 | /**
43 | * This is a quick and dirty implementation of XmlSerializer that isn't horribly
44 | * painfully slow like the normal one. It only does what is needed for the
45 | * specific XML files being written with it.
46 | */
47 | public class FastXmlSerializer implements XmlSerializer {
48 | private static final String ESCAPE_TABLE[] = new String[] {
49 | "", "", "", "", "", "", "", "", // 0-7
50 | "", " ", "
", "", "", "
", "", "", // 8-15
51 | "", "", "", "", "", "", "", "", // 16-23
52 | "", "", "", "", "", "", "", "", // 24-31
53 | null, null, """, null, null, null, "&", null, // 32-39
54 | null, null, null, null, null, null, null, null, // 40-47
55 | null, null, null, null, null, null, null, null, // 48-55
56 | null, null, null, null, "<", null, ">", null, // 56-63
57 | };
58 |
59 | private static final int DEFAULT_BUFFER_LEN = 32*1024;
60 |
61 | private static String sSpace = " ";
62 |
63 | private final int mBufferLen;
64 | private final char[] mText;
65 | private int mPos;
66 |
67 | private Writer mWriter;
68 |
69 | private OutputStream mOutputStream;
70 | private CharsetEncoder mCharset;
71 | private ByteBuffer mBytes;
72 |
73 | private boolean mIndent = false;
74 | private boolean mInTag;
75 |
76 | private int mNesting = 0;
77 | private boolean mLineStart = true;
78 |
79 | //@UnsupportedAppUsage
80 | public FastXmlSerializer() {
81 | this(DEFAULT_BUFFER_LEN);
82 | }
83 |
84 | /**
85 | * Allocate a FastXmlSerializer with the given internal output buffer size. If the
86 | * size is zero or negative, then the default buffer size will be used.
87 | *
88 | * @param bufferSize Size in bytes of the in-memory output buffer that the writer will use.
89 | */
90 | public FastXmlSerializer(int bufferSize) {
91 | mBufferLen = (bufferSize > 0) ? bufferSize : DEFAULT_BUFFER_LEN;
92 | mText = new char[mBufferLen];
93 | mBytes = ByteBuffer.allocate(mBufferLen);
94 | }
95 |
96 | private void append(char c) throws IOException {
97 | int pos = mPos;
98 | if (pos >= (mBufferLen-1)) {
99 | flush();
100 | pos = mPos;
101 | }
102 | mText[pos] = c;
103 | mPos = pos+1;
104 | }
105 |
106 | private void append(String str, int i, final int length) throws IOException {
107 | if (length > mBufferLen) {
108 | final int end = i + length;
109 | while (i < end) {
110 | int next = i + mBufferLen;
111 | append(str, i, next There are following different
16 | * kinds of parser depending on which features are set: There are two key methods: next() and nextToken(). While next() provides
31 | * access to high level parsing events, nextToken() allows access to lower
32 | * level tokens.
33 | *
34 | * The current event state of the parser
35 | * can be determined by calling the
36 | * getEventType() method.
37 | * Initially, the parser is in the START_DOCUMENT
38 | * state.
39 | *
40 | * The method next() advances the parser to the
41 | * next event. The int value returned from next determines the current parser
42 | * state and is identical to the value returned from following calls to
43 | * getEventType ().
44 | *
45 | * Th following event types are seen by next() after first next() or nextToken() (or any other next*() method)
55 | * is called user application can obtain
56 | * XML version, standalone and encoding from XML declaration
57 | * in following ways: The above example will generate the following output:
110 | * For more details on API usage, please refer to the
119 | * quick Introduction available at http://www.xmlpull.org
120 | *
121 | * @see XmlPullParserFactory
122 | * @see #defineEntityReplacementText
123 | * @see #getName
124 | * @see #getNamespace
125 | * @see #getText
126 | * @see #next
127 | * @see #nextToken
128 | * @see #setInput
129 | * @see #FEATURE_PROCESS_DOCDECL
130 | * @see #FEATURE_VALIDATION
131 | * @see #START_DOCUMENT
132 | * @see #START_TAG
133 | * @see #TEXT
134 | * @see #END_TAG
135 | * @see #END_DOCUMENT
136 | *
137 | * @author Stefan Haustein
138 | * @author Aleksander Slominski
139 | */
140 |
141 | public interface XmlPullParser {
142 |
143 | /** This constant represents the default namespace (empty string "") */
144 | String NO_NAMESPACE = "";
145 |
146 | // ----------------------------------------------------------------------------
147 | // EVENT TYPES as reported by next()
148 |
149 | /**
150 | * Signalize that parser is at the very beginning of the document
151 | * and nothing was read yet.
152 | * This event type can only be observed by calling getEvent()
153 | * before the first call to next(), nextToken, or nextTag()).
154 | *
155 | * @see #next
156 | * @see #nextToken
157 | */
158 | int START_DOCUMENT = 0;
159 |
160 | /**
161 | * Logical end of the xml document. Returned from getEventType, next()
162 | * and nextToken()
163 | * when the end of the input document has been reached.
164 | * NOTE: subsequent calls to
165 | * next() or nextToken()
166 | * may result in exception being thrown.
167 | *
168 | * @see #next
169 | * @see #nextToken
170 | */
171 | int END_DOCUMENT = 1;
172 |
173 | /**
174 | * Returned from getEventType(),
175 | * next(), nextToken() when
176 | * a start tag was read.
177 | * The name of start tag is available from getName(), its namespace and prefix are
178 | * available from getNamespace() and getPrefix()
179 | * if namespaces are enabled.
180 | * See getAttribute* methods to retrieve element attributes.
181 | * See getNamespace* methods to retrieve newly declared namespaces.
182 | *
183 | * @see #next
184 | * @see #nextToken
185 | * @see #getName
186 | * @see #getPrefix
187 | * @see #getNamespace
188 | * @see #getAttributeCount
189 | * @see #getDepth
190 | * @see #getNamespaceCount
191 | * @see #getNamespace
192 | * @see #FEATURE_PROCESS_NAMESPACES
193 | */
194 | int START_TAG = 2;
195 |
196 | /**
197 | * Returned from getEventType(), next(), or
198 | * nextToken() when an end tag was read.
199 | * The name of start tag is available from getName(), its
200 | * namespace and prefix are
201 | * available from getNamespace() and getPrefix().
202 | *
203 | * @see #next
204 | * @see #nextToken
205 | * @see #getName
206 | * @see #getPrefix
207 | * @see #getNamespace
208 | * @see #FEATURE_PROCESS_NAMESPACES
209 | */
210 | int END_TAG = 3;
211 |
212 |
213 | /**
214 | * Character data was read and will is available by calling getText().
215 | * Please note: next() will
216 | * accumulate multiple
217 | * events into one TEXT event, skipping IGNORABLE_WHITESPACE,
218 | * PROCESSING_INSTRUCTION and COMMENT events,
219 | * In contrast, nextToken() will stop reading
220 | * text when any other event is observed.
221 | * Also, when the state was reached by calling next(), the text value will
222 | * be normalized, whereas getText() will
223 | * return unnormalized content in the case of nextToken(). This allows
224 | * an exact roundtrip without changing line ends when examining low
225 | * level events, whereas for high level applications the text is
226 | * normalized appropriately.
227 | *
228 | * @see #next
229 | * @see #nextToken
230 | * @see #getText
231 | */
232 | int TEXT = 4;
233 |
234 | // ----------------------------------------------------------------------------
235 | // additional events exposed by lower level nextToken()
236 |
237 | /**
238 | * A CDATA sections was just read;
239 | * this token is available only from calls to nextToken().
240 | * A call to next() will accumulate various text events into a single event
241 | * of type TEXT. The text contained in the CDATA section is available
242 | * by calling getText().
243 | *
244 | * @see #nextToken
245 | * @see #getText
246 | */
247 | int CDSECT = 5;
248 |
249 | /**
250 | * An entity reference was just read;
251 | * this token is available from nextToken()
252 | * only. The entity name is available by calling getName(). If available,
253 | * the replacement text can be obtained by calling getText(); otherwise,
254 | * the user is responsible for resolving the entity reference.
255 | * This event type is never returned from next(); next() will
256 | * accumulate the replacement text and other text
257 | * events to a single TEXT event.
258 | *
259 | * @see #nextToken
260 | * @see #getText
261 | */
262 | int ENTITY_REF = 6;
263 |
264 | /**
265 | * Ignorable whitespace was just read.
266 | * This token is available only from nextToken()).
267 | * For non-validating
268 | * parsers, this event is only reported by nextToken() when outside
269 | * the root element.
270 | * Validating parsers may be able to detect ignorable whitespace at
271 | * other locations.
272 | * The ignorable whitespace string is available by calling getText()
273 | *
274 | * NOTE: this is different from calling the
275 | * isWhitespace() method, since text content
276 | * may be whitespace but not ignorable.
277 | *
278 | * Ignorable whitespace is skipped by next() automatically; this event
279 | * type is never returned from next().
280 | *
281 | * @see #nextToken
282 | * @see #getText
283 | */
284 | int IGNORABLE_WHITESPACE = 7;
285 |
286 | /**
287 | * An XML processing instruction declaration was just read. This
288 | * event type is available only via nextToken().
289 | * getText() will return text that is inside the processing instruction.
290 | * Calls to next() will skip processing instructions automatically.
291 | * @see #nextToken
292 | * @see #getText
293 | */
294 | int PROCESSING_INSTRUCTION = 8;
295 |
296 | /**
297 | * An XML comment was just read. This event type is this token is
298 | * available via nextToken() only;
299 | * calls to next() will skip comments automatically.
300 | * The content of the comment can be accessed using the getText()
301 | * method.
302 | *
303 | * @see #nextToken
304 | * @see #getText
305 | */
306 | int COMMENT = 9;
307 |
308 | /**
309 | * An XML document type declaration was just read. This token is
310 | * available from nextToken() only.
311 | * The unparsed text inside the doctype is available via
312 | * the getText() method.
313 | *
314 | * @see #nextToken
315 | * @see #getText
316 | */
317 | int DOCDECL = 10;
318 |
319 | /**
320 | * This array can be used to convert the event type integer constants
321 | * such as START_TAG or TEXT to
322 | * to a string. For example, the value of TYPES[START_TAG] is
323 | * the string "START_TAG".
324 | *
325 | * This array is intended for diagnostic output only. Relying
326 | * on the contents of the array may be dangerous since malicious
327 | * applications may alter the array, although it is final, due
328 | * to limitations of the Java language.
329 | */
330 | String [] TYPES = {
331 | "START_DOCUMENT",
332 | "END_DOCUMENT",
333 | "START_TAG",
334 | "END_TAG",
335 | "TEXT",
336 | "CDSECT",
337 | "ENTITY_REF",
338 | "IGNORABLE_WHITESPACE",
339 | "PROCESSING_INSTRUCTION",
340 | "COMMENT",
341 | "DOCDECL"
342 | };
343 |
344 |
345 | // ----------------------------------------------------------------------------
346 | // namespace related features
347 |
348 | /**
349 | * This feature determines whether the parser processes
350 | * namespaces. As for all features, the default value is false.
351 | * NOTE: The value can not be changed during
352 | * parsing an must be set before parsing.
353 | *
354 | * @see #getFeature
355 | * @see #setFeature
356 | */
357 | String FEATURE_PROCESS_NAMESPACES =
358 | "http://xmlpull.org/v1/doc/features.html#process-namespaces";
359 |
360 | /**
361 | * This feature determines whether namespace attributes are
362 | * exposed via the attribute access methods. Like all features,
363 | * the default value is false. This feature cannot be changed
364 | * during parsing.
365 | *
366 | * @see #getFeature
367 | * @see #setFeature
368 | */
369 | String FEATURE_REPORT_NAMESPACE_ATTRIBUTES =
370 | "http://xmlpull.org/v1/doc/features.html#report-namespace-prefixes";
371 |
372 | /**
373 | * This feature determines whether the document declaration
374 | * is processed. If set to false,
375 | * the DOCDECL event type is reported by nextToken()
376 | * and ignored by next().
377 | *
378 | * If this feature is activated, then the document declaration
379 | * must be processed by the parser.
380 | *
381 | * Please note: If the document type declaration
382 | * was ignored, entity references may cause exceptions
383 | * later in the parsing process.
384 | * The default value of this feature is false. It cannot be changed
385 | * during parsing.
386 | *
387 | * @see #getFeature
388 | * @see #setFeature
389 | */
390 | String FEATURE_PROCESS_DOCDECL =
391 | "http://xmlpull.org/v1/doc/features.html#process-docdecl";
392 |
393 | /**
394 | * If this feature is activated, all validation errors as
395 | * defined in the XML 1.0 specification are reported.
396 | * This implies that FEATURE_PROCESS_DOCDECL is true and both, the
397 | * internal and external document type declaration will be processed.
398 | * Please Note: This feature can not be changed
399 | * during parsing. The default value is false.
400 | *
401 | * @see #getFeature
402 | * @see #setFeature
403 | */
404 | String FEATURE_VALIDATION =
405 | "http://xmlpull.org/v1/doc/features.html#validation";
406 |
407 | /**
408 | * Use this call to change the general behaviour of the parser,
409 | * such as namespace processing or doctype declaration handling.
410 | * This method must be called before the first call to next or
411 | * nextToken. Otherwise, an exception is thrown.
412 | * Example: call setFeature(FEATURE_PROCESS_NAMESPACES, true) in order
413 | * to switch on namespace processing. The initial settings correspond
414 | * to the properties requested from the XML Pull Parser factory.
415 | * If none were requested, all features are deactivated by default.
416 | *
417 | * @exception XmlPullParserException If the feature is not supported or can not be set
418 | * @exception IllegalArgumentException If string with the feature name is null
419 | */
420 | void setFeature(String name,
421 | boolean state) throws XmlPullParserException;
422 |
423 | /**
424 | * Returns the current value of the given feature.
425 | * Please note: unknown features are
426 | * always returned as false.
427 | *
428 | * @param name The name of feature to be retrieved.
429 | * @return The value of the feature.
430 | * @exception IllegalArgumentException if string the feature name is null
431 | */
432 |
433 | boolean getFeature(String name);
434 |
435 | /**
436 | * Set the value of a property.
437 | *
438 | * The property name is any fully-qualified URI.
439 | *
440 | * @exception XmlPullParserException If the property is not supported or can not be set
441 | * @exception IllegalArgumentException If string with the property name is null
442 | */
443 | void setProperty(String name,
444 | Object value) throws XmlPullParserException;
445 |
446 | /**
447 | * Look up the value of a property.
448 | *
449 | * The property name is any fully-qualified URI.
450 | * NOTE: unknown properties are always
451 | * returned as null.
452 | *
453 | * @param name The name of property to be retrieved.
454 | * @return The value of named property.
455 | */
456 | Object getProperty(String name);
457 |
458 |
459 | /**
460 | * Set the input source for parser to the given reader and
461 | * resets the parser. The event type is set to the initial value
462 | * START_DOCUMENT.
463 | * Setting the reader to null will just stop parsing and
464 | * reset parser state,
465 | * allowing the parser to free internal resources
466 | * such as parsing buffers.
467 | */
468 | void setInput(Reader in) throws XmlPullParserException;
469 |
470 |
471 | /**
472 | * Sets the input stream the parser is going to process.
473 | * This call resets the parser state and sets the event type
474 | * to the initial value START_DOCUMENT.
475 | *
476 | * NOTE: If an input encoding string is passed,
477 | * it MUST be used. Otherwise,
478 | * if inputEncoding is null, the parser SHOULD try to determine
479 | * input encoding following XML 1.0 specification (see below).
480 | * If encoding detection is supported then following feature
481 | * http://xmlpull.org/v1/doc/features.html#detect-encoding
482 | * MUST be true amd otherwise it must be false
483 | *
484 | * @param inputStream contains a raw byte input stream of possibly
485 | * unknown encoding (when inputEncoding is null).
486 | *
487 | * @param inputEncoding if not null it MUST be used as encoding for inputStream
488 | */
489 | void setInput(InputStream inputStream, String inputEncoding)
490 | throws XmlPullParserException;
491 |
492 | /**
493 | * Returns the input encoding if known, null otherwise.
494 | * If setInput(InputStream, inputEncoding) was called with an inputEncoding
495 | * value other than null, this value must be returned
496 | * from this method. Otherwise, if inputEncoding is null and
497 | * the parser supports the encoding detection feature
498 | * (http://xmlpull.org/v1/doc/features.html#detect-encoding),
499 | * it must return the detected encoding.
500 | * If setInput(Reader) was called, null is returned.
501 | * After first call to next if XML declaration was present this method
502 | * will return encoding declared.
503 | */
504 | String getInputEncoding();
505 |
506 | /**
507 | * Set new value for entity replacement text as defined in
508 | * XML 1.0 Section 4.5
509 | * Construction of Internal Entity Replacement Text.
510 | * If FEATURE_PROCESS_DOCDECL or FEATURE_VALIDATION are set, calling this
511 | * function will result in an exception -- when processing of DOCDECL is
512 | * enabled, there is no need to the entity replacement text manually.
513 | *
514 | * The motivation for this function is to allow very small
515 | * implementations of XMLPULL that will work in J2ME environments.
516 | * Though these implementations may not be able to process the document type
517 | * declaration, they still can work with known DTDs by using this function.
518 | *
519 | * Please notes: The given value is used literally as replacement text
520 | * and it corresponds to declaring entity in DTD that has all special characters
521 | * escaped: left angle bracket is replaced with <, ampersand with &
522 | * and so on.
523 | *
524 | * Note: The given value is the literal replacement text and must not
525 | * contain any other entity reference (if it contains any entity reference
526 | * there will be no further replacement).
527 | *
528 | * Note: The list of pre-defined entity names will
529 | * always contain standard XML entities such as
530 | * amp (&), lt (<), gt (>), quot ("), and apos (').
531 | * Those cannot be redefined by this method!
532 | *
533 | * @see #setInput
534 | * @see #FEATURE_PROCESS_DOCDECL
535 | * @see #FEATURE_VALIDATION
536 | */
537 | void defineEntityReplacementText( String entityName,
538 | String replacementText ) throws XmlPullParserException;
539 |
540 | /**
541 | * Returns the numbers of elements in the namespace stack for the given
542 | * depth.
543 | * If namespaces are not enabled, 0 is returned.
544 | *
545 | * NOTE: when parser is on END_TAG then it is allowed to call
546 | * this function with getDepth()+1 argument to retrieve position of namespace
547 | * prefixes and URIs that were declared on corresponding START_TAG.
548 | * NOTE: to retrieve list of namespaces declared in current element: Please note: when the parser is on an END_TAG,
572 | * namespace prefixes that were declared
573 | * in the corresponding START_TAG are still accessible
574 | * although they are no longer in scope.
575 | */
576 | String getNamespacePrefix(int pos) throws XmlPullParserException;
577 |
578 | /**
579 | * Returns the namespace URI for the given position in the
580 | * namespace stack
581 | * If the position is out of range, an exception is thrown.
582 | * NOTE: when parser is on END_TAG then namespace prefixes that were declared
583 | * in corresponding START_TAG are still accessible even though they are not in scope
584 | */
585 | String getNamespaceUri(int pos) throws XmlPullParserException;
586 |
587 | /**
588 | * Returns the URI corresponding to the given prefix,
589 | * depending on current state of the parser.
590 | *
591 | * If the prefix was not declared in the current scope,
592 | * null is returned. The default namespace is included
593 | * in the namespace table and is available via
594 | * getNamespace (null).
595 | *
596 | * This method is a convenience method for
597 | *
598 | * Please note: parser implementations
608 | * may provide more efficient lookup, e.g. using a Hashtable.
609 | * The 'xml' prefix is bound to "http://www.w3.org/XML/1998/namespace", as
610 | * defined in the
611 | * Namespaces in XML
612 | * specification. Analogous, the 'xmlns' prefix is resolved to
613 | * http://www.w3.org/2000/xmlns/
614 | *
615 | * @see #getNamespaceCount
616 | * @see #getNamespacePrefix
617 | * @see #getNamespaceUri
618 | */
619 | String getNamespace (String prefix);
620 |
621 |
622 | // --------------------------------------------------------------------------
623 | // miscellaneous reporting methods
624 |
625 | /**
626 | * Returns the current depth of the element.
627 | * Outside the root element, the depth is 0. The
628 | * depth is incremented by 1 when a start tag is reached.
629 | * The depth is decremented AFTER the end tag
630 | * event was observed.
631 | *
632 | * Please note: non-validating parsers are not
685 | * able to distinguish whitespace and ignorable whitespace,
686 | * except from whitespace outside the root element. Ignorable
687 | * whitespace is reported as separate event, which is exposed
688 | * via nextToken only.
689 | *
690 | */
691 | boolean isWhitespace() throws XmlPullParserException;
692 |
693 | /**
694 | * Returns the text content of the current event as String.
695 | * The value returned depends on current event type,
696 | * for example for TEXT event it is element content
697 | * (this is typical case when next() is used).
698 | *
699 | * See description of nextToken() for detailed description of
700 | * possible returned values for different types of events.
701 | *
702 | * NOTE: in case of ENTITY_REF, this method returns
703 | * the entity replacement text (or null if not available). This is
704 | * the only case where
705 | * getText() and getTextCharacters() return different values.
706 | *
707 | * @see #getEventType
708 | * @see #next
709 | * @see #nextToken
710 | */
711 | String getText ();
712 |
713 |
714 | /**
715 | * Returns the buffer that contains the text of the current event,
716 | * as well as the start offset and length relevant for the current
717 | * event. See getText(), next() and nextToken() for description of possible returned values.
718 | *
719 | * Please note: this buffer must not
720 | * be modified and its content MAY change after a call to
721 | * next() or nextToken(). This method will always return the
722 | * same value as getText(), except for ENTITY_REF. In the case
723 | * of ENTITY ref, getText() returns the replacement text and
724 | * this method returns the actual input buffer containing the
725 | * entity name.
726 | * If getText() returns null, this method returns null as well and
727 | * the values returned in the holder array MUST be -1 (both start
728 | * and length).
729 | *
730 | * @see #getText
731 | * @see #next
732 | * @see #nextToken
733 | *
734 | * @param holderForStartAndLength Must hold an 2-element int array
735 | * into which the start offset and length values will be written.
736 | * @return char buffer that contains the text of the current event
737 | * (null if the current event has no text associated).
738 | */
739 | char[] getTextCharacters(int [] holderForStartAndLength);
740 |
741 | // --------------------------------------------------------------------------
742 | // START_TAG / END_TAG shared methods
743 |
744 | /**
745 | * Returns the namespace URI of the current element.
746 | * The default namespace is represented
747 | * as empty string.
748 | * If namespaces are not enabled, an empty String ("") is always returned.
749 | * The current event must be START_TAG or END_TAG; otherwise,
750 | * null is returned.
751 | */
752 | String getNamespace ();
753 |
754 | /**
755 | * For START_TAG or END_TAG events, the (local) name of the current
756 | * element is returned when namespaces are enabled. When namespace
757 | * processing is disabled, the raw name is returned.
758 | * For ENTITY_REF events, the entity name is returned.
759 | * If the current event is not START_TAG, END_TAG, or ENTITY_REF,
760 | * null is returned.
761 | * Please note: To reconstruct the raw element name
762 | * when namespaces are enabled and the prefix is not null,
763 | * you will need to add the prefix and a colon to localName..
764 | *
765 | */
766 | String getName();
767 |
768 | /**
769 | * Returns the prefix of the current element.
770 | * If the element is in the default namespace (has no prefix),
771 | * null is returned.
772 | * If namespaces are not enabled, or the current event
773 | * is not START_TAG or END_TAG, null is returned.
774 | */
775 | String getPrefix();
776 |
777 | /**
778 | * Returns true if the current event is START_TAG and the tag
779 | * is degenerated
780 | * (e.g. <foobar/>).
781 | * NOTE: if the parser is not on START_TAG, an exception
782 | * will be thrown.
783 | */
784 | boolean isEmptyElementTag() throws XmlPullParserException;
785 |
786 | // --------------------------------------------------------------------------
787 | // START_TAG Attributes retrieval methods
788 |
789 | /**
790 | * Returns the number of attributes of the current start tag, or
791 | * -1 if the current event type is not START_TAG
792 | *
793 | * @see #getAttributeNamespace
794 | * @see #getAttributeName
795 | * @see #getAttributePrefix
796 | * @see #getAttributeValue
797 | */
798 | int getAttributeCount();
799 |
800 | /**
801 | * Returns the namespace URI of the attribute
802 | * with the given index (starts from 0).
803 | * Returns an empty string ("") if namespaces are not enabled
804 | * or the attribute has no namespace.
805 | * Throws an IndexOutOfBoundsException if the index is out of range
806 | * or the current event type is not START_TAG.
807 | *
808 | * NOTE: if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set
809 | * then namespace attributes (xmlns:ns='...') must be reported
810 | * with namespace
811 | * http://www.w3.org/2000/xmlns/
812 | * (visit this URL for description!).
813 | * The default namespace attribute (xmlns="...") will be reported with empty namespace.
814 | * NOTE:The xml prefix is bound as defined in
815 | * Namespaces in XML
816 | * specification to "http://www.w3.org/XML/1998/namespace".
817 | *
818 | * @param index zero-based index of attribute
819 | * @return attribute namespace,
820 | * empty string ("") is returned if namespaces processing is not enabled or
821 | * namespaces processing is enabled but attribute has no namespace (it has no prefix).
822 | */
823 | String getAttributeNamespace (int index);
824 |
825 | /**
826 | * Returns the local name of the specified attribute
827 | * if namespaces are enabled or just attribute name if namespaces are disabled.
828 | * Throws an IndexOutOfBoundsException if the index is out of range
829 | * or current event type is not START_TAG.
830 | *
831 | * @param index zero-based index of attribute
832 | * @return attribute name (null is never returned)
833 | */
834 | String getAttributeName (int index);
835 |
836 | /**
837 | * Returns the prefix of the specified attribute
838 | * Returns null if the element has no prefix.
839 | * If namespaces are disabled it will always return null.
840 | * Throws an IndexOutOfBoundsException if the index is out of range
841 | * or current event type is not START_TAG.
842 | *
843 | * @param index zero-based index of attribute
844 | * @return attribute prefix or null if namespaces processing is not enabled.
845 | */
846 | String getAttributePrefix(int index);
847 |
848 | /**
849 | * Returns the type of the specified attribute
850 | * If parser is non-validating it MUST return CDATA.
851 | *
852 | * @param index zero-based index of attribute
853 | * @return attribute type (null is never returned)
854 | */
855 | String getAttributeType(int index);
856 |
857 | /**
858 | * Returns if the specified attribute was not in input was declared in XML.
859 | * If parser is non-validating it MUST always return false.
860 | * This information is part of XML infoset:
861 | *
862 | * @param index zero-based index of attribute
863 | * @return false if attribute was in input
864 | */
865 | boolean isAttributeDefault(int index);
866 |
867 | /**
868 | * Returns the given attributes value.
869 | * Throws an IndexOutOfBoundsException if the index is out of range
870 | * or current event type is not START_TAG.
871 | *
872 | * NOTE: attribute value must be normalized
873 | * (including entity replacement text if PROCESS_DOCDECL is false) as described in
874 | * XML 1.0 section
875 | * 3.3.3 Attribute-Value Normalization
876 | *
877 | * @see #defineEntityReplacementText
878 | *
879 | * @param index zero-based index of attribute
880 | * @return value of attribute (null is never returned)
881 | */
882 | String getAttributeValue(int index);
883 |
884 | /**
885 | * Returns the attributes value identified by namespace URI and namespace localName.
886 | * If namespaces are disabled namespace must be null.
887 | * If current event type is not START_TAG then IndexOutOfBoundsException will be thrown.
888 | *
889 | * NOTE: attribute value must be normalized
890 | * (including entity replacement text if PROCESS_DOCDECL is false) as described in
891 | * XML 1.0 section
892 | * 3.3.3 Attribute-Value Normalization
893 | *
894 | * @see #defineEntityReplacementText
895 | *
896 | * @param namespace Namespace of the attribute if namespaces are enabled otherwise must be null
897 | * @param name If namespaces enabled local name of attribute otherwise just attribute name
898 | * @return value of attribute or null if attribute with given name does not exist
899 | */
900 | String getAttributeValue(String namespace,
901 | String name);
902 |
903 | // --------------------------------------------------------------------------
904 | // actual parsing methods
905 |
906 | /**
907 | * Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)
908 | *
909 | * @see #next()
910 | * @see #nextToken()
911 | */
912 | int getEventType()
913 | throws XmlPullParserException;
914 |
915 | /**
916 | * Get next parsing event - element content will be coalesced and only one
917 | * TEXT event must be returned for whole element content
918 | * (comments and processing instructions will be ignored and entity references
919 | * must be expanded or exception must be thrown if entity reference can not be expanded).
920 | * If element content is empty (content is "") then no TEXT event will be reported.
921 | *
922 | * NOTE: empty element (such as <tag/>) will be reported
923 | * with two separate events: START_TAG, END_TAG - it must be so to preserve
924 | * parsing equivalency of empty element to <tag></tag>.
925 | * (see isEmptyElementTag ())
926 | *
927 | * @see #isEmptyElementTag
928 | * @see #START_TAG
929 | * @see #TEXT
930 | * @see #END_TAG
931 | * @see #END_DOCUMENT
932 | */
933 |
934 | int next()
935 | throws XmlPullParserException, IOException;
936 |
937 |
938 | /**
939 | * This method works similarly to next() but will expose
940 | * additional event types (COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, or
941 | * IGNORABLE_WHITESPACE) if they are available in input.
942 | *
943 | * If special feature
944 | * FEATURE_XML_ROUNDTRIP
945 | * (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip)
946 | * is enabled it is possible to do XML document round trip ie. reproduce
947 | * exectly on output the XML input using getText():
948 | * returned content is always unnormalized (exactly as in input).
949 | * Otherwise returned content is end-of-line normalized as described
950 | * XML 1.0 End-of-Line Handling
951 | * and. Also when this feature is enabled exact content of START_TAG, END_TAG,
952 | * DOCDECL and PROCESSING_INSTRUCTION is available.
953 | *
954 | * Here is the list of tokens that can be returned from nextToken()
955 | * and what getText() and getTextCharacters() returns: for input document that contained: NOTE: there is no guarantee that there will only one TEXT or
1006 | * IGNORABLE_WHITESPACE event from nextToken() as parser may chose to deliver element content in
1007 | * multiple tokens (dividing element content into chunks)
1008 | *
1009 | * NOTE: whether returned text of token is end-of-line normalized
1010 | * is depending on FEATURE_XML_ROUNDTRIP.
1011 | *
1012 | * NOTE: XMLDecl (<?xml ...?>) is not reported but its content
1013 | * is available through optional properties (see class description above).
1014 | *
1015 | * @see #next
1016 | * @see #START_TAG
1017 | * @see #TEXT
1018 | * @see #END_TAG
1019 | * @see #END_DOCUMENT
1020 | * @see #COMMENT
1021 | * @see #DOCDECL
1022 | * @see #PROCESSING_INSTRUCTION
1023 | * @see #ENTITY_REF
1024 | * @see #IGNORABLE_WHITESPACE
1025 | */
1026 | int nextToken()
1027 | throws XmlPullParserException, IOException;
1028 |
1029 | //-----------------------------------------------------------------------------
1030 | // utility methods to mak XML parsing easier ...
1031 |
1032 | /**
1033 | * Test if the current event is of the given type and if the
1034 | * namespace and name do match. null will match any namespace
1035 | * and any name. If the test is not passed, an exception is
1036 | * thrown. The exception text indicates the parser position,
1037 | * the expected event and the current event that is not meeting the
1038 | * requirement.
1039 | *
1040 | * Essentially it does this
1041 | * The motivation for this function is to allow to parse consistently both
1057 | * empty elements and elements that has non empty content, for example for input: Essentially it does this
1072 | * Warning: Prior to API level 14, the pull parser returned by {@code
1095 | * android.util.Xml} did not always advance to the END_TAG event when this method was called.
1096 | * Work around by using manually advancing after calls to nextText(): essentially it does this
1111 | * NOTE: factory features are not used for XML Serializer.
50 | *
51 | * @param name string with URI identifying feature
52 | * @param state if true feature will be set; if false will be ignored
53 | */
54 | public void setFeature(String name, boolean state) throws XmlPullParserException {
55 | features.put(name, state);
56 | }
57 |
58 |
59 | /**
60 | * Return the current value of the feature with given name.
61 | * NOTE: factory features are not used for XML Serializer.
62 | *
63 | * @param name The name of feature to be retrieved.
64 | * @return The value of named feature.
65 | * Unknown features are NOTE: factory features are not used for XML Serializer.
205 | *
206 | * @return A new instance of a XML Serializer.
207 | * @throws XmlPullParserException if a parser cannot be created which satisfies the
208 | * requested configuration.
209 | */
210 |
211 | public XmlSerializer newSerializer() throws XmlPullParserException {
212 | return getSerializerInstance();
213 | }
214 |
215 | /**
216 | * Creates a new instance of a PullParserFactory that can be used
217 | * to create XML pull parsers. The factory will always return instances
218 | * of Android's built-in {@link XmlPullParser} and {@link XmlSerializer}.
219 | */
220 | public static XmlPullParserFactory newInstance () throws XmlPullParserException {
221 | return new XmlPullParserFactory();
222 | }
223 |
224 | /**
225 | * Creates a factory that always returns instances of Android's built-in
226 | * {@link XmlPullParser} and {@link XmlSerializer} implementation. This
227 | * does not support factories capable of creating arbitrary parser
228 | * and serializer implementations. Both arguments to this method are unused.
229 | */
230 | public static XmlPullParserFactory newInstance (String unused, Class unused2)
231 | throws XmlPullParserException {
232 | return newInstance();
233 | }
234 | }
--------------------------------------------------------------------------------
/makeabx/src/org/xmlpull/v1/XmlSerializer.java:
--------------------------------------------------------------------------------
1 | package org.xmlpull.v1;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 | import java.io.Writer;
6 |
7 | /**
8 | * Define an interface to serialization of XML Infoset.
9 | * This interface abstracts away if serialized XML is XML 1.0 compatible text or
10 | * other formats of XML 1.0 serializations (such as binary XML for example with WBXML).
11 | *
12 | * PLEASE NOTE: This interface will be part of XmlPull 1.2 API.
13 | * It is included as basis for discussion. It may change in any way.
14 | *
15 | * Exceptions that may be thrown are: IOException or runtime exception
16 | * (more runtime exceptions can be thrown but are not declared and as such
17 | * have no semantics defined for this interface):
18 | * NOTE: writing CDSECT, ENTITY_REF, IGNORABLE_WHITESPACE,
28 | * PROCESSING_INSTRUCTION, COMMENT, and DOCDECL in some implementations
29 | * may not be supported (for example when serializing to WBXML).
30 | * In such case IllegalStateException will be thrown and it is recommended
31 | * to use an optional feature to signal that implementation is not
32 | * supporting this kind of output.
33 | */
34 |
35 | public interface XmlSerializer {
36 |
37 | /**
38 | * Set feature identified by name (recommended to be URI for uniqueness).
39 | * Some well known optional features are defined in
40 | *
41 | * http://www.xmlpull.org/v1/doc/features.html.
42 | *
43 | * If feature is not recognized or can not be set
44 | * then IllegalStateException MUST be thrown.
45 | *
46 | * @exception IllegalStateException If the feature is not supported or can not be set
47 | */
48 | void setFeature(String name,
49 | boolean state)
50 | throws IllegalArgumentException, IllegalStateException;
51 |
52 |
53 | /**
54 | * Return the current value of the feature with given name.
55 | * NOTE: unknown properties are always returned as null
56 | *
57 | * @param name The name of feature to be retrieved.
58 | * @return The value of named feature.
59 | * @exception IllegalArgumentException if feature string is null
60 | */
61 | boolean getFeature(String name);
62 |
63 |
64 | /**
65 | * Set the value of a property.
66 | * (the property name is recommended to be URI for uniqueness).
67 | * Some well known optional properties are defined in
68 | *
69 | * http://www.xmlpull.org/v1/doc/properties.html.
70 | *
71 | * If property is not recognized or can not be set
72 | * then IllegalStateException MUST be thrown.
73 | *
74 | * @exception IllegalStateException if the property is not supported or can not be set
75 | */
76 | void setProperty(String name,
77 | Object value)
78 | throws IllegalArgumentException, IllegalStateException;
79 |
80 | /**
81 | * Look up the value of a property.
82 | *
83 | * The property name is any fully-qualified URI. I
84 | * NOTE: unknown properties are WARNING no information about encoding is available!
100 | */
101 | void setOutput (Writer writer)
102 | throws IOException, IllegalArgumentException, IllegalStateException;
103 |
104 | /**
105 | * Write <?xml declaration with encoding (if encoding not null)
106 | * and standalone flag (if standalone not null)
107 | * This method can only be called just after setOutput.
108 | */
109 | void startDocument (String encoding, Boolean standalone)
110 | throws IOException, IllegalArgumentException, IllegalStateException;
111 |
112 | /**
113 | * Finish writing. All unclosed start tags will be closed and output
114 | * will be flushed. After calling this method no more output can be
115 | * serialized until next call to setOutput()
116 | */
117 | void endDocument ()
118 | throws IOException, IllegalArgumentException, IllegalStateException;
119 |
120 | /**
121 | * Binds the given prefix to the given namespace.
122 | * This call is valid for the next element including child elements.
123 | * The prefix and namespace MUST be always declared even if prefix
124 | * is not used in element (startTag() or attribute()) - for XML 1.0
125 | * it must result in declaring NOTE: this method MUST be called directly before startTag()
130 | * and if anything but startTag() or setPrefix() is called next there will be exception.
131 | * NOTE: prefixes "xml" and "xmlns" are already bound
132 | * and can not be redefined see:
133 | * Namespaces in XML Errata.
134 | * NOTE: to set default namespace use as prefix empty string.
135 | *
136 | * @param prefix must be not null (or IllegalArgumentException is thrown)
137 | * @param namespace must be not null
138 | */
139 | void setPrefix (String prefix, String namespace)
140 | throws IOException, IllegalArgumentException, IllegalStateException;
141 |
142 | /**
143 | * Return namespace that corresponds to given prefix
144 | * If there is no prefix bound to this namespace return null
145 | * but if generatePrefix is false then return generated prefix.
146 | *
147 | * NOTE: if the prefix is empty string "" and default namespace is bound
148 | * to this prefix then empty string ("") is returned.
149 | *
150 | * NOTE: prefixes "xml" and "xmlns" are already bound
151 | * will have values as defined
152 | * Namespaces in XML specification
153 | */
154 | String getPrefix (String namespace, boolean generatePrefix)
155 | throws IllegalArgumentException;
156 |
157 | /**
158 | * Returns the current depth of the element.
159 | * Outside the root element, the depth is 0. The
160 | * depth is incremented by 1 when startTag() is called.
161 | * The depth is decremented after the call to endTag()
162 | * event was observed.
163 | *
164 | * NOTE: that means in particular that: Background: in kXML endTag had no arguments, and non matching tags were
225 | * very difficult to find...
226 | * If namespace is null no namespace prefix is printed but just name.
227 | * If namespace is empty string then serializer will make sure that
228 | * default empty namespace is declared (in XML 1.0 xmlns='').
229 | */
230 | XmlSerializer endTag (String namespace, String name)
231 | throws IOException, IllegalArgumentException, IllegalStateException;
232 |
233 |
234 | // /**
235 | // * Writes a start tag with the given namespace and name.
236 | // * Background: in kXML endTag had no arguments, and non matching tags were
281 | // * very difficult to find... NOTE: if there is need to close start tag
318 | * (so no more attribute() calls are allowed) but without flushing output
319 | * call method text() with empty string (text("")).
320 | *
321 | */
322 | void flush ()
323 | throws IOException;
324 |
325 | }
--------------------------------------------------------------------------------
17 | *
28 | *
29 | *
30 | *
46 | *
53 | *
54 | *
58 | *
70 | *
71 | * A minimal example for using this API may look as follows:
72 | *
73 | * import java.io.IOException;
74 | * import java.io.StringReader;
75 | *
76 | * import org.xmlpull.v1.XmlPullParser;
77 | * import org.xmlpull.v1.XmlPullParserException;
78 | * import org.xmlpull.v1.XmlPullParserFactory;
79 | *
80 | * public class SimpleXmlPullApp
81 | * {
82 | *
83 | * public static void main (String args[])
84 | * throws XmlPullParserException, IOException
85 | * {
86 | * XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
87 | * factory.setNamespaceAware(true);
88 | * XmlPullParser xpp = factory.newPullParser();
89 | *
90 | * xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
91 | * int eventType = xpp.getEventType();
92 | * while (eventType != XmlPullParser.END_DOCUMENT) {
93 | * if(eventType == XmlPullParser.START_DOCUMENT) {
94 | * System.out.println("Start document");
95 | * } else if(eventType == XmlPullParser.START_TAG) {
96 | * System.out.println("Start tag "+xpp.getName());
97 | * } else if(eventType == XmlPullParser.END_TAG) {
98 | * System.out.println("End tag "+xpp.getName());
99 | * } else if(eventType == XmlPullParser.TEXT) {
100 | * System.out.println("Text "+xpp.getText());
101 | * }
102 | * eventType = xpp.next();
103 | * }
104 | * System.out.println("End document");
105 | * }
106 | * }
107 | *
108 | *
109 | *
111 | * Start document
112 | * Start tag foo
113 | * Text Hello World!
114 | * End tag foo
115 | * End document
116 | *
117 | *
118 | *
549 | * XmlPullParser pp = ...
550 | * int nsStart = pp.getNamespaceCount(pp.getDepth()-1);
551 | * int nsEnd = pp.getNamespaceCount(pp.getDepth());
552 | * for (int i = nsStart; i < nsEnd; i++) {
553 | * String prefix = pp.getNamespacePrefix(i);
554 | * String ns = pp.getNamespaceUri(i);
555 | * // ...
556 | * }
557 | *
558 | *
559 | * @see #getNamespacePrefix
560 | * @see #getNamespaceUri
561 | * @see #getNamespace()
562 | * @see #getNamespace(String)
563 | */
564 | int getNamespaceCount(int depth) throws XmlPullParserException;
565 |
566 | /**
567 | * Returns the namespace prefix for the given position
568 | * in the namespace stack.
569 | * Default namespace declaration (xmlns='...') will have null as prefix.
570 | * If the given index is out of range, an exception is thrown.
571 | *
599 | * for (int i = getNamespaceCount(getDepth ())-1; i >= 0; i--) {
600 | * if (getNamespacePrefix(i).equals( prefix )) {
601 | * return getNamespaceUri(i);
602 | * }
603 | * }
604 | * return null;
605 | *
606 | *
607 | *
633 | * <!-- outside --> 0
634 | * <root> 1
635 | * sometext 1
636 | * <foobar> 2
637 | * </foobar> 2
638 | * </root> 1
639 | * <!-- outside --> 0
640 | *
641 | */
642 | int getDepth();
643 |
644 | /**
645 | * Returns a short text describing the current parser state, including
646 | * the position, a
647 | * description of the current event and the data source if known.
648 | * This method is especially useful to provide meaningful
649 | * error messages and for debugging purposes.
650 | */
651 | String getPositionDescription ();
652 |
653 |
654 | /**
655 | * Returns the current line number, starting from 1.
656 | * When the parser does not know the current line number
657 | * or can not determine it, -1 is returned (e.g. for WBXML).
658 | *
659 | * @return current line number or -1 if unknown.
660 | */
661 | int getLineNumber();
662 |
663 | /**
664 | * Returns the current column number, starting from 1.
665 | * When the parser does not know the current column number
666 | * or can not determine it, -1 is returned (e.g. for WBXML).
667 | *
668 | * @return current column number or -1 if unknown.
669 | */
670 | int getColumnNumber();
671 |
672 |
673 | // --------------------------------------------------------------------------
674 | // TEXT related methods
675 |
676 | /**
677 | * Checks whether the current TEXT event contains only whitespace
678 | * characters.
679 | * For IGNORABLE_WHITESPACE, this is always true.
680 | * For TEXT and CDSECT, false is returned when the current event text
681 | * contains at least one non-white space character. For any other
682 | * event type an exception is thrown.
683 | *
684 | *
956 | *
1004 | *
1005 | *
Note: that element content may be delivered in multiple consecutive TEXT events.
964 | *
Note: that element content may be delivered in multiple consecutive IGNORABLE_WHITESPACE events.
968 | *
NOTE: this is the only place where value returned from getText() and
984 | * getTextCharacters() are different
985 | *
NOTE: it is user responsibility to resolve entity reference
986 | * if PROCESS_DOCDECL is false and there is no entity replacement text set in
987 | * defineEntityReplacementText() method (getText() will be null)
988 | *
NOTE: character entities (ex.  ) and standard entities such as
989 | * & < > " ' are reported as well
990 | * and are not reported as TEXT tokens but as ENTITY_REF tokens!
991 | * This requirement is added to allow to do roundtrip of XML documents!
992 | *
995 | * " titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
996 | * [<!ENTITY % active.links "INCLUDE">]"
997 | *
998 | * <!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
999 | * [<!ENTITY % active.links "INCLUDE">]>
1000 | * otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true
1001 | * then what is returned is undefined (it may be even null)
1002 | *
1042 | * if (type != getEventType()
1043 | * || (namespace != null && !namespace.equals( getNamespace () ) )
1044 | * || (name != null && !name.equals( getName() ) ) )
1045 | * throw new XmlPullParserException( "expected "+ TYPES[ type ]+getPositionDescription());
1046 | *
1047 | */
1048 | void require(int type, String namespace, String name)
1049 | throws XmlPullParserException, IOException;
1050 |
1051 | /**
1052 | * If current event is START_TAG then if next element is TEXT then element content is returned
1053 | * or if next event is END_TAG then empty string is returned, otherwise exception is thrown.
1054 | * After calling this function successfully parser will be positioned on END_TAG.
1055 | *
1056 | *
1058 | *
1062 | * p.nextTag()
1063 | * p.requireEvent(p.START_TAG, "", "tag");
1064 | * String content = p.nextText();
1065 | * p.requireEvent(p.END_TAG, "", "tag");
1066 | *
1067 | * This function together with nextTag make it very easy to parse XML that has
1068 | * no mixed content.
1069 | *
1070 | *
1071 | *
1073 | * if(getEventType() != START_TAG) {
1074 | * throw new XmlPullParserException(
1075 | * "parser must be on START_TAG to read next text", this, null);
1076 | * }
1077 | * int eventType = next();
1078 | * if(eventType == TEXT) {
1079 | * String result = getText();
1080 | * eventType = next();
1081 | * if(eventType != END_TAG) {
1082 | * throw new XmlPullParserException(
1083 | * "event TEXT it must be immediately followed by END_TAG", this, null);
1084 | * }
1085 | * return result;
1086 | * } else if(eventType == END_TAG) {
1087 | * return "";
1088 | * } else {
1089 | * throw new XmlPullParserException(
1090 | * "parser must be on START_TAG or TEXT to read text", this, null);
1091 | * }
1092 | *
1093 | *
1094 | *
1097 | * String text = xpp.nextText();
1098 | * if (xpp.getEventType() != XmlPullParser.END_TAG) {
1099 | * xpp.next();
1100 | * }
1101 | *
1102 | */
1103 | String nextText() throws XmlPullParserException, IOException;
1104 |
1105 | /**
1106 | * Call next() and return event if it is START_TAG or END_TAG
1107 | * otherwise throw an exception.
1108 | * It will skip whitespace TEXT before actual tag if any.
1109 | *
1110 | *
1112 | * int eventType = next();
1113 | * if(eventType == TEXT && isWhitespace()) { // skip whitespace
1114 | * eventType = next();
1115 | * }
1116 | * if (eventType != START_TAG && eventType != END_TAG) {
1117 | * throw new XmlPullParserException("expected start or end tag", this, null);
1118 | * }
1119 | * return eventType;
1120 | *
1121 | */
1122 | int nextTag() throws XmlPullParserException, IOException;
1123 |
1124 | }
--------------------------------------------------------------------------------
/makeabx/src/org/xmlpull/v1/XmlPullParserException.java:
--------------------------------------------------------------------------------
1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 | // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
3 |
4 | package org.xmlpull.v1;
5 |
6 | /**
7 | * This exception is thrown to signal XML Pull Parser related faults.
8 | *
9 | * @author Aleksander Slominski
10 | */
11 | public class XmlPullParserException extends Exception {
12 | protected Throwable detail;
13 | protected int row = -1;
14 | protected int column = -1;
15 |
16 | /* public XmlPullParserException() {
17 | }*/
18 |
19 | public XmlPullParserException(String s) {
20 | super(s);
21 | }
22 |
23 | /*
24 | public XmlPullParserException(String s, Throwable thrwble) {
25 | super(s);
26 | this.detail = thrwble;
27 | }
28 |
29 | public XmlPullParserException(String s, int row, int column) {
30 | super(s);
31 | this.row = row;
32 | this.column = column;
33 | }
34 | */
35 |
36 | public XmlPullParserException(String msg, XmlPullParser parser, Throwable chain) {
37 | super ((msg == null ? "" : msg+" ")
38 | + (parser == null ? "" : "(position:"+parser.getPositionDescription()+") ")
39 | + (chain == null ? "" : "caused by: "+chain));
40 |
41 | if (parser != null) {
42 | this.row = parser.getLineNumber();
43 | this.column = parser.getColumnNumber();
44 | }
45 | this.detail = chain;
46 | }
47 |
48 | public Throwable getDetail() { return detail; }
49 | // public void setDetail(Throwable cause) { this.detail = cause; }
50 | public int getLineNumber() { return row; }
51 | public int getColumnNumber() { return column; }
52 |
53 | /*
54 | public String getMessage() {
55 | if(detail == null)
56 | return super.getMessage();
57 | else
58 | return super.getMessage() + "; nested exception is: \n\t"
59 | + detail.getMessage();
60 | }
61 | */
62 |
63 | //NOTE: code that prints this and detail is difficult in J2ME
64 | public void printStackTrace() {
65 | if (detail == null) {
66 | super.printStackTrace();
67 | } else {
68 | synchronized(System.err) {
69 | System.err.println(super.getMessage() + "; nested exception is:");
70 | detail.printStackTrace();
71 | }
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/makeabx/src/org/xmlpull/v1/XmlPullParserFactory.java:
--------------------------------------------------------------------------------
1 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 | // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
3 |
4 | package org.xmlpull.v1;
5 |
6 | import java.util.ArrayList;
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | /**
11 | * This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API.
12 | *
13 | * @see XmlPullParser
14 | *
15 | * @author Aleksander Slominski
16 | * @author Stefan Haustein
17 | */
18 |
19 | public class XmlPullParserFactory {
20 |
21 | public static final String PROPERTY_NAME = "org.xmlpull.v1.XmlPullParserFactory";
22 | protected ArrayList parserClasses;
23 | protected ArrayList serializerClasses;
24 |
25 | /** Unused, but we have to keep it because it's public API. */
26 | protected String classNamesLocation = null;
27 |
28 | // features are kept there
29 | // TODO: This can't be made final because it's a public API.
30 | protected HashMap
19 | *
26 | *
27 | * xmlns:prefix='namespace'
126 | * (or xmlns:prefix="namespace"
depending what character is used
127 | * to quote attribute value).
128 | *
129 | *
165 | * <!-- outside --> 0
166 | * <root> 1
167 | * sometext 1
168 | * <foobar> 2
169 | * </foobar> 2
170 | * </root> 1
171 | * <!-- outside --> 0
172 | *
173 | */
174 | int getDepth();
175 |
176 | /**
177 | * Returns the namespace URI of the current element as set by startTag().
178 | *
179 | *
180 | *
183 | *
184 | * @return namespace set by startTag() that is currently in scope
185 | */
186 | String getNamespace ();
187 |
188 | /**
189 | * Returns the name of the current element as set by startTag().
190 | * It can only be null before first call to startTag()
191 | * or when last endTag() is called to close first startTag().
192 | *
193 | * @return namespace set by startTag() that is currently in scope
194 | */
195 | String getName();
196 |
197 | /**
198 | * Writes a start tag with the given namespace and name.
199 | * If there is no prefix defined for the given namespace,
200 | * a prefix will be defined automatically.
201 | * The explicit prefixes for namespaces can be established by calling setPrefix()
202 | * immediately before this method.
203 | * If namespace is null no namespace prefix is printed but just name.
204 | * If namespace is empty string then serializer will make sure that
205 | * default empty namespace is declared (in XML 1.0 xmlns='')
206 | * or throw IllegalStateException if default namespace is already bound
207 | * to non-empty string.
208 | */
209 | XmlSerializer startTag (String namespace, String name)
210 | throws IOException, IllegalArgumentException, IllegalStateException;
211 |
212 | /**
213 | * Write an attribute. Calls to attribute() MUST follow a call to
214 | * startTag() immediately. If there is no prefix defined for the
215 | * given namespace, a prefix will be defined automatically.
216 | * If namespace is null or empty string
217 | * no namespace prefix is printed but just name.
218 | */
219 | XmlSerializer attribute (String namespace, String name, String value)
220 | throws IOException, IllegalArgumentException, IllegalStateException;
221 |
222 | /**
223 | * Write end tag. Repetition of namespace and name is just for avoiding errors.
224 | *
If there is no prefix defined (prefix == null) for the given namespace,
237 | // * a prefix will be defined automatically.
238 | // *
If explicit prefixes is passed (prefix != null) then it will be used
239 | // *and namespace declared if not already declared or
240 | // * throw IllegalStateException the same prefix was already set on this
241 | // * element (setPrefix()) and was bound to different namespace.
242 | // *
If namespace is null then prefix must be null too or IllegalStateException is thrown.
243 | // *
If namespace is null then no namespace prefix is printed but just name.
244 | // *
If namespace is empty string then serializer will make sure that
245 | // * default empty namespace is declared (in XML 1.0 xmlns='')
246 | // * or throw IllegalStateException if default namespace is already bound
247 | // * to non-empty string.
248 | // */
249 | // XmlSerializer startTag (String prefix, String namespace, String name)
250 | // throws IOException, IllegalArgumentException, IllegalStateException;
251 | //
252 | // /**
253 | // * Write an attribute. Calls to attribute() MUST follow a call to
254 | // * startTag() immediately.
255 | // *
If there is no prefix defined (prefix == null) for the given namespace,
256 | // * a prefix will be defined automatically.
257 | // *
If explicit prefixes is passed (prefix != null) then it will be used
258 | // * and namespace declared if not already declared or
259 | // * throw IllegalStateException the same prefix was already set on this
260 | // * element (setPrefix()) and was bound to different namespace.
261 | // *
If namespace is null then prefix must be null too or IllegalStateException is thrown.
262 | // *
If namespace is null then no namespace prefix is printed but just name.
263 | // *
If namespace is empty string then serializer will make sure that
264 | // * default empty namespace is declared (in XML 1.0 xmlns='')
265 | // * or throw IllegalStateException if default namespace is already bound
266 | // * to non-empty string.
267 | // */
268 | // XmlSerializer attribute (String prefix, String namespace, String name, String value)
269 | // throws IOException, IllegalArgumentException, IllegalStateException;
270 | //
271 | // /**
272 | // * Write end tag. Repetition of namespace, prefix, and name is just for avoiding errors.
273 | // *
If namespace or name arguments are different from corresponding startTag call
274 | // * then IllegalArgumentException is thrown, if prefix argument is not null and is different
275 | // * from corresponding starTag then IllegalArgumentException is thrown.
276 | // *
If namespace is null then prefix must be null too or IllegalStateException is thrown.
277 | // *
If namespace is null then no namespace prefix is printed but just name.
278 | // *
If namespace is empty string then serializer will make sure that
279 | // * default empty namespace is declared (in XML 1.0 xmlns='').
280 | // *