解析AndroidManifest.xml

源码地址:

http://code.google.com/p/android4me/source/browse/src/android4me/res/AXMLParser.java

/* * Copyright 2008 Android4ME * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android4me.res; import java.io.IOException; import java.io.InputStream; /** * @author Dmitry Skiba * * Parser for Android's binary xml files (axml). * * TODO: * * understand ? values */ public final class AXMLParser { /** * Types of returned tags. * Values are compatible to those in XmlPullParser. */ public static final int START_DOCUMENT =0, END_DOCUMENT =1, START_TAG =2, END_TAG =3, TEXT =4; /** * Creates object and reads file info. * Call next() to read first tag. */ public AXMLParser(InputStream stream) throws IOException { m_stream=stream; doStart(); } /** * Closes parser: * * closes (and nulls) underlying stream * * nulls dynamic data * * moves object to 'closed' state, where methods * return invalid values and next() throws IOException. */ public final void close() { if (m_stream==null) { return; } try { m_stream.close(); } catch (IOException e) { } if (m_nextException==null) { m_nextException=new IOException("Closed."); } m_stream=null; resetState(); } /** * Advances to the next tag. * Once method returns END_DOCUMENT, it always returns END_DOCUMENT. * Once method throws an exception, it always throws the same exception. * */ public final int next() throws IOException { if (m_nextException!=null) { throw m_nextException; } try { return doNext(); } catch (IOException e) { m_nextException=e; resetState(); throw e; } } /** * Returns current tag type. */ public final int getType() { return m_tagType; } /** * Returns name for the current tag. */ public final String getName() { if (m_tagName==-1) { return null; } return getString(m_tagName); } /** * Returns line number in the original XML where the current tag was. */ public final int getLineNumber() { return m_tagSourceLine; } /** * Returns count of attributes for the current tag. */ public final int getAttributeCount() { if (m_tagAttributes==null) { return -1; } return m_tagAttributes.length; } /** * Returns attribute namespace. */ public final String getAttributeNamespace(int index) { return getString(getAttribute(index).namespace); } /** * Returns attribute name. */ public final String getAttributeName(int index) { return getString(getAttribute(index).name); } /** * Returns attribute resource ID. */ public final int getAttributeResourceID(int index) { int resourceIndex=getAttribute(index).name; if (m_resourceIDs==null || resourceIndex<0 || resourceIndex>=m_resourceIDs.length) { return 0; } return m_resourceIDs[resourceIndex]; } /** * Returns type of attribute value. * See TypedValue.TYPE_ values. */ public final int getAttributeValueType(int index) { return getAttribute(index).valueType; } /** * For attributes of type TypedValue.TYPE_STRING returns * string value. For other types returns empty string. */ public final String getAttributeValueString(int index) { return getString(getAttribute(index).valueString); } /** * Returns integer attribute value. * This integer interpreted according to attribute type. */ public final int getAttributeValue(int index) { return getAttribute(index).value; } / implementation private static final class TagAttribute { public int namespace; public int name; public int valueString; public int valueType; public int value; } private final void resetState() { m_tagType=-1; m_tagSourceLine=-1; m_tagName=-1; m_tagAttributes=null; } private final void doStart() throws IOException { ReadUtil.readCheckType(m_stream,AXML_CHUNK_TYPE); /*chunk size*/ReadUtil.readInt(m_stream); m_strings=StringBlock.read(new IntReader(m_stream,false)); ReadUtil.readCheckType(m_stream,RESOURCEIDS_CHUNK_TYPE); int chunkSize=ReadUtil.readInt(m_stream); if (chunkSize<8 || (chunkSize%4)!=0) { throw new IOException("Invalid resource ids size ("+chunkSize+")."); } m_resourceIDs=ReadUtil.readIntArray(m_stream,chunkSize/4-2); resetState(); } private final int doNext() throws IOException { if (m_tagType==END_DOCUMENT) { return END_DOCUMENT; } m_tagType=(ReadUtil.readInt(m_stream) & 0xFF);/*other 3 bytes?*/ /*some source length*/ReadUtil.readInt(m_stream); m_tagSourceLine=ReadUtil.readInt(m_stream); /*0xFFFFFFFF*/ReadUtil.readInt(m_stream); m_tagName=-1; m_tagAttributes=null; switch (m_tagType) { case START_DOCUMENT: { /*namespace?*/ReadUtil.readInt(m_stream); /*name?*/ReadUtil.readInt(m_stream); break; } case START_TAG: { /*0xFFFFFFFF*/ReadUtil.readInt(m_stream); m_tagName=ReadUtil.readInt(m_stream); /*flags?*/ReadUtil.readInt(m_stream); int attributeCount=ReadUtil.readInt(m_stream); /*?*/ReadUtil.readInt(m_stream); m_tagAttributes=new TagAttribute[attributeCount]; for (int i=0;i!=attributeCount;++i) { TagAttribute attribute=new TagAttribute(); attribute.namespace=ReadUtil.readInt(m_stream); attribute.name=ReadUtil.readInt(m_stream); attribute.valueString=ReadUtil.readInt(m_stream); attribute.valueType=(ReadUtil.readInt(m_stream)>>>24);/*other 3 bytes?*/ attribute.value=ReadUtil.readInt(m_stream); m_tagAttributes[i]=attribute; } break; } case END_TAG: { /*0xFFFFFFFF*/ReadUtil.readInt(m_stream); m_tagName=ReadUtil.readInt(m_stream); break; } case TEXT: { m_tagName=ReadUtil.readInt(m_stream); /*?*/ReadUtil.readInt(m_stream); /*?*/ReadUtil.readInt(m_stream); break; } case END_DOCUMENT: { /*namespace?*/ReadUtil.readInt(m_stream); /*name?*/ReadUtil.readInt(m_stream); break; } default: { throw new IOException("Invalid tag type ("+m_tagType+")."); } } return m_tagType; } private final TagAttribute getAttribute(int index) { if (m_tagAttributes==null) { throw new IndexOutOfBoundsException("Attributes are not available."); } if (index>=m_tagAttributes.length) { throw new IndexOutOfBoundsException("Invalid attribute index ("+index+")."); } return m_tagAttributes[index]; } private final String getString(int index) { if (index==-1) { return ""; } return m_strings.getRaw(index); } /// data private InputStream m_stream; private StringBlock m_strings; private int[] m_resourceIDs; private IOException m_nextException; private int m_tagType; private int m_tagSourceLine; private int m_tagName; private TagAttribute[] m_tagAttributes; private static final int AXML_CHUNK_TYPE =0x00080003, RESOURCEIDS_CHUNK_TYPE =0x00080180; }

java 解析 manifest_解析AndroidManifest.xml之AXMLParser.java | 学步园相关推荐

  1. java调用存储过程sqlserver_Java调用SqlServer存储过程怎么实现 | 学步园

    在使用Java开发时,经常会遇到调用SqlServer存储过程的问题.下面学步园小编来讲解下Java调用SqlServer存储过程怎么实现? Java调用SqlServer存储过程怎么实现 1.数据库 ...

  2. 自己动手写搜索引擎(常搜吧历程五#解析文档之XML#)(Java、Lucene、hadoop)

    今天我们来进行对XML的学习. 认识XML XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized M ...

  3. java xsd_『XSD校验XML』使用java通过XSD校验XML文件

    本文简单介绍使用java语言,通过XSD文件来校验XML文件结构. XML和XSD介绍 MySQL数据迁移和项目魔法计算器都用到了XML文件,这样可以解耦出配置属性,使项目更加灵活,不用动不动就去改动 ...

  4. java 嵌套对象转xml_Gson对Java嵌套对象和JSON字符串之间的转换 | 学步园

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  5. java axis2 调用webservice 接口_Axis2 调用Webservice 接口 | 学步园

    调用方法: TranslatorString  输入中文,翻译成 拼音.英文. 参数:wordKey(中文) 现在要做,翻译词:[随便],代码如下: package cn.com.webxml; im ...

  6. java card applet_可多选的javacard applet | 学步园

    可多选的javacard applet,与多个逻辑通道上设置各自不同的applet是有区别的. Java Card 2.2支持逻辑通道(logical channels)的概念,允许最多智能卡中的16 ...

  7. java 读取远程文件夹_java读取远程共享文件 | 学步园

    方式一: package example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  8. jsp里面java和js交互_jsp与js交互实例 | 学步园

    使用jsp(就是java代码)拼js脚本: lefthead.jsp String basepath = request.getContextPath()+"/"; String ...

  9. java 加减运算_JAVA日期加减运算 | 学步园

    1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); Sys ...

最新文章

  1. OKR不会颠覆KPI,而是融合应用!
  2. No execution.target specified in your configuration file.
  3. kafka启动_Kafka安装部署——单节点
  4. map语法获取index_python获取慧聪企业信息
  5. js 高级 call()
  6. 云服务器与传统服务器的优劣对比_相比于传统服务器,云服务器的优势在哪
  7. 阿里云服务器配置好了,为什么访问不了?阿里云安全组放行1433端口设置您知道吗?
  8. Golang常量无法取地址
  9. HTML5的骨架是什么,HTML5 骨架
  10. [游戏杂谈]浅谈游戏打击感
  11. android qq语音按钮,qq语音设置怎么操作?手机qq语音设置在哪里
  12. 关闭win10自动更新的方法
  13. 今日头条自动开宝箱脚本
  14. CodeSys Rand Numer 随机数生成
  15. 网球爱好者小程序的设计与实现
  16. 113道C语言题目,超经典的~~~
  17. 使用qrcode生成的二维码安卓手机长按不识别问题
  18. 网络运营推广具体做什么工作
  19. 斜杠青年:如何开启你的多重身份 作者:Susan Kuang
  20. 天基实业做好投资理财规划

热门文章

  1. Mysql数据库安全性问题【防注入】
  2. Android数据手册:Android颜色码制表
  3. 【遥感物候】植被物候与气候(气温和降水)条件的空间相关性分析
  4. C#连接Excel和Access(包括2003和2007版)方法总结
  5. Android之在ubuntu上过滤多条关键字日志
  6. LeetCode之Remove Duplicates from Sorted List
  7. 【C语言简单说】三:整数变量扩展和输出扩展(3)
  8. 我的世界java版月步教程_《我的世界》月步?幻影剑?大神才会的骚操作 第一个我就跪了!...
  9. html三列布局源码,HTML三列布局 - 黄柳淞的个人页面 - OSCHINA - 中文开源技术交流社区...
  10. python框架django文档_Django基础——Django框架介绍及模板语言