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"
21 #include <utils/misc.h>
22 #include <android_runtime/AndroidRuntime.h>
23 #include <utils/AssetManager.h>
24 #include <utils/Log.h>
26 #include <utils/ResourceTypes.h>
32 // ----------------------------------------------------------------------------
34 static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
38 npeClazz = env->FindClass(exc);
39 LOG_FATAL_IF(npeClazz == NULL, "Unable to find class %s", exc);
41 env->ThrowNew(npeClazz, msg);
44 static jint android_content_XmlBlock_nativeCreate(JNIEnv* env, jobject clazz,
49 doThrow(env, "java/lang/NullPointerException");
53 jsize bLen = env->GetArrayLength(bArray);
54 if (off < 0 || off >= bLen || len < 0 || len > bLen || (off+len) > bLen) {
55 doThrow(env, "java/lang/IndexOutOfBoundsException");
59 jbyte* b = env->GetByteArrayElements(bArray, NULL);
60 ResXMLTree* osb = new ResXMLTree(b+off, len, true);
61 env->ReleaseByteArrayElements(bArray, b, 0);
63 if (osb == NULL || osb->getError() != NO_ERROR) {
64 doThrow(env, "java/lang/IllegalArgumentException");
71 static jint android_content_XmlBlock_nativeGetStringBlock(JNIEnv* env, jobject clazz,
74 ResXMLTree* osb = (ResXMLTree*)token;
76 doThrow(env, "java/lang/NullPointerException");
80 return (jint)&osb->getStrings();
83 static jint android_content_XmlBlock_nativeCreateParseState(JNIEnv* env, jobject clazz,
86 ResXMLTree* osb = (ResXMLTree*)token;
88 doThrow(env, "java/lang/NullPointerException");
92 ResXMLParser* st = new ResXMLParser(*osb);
94 doThrow(env, "java/lang/OutOfMemoryError");
103 static jint android_content_XmlBlock_nativeNext(JNIEnv* env, jobject clazz,
106 ResXMLParser* st = (ResXMLParser*)token;
108 return ResXMLParser::END_DOCUMENT;
112 jint code = (jint)st->next();
114 case ResXMLParser::START_TAG:
116 case ResXMLParser::END_TAG:
118 case ResXMLParser::TEXT:
120 case ResXMLParser::START_DOCUMENT:
122 case ResXMLParser::END_DOCUMENT:
124 case ResXMLParser::BAD_DOCUMENT:
130 doThrow(env, "org/xmlpull/v1/XmlPullParserException",
131 "Corrupt XML binary file");
132 return ResXMLParser::BAD_DOCUMENT;
135 static jint android_content_XmlBlock_nativeGetNamespace(JNIEnv* env, jobject clazz,
138 ResXMLParser* st = (ResXMLParser*)token;
143 return (jint)st->getElementNamespaceID();
146 static jint android_content_XmlBlock_nativeGetName(JNIEnv* env, jobject clazz,
149 ResXMLParser* st = (ResXMLParser*)token;
154 return (jint)st->getElementNameID();
157 static jint android_content_XmlBlock_nativeGetText(JNIEnv* env, jobject clazz,
160 ResXMLParser* st = (ResXMLParser*)token;
165 return (jint)st->getTextID();
168 static jint android_content_XmlBlock_nativeGetLineNumber(JNIEnv* env, jobject clazz,
171 ResXMLParser* st = (ResXMLParser*)token;
173 doThrow(env, "java/lang/NullPointerException");
177 return (jint)st->getLineNumber();
180 static jint android_content_XmlBlock_nativeGetAttributeCount(JNIEnv* env, jobject clazz,
183 ResXMLParser* st = (ResXMLParser*)token;
185 doThrow(env, "java/lang/NullPointerException");
189 return (jint)st->getAttributeCount();
192 static jint android_content_XmlBlock_nativeGetAttributeNamespace(JNIEnv* env, jobject clazz,
193 jint token, jint idx)
195 ResXMLParser* st = (ResXMLParser*)token;
197 doThrow(env, "java/lang/NullPointerException");
201 return (jint)st->getAttributeNamespaceID(idx);
204 static jint android_content_XmlBlock_nativeGetAttributeName(JNIEnv* env, jobject clazz,
205 jint token, jint idx)
207 ResXMLParser* st = (ResXMLParser*)token;
209 doThrow(env, "java/lang/NullPointerException");
213 return (jint)st->getAttributeNameID(idx);
216 static jint android_content_XmlBlock_nativeGetAttributeResource(JNIEnv* env, jobject clazz,
217 jint token, jint idx)
219 ResXMLParser* st = (ResXMLParser*)token;
221 doThrow(env, "java/lang/NullPointerException");
225 return (jint)st->getAttributeNameResID(idx);
228 static jint android_content_XmlBlock_nativeGetAttributeDataType(JNIEnv* env, jobject clazz,
229 jint token, jint idx)
231 ResXMLParser* st = (ResXMLParser*)token;
233 doThrow(env, "java/lang/NullPointerException");
237 return (jint)st->getAttributeDataType(idx);
240 static jint android_content_XmlBlock_nativeGetAttributeData(JNIEnv* env, jobject clazz,
241 jint token, jint idx)
243 ResXMLParser* st = (ResXMLParser*)token;
245 doThrow(env, "java/lang/NullPointerException");
249 return (jint)st->getAttributeData(idx);
252 static jint android_content_XmlBlock_nativeGetAttributeStringValue(JNIEnv* env, jobject clazz,
253 jint token, jint idx)
255 ResXMLParser* st = (ResXMLParser*)token;
257 doThrow(env, "java/lang/NullPointerException");
261 return (jint)st->getAttributeValueStringID(idx);
264 static jint android_content_XmlBlock_nativeGetAttributeIndex(JNIEnv* env, jobject clazz,
266 jstring ns, jstring name)
268 ResXMLParser* st = (ResXMLParser*)token;
269 if (st == NULL || name == NULL) {
270 doThrow(env, "java/lang/NullPointerException");
274 const char16_t* ns16 = NULL;
277 ns16 = env->GetStringChars(ns, NULL);
278 nsLen = env->GetStringLength(ns);
281 const char16_t* name16 = env->GetStringChars(name, NULL);
282 jsize nameLen = env->GetStringLength(name);
284 jint idx = (jint)st->indexOfAttribute(ns16, nsLen, name16, nameLen);
287 env->ReleaseStringChars(ns, ns16);
289 env->ReleaseStringChars(name, name16);
294 static jint android_content_XmlBlock_nativeGetIdAttribute(JNIEnv* env, jobject clazz,
297 ResXMLParser* st = (ResXMLParser*)token;
299 doThrow(env, "java/lang/NullPointerException");
303 ssize_t idx = st->indexOfID();
304 return idx >= 0 ? (jint)st->getAttributeValueStringID(idx) : -1;
307 static jint android_content_XmlBlock_nativeGetClassAttribute(JNIEnv* env, jobject clazz,
310 ResXMLParser* st = (ResXMLParser*)token;
312 doThrow(env, "java/lang/NullPointerException");
316 ssize_t idx = st->indexOfClass();
317 return idx >= 0 ? (jint)st->getAttributeValueStringID(idx) : -1;
320 static jint android_content_XmlBlock_nativeGetStyleAttribute(JNIEnv* env, jobject clazz,
323 ResXMLParser* st = (ResXMLParser*)token;
325 doThrow(env, "java/lang/NullPointerException");
329 ssize_t idx = st->indexOfStyle();
335 if (st->getAttributeValue(idx, &value) < 0) {
339 return value.dataType == value.TYPE_REFERENCE
340 || value.dataType == value.TYPE_ATTRIBUTE
344 static void android_content_XmlBlock_nativeDestroyParseState(JNIEnv* env, jobject clazz,
347 ResXMLParser* st = (ResXMLParser*)token;
349 doThrow(env, "java/lang/NullPointerException");
356 static void android_content_XmlBlock_nativeDestroy(JNIEnv* env, jobject clazz,
359 ResXMLTree* osb = (ResXMLTree*)token;
361 doThrow(env, "java/lang/NullPointerException");
368 // ----------------------------------------------------------------------------
373 static JNINativeMethod gXmlBlockMethods[] = {
374 /* name, signature, funcPtr */
375 { "nativeCreate", "([BII)I",
376 (void*) android_content_XmlBlock_nativeCreate },
377 { "nativeGetStringBlock", "(I)I",
378 (void*) android_content_XmlBlock_nativeGetStringBlock },
379 { "nativeCreateParseState", "(I)I",
380 (void*) android_content_XmlBlock_nativeCreateParseState },
381 { "nativeNext", "(I)I",
382 (void*) android_content_XmlBlock_nativeNext },
383 { "nativeGetNamespace", "(I)I",
384 (void*) android_content_XmlBlock_nativeGetNamespace },
385 { "nativeGetName", "(I)I",
386 (void*) android_content_XmlBlock_nativeGetName },
387 { "nativeGetText", "(I)I",
388 (void*) android_content_XmlBlock_nativeGetText },
389 { "nativeGetLineNumber", "(I)I",
390 (void*) android_content_XmlBlock_nativeGetLineNumber },
391 { "nativeGetAttributeCount", "(I)I",
392 (void*) android_content_XmlBlock_nativeGetAttributeCount },
393 { "nativeGetAttributeNamespace","(II)I",
394 (void*) android_content_XmlBlock_nativeGetAttributeNamespace },
395 { "nativeGetAttributeName", "(II)I",
396 (void*) android_content_XmlBlock_nativeGetAttributeName },
397 { "nativeGetAttributeResource", "(II)I",
398 (void*) android_content_XmlBlock_nativeGetAttributeResource },
399 { "nativeGetAttributeDataType", "(II)I",
400 (void*) android_content_XmlBlock_nativeGetAttributeDataType },
401 { "nativeGetAttributeData", "(II)I",
402 (void*) android_content_XmlBlock_nativeGetAttributeData },
403 { "nativeGetAttributeStringValue", "(II)I",
404 (void*) android_content_XmlBlock_nativeGetAttributeStringValue },
405 { "nativeGetAttributeIndex", "(ILjava/lang/String;Ljava/lang/String;)I",
406 (void*) android_content_XmlBlock_nativeGetAttributeIndex },
407 { "nativeGetIdAttribute", "(I)I",
408 (void*) android_content_XmlBlock_nativeGetIdAttribute },
409 { "nativeGetClassAttribute", "(I)I",
410 (void*) android_content_XmlBlock_nativeGetClassAttribute },
411 { "nativeGetStyleAttribute", "(I)I",
412 (void*) android_content_XmlBlock_nativeGetStyleAttribute },
413 { "nativeDestroyParseState", "(I)V",
414 (void*) android_content_XmlBlock_nativeDestroyParseState },
415 { "nativeDestroy", "(I)V",
416 (void*) android_content_XmlBlock_nativeDestroy },
419 int register_android_content_XmlBlock(JNIEnv* env)
421 return AndroidRuntime::registerNativeMethods(env,
422 "android/content/res/XmlBlock", gXmlBlockMethods, NELEM(gXmlBlockMethods));
425 }; // namespace android