1 /* //device/libs/android_runtime/android_util_XmlBlock.cpp
3 ** Copyright 2006, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
18 #define LOG_TAG "XmlBlock"
22 #include <android_runtime/AndroidRuntime.h>
23 #include <androidfw/AssetManager.h>
24 #include <androidfw/ResourceTypes.h>
25 #include <utils/Log.h>
26 #include <utils/misc.h>
32 // ----------------------------------------------------------------------------
34 static jint android_content_XmlBlock_nativeCreate(JNIEnv* env, jobject clazz,
39 jniThrowNullPointerException(env, NULL);
43 jsize bLen = env->GetArrayLength(bArray);
44 if (off < 0 || off >= bLen || len < 0 || len > bLen || (off+len) > bLen) {
45 jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL);
49 jbyte* b = env->GetByteArrayElements(bArray, NULL);
50 ResXMLTree* osb = new ResXMLTree(b+off, len, true);
51 env->ReleaseByteArrayElements(bArray, b, 0);
53 if (osb == NULL || osb->getError() != NO_ERROR) {
54 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
61 static jint android_content_XmlBlock_nativeGetStringBlock(JNIEnv* env, jobject clazz,
64 ResXMLTree* osb = (ResXMLTree*)token;
66 jniThrowNullPointerException(env, NULL);
70 return (jint)&osb->getStrings();
73 static jint android_content_XmlBlock_nativeCreateParseState(JNIEnv* env, jobject clazz,
76 ResXMLTree* osb = (ResXMLTree*)token;
78 jniThrowNullPointerException(env, NULL);
82 ResXMLParser* st = new ResXMLParser(*osb);
84 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
93 static jint android_content_XmlBlock_nativeNext(JNIEnv* env, jobject clazz,
96 ResXMLParser* st = (ResXMLParser*)token;
98 return ResXMLParser::END_DOCUMENT;
102 jint code = (jint)st->next();
104 case ResXMLParser::START_TAG:
106 case ResXMLParser::END_TAG:
108 case ResXMLParser::TEXT:
110 case ResXMLParser::START_DOCUMENT:
112 case ResXMLParser::END_DOCUMENT:
114 case ResXMLParser::BAD_DOCUMENT:
120 jniThrowException(env, "org/xmlpull/v1/XmlPullParserException",
121 "Corrupt XML binary file");
122 return ResXMLParser::BAD_DOCUMENT;
125 static jint android_content_XmlBlock_nativeGetNamespace(JNIEnv* env, jobject clazz,
128 ResXMLParser* st = (ResXMLParser*)token;
133 return (jint)st->getElementNamespaceID();
136 static jint android_content_XmlBlock_nativeGetName(JNIEnv* env, jobject clazz,
139 ResXMLParser* st = (ResXMLParser*)token;
144 return (jint)st->getElementNameID();
147 static jint android_content_XmlBlock_nativeGetText(JNIEnv* env, jobject clazz,
150 ResXMLParser* st = (ResXMLParser*)token;
155 return (jint)st->getTextID();
158 static jint android_content_XmlBlock_nativeGetLineNumber(JNIEnv* env, jobject clazz,
161 ResXMLParser* st = (ResXMLParser*)token;
163 jniThrowNullPointerException(env, NULL);
167 return (jint)st->getLineNumber();
170 static jint android_content_XmlBlock_nativeGetAttributeCount(JNIEnv* env, jobject clazz,
173 ResXMLParser* st = (ResXMLParser*)token;
175 jniThrowNullPointerException(env, NULL);
179 return (jint)st->getAttributeCount();
182 static jint android_content_XmlBlock_nativeGetAttributeNamespace(JNIEnv* env, jobject clazz,
183 jint token, jint idx)
185 ResXMLParser* st = (ResXMLParser*)token;
187 jniThrowNullPointerException(env, NULL);
191 return (jint)st->getAttributeNamespaceID(idx);
194 static jint android_content_XmlBlock_nativeGetAttributeName(JNIEnv* env, jobject clazz,
195 jint token, jint idx)
197 ResXMLParser* st = (ResXMLParser*)token;
199 jniThrowNullPointerException(env, NULL);
203 return (jint)st->getAttributeNameID(idx);
206 static jint android_content_XmlBlock_nativeGetAttributeResource(JNIEnv* env, jobject clazz,
207 jint token, jint idx)
209 ResXMLParser* st = (ResXMLParser*)token;
211 jniThrowNullPointerException(env, NULL);
215 return (jint)st->getAttributeNameResID(idx);
218 static jint android_content_XmlBlock_nativeGetAttributeDataType(JNIEnv* env, jobject clazz,
219 jint token, jint idx)
221 ResXMLParser* st = (ResXMLParser*)token;
223 jniThrowNullPointerException(env, NULL);
227 return (jint)st->getAttributeDataType(idx);
230 static jint android_content_XmlBlock_nativeGetAttributeData(JNIEnv* env, jobject clazz,
231 jint token, jint idx)
233 ResXMLParser* st = (ResXMLParser*)token;
235 jniThrowNullPointerException(env, NULL);
239 return (jint)st->getAttributeData(idx);
242 static jint android_content_XmlBlock_nativeGetAttributeStringValue(JNIEnv* env, jobject clazz,
243 jint token, jint idx)
245 ResXMLParser* st = (ResXMLParser*)token;
247 jniThrowNullPointerException(env, NULL);
251 return (jint)st->getAttributeValueStringID(idx);
254 static jint android_content_XmlBlock_nativeGetAttributeIndex(JNIEnv* env, jobject clazz,
256 jstring ns, jstring name)
258 ResXMLParser* st = (ResXMLParser*)token;
259 if (st == NULL || name == NULL) {
260 jniThrowNullPointerException(env, NULL);
264 const char16_t* ns16 = NULL;
267 ns16 = env->GetStringChars(ns, NULL);
268 nsLen = env->GetStringLength(ns);
271 const char16_t* name16 = env->GetStringChars(name, NULL);
272 jsize nameLen = env->GetStringLength(name);
274 jint idx = (jint)st->indexOfAttribute(ns16, nsLen, name16, nameLen);
277 env->ReleaseStringChars(ns, ns16);
279 env->ReleaseStringChars(name, name16);
284 static jint android_content_XmlBlock_nativeGetIdAttribute(JNIEnv* env, jobject clazz,
287 ResXMLParser* st = (ResXMLParser*)token;
289 jniThrowNullPointerException(env, NULL);
293 ssize_t idx = st->indexOfID();
294 return idx >= 0 ? (jint)st->getAttributeValueStringID(idx) : -1;
297 static jint android_content_XmlBlock_nativeGetClassAttribute(JNIEnv* env, jobject clazz,
300 ResXMLParser* st = (ResXMLParser*)token;
302 jniThrowNullPointerException(env, NULL);
306 ssize_t idx = st->indexOfClass();
307 return idx >= 0 ? (jint)st->getAttributeValueStringID(idx) : -1;
310 static jint android_content_XmlBlock_nativeGetStyleAttribute(JNIEnv* env, jobject clazz,
313 ResXMLParser* st = (ResXMLParser*)token;
315 jniThrowNullPointerException(env, NULL);
319 ssize_t idx = st->indexOfStyle();
325 if (st->getAttributeValue(idx, &value) < 0) {
329 return value.dataType == value.TYPE_REFERENCE
330 || value.dataType == value.TYPE_ATTRIBUTE
334 static void android_content_XmlBlock_nativeDestroyParseState(JNIEnv* env, jobject clazz,
337 ResXMLParser* st = (ResXMLParser*)token;
339 jniThrowNullPointerException(env, NULL);
346 static void android_content_XmlBlock_nativeDestroy(JNIEnv* env, jobject clazz,
349 ResXMLTree* osb = (ResXMLTree*)token;
351 jniThrowNullPointerException(env, NULL);
358 // ----------------------------------------------------------------------------
363 static JNINativeMethod gXmlBlockMethods[] = {
364 /* name, signature, funcPtr */
365 { "nativeCreate", "([BII)I",
366 (void*) android_content_XmlBlock_nativeCreate },
367 { "nativeGetStringBlock", "(I)I",
368 (void*) android_content_XmlBlock_nativeGetStringBlock },
369 { "nativeCreateParseState", "(I)I",
370 (void*) android_content_XmlBlock_nativeCreateParseState },
371 { "nativeNext", "(I)I",
372 (void*) android_content_XmlBlock_nativeNext },
373 { "nativeGetNamespace", "(I)I",
374 (void*) android_content_XmlBlock_nativeGetNamespace },
375 { "nativeGetName", "(I)I",
376 (void*) android_content_XmlBlock_nativeGetName },
377 { "nativeGetText", "(I)I",
378 (void*) android_content_XmlBlock_nativeGetText },
379 { "nativeGetLineNumber", "(I)I",
380 (void*) android_content_XmlBlock_nativeGetLineNumber },
381 { "nativeGetAttributeCount", "(I)I",
382 (void*) android_content_XmlBlock_nativeGetAttributeCount },
383 { "nativeGetAttributeNamespace","(II)I",
384 (void*) android_content_XmlBlock_nativeGetAttributeNamespace },
385 { "nativeGetAttributeName", "(II)I",
386 (void*) android_content_XmlBlock_nativeGetAttributeName },
387 { "nativeGetAttributeResource", "(II)I",
388 (void*) android_content_XmlBlock_nativeGetAttributeResource },
389 { "nativeGetAttributeDataType", "(II)I",
390 (void*) android_content_XmlBlock_nativeGetAttributeDataType },
391 { "nativeGetAttributeData", "(II)I",
392 (void*) android_content_XmlBlock_nativeGetAttributeData },
393 { "nativeGetAttributeStringValue", "(II)I",
394 (void*) android_content_XmlBlock_nativeGetAttributeStringValue },
395 { "nativeGetAttributeIndex", "(ILjava/lang/String;Ljava/lang/String;)I",
396 (void*) android_content_XmlBlock_nativeGetAttributeIndex },
397 { "nativeGetIdAttribute", "(I)I",
398 (void*) android_content_XmlBlock_nativeGetIdAttribute },
399 { "nativeGetClassAttribute", "(I)I",
400 (void*) android_content_XmlBlock_nativeGetClassAttribute },
401 { "nativeGetStyleAttribute", "(I)I",
402 (void*) android_content_XmlBlock_nativeGetStyleAttribute },
403 { "nativeDestroyParseState", "(I)V",
404 (void*) android_content_XmlBlock_nativeDestroyParseState },
405 { "nativeDestroy", "(I)V",
406 (void*) android_content_XmlBlock_nativeDestroy },
409 int register_android_content_XmlBlock(JNIEnv* env)
411 return AndroidRuntime::registerNativeMethods(env,
412 "android/content/res/XmlBlock", gXmlBlockMethods, NELEM(gXmlBlockMethods));
415 }; // namespace android