使用XmlPullParser制作BindView工具

来源:互联网 发布:淘宝可以邮寄到国外吗 编辑:程序博客网 时间:2024/06/04 18:53

在之前我写过了一个BindView的工具,之前使用的最要是正则表达的文本分析做的。最近,工作我认识了Android的XML解析,我又想起了这个问题。发现这个问题,其实用XmlPullParser更好解决。所以我重新写了这个工具。简单多了,而且不用格式化代码。

先分析一下如何写,简易思路如下

Created with Raphaël 2.1.0输入文本路径读取xml标签是否存在ID生成一个findView拷贝即可yesno

代码如下:

package com.owant.example.java;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;/** * Created by owant on 19/10/2016. * 查找XML布局下的控件,进行生产findViewById的代码 */public class BindViewTool {    public static void main(String[] arg) {        bindView("/Users/owant/AndroidStudioProjects/201610/owant2/src/main/res/layout/test_bind_view.xml");    }    /**     * private TextView info;     */    private static String declare_format = "private {0} {1};";    /**     * info=(TextView)findViewById(R.id.text);     * info=(TextView)getView().findViewById(R.id.text);     */    private static String find_view_format = "{0} = ({1}){2}findViewById({3});";    private static boolean isFragment = false;    /**     * 忽略的标识     */    private static String ignoreMark = "";    //找到了需要绑定的View    private static ArrayList<Model> bindViews;    public static void bindView(String xmlPath) {        try {            InputStream inputStream = new FileInputStream(new File(xmlPath));            //bindView的集合            bindViews = new ArrayList<>();            XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();            xmlPullParser.setInput(inputStream, "utf-8");            //xmlpullparser是以事件触发为设计的代码            int eventType = xmlPullParser.getEventType();            while (eventType != XmlPullParser.END_DOCUMENT) {//文档结束                switch (eventType) {                    case XmlPullParser.START_DOCUMENT://文档开始                        break;                    case XmlPullParser.START_TAG://标签开始                        //对于这个情况需要进行com.owant.example.view.DivView                        String type = xmlPullParser.getName();                        int pointExist = type.lastIndexOf(".");                        if (pointExist != -1) {                            //substring这end是到end之前的                            //String sub = new String("你好呀!");                            //System.out.println(sub.substring(0, 1));>>你                            type = type.substring(pointExist + 1, type.length());                        }                        String androidIdValue = null;                        int count = xmlPullParser.getAttributeCount();                        for (int i = 0; i < count; i++) {                            String androidIdTag = xmlPullParser.getAttributeName(i);                            if (androidIdTag.equals("android:id")) {                                String androidIdTagValue = xmlPullParser.getAttributeValue(i);                                if (androidIdTagValue.startsWith("@+id/")) {                                    androidIdValue = androidIdTagValue.replace("@+id/", "");                                    //TODO ignore                                }                            }                        }                        if (androidIdValue != null) {                            Model model = new Model();                            model.mId = androidIdValue;                            model.mType = type;                            bindViews.add(model);                        }                        break;                    case XmlPullParser.END_TAG://标签结束                        break;                }                eventType = xmlPullParser.next();            }            //打印需要BindView的控件            for (Model m : bindViews) {                String declare = declare_format.replace("{0}", m.mType);                declare = declare.replace("{1}", m.mId);                System.out.println(declare);            }            System.out.println("\n\n");            for (Model m : bindViews) {                String find = find_view_format.replace("{0}", m.mId);                find = find.replace("{1}", m.mType);                if (isFragment) {                    find = find.replace("{2}", "getView().");                } else {                    find = find.replace("{2}", "");                }                find = find.replace("{3}", "R.id." + m.mId);                System.out.println(find);            }        } catch (XmlPullParserException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public static class Model {        public String mType;        public String mId;    }}

需要注意的有:

  • 对于com.view.DivView的情况;
  • 对于是否有ignore标记的情况;
  • 对于是否为fragment的情况

运行结果:
这里写图片描述

0 0
原创粉丝点击