The Android Open Source Project | dd828f4 | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "ExpatParser" |
| 18 | |
Elliott Hughes | 845ce3c | 2009-11-13 17:07:00 -0800 | [diff] [blame] | 19 | #include "JNIHelp.h" |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 20 | #include "JniConstants.h" |
Elliott Hughes | 845ce3c | 2009-11-13 17:07:00 -0800 | [diff] [blame] | 21 | #include "LocalArray.h" |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 22 | #include "ScopedJavaUnicodeString.h" |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 23 | #include "ScopedLocalRef.h" |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 24 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 25 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 26 | #include "UniquePtr.h" |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | #include "jni.h" |
| 28 | #include "utils/Log.h" |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
| 30 | #include <string.h> |
| 31 | #include <utils/misc.h> |
| 32 | #include <expat.h> |
| 33 | #include <cutils/jstring.h> |
| 34 | |
| 35 | #define BUCKET_COUNT 128 |
| 36 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 37 | static void throw_OutOfMemoryError(JNIEnv* env) { |
| 38 | jniThrowException(env, "java/lang/OutOfMemoryError", "Out of memory."); |
| 39 | } |
| 40 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Wrapper around an interned string. |
| 43 | */ |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 44 | struct InternedString { |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 45 | InternedString() : interned(NULL), bytes(NULL) { |
| 46 | } |
| 47 | |
| 48 | ~InternedString() { |
| 49 | delete[] bytes; |
| 50 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
| 52 | /** The interned string itself. */ |
| 53 | jstring interned; |
| 54 | |
| 55 | /** UTF-8 equivalent of the interned string. */ |
| 56 | const char* bytes; |
| 57 | |
| 58 | /** Hash code of the interned string. */ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 59 | int hash; |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 60 | }; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Keeps track of strings between start and end events. |
| 64 | */ |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 65 | class StringStack { |
| 66 | public: |
| 67 | StringStack() : array(new jstring[DEFAULT_CAPACITY]), capacity(DEFAULT_CAPACITY), size(0) { |
| 68 | } |
| 69 | |
| 70 | ~StringStack() { |
| 71 | delete[] array; |
| 72 | } |
| 73 | |
| 74 | void push(JNIEnv* env, jstring s) { |
| 75 | if (size == capacity) { |
| 76 | int newCapacity = capacity * 2; |
| 77 | jstring* newArray = new jstring[newCapacity]; |
| 78 | if (newArray == NULL) { |
| 79 | throw_OutOfMemoryError(env); |
| 80 | return; |
| 81 | } |
| 82 | memcpy(newArray, array, capacity * sizeof(jstring)); |
| 83 | |
| 84 | array = newArray; |
| 85 | capacity = newCapacity; |
| 86 | } |
| 87 | |
| 88 | array[size++] = s; |
| 89 | } |
| 90 | |
| 91 | jstring pop() { |
| 92 | return (size == 0) ? NULL : array[--size]; |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | enum { DEFAULT_CAPACITY = 10 }; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
| 98 | jstring* array; |
| 99 | int capacity; |
| 100 | int size; |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 101 | }; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Data passed to parser handler method by the parser. |
| 105 | */ |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 106 | struct ParsingContext { |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 107 | ParsingContext(jobject object) : env(NULL), object(object), buffer(NULL), bufferSize(-1) { |
| 108 | for (int i = 0; i < BUCKET_COUNT; i++) { |
| 109 | internedStrings[i] = NULL; |
| 110 | } |
| 111 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 113 | // Warning: 'env' must be valid on entry. |
| 114 | ~ParsingContext() { |
| 115 | freeBuffer(); |
| 116 | |
| 117 | // Free interned string cache. |
| 118 | for (int i = 0; i < BUCKET_COUNT; i++) { |
| 119 | if (internedStrings[i]) { |
| 120 | InternedString** bucket = internedStrings[i]; |
| 121 | InternedString* current; |
| 122 | while ((current = *(bucket++)) != NULL) { |
| 123 | // Free the interned string reference. |
| 124 | env->DeleteGlobalRef(current->interned); |
| 125 | |
| 126 | // Free the bucket. |
| 127 | delete current; |
| 128 | } |
| 129 | |
| 130 | // Free the buckets. |
| 131 | delete[] internedStrings[i]; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | jcharArray ensureCapacity(int length) { |
| 137 | if (bufferSize < length) { |
| 138 | // Free the existing char[]. |
| 139 | freeBuffer(); |
| 140 | |
| 141 | // Allocate a new char[]. |
| 142 | jcharArray javaBuffer = env->NewCharArray(length); |
| 143 | if (javaBuffer == NULL) return NULL; |
| 144 | |
| 145 | // Create a global reference. |
| 146 | javaBuffer = (jcharArray) env->NewGlobalRef(javaBuffer); |
| 147 | if (javaBuffer == NULL) return NULL; |
| 148 | |
| 149 | buffer = javaBuffer; |
| 150 | bufferSize = length; |
| 151 | } |
| 152 | return buffer; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | void freeBuffer() { |
| 157 | if (buffer != NULL) { |
| 158 | env->DeleteGlobalRef(buffer); |
| 159 | buffer = NULL; |
| 160 | bufferSize = -1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public: |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | /** |
| 166 | * The JNI environment for the current thread. This should only be used |
| 167 | * to keep a reference to the env for use in Expat callbacks. |
| 168 | */ |
| 169 | JNIEnv* env; |
| 170 | |
| 171 | /** The Java parser object. */ |
| 172 | jobject object; |
| 173 | |
| 174 | /** Buffer for text events. */ |
| 175 | jcharArray buffer; |
| 176 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 177 | private: |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | /** The size of our buffer in jchars. */ |
| 179 | int bufferSize; |
| 180 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 181 | public: |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | /** Current attributes. */ |
| 183 | const char** attributes; |
| 184 | |
| 185 | /** Number of attributes. */ |
| 186 | int attributeCount; |
| 187 | |
| 188 | /** True if namespace support is enabled. */ |
| 189 | bool processNamespaces; |
| 190 | |
| 191 | /** Keep track of names. */ |
| 192 | StringStack stringStack; |
| 193 | |
| 194 | /** Cache of interned strings. */ |
| 195 | InternedString** internedStrings[BUCKET_COUNT]; |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 196 | }; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | static jmethodID commentMethod; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | static jmethodID endCdataMethod; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | static jmethodID endDtdMethod; |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 201 | static jmethodID endElementMethod; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | static jmethodID endNamespaceMethod; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 203 | static jmethodID handleExternalEntityMethod; |
| 204 | static jmethodID internMethod; |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 205 | static jmethodID notationDeclMethod; |
| 206 | static jmethodID processingInstructionMethod; |
| 207 | static jmethodID startCdataMethod; |
| 208 | static jmethodID startDtdMethod; |
| 209 | static jmethodID startElementMethod; |
| 210 | static jmethodID startNamespaceMethod; |
| 211 | static jmethodID textMethod; |
| 212 | static jmethodID unparsedEntityDeclMethod; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | static jstring emptyString; |
| 214 | |
| 215 | /** |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | * Calculates a hash code for a null-terminated string. This is *not* equivalent |
| 217 | * to Java's String.hashCode(). This hashes the bytes while String.hashCode() |
| 218 | * hashes UTF-16 chars. |
| 219 | * |
| 220 | * @param s null-terminated string to hash |
| 221 | * @returns hash code |
| 222 | */ |
| 223 | static int hashString(const char* s) { |
| 224 | int hash = 0; |
| 225 | if (s) { |
| 226 | while (*s) { |
| 227 | hash = hash * 31 + *s++; |
| 228 | } |
| 229 | } |
| 230 | return hash; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Creates a new interned string wrapper. Looks up the interned string |
| 235 | * representing the given UTF-8 bytes. |
| 236 | * |
| 237 | * @param bytes null-terminated string to intern |
| 238 | * @param hash of bytes |
| 239 | * @returns wrapper of interned Java string |
| 240 | */ |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 241 | static InternedString* newInternedString(JNIEnv* env, const char* bytes, int hash) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 242 | // Allocate a new wrapper. |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 243 | UniquePtr<InternedString> wrapper(new InternedString); |
| 244 | if (wrapper.get() == NULL) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 245 | throw_OutOfMemoryError(env); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | // Create a copy of the UTF-8 bytes. |
| 250 | // TODO: sometimes we already know the length. Reuse it if so. |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 251 | char* copy = new char[strlen(bytes) + 1]; |
| 252 | if (copy == NULL) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | throw_OutOfMemoryError(env); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 256 | strcpy(copy, bytes); |
| 257 | wrapper->bytes = copy; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | |
| 259 | // Save the hash. |
| 260 | wrapper->hash = hash; |
| 261 | |
| 262 | // To intern a string, we must first create a new string and then call |
| 263 | // intern() on it. We then keep a global reference to the interned string. |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 264 | ScopedLocalRef<jstring> newString(env, env->NewStringUTF(bytes)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 265 | if (env->ExceptionCheck()) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | // Call intern(). |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 270 | ScopedLocalRef<jstring> interned(env, |
| 271 | reinterpret_cast<jstring>(env->CallObjectMethod(newString.get(), internMethod))); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | if (env->ExceptionCheck()) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | // Create a global reference to the interned string. |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 277 | wrapper->interned = (jstring) env->NewGlobalRef(interned.get()); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 278 | if (env->ExceptionCheck()) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 | return NULL; |
| 280 | } |
| 281 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 282 | return wrapper.release(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Allocates a new bucket with one entry. |
| 287 | * |
| 288 | * @param entry to store in the bucket |
| 289 | * @returns a reference to the bucket |
| 290 | */ |
| 291 | static InternedString** newInternedStringBucket(InternedString* entry) { |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 292 | InternedString** bucket = new InternedString*[2]; |
| 293 | if (bucket != NULL) { |
| 294 | bucket[0] = entry; |
| 295 | bucket[1] = NULL; |
| 296 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | return bucket; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Expands an interned string bucket and adds the given entry. Frees the |
| 302 | * provided bucket and returns a new one. |
| 303 | * |
| 304 | * @param existingBucket the bucket to replace |
| 305 | * @param entry to add to the bucket |
| 306 | * @returns a reference to the newly-allocated bucket containing the given entry |
| 307 | */ |
| 308 | static InternedString** expandInternedStringBucket( |
| 309 | InternedString** existingBucket, InternedString* entry) { |
| 310 | // Determine the size of the existing bucket. |
| 311 | int size = 0; |
| 312 | while (existingBucket[size]) size++; |
| 313 | |
| 314 | // Allocate the new bucket with enough space for one more entry and |
| 315 | // a null terminator. |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 316 | InternedString** newBucket = new InternedString*[size + 2]; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 317 | if (newBucket == NULL) return NULL; |
| 318 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 319 | memcpy(newBucket, existingBucket, size * sizeof(InternedString*)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 320 | newBucket[size] = entry; |
| 321 | newBucket[size + 1] = NULL; |
| 322 | |
| 323 | return newBucket; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Returns an interned string for the given UTF-8 string. |
| 328 | * |
| 329 | * @param bucket to search for s |
| 330 | * @param s null-terminated string to find |
| 331 | * @param hash of s |
| 332 | * @returns interned Java string equivalent of s or null if not found |
| 333 | */ |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 334 | static jstring findInternedString(InternedString** bucket, const char* s, int hash) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 335 | InternedString* current; |
| 336 | while ((current = *(bucket++)) != NULL) { |
| 337 | if (current->hash != hash) continue; |
| 338 | if (!strcmp(s, current->bytes)) return current->interned; |
| 339 | } |
| 340 | return NULL; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Returns an interned string for the given UTF-8 string. |
| 345 | * |
| 346 | * @param s null-terminated string to intern |
| 347 | * @returns interned Java string equivelent of s or NULL if s is null |
| 348 | */ |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 349 | static jstring internString(JNIEnv* env, ParsingContext* parsingContext, const char* s) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 350 | if (s == NULL) return NULL; |
| 351 | |
| 352 | int hash = hashString(s); |
| 353 | int bucketIndex = hash & (BUCKET_COUNT - 1); |
| 354 | |
| 355 | InternedString*** buckets = parsingContext->internedStrings; |
| 356 | InternedString** bucket = buckets[bucketIndex]; |
| 357 | InternedString* internedString; |
| 358 | |
| 359 | if (bucket) { |
| 360 | // We have a bucket already. Look for the given string. |
| 361 | jstring found = findInternedString(bucket, s, hash); |
| 362 | if (found) { |
| 363 | // We found it! |
| 364 | return found; |
| 365 | } |
| 366 | |
| 367 | // We didn't find it. :( |
| 368 | // Create a new entry. |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 369 | internedString = newInternedString(env, s, hash); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | if (internedString == NULL) return NULL; |
| 371 | |
| 372 | // Expand the bucket. |
| 373 | bucket = expandInternedStringBucket(bucket, internedString); |
| 374 | if (bucket == NULL) { |
| 375 | throw_OutOfMemoryError(env); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | buckets[bucketIndex] = bucket; |
| 380 | |
| 381 | return internedString->interned; |
| 382 | } else { |
| 383 | // We don't even have a bucket yet. Create an entry. |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 384 | internedString = newInternedString(env, s, hash); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 385 | if (internedString == NULL) return NULL; |
| 386 | |
| 387 | // Create a new bucket with one entry. |
| 388 | bucket = newInternedStringBucket(internedString); |
| 389 | if (bucket == NULL) { |
| 390 | throw_OutOfMemoryError(env); |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | buckets[bucketIndex] = bucket; |
| 395 | |
| 396 | return internedString->interned; |
| 397 | } |
| 398 | } |
| 399 | |
Elliott Hughes | 738f950 | 2009-10-01 11:20:29 -0700 | [diff] [blame] | 400 | static void jniThrowExpatException(JNIEnv* env, XML_Error error) { |
| 401 | const char* message = XML_ErrorString(error); |
| 402 | jniThrowException(env, "org/apache/harmony/xml/ExpatException", message); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 403 | } |
| 404 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 405 | /** |
| 406 | * Copies UTF-8 characters into the buffer. Returns the number of Java chars |
| 407 | * which were buffered. |
| 408 | * |
| 409 | * @param characters to copy into the buffer |
| 410 | * @param length of characters to copy (in bytes) |
| 411 | * @returns number of UTF-16 characters which were copied |
| 412 | */ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 413 | static size_t fillBuffer(ParsingContext* parsingContext, const char* characters, int length) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 414 | JNIEnv* env = parsingContext->env; |
| 415 | |
| 416 | // Grow buffer if necessary. |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 417 | jcharArray buffer = parsingContext->ensureCapacity(length); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 418 | if (buffer == NULL) return -1; |
| 419 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | // Decode UTF-8 characters into our buffer. |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 421 | ScopedCharArrayRW nativeBuffer(env, buffer); |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 422 | if (nativeBuffer.get() == NULL) { |
| 423 | return -1; |
| 424 | } |
| 425 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 426 | size_t utf16length; |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 427 | strcpylen8to16((char16_t*) nativeBuffer.get(), characters, length, &utf16length); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 428 | return utf16length; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Buffers the given text and passes it to the given method. |
| 433 | * |
| 434 | * @param method to pass the characters and length to with signature |
| 435 | * (char[], int) |
| 436 | * @param data parsing context |
| 437 | * @param text to copy into the buffer |
| 438 | * @param length of text to copy (in bytes) |
| 439 | */ |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 440 | static void bufferAndInvoke(jmethodID method, void* data, const char* text, size_t length) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 441 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 442 | JNIEnv* env = parsingContext->env; |
| 443 | |
| 444 | // Bail out if a previously called handler threw an exception. |
| 445 | if (env->ExceptionCheck()) return; |
| 446 | |
| 447 | // Buffer the element name. |
| 448 | size_t utf16length = fillBuffer(parsingContext, text, length); |
| 449 | |
| 450 | // Invoke given method. |
| 451 | jobject javaParser = parsingContext->object; |
| 452 | jcharArray buffer = parsingContext->buffer; |
| 453 | env->CallVoidMethod(javaParser, method, buffer, utf16length); |
| 454 | } |
| 455 | |
| 456 | /** |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 457 | * The component parts of an attribute or element name. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 458 | */ |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 459 | class ExpatElementName { |
| 460 | public: |
| 461 | ExpatElementName(JNIEnv* env, ParsingContext* parsingContext, jint attributePointer, jint index) { |
| 462 | const char** attributes = (const char**) attributePointer; |
| 463 | const char* name = attributes[index * 2]; |
| 464 | init(env, parsingContext, name); |
| 465 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 466 | |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 467 | ExpatElementName(JNIEnv* env, ParsingContext* parsingContext, const char* s) { |
| 468 | init(env, parsingContext, s); |
| 469 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 470 | |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 471 | ~ExpatElementName() { |
| 472 | free(mCopy); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Returns the namespace URI, like "http://www.w3.org/1999/xhtml". |
| 477 | * Possibly empty. |
| 478 | */ |
| 479 | jstring uri() { |
| 480 | return internString(mEnv, mParsingContext, mUri); |
| 481 | } |
| 482 | |
| 483 | /** |
Jesse Wilson | a144051 | 2010-01-29 17:06:44 -0800 | [diff] [blame] | 484 | * Returns the element or attribute local name, like "h1". Never empty. When |
| 485 | * namespace processing is disabled, this may contain a prefix, yielding a |
| 486 | * local name like "html:h1". In such cases, the qName will always be empty. |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 487 | */ |
| 488 | jstring localName() { |
| 489 | return internString(mEnv, mParsingContext, mLocalName); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Returns the namespace prefix, like "html". Possibly empty. |
| 494 | */ |
| 495 | jstring qName() { |
| 496 | if (*mPrefix == 0) { |
| 497 | return localName(); |
| 498 | } |
| 499 | |
| 500 | // return prefix + ":" + localName |
| 501 | LocalArray<1024> qName(strlen(mPrefix) + 1 + strlen(mLocalName) + 1); |
| 502 | snprintf(&qName[0], qName.size(), "%s:%s", mPrefix, mLocalName); |
| 503 | return internString(mEnv, mParsingContext, &qName[0]); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Returns true if this expat name has the same URI and local name. |
| 508 | */ |
| 509 | bool matches(const char* uri, const char* localName) { |
| 510 | return strcmp(uri, mUri) == 0 && strcmp(localName, mLocalName) == 0; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Returns true if this expat name has the same qualified name. |
| 515 | */ |
| 516 | bool matchesQName(const char* qName) { |
Brian Carlstrom | bf87c56 | 2010-05-27 23:09:59 -0700 | [diff] [blame] | 517 | const char* lastColon = strrchr(qName, ':'); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 518 | |
Jesse Wilson | a144051 | 2010-01-29 17:06:44 -0800 | [diff] [blame] | 519 | // Compare local names only if either: |
| 520 | // - the input qualified name doesn't have a colon (like "h1") |
| 521 | // - this element doesn't have a prefix. Such is the case when it |
| 522 | // doesn't belong to a namespace, or when this parser's namespace |
| 523 | // processing is disabled. In the latter case, this element's local |
| 524 | // name may still contain a colon (like "html:h1"). |
| 525 | if (lastColon == NULL || *mPrefix == 0) { |
| 526 | return strcmp(qName, mLocalName) == 0; |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Jesse Wilson | a144051 | 2010-01-29 17:06:44 -0800 | [diff] [blame] | 529 | // Otherwise compare both prefix and local name |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 530 | size_t prefixLength = lastColon - qName; |
| 531 | return strlen(mPrefix) == prefixLength |
| 532 | && strncmp(qName, mPrefix, prefixLength) == 0 |
| 533 | && strcmp(lastColon + 1, mLocalName) == 0; |
| 534 | } |
| 535 | |
| 536 | private: |
| 537 | JNIEnv* mEnv; |
| 538 | ParsingContext* mParsingContext; |
| 539 | char* mCopy; |
| 540 | const char* mUri; |
| 541 | const char* mLocalName; |
| 542 | const char* mPrefix; |
| 543 | |
| 544 | /** |
| 545 | * Decodes an Expat-encoded name of one of these three forms: |
| 546 | * "uri|localName|prefix" (example: "http://www.w3.org/1999/xhtml|h1|html") |
| 547 | * "uri|localName" (example: "http://www.w3.org/1999/xhtml|h1") |
| 548 | * "localName" (example: "h1") |
| 549 | */ |
| 550 | void init(JNIEnv* env, ParsingContext* parsingContext, const char* s) { |
| 551 | mEnv = env; |
| 552 | mParsingContext = parsingContext; |
| 553 | mCopy = strdup(s); |
| 554 | |
| 555 | // split the input into up to 3 parts: a|b|c |
| 556 | char* context = NULL; |
| 557 | char* a = strtok_r(mCopy, "|", &context); |
| 558 | char* b = strtok_r(NULL, "|", &context); |
| 559 | char* c = strtok_r(NULL, "|", &context); |
| 560 | |
| 561 | if (c != NULL) { // input of the form "uri|localName|prefix" |
| 562 | mUri = a; |
| 563 | mLocalName = b; |
| 564 | mPrefix = c; |
| 565 | } else if (b != NULL) { // input of the form "uri|localName" |
| 566 | mUri = a; |
| 567 | mLocalName = b; |
| 568 | mPrefix = ""; |
| 569 | } else { // input of the form "localName" |
| 570 | mLocalName = a; |
| 571 | mUri = ""; |
| 572 | mPrefix = ""; |
| 573 | } |
| 574 | } |
Elliott Hughes | 7ca6fd0 | 2010-03-29 20:51:48 -0700 | [diff] [blame] | 575 | |
| 576 | // Disallow copy and assignment. |
| 577 | ExpatElementName(const ExpatElementName&); |
| 578 | void operator=(const ExpatElementName&); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 579 | }; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 580 | |
| 581 | /** |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 582 | * Called by Expat at the start of an element. Delegates to the same method |
| 583 | * on the Java parser. |
| 584 | * |
| 585 | * @param data parsing context |
| 586 | * @param elementName "uri|localName" or "localName" for the current element |
| 587 | * @param attributes alternating attribute names and values. Like element |
| 588 | * names, attribute names follow the format "uri|localName" or "localName". |
| 589 | */ |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 590 | static void startElement(void* data, const char* elementName, const char** attributes) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 592 | JNIEnv* env = parsingContext->env; |
| 593 | |
| 594 | // Bail out if a previously called handler threw an exception. |
| 595 | if (env->ExceptionCheck()) return; |
| 596 | |
| 597 | // Count the number of attributes. |
| 598 | int count = 0; |
| 599 | while (attributes[count << 1]) count++; |
| 600 | |
| 601 | // Make the attributes available for the duration of this call. |
| 602 | parsingContext->attributes = attributes; |
| 603 | parsingContext->attributeCount = count; |
| 604 | |
| 605 | jobject javaParser = parsingContext->object; |
| 606 | |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 607 | ExpatElementName e(env, parsingContext, elementName); |
Jesse Wilson | 4a5c3fd | 2010-03-19 12:51:43 -0700 | [diff] [blame] | 608 | jstring uri = parsingContext->processNamespaces ? e.uri() : emptyString; |
| 609 | jstring localName = parsingContext->processNamespaces ? e.localName() : emptyString; |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 610 | jstring qName = e.qName(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 611 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 612 | parsingContext->stringStack.push(env, qName); |
| 613 | parsingContext->stringStack.push(env, uri); |
| 614 | parsingContext->stringStack.push(env, localName); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 615 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 616 | env->CallVoidMethod(javaParser, startElementMethod, uri, localName, qName, attributes, count); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 617 | |
| 618 | parsingContext->attributes = NULL; |
| 619 | parsingContext->attributeCount = -1; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Called by Expat at the end of an element. Delegates to the same method |
| 624 | * on the Java parser. |
| 625 | * |
| 626 | * @param data parsing context |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 627 | * @param elementName "uri|localName" or "localName" for the current element; |
| 628 | * we assume that this matches the last data on our stack. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 629 | */ |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 630 | static void endElement(void* data, const char* /*elementName*/) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 631 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 632 | JNIEnv* env = parsingContext->env; |
| 633 | |
| 634 | // Bail out if a previously called handler threw an exception. |
| 635 | if (env->ExceptionCheck()) return; |
| 636 | |
| 637 | jobject javaParser = parsingContext->object; |
| 638 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 639 | jstring localName = parsingContext->stringStack.pop(); |
| 640 | jstring uri = parsingContext->stringStack.pop(); |
| 641 | jstring qName = parsingContext->stringStack.pop(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 642 | |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 643 | env->CallVoidMethod(javaParser, endElementMethod, uri, localName, qName); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Called by Expat when it encounters text. Delegates to the same method |
| 648 | * on the Java parser. This may be called mutiple times with incremental pieces |
| 649 | * of the same contiguous block of text. |
| 650 | * |
| 651 | * @param data parsing context |
| 652 | * @param characters buffer containing encountered text |
| 653 | * @param length number of characters in the buffer |
| 654 | */ |
| 655 | static void text(void* data, const char* characters, int length) { |
| 656 | bufferAndInvoke(textMethod, data, characters, length); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Called by Expat when it encounters a comment. Delegates to the same method |
| 661 | * on the Java parser. |
| 662 | |
| 663 | * @param data parsing context |
| 664 | * @param comment 0-terminated |
| 665 | */ |
| 666 | static void comment(void* data, const char* comment) { |
| 667 | bufferAndInvoke(commentMethod, data, comment, strlen(comment)); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Called by Expat at the beginning of a namespace mapping. |
| 672 | * |
| 673 | * @param data parsing context |
| 674 | * @param prefix null-terminated namespace prefix used in the XML |
| 675 | * @param uri of the namespace |
| 676 | */ |
| 677 | static void startNamespace(void* data, const char* prefix, const char* uri) { |
| 678 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 679 | JNIEnv* env = parsingContext->env; |
| 680 | |
| 681 | // Bail out if a previously called handler threw an exception. |
| 682 | if (env->ExceptionCheck()) return; |
| 683 | |
| 684 | jstring internedPrefix = emptyString; |
| 685 | if (prefix != NULL) { |
| 686 | internedPrefix = internString(env, parsingContext, prefix); |
| 687 | if (env->ExceptionCheck()) return; |
| 688 | } |
| 689 | |
| 690 | jstring internedUri = emptyString; |
| 691 | if (uri != NULL) { |
| 692 | internedUri = internString(env, parsingContext, uri); |
| 693 | if (env->ExceptionCheck()) return; |
| 694 | } |
| 695 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 696 | parsingContext->stringStack.push(env, internedPrefix); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 697 | |
| 698 | jobject javaParser = parsingContext->object; |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 699 | env->CallVoidMethod(javaParser, startNamespaceMethod, internedPrefix, internedUri); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Called by Expat at the end of a namespace mapping. |
| 704 | * |
| 705 | * @param data parsing context |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 706 | * @param prefix null-terminated namespace prefix used in the XML; |
| 707 | * we assume this is the same as the last prefix on the stack. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 708 | */ |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 709 | static void endNamespace(void* data, const char* /*prefix*/) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 710 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 711 | JNIEnv* env = parsingContext->env; |
| 712 | |
| 713 | // Bail out if a previously called handler threw an exception. |
| 714 | if (env->ExceptionCheck()) return; |
| 715 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 716 | jstring internedPrefix = parsingContext->stringStack.pop(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 717 | |
| 718 | jobject javaParser = parsingContext->object; |
| 719 | env->CallVoidMethod(javaParser, endNamespaceMethod, internedPrefix); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Called by Expat at the beginning of a CDATA section. |
| 724 | * |
| 725 | * @param data parsing context |
| 726 | */ |
| 727 | static void startCdata(void* data) { |
| 728 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 729 | JNIEnv* env = parsingContext->env; |
| 730 | |
| 731 | // Bail out if a previously called handler threw an exception. |
| 732 | if (env->ExceptionCheck()) return; |
| 733 | |
| 734 | jobject javaParser = parsingContext->object; |
| 735 | env->CallVoidMethod(javaParser, startCdataMethod); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Called by Expat at the end of a CDATA section. |
| 740 | * |
| 741 | * @param data parsing context |
| 742 | */ |
| 743 | static void endCdata(void* data) { |
| 744 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 745 | JNIEnv* env = parsingContext->env; |
| 746 | |
| 747 | // Bail out if a previously called handler threw an exception. |
| 748 | if (env->ExceptionCheck()) return; |
| 749 | |
| 750 | jobject javaParser = parsingContext->object; |
| 751 | env->CallVoidMethod(javaParser, endCdataMethod); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Called by Expat at the beginning of a DOCTYPE section. |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 756 | * Expat gives us 'hasInternalSubset', but the Java API doesn't expect it, so we don't need it. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | */ |
| 758 | static void startDtd(void* data, const char* name, |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 759 | const char* systemId, const char* publicId, int /*hasInternalSubset*/) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 760 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 761 | JNIEnv* env = parsingContext->env; |
| 762 | |
| 763 | // Bail out if a previously called handler threw an exception. |
| 764 | if (env->ExceptionCheck()) return; |
| 765 | |
| 766 | jstring javaName = internString(env, parsingContext, name); |
| 767 | if (env->ExceptionCheck()) return; |
| 768 | |
| 769 | jstring javaPublicId = internString(env, parsingContext, publicId); |
| 770 | if (env->ExceptionCheck()) return; |
| 771 | |
| 772 | jstring javaSystemId = internString(env, parsingContext, systemId); |
| 773 | if (env->ExceptionCheck()) return; |
| 774 | |
| 775 | jobject javaParser = parsingContext->object; |
| 776 | env->CallVoidMethod(javaParser, startDtdMethod, javaName, javaPublicId, |
| 777 | javaSystemId); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Called by Expat at the end of a DOCTYPE section. |
| 782 | * |
| 783 | * @param data parsing context |
| 784 | */ |
| 785 | static void endDtd(void* data) { |
| 786 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 787 | JNIEnv* env = parsingContext->env; |
| 788 | |
| 789 | // Bail out if a previously called handler threw an exception. |
| 790 | if (env->ExceptionCheck()) return; |
| 791 | |
| 792 | jobject javaParser = parsingContext->object; |
| 793 | env->CallVoidMethod(javaParser, endDtdMethod); |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Called by Expat when it encounters processing instructions. |
| 798 | * |
| 799 | * @param data parsing context |
| 800 | * @param target of the instruction |
| 801 | * @param instructionData |
| 802 | */ |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 803 | static void processingInstruction(void* data, const char* target, const char* instructionData) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 804 | ParsingContext* parsingContext = (ParsingContext*) data; |
| 805 | JNIEnv* env = parsingContext->env; |
| 806 | |
| 807 | // Bail out if a previously called handler threw an exception. |
| 808 | if (env->ExceptionCheck()) return; |
| 809 | |
| 810 | jstring javaTarget = internString(env, parsingContext, target); |
| 811 | if (env->ExceptionCheck()) return; |
| 812 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 813 | ScopedLocalRef<jstring> javaInstructionData(env, env->NewStringUTF(instructionData)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 814 | if (env->ExceptionCheck()) return; |
| 815 | |
| 816 | jobject javaParser = parsingContext->object; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 817 | env->CallVoidMethod(javaParser, processingInstructionMethod, javaTarget, javaInstructionData.get()); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Creates a new entity parser. |
| 822 | * |
| 823 | * @param object the Java ExpatParser instance |
| 824 | * @param parentParser pointer |
| 825 | * @param javaEncoding the character encoding name |
| 826 | * @param javaContext that was provided to handleExternalEntity |
| 827 | * @returns the pointer to the C Expat entity parser |
| 828 | */ |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 829 | static jint createEntityParser(JNIEnv* env, jobject, jint parentParser, jstring javaContext) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 830 | ScopedUtfChars context(env, javaContext); |
| 831 | if (context.c_str() == NULL) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | XML_Parser parent = (XML_Parser) parentParser; |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 836 | XML_Parser entityParser = XML_ExternalEntityParserCreate(parent, context.c_str(), NULL); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 837 | if (entityParser == NULL) { |
| 838 | throw_OutOfMemoryError(env); |
| 839 | } |
| 840 | |
| 841 | return (jint) entityParser; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Handles external entities. We ignore the "base" URI and keep track of it |
| 846 | * ourselves. |
| 847 | */ |
| 848 | static int handleExternalEntity(XML_Parser parser, const char* context, |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 849 | const char*, const char* systemId, const char* publicId) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 850 | ParsingContext* parsingContext = (ParsingContext*) XML_GetUserData(parser); |
| 851 | jobject javaParser = parsingContext->object; |
| 852 | JNIEnv* env = parsingContext->env; |
| 853 | jobject object = parsingContext->object; |
| 854 | |
| 855 | // Bail out if a previously called handler threw an exception. |
| 856 | if (env->ExceptionCheck()) { |
| 857 | return XML_STATUS_ERROR; |
| 858 | } |
| 859 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 860 | ScopedLocalRef<jstring> javaSystemId(env, env->NewStringUTF(systemId)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 861 | if (env->ExceptionCheck()) { |
| 862 | return XML_STATUS_ERROR; |
| 863 | } |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 864 | ScopedLocalRef<jstring> javaPublicId(env, env->NewStringUTF(publicId)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 865 | if (env->ExceptionCheck()) { |
| 866 | return XML_STATUS_ERROR; |
| 867 | } |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 868 | ScopedLocalRef<jstring> javaContext(env, env->NewStringUTF(context)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | if (env->ExceptionCheck()) { |
| 870 | return XML_STATUS_ERROR; |
| 871 | } |
| 872 | |
| 873 | // Pass the wrapped parser and both strings to java. |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 874 | env->CallVoidMethod(javaParser, handleExternalEntityMethod, javaContext.get(), |
| 875 | javaPublicId.get(), javaSystemId.get()); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * Parsing the external entity leaves parsingContext->env and object set to |
| 879 | * NULL, so we need to restore both. |
| 880 | * |
| 881 | * TODO: consider restoring the original env and object instead of setting |
| 882 | * them to NULL in the append() functions. |
| 883 | */ |
| 884 | parsingContext->env = env; |
| 885 | parsingContext->object = object; |
| 886 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 887 | return env->ExceptionCheck() ? XML_STATUS_ERROR : XML_STATUS_OK; |
| 888 | } |
| 889 | |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 890 | /** |
| 891 | * Expat gives us 'base', but the Java API doesn't expect it, so we don't need it. |
| 892 | */ |
| 893 | static void unparsedEntityDecl(void* data, const char* name, const char* /*base*/, const char* systemId, const char* publicId, const char* notationName) { |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 894 | ParsingContext* parsingContext = reinterpret_cast<ParsingContext*>(data); |
| 895 | jobject javaParser = parsingContext->object; |
| 896 | JNIEnv* env = parsingContext->env; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 897 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 898 | // Bail out if a previously called handler threw an exception. |
| 899 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 900 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 901 | ScopedLocalRef<jstring> javaName(env, env->NewStringUTF(name)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 902 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 903 | ScopedLocalRef<jstring> javaPublicId(env, env->NewStringUTF(publicId)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 904 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 905 | ScopedLocalRef<jstring> javaSystemId(env, env->NewStringUTF(systemId)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 906 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 907 | ScopedLocalRef<jstring> javaNotationName(env, env->NewStringUTF(notationName)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 908 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 909 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 910 | env->CallVoidMethod(javaParser, unparsedEntityDeclMethod, javaName.get(), javaPublicId.get(), javaSystemId.get(), javaNotationName.get()); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 911 | } |
| 912 | |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 913 | /** |
| 914 | * Expat gives us 'base', but the Java API doesn't expect it, so we don't need it. |
| 915 | */ |
| 916 | static void notationDecl(void* data, const char* name, const char* /*base*/, const char* systemId, const char* publicId) { |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 917 | ParsingContext* parsingContext = reinterpret_cast<ParsingContext*>(data); |
| 918 | jobject javaParser = parsingContext->object; |
| 919 | JNIEnv* env = parsingContext->env; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 920 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 921 | // Bail out if a previously called handler threw an exception. |
| 922 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 923 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 924 | ScopedLocalRef<jstring> javaName(env, env->NewStringUTF(name)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 925 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 926 | ScopedLocalRef<jstring> javaPublicId(env, env->NewStringUTF(publicId)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 927 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 928 | ScopedLocalRef<jstring> javaSystemId(env, env->NewStringUTF(systemId)); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 929 | if (env->ExceptionCheck()) return; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 930 | |
Elliott Hughes | 8044bf6 | 2010-05-17 22:34:46 -0700 | [diff] [blame] | 931 | env->CallVoidMethod(javaParser, notationDeclMethod, javaName.get(), javaPublicId.get(), javaSystemId.get()); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 932 | } |
| 933 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 934 | /** |
Jesse Wilson | a144051 | 2010-01-29 17:06:44 -0800 | [diff] [blame] | 935 | * Creates a new Expat parser. Called from the Java ExpatParser constructor. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 936 | * |
| 937 | * @param object the Java ExpatParser instance |
| 938 | * @param javaEncoding the character encoding name |
| 939 | * @param processNamespaces true if the parser should handle namespaces |
| 940 | * @returns the pointer to the C Expat parser |
| 941 | */ |
| 942 | static jint initialize(JNIEnv* env, jobject object, jstring javaEncoding, |
| 943 | jboolean processNamespaces) { |
| 944 | // Allocate parsing context. |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 945 | UniquePtr<ParsingContext> context(new ParsingContext(object)); |
| 946 | if (context.get() == NULL) { |
| 947 | throw_OutOfMemoryError(env); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | context->processNamespaces = (bool) processNamespaces; |
| 952 | |
| 953 | // Create a parser. |
| 954 | XML_Parser parser; |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 955 | ScopedUtfChars encoding(env, javaEncoding); |
| 956 | if (encoding.c_str() == NULL) { |
| 957 | return 0; |
| 958 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 959 | if (processNamespaces) { |
| 960 | // Use '|' to separate URIs from local names. |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 961 | parser = XML_ParserCreateNS(encoding.c_str(), '|'); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 962 | } else { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 963 | parser = XML_ParserCreate(encoding.c_str()); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 964 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 965 | |
| 966 | if (parser != NULL) { |
| 967 | if (processNamespaces) { |
| 968 | XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 969 | XML_SetReturnNSTriplet(parser, 1); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 970 | } |
| 971 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 972 | XML_SetCdataSectionHandler(parser, startCdata, endCdata); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 973 | XML_SetCharacterDataHandler(parser, text); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 974 | XML_SetCommentHandler(parser, comment); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 975 | XML_SetDoctypeDeclHandler(parser, startDtd, endDtd); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 976 | XML_SetElementHandler(parser, startElement, endElement); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 977 | XML_SetExternalEntityRefHandler(parser, handleExternalEntity); |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 978 | XML_SetNotationDeclHandler(parser, notationDecl); |
| 979 | XML_SetProcessingInstructionHandler(parser, processingInstruction); |
| 980 | XML_SetUnparsedEntityDeclHandler(parser, unparsedEntityDecl); |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 981 | XML_SetUserData(parser, context.release()); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 982 | } else { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 983 | throw_OutOfMemoryError(env); |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | return (jint) parser; |
| 988 | } |
| 989 | |
| 990 | /** |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 991 | * Expat decides for itself what character encoding it's looking at. The interface is in terms of |
| 992 | * bytes, which may point to UTF-8, UTF-16, ISO-8859-1, or US-ASCII. appendBytes, appendCharacters, |
| 993 | * and appendString thus all call through to this method, strange though that appears. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 994 | */ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 995 | static void append(JNIEnv* env, jobject object, jint pointer, |
| 996 | const char* bytes, size_t byteOffset, size_t byteCount, jboolean isFinal) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 997 | XML_Parser parser = (XML_Parser) pointer; |
| 998 | ParsingContext* context = (ParsingContext*) XML_GetUserData(parser); |
| 999 | context->env = env; |
| 1000 | context->object = object; |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1001 | if (!XML_Parse(parser, bytes + byteOffset, byteCount, isFinal) && !env->ExceptionCheck()) { |
Elliott Hughes | 738f950 | 2009-10-01 11:20:29 -0700 | [diff] [blame] | 1002 | jniThrowExpatException(env, XML_GetErrorCode(parser)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1003 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1004 | context->object = NULL; |
| 1005 | context->env = NULL; |
| 1006 | } |
| 1007 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1008 | static void appendBytes(JNIEnv* env, jobject object, jint pointer, |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1009 | jbyteArray xml, jint byteOffset, jint byteCount) { |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 1010 | ScopedByteArrayRO byteArray(env, xml); |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 1011 | if (byteArray.get() == NULL) { |
| 1012 | return; |
| 1013 | } |
| 1014 | |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1015 | const char* bytes = reinterpret_cast<const char*>(byteArray.get()); |
| 1016 | append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE); |
| 1017 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1018 | |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1019 | static void appendCharacters(JNIEnv* env, jobject object, jint pointer, |
| 1020 | jcharArray xml, jint charOffset, jint charCount) { |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 1021 | ScopedCharArrayRO charArray(env, xml); |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 1022 | if (charArray.get() == NULL) { |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1026 | const char* bytes = reinterpret_cast<const char*>(charArray.get()); |
| 1027 | size_t byteOffset = 2 * charOffset; |
| 1028 | size_t byteCount = 2 * charCount; |
| 1029 | append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE); |
| 1030 | } |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1031 | |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 1032 | static void appendString(JNIEnv* env, jobject object, jint pointer, |
| 1033 | jstring javaXml, jboolean isFinal) { |
| 1034 | ScopedJavaUnicodeString xml(env, javaXml); |
| 1035 | const char* bytes = reinterpret_cast<const char*>(xml.unicodeString().getBuffer()); |
| 1036 | size_t byteCount = 2 * xml.unicodeString().length(); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1037 | append(env, object, pointer, bytes, 0, byteCount, isFinal); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Releases parser only. |
| 1042 | * |
| 1043 | * @param object the Java ExpatParser instance |
| 1044 | * @param i pointer to the C expat parser |
| 1045 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1046 | static void releaseParser(JNIEnv*, jobject, jint i) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1047 | XML_Parser parser = (XML_Parser) i; |
| 1048 | XML_ParserFree(parser); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Cleans up after the parser. Called at garbage collection time. |
| 1053 | * |
| 1054 | * @param object the Java ExpatParser instance |
| 1055 | * @param i pointer to the C expat parser |
| 1056 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1057 | static void release(JNIEnv* env, jobject, jint i) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1058 | XML_Parser parser = (XML_Parser) i; |
| 1059 | |
| 1060 | ParsingContext* context = (ParsingContext*) XML_GetUserData(parser); |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 1061 | context->env = env; |
| 1062 | delete context; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1063 | |
| 1064 | XML_ParserFree(parser); |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * Gets the current line. |
| 1069 | * |
| 1070 | * @param object the Java ExpatParser instance |
| 1071 | * @param pointer to the C expat parser |
| 1072 | * @returns current line number |
| 1073 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1074 | static int line(JNIEnv*, jobject, jint pointer) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1075 | XML_Parser parser = (XML_Parser) pointer; |
| 1076 | return XML_GetCurrentLineNumber(parser); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * Gets the current column. |
| 1081 | * |
| 1082 | * @param object the Java ExpatParser instance |
| 1083 | * @param pointer to the C expat parser |
| 1084 | * @returns current column number |
| 1085 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1086 | static int column(JNIEnv*, jobject, jint pointer) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1087 | XML_Parser parser = (XML_Parser) pointer; |
| 1088 | return XML_GetCurrentColumnNumber(parser); |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * Gets the URI of the attribute at the given index. |
| 1093 | * |
| 1094 | * @param object Java ExpatParser instance |
| 1095 | * @param pointer to the C expat parser |
| 1096 | * @param attributePointer to the attribute array |
| 1097 | * @param index of the attribute |
| 1098 | * @returns interned Java string containing attribute's URI |
| 1099 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1100 | static jstring getAttributeURI(JNIEnv* env, jobject, jint pointer, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1101 | jint attributePointer, jint index) { |
| 1102 | XML_Parser parser = (XML_Parser) pointer; |
| 1103 | ParsingContext* context = (ParsingContext*) XML_GetUserData(parser); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1104 | return ExpatElementName(env, context, attributePointer, index).uri(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * Gets the local name of the attribute at the given index. |
| 1109 | * |
| 1110 | * @param object Java ExpatParser instance |
| 1111 | * @param pointer to the C expat parser |
| 1112 | * @param attributePointer to the attribute array |
| 1113 | * @param index of the attribute |
| 1114 | * @returns interned Java string containing attribute's local name |
| 1115 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1116 | static jstring getAttributeLocalName(JNIEnv* env, jobject, jint pointer, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1117 | jint attributePointer, jint index) { |
| 1118 | XML_Parser parser = (XML_Parser) pointer; |
| 1119 | ParsingContext* context = (ParsingContext*) XML_GetUserData(parser); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1120 | return ExpatElementName(env, context, attributePointer, index).localName(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Gets the qualified name of the attribute at the given index. |
| 1125 | * |
| 1126 | * @param object Java ExpatParser instance |
| 1127 | * @param pointer to the C expat parser |
| 1128 | * @param attributePointer to the attribute array |
| 1129 | * @param index of the attribute |
| 1130 | * @returns interned Java string containing attribute's local name |
| 1131 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1132 | static jstring getAttributeQName(JNIEnv* env, jobject, jint pointer, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1133 | jint attributePointer, jint index) { |
| 1134 | XML_Parser parser = (XML_Parser) pointer; |
| 1135 | ParsingContext* context = (ParsingContext*) XML_GetUserData(parser); |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1136 | return ExpatElementName(env, context, attributePointer, index).qName(); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Gets the value of the attribute at the given index. |
| 1141 | * |
| 1142 | * @param object Java ExpatParser instance |
| 1143 | * @param attributePointer to the attribute array |
| 1144 | * @param index of the attribute |
| 1145 | * @returns Java string containing attribute's value |
| 1146 | */ |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1147 | static jstring getAttributeValueByIndex(JNIEnv* env, jobject, jint attributePointer, jint index) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1148 | const char** attributes = (const char**) attributePointer; |
| 1149 | const char* value = attributes[(index << 1) + 1]; |
| 1150 | return env->NewStringUTF(value); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1154 | * Gets the index of the attribute with the given qualified name. |
| 1155 | * |
| 1156 | * @param attributePointer to the attribute array |
| 1157 | * @param qName to look for |
| 1158 | * @returns index of attribute with the given uri and local name or -1 if not |
| 1159 | * found |
| 1160 | */ |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1161 | static jint getAttributeIndexForQName(JNIEnv* env, jobject, jint attributePointer, jstring qName) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1162 | const char** attributes = (const char**) attributePointer; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1163 | |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1164 | ScopedUtfChars qNameBytes(env, qName); |
| 1165 | if (qNameBytes.c_str() == NULL) { |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1166 | return -1; |
| 1167 | } |
| 1168 | |
| 1169 | int found = -1; |
| 1170 | for (int index = 0; attributes[index * 2]; ++index) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1171 | if (ExpatElementName(NULL, NULL, attributePointer, index).matchesQName(qNameBytes.c_str())) { |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1172 | found = index; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1177 | return found; |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Gets the index of the attribute with the given URI and name. |
| 1182 | * |
| 1183 | * @param attributePointer to the attribute array |
| 1184 | * @param uri to look for |
| 1185 | * @param localName to look for |
| 1186 | * @returns index of attribute with the given uri and local name or -1 if not |
| 1187 | * found |
| 1188 | */ |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1189 | static jint getAttributeIndex(JNIEnv* env, jobject, jint attributePointer, |
| 1190 | jstring uri, jstring localName) { |
| 1191 | ScopedUtfChars uriBytes(env, uri); |
| 1192 | if (uriBytes.c_str() == NULL) { |
| 1193 | return -1; |
| 1194 | } |
| 1195 | |
| 1196 | ScopedUtfChars localNameBytes(env, localName); |
| 1197 | if (localNameBytes.c_str() == NULL) { |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1201 | const char** attributes = (const char**) attributePointer; |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1202 | for (int index = 0; attributes[index * 2]; ++index) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1203 | if (ExpatElementName(NULL, NULL, attributePointer, index).matches(uriBytes.c_str(), |
| 1204 | localNameBytes.c_str())) { |
| 1205 | return index; |
Jesse Wilson | df36e8c | 2010-01-23 00:49:53 -0800 | [diff] [blame] | 1206 | } |
| 1207 | } |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1208 | return -1; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Gets the value of the attribute with the given qualified name. |
| 1213 | * |
| 1214 | * @param attributePointer to the attribute array |
| 1215 | * @param uri to look for |
| 1216 | * @param localName to look for |
| 1217 | * @returns value of attribute with the given uri and local name or NULL if not |
| 1218 | * found |
| 1219 | */ |
| 1220 | static jstring getAttributeValueForQName(JNIEnv* env, jobject clazz, |
| 1221 | jint attributePointer, jstring qName) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1222 | jint index = getAttributeIndexForQName(env, clazz, attributePointer, qName); |
| 1223 | return index == -1 ? NULL : getAttributeValueByIndex(env, clazz, attributePointer, index); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /** |
| 1227 | * Gets the value of the attribute with the given URI and name. |
| 1228 | * |
| 1229 | * @param attributePointer to the attribute array |
| 1230 | * @param uri to look for |
| 1231 | * @param localName to look for |
| 1232 | * @returns value of attribute with the given uri and local name or NULL if not |
| 1233 | * found |
| 1234 | */ |
| 1235 | static jstring getAttributeValue(JNIEnv* env, jobject clazz, |
| 1236 | jint attributePointer, jstring uri, jstring localName) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1237 | jint index = getAttributeIndex(env, clazz, attributePointer, uri, localName); |
| 1238 | return index == -1 ? NULL : getAttributeValueByIndex(env, clazz, attributePointer, index); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * Clones an array of strings. Uses one contiguous block of memory so as to |
| 1243 | * maximize performance. |
Elliott Hughes | 9149904 | 2010-06-21 18:06:12 -0700 | [diff] [blame] | 1244 | * |
| 1245 | * @param address char** to clone |
| 1246 | * @param count number of attributes |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1247 | */ |
Elliott Hughes | 9149904 | 2010-06-21 18:06:12 -0700 | [diff] [blame] | 1248 | static jint cloneAttributes(JNIEnv* env, jobject, jint address, jint count) { |
| 1249 | const char** source = reinterpret_cast<const char**>(static_cast<uintptr_t>(address)); |
| 1250 | count <<= 1; |
| 1251 | |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1252 | // Figure out how big the buffer needs to be. |
| 1253 | int arraySize = (count + 1) * sizeof(char*); |
| 1254 | int totalSize = arraySize; |
| 1255 | int stringLengths[count]; |
| 1256 | for (int i = 0; i < count; i++) { |
| 1257 | int length = strlen(source[i]); |
| 1258 | stringLengths[i] = length; |
| 1259 | totalSize += length + 1; |
| 1260 | } |
| 1261 | |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 1262 | char* buffer = new char[totalSize]; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1263 | if (buffer == NULL) { |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 1264 | throw_OutOfMemoryError(env); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1265 | return NULL; |
| 1266 | } |
| 1267 | |
| 1268 | // Array is at the beginning of the buffer. |
| 1269 | char** clonedArray = (char**) buffer; |
| 1270 | clonedArray[count] = NULL; // null terminate |
| 1271 | |
Elliott Hughes | 9149904 | 2010-06-21 18:06:12 -0700 | [diff] [blame] | 1272 | // String data follows immediately after. |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1273 | char* destinationString = buffer + arraySize; |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1274 | for (int i = 0; i < count; i++) { |
| 1275 | const char* sourceString = source[i]; |
| 1276 | int stringLength = stringLengths[i]; |
| 1277 | memcpy(destinationString, sourceString, stringLength + 1); |
| 1278 | clonedArray[i] = destinationString; |
| 1279 | destinationString += stringLength + 1; |
| 1280 | } |
| 1281 | |
Elliott Hughes | 9149904 | 2010-06-21 18:06:12 -0700 | [diff] [blame] | 1282 | return static_cast<jint>(reinterpret_cast<uintptr_t>(buffer)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * Frees cloned attributes. |
| 1287 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame] | 1288 | static void freeAttributes(JNIEnv*, jobject, jint pointer) { |
Elliott Hughes | 4eafbcc | 2010-06-21 16:52:06 -0700 | [diff] [blame] | 1289 | delete[] reinterpret_cast<char*>(static_cast<uintptr_t>(pointer)); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * Called when we initialize our Java parser class. |
| 1294 | * |
| 1295 | * @param clazz Java ExpatParser class |
| 1296 | */ |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1297 | static void staticInitialize(JNIEnv* env, jobject classObject, jstring empty) { |
| 1298 | jclass clazz = reinterpret_cast<jclass>(classObject); |
| 1299 | startElementMethod = env->GetMethodID(clazz, "startElement", |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1300 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)V"); |
| 1301 | if (startElementMethod == NULL) return; |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 1302 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1303 | endElementMethod = env->GetMethodID(clazz, "endElement", |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1304 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 1305 | if (endElementMethod == NULL) return; |
| 1306 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1307 | textMethod = env->GetMethodID(clazz, "text", "([CI)V"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1308 | if (textMethod == NULL) return; |
| 1309 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1310 | commentMethod = env->GetMethodID(clazz, "comment", "([CI)V"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1311 | if (commentMethod == NULL) return; |
| 1312 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1313 | startCdataMethod = env->GetMethodID(clazz, "startCdata", "()V"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1314 | if (startCdataMethod == NULL) return; |
| 1315 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1316 | endCdataMethod = env->GetMethodID(clazz, "endCdata", "()V"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1317 | if (endCdataMethod == NULL) return; |
| 1318 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1319 | startDtdMethod = env->GetMethodID(clazz, "startDtd", |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1320 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 1321 | if (startDtdMethod == NULL) return; |
| 1322 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1323 | endDtdMethod = env->GetMethodID(clazz, "endDtd", "()V"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1324 | if (endDtdMethod == NULL) return; |
| 1325 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1326 | startNamespaceMethod = env->GetMethodID(clazz, "startNamespace", |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1327 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 1328 | if (startNamespaceMethod == NULL) return; |
| 1329 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1330 | endNamespaceMethod = env->GetMethodID(clazz, "endNamespace", |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1331 | "(Ljava/lang/String;)V"); |
| 1332 | if (endNamespaceMethod == NULL) return; |
| 1333 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1334 | processingInstructionMethod = env->GetMethodID(clazz, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1335 | "processingInstruction", "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 1336 | if (processingInstructionMethod == NULL) return; |
| 1337 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1338 | handleExternalEntityMethod = env->GetMethodID(clazz, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1339 | "handleExternalEntity", |
| 1340 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 1341 | if (handleExternalEntityMethod == NULL) return; |
| 1342 | |
Elliott Hughes | c9b92b4 | 2009-12-09 16:05:29 -0800 | [diff] [blame] | 1343 | notationDeclMethod = env->GetMethodID(clazz, "notationDecl", |
| 1344 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 1345 | if (notationDeclMethod == NULL) return; |
| 1346 | |
| 1347 | unparsedEntityDeclMethod = env->GetMethodID(clazz, "unparsedEntityDecl", |
| 1348 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 1349 | if (unparsedEntityDeclMethod == NULL) return; |
| 1350 | |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 1351 | internMethod = env->GetMethodID(JniConstants::stringClass, "intern", "()Ljava/lang/String;"); |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1352 | if (internMethod == NULL) return; |
| 1353 | |
| 1354 | // Reference to "". |
| 1355 | emptyString = (jstring) env->NewGlobalRef(empty); |
| 1356 | } |
| 1357 | |
| 1358 | static JNINativeMethod parserMethods[] = { |
| 1359 | { "line", "(I)I", (void*) line }, |
| 1360 | { "column", "(I)I", (void*) column }, |
| 1361 | { "release", "(I)V", (void*) release }, |
| 1362 | { "releaseParser", "(I)V", (void*) releaseParser }, |
| 1363 | { "append", "(ILjava/lang/String;Z)V", (void*) appendString }, |
| 1364 | { "append", "(I[CII)V", (void*) appendCharacters }, |
| 1365 | { "append", "(I[BII)V", (void*) appendBytes }, |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 1366 | { "initialize", "(Ljava/lang/String;Z)I", (void*) initialize}, |
Elliott Hughes | 4e3714f | 2010-06-22 14:32:47 -0700 | [diff] [blame] | 1367 | { "createEntityParser", "(ILjava/lang/String;)I", (void*) createEntityParser}, |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1368 | { "staticInitialize", "(Ljava/lang/String;)V", (void*) staticInitialize}, |
| 1369 | { "cloneAttributes", "(II)I", (void*) cloneAttributes }, |
| 1370 | }; |
| 1371 | |
| 1372 | static JNINativeMethod attributeMethods[] = { |
| 1373 | { "getURI", "(III)Ljava/lang/String;", (void*) getAttributeURI }, |
| 1374 | { "getLocalName", "(III)Ljava/lang/String;", (void*) getAttributeLocalName }, |
| 1375 | { "getQName", "(III)Ljava/lang/String;", (void*) getAttributeQName }, |
| 1376 | { "getValue", "(II)Ljava/lang/String;", (void*) getAttributeValueByIndex }, |
| 1377 | { "getIndex", "(ILjava/lang/String;Ljava/lang/String;)I", |
| 1378 | (void*) getAttributeIndex }, |
| 1379 | { "getIndex", "(ILjava/lang/String;)I", |
| 1380 | (void*) getAttributeIndexForQName }, |
| 1381 | { "getValue", "(ILjava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 1382 | (void*) getAttributeValue }, |
| 1383 | { "getValue", "(ILjava/lang/String;)Ljava/lang/String;", |
| 1384 | (void*) getAttributeValueForQName }, |
| 1385 | { "freeAttributes", "(I)V", (void*) freeAttributes }, |
| 1386 | }; |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 1387 | int register_org_apache_harmony_xml_ExpatParser(JNIEnv* env) { |
The Android Open Source Project | fdb2704 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1388 | int result = jniRegisterNativeMethods(env, "org/apache/harmony/xml/ExpatParser", |
| 1389 | parserMethods, NELEM(parserMethods)); |
| 1390 | if (result != 0) { |
| 1391 | return result; |
| 1392 | } |
| 1393 | |
| 1394 | return jniRegisterNativeMethods(env, "org/apache/harmony/xml/ExpatAttributes", |
| 1395 | attributeMethods, NELEM(attributeMethods)); |
| 1396 | } |