SAXpraseHelper

package com.k1.frame.utils;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.k1.frame.data.Contents;
import com.k1.frame.data.NavigationItems;
import com.k1.frame.data.Navigations;
import com.k1.frame.data.PageInfo;
import com.k1.frame.data.TabbarItems;
import com.k1.frame.data.Tabbars;

import android.content.Context;

public class SAXPraserHelper extends DefaultHandler {

    final int SAX_PAGE = 0;
    final int SAX_TITLE = 1;
    final int SAX_COLOR = 2;
    final int SAX_TABBAR = 3;

    final int TITLE_BACKGROUND = 4;
    final int TITLE_LEFT_IMAGE = 5;
    final int TITLE_LEFT_TEXT = 6;
    final int TITLE_SEARCH_IMAGE = 7;
    final int TITLE_LEFT_TEXT_SIZE = 8;
    final int TITLE_LEFT_COLOR = 9;
    final int TITLE_TEXT_SIZE = 10;
    final int TITLE_TEXT_COLOR = 11;
    final int TITLE_HEIGHT = 12;

    final int TABS_BACKGROUD = 13;
    final int TABS_PADDINGTOP = 14;
    final int TABS_PADDINGBOTTOM = 15;

    final int TABS_TAB_TYPE = 16;
    final int TABS_TAB_FILE = 17;
    final int TABS_TAB_ICON = 18;
    final int TABS_TAB_NAME = 19;
    final int TABS_TAB_COLOR = 20;
    final int TABS_TAB_SELECT_COLOR = 21;
    final int TABS_TAB_TEXT_COLOR = 22;
    final int TABS_TAB_TEXT_COLOR_SELECT = 23;
    final int TABS_TAB_TEXT_SIZE = 24;
    final int TABS_TAB_INCO_SELECT = 25;
    final int TABS_TAB_HTML = 26;

    public Navigations navigation = new Navigations();
    public Contents content = new Contents();
    public Tabbars tab = new Tabbars();
    public NavigationItems item = new NavigationItems();

    public NavigationItems mavigationItems = new NavigationItems();
    public TabbarItems tabItems = new TabbarItems();
    PageInfo pageinfo = new PageInfo();
    int currentState = 0;

    /*
     * 接口字符块通知
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        // TODO Auto-generated method stub
        // super.characters(ch, start, length);
        String theString = new String(ch, start, length);
        switch (currentState) {
        case TITLE_BACKGROUND:
            mavigationItems.setTitleBackground(theString);
            currentState = TITLE_BACKGROUND;
            break;
        case TITLE_LEFT_IMAGE:
            mavigationItems.setLeftImage(theString);
            currentState = TITLE_BACKGROUND;
            break;
        case TITLE_LEFT_COLOR:
            mavigationItems.setLeftTextColor(theString);
            currentState = 0;
            break;
        case TITLE_LEFT_TEXT:
            mavigationItems.setLeftText(theString);
            currentState = 0;
            break;
        case TITLE_SEARCH_IMAGE:
            mavigationItems.setSearchImage(theString);
            currentState = 0;
            break;
        case TITLE_TEXT_SIZE:
            mavigationItems.setTitleTextSize(theString);
            currentState = 0;
            break;
        case TITLE_TEXT_COLOR:
            mavigationItems.setTitleTextColor(theString);
            currentState = 0;
            break;
        case TITLE_LEFT_TEXT_SIZE:
            mavigationItems.setLeftTextSize(theString);
            currentState = 0;
            break;
        case TITLE_HEIGHT:
            mavigationItems.setTitleHeight(theString);
            currentState = 0;
            break;
        case TABS_BACKGROUD:
            tab.setBackground(theString);
            currentState = 0;
            break;
        case TABS_PADDINGTOP:
            tab.setPaddingTop(theString);
            currentState = 0;
            break;
        case TABS_PADDINGBOTTOM:
            tab.setPaddingBottom(theString);
            currentState = 0;
            break;
        case TABS_TAB_TYPE:
            tabItems.setType(theString);
            currentState = 0;
            break;
        case TABS_TAB_FILE:
            tabItems.setFile(theString);
            currentState = 0;
            break;
        case TABS_TAB_ICON:
            tabItems.setIcon(theString);
            currentState = 0;
            break;
        case TABS_TAB_NAME:
            tabItems.setName(theString);
            currentState = 0;
            break;
        case TABS_TAB_COLOR:
            tabItems.setColor(theString);
            currentState = 0;
            break;
        case TABS_TAB_SELECT_COLOR:
            tabItems.setSelectColor(theString);

            currentState = 0;
            break;
        case TABS_TAB_TEXT_COLOR:
            tabItems.setTextColor(theString);
            currentState = 0;
            break;
        case TABS_TAB_TEXT_COLOR_SELECT:
            tabItems.setTextColorSelect(theString);
            currentState = 0;
            break;
        case TABS_TAB_TEXT_SIZE:
            tabItems.setTextSize(theString);
            currentState = 0;
            break;
        case TABS_TAB_INCO_SELECT:
            tabItems.setIconSelect(theString);
            currentState = 0;
            break;
        case TABS_TAB_HTML:
            tabItems.setHtmlpage(theString);
            currentState = 0;
        default:
            return;
        }
    }

    public PageInfo getPageInfo() {
        pageinfo.setNavigation(navigation);
        pageinfo.setTab(tab);
        return pageinfo;

    }

    /*
     * 接收文档结束通知
     */
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    /*
     * 接收标签结束通知
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (localName.equals("title")) {
            navigation.setItems(mavigationItems);
            Object O = mavigationItems.getTitleHeight();
            System.out.println(O);

        }

        if (localName.equals("tab")) {
            String s = tabItems.getHtmlpage();
            System.out.println(s);
            tab.setItem(tabItems);
            tabItems = new TabbarItems();
        }
    }

    /*
     * 文档开始通知
     */
    @Override
    public void startDocument() throws SAXException {

    }

    /*
     * 标签开始通知
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (localName.equals("title_backgroud")) {
            currentState = TITLE_BACKGROUND;
            return;
        }
        if (localName.equals("left_text")) {
            currentState = TITLE_LEFT_TEXT;
            return;
        }

        if (localName.equals("left_image")) {
            currentState = TITLE_LEFT_IMAGE;
            return;
        }
        if (localName.equals("search_image")) {
            currentState = TITLE_SEARCH_IMAGE;
            return;
        }
        if (localName.equals("left_text_size")) {
            currentState = TITLE_LEFT_TEXT_SIZE;
            return;
        }
        if (localName.equals("left_text_color")) {
            currentState = TITLE_LEFT_COLOR;
            return;
        }
        if (localName.equals("title_text_size")) {
            currentState = TITLE_TEXT_SIZE;
            return;
        }
        if (localName.equals("title_text_color")) {
            currentState = TITLE_TEXT_COLOR;
            return;
        }
        if (localName.equals("title_height")) {
            currentState = TITLE_HEIGHT;
            return;
        }
        if (localName.equals("background")) {
            currentState = TABS_BACKGROUD;
            return;
        }
        if (localName.equals("paddingTop")) {
            currentState = TABS_PADDINGTOP;
            return;
        }
        if (localName.equals("paddingBottom")) {
            currentState = TABS_PADDINGBOTTOM;
            return;
        }
        if (localName.equals("type")) {
            currentState = TABS_TAB_TYPE;
            return;
        }
        if (localName.equals("type")) {
            currentState = TABS_TAB_TYPE;
            return;
        }
        if (localName.equals("file")) {
            currentState = TABS_TAB_FILE;
            return;
        }
        if (localName.equals("icon")) {
            currentState = TABS_TAB_ICON;
            return;
        }
        if (localName.equals("name")) {
            currentState = TABS_TAB_NAME;
            return;
        }
        if (localName.equals("color")) {
            currentState = TABS_TAB_COLOR;
            return;
        }
        if (localName.equals("select_color")) {
            currentState = TABS_TAB_SELECT_COLOR;
            return;
        }
        if (localName.equals("text_color")) {
            currentState = TABS_TAB_TEXT_COLOR;
            return;
        }
        if (localName.equals("text_color_select")) {
            currentState = TABS_TAB_TEXT_COLOR_SELECT;
            return;
        }
        if (localName.equals("text_size")) {
            currentState = TABS_TAB_TEXT_SIZE;
            return;
        }
        if (localName.equals("tabbar")) {
            currentState = SAX_TABBAR;
            return;
        }
        if (localName.equals("icon_select")) {
            currentState = TABS_TAB_INCO_SELECT;
            return;
        }
        if (localName.equals("url")) {
            currentState = TABS_TAB_HTML;
            return;
        }

        currentState = 0;
    }

    public static PageInfo getpage(Context context) {
        SAXPraserHelper handler = null;
        try {
            // 这里我们实现了本地解析,所以注掉了这个取网络数据的。
            // URL url = new URL(rssUrl);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            handler = new SAXPraserHelper();
            reader.setContentHandler(handler);
            InputSource is = new InputSource(context.getResources().getAssets().open("skin.xml"));// 取得本地xml文件

            reader.parse(is);
            return handler.getPageInfo();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<skin>
    <!-- 皮肤版本号 -->
    <version>1</version>
    <path>PhonePage</path>
    <title>
        <!-- 标题栏背景,可以图片或者颜色(颜色开头) -->
        <title_background>title_background</title_background>
        <!-- 标题栏左边的图片 -->
        <left_image>image/title_left.png</left_image>
        <!-- 标题栏左边的文字 -->
        <left_text>返回</left_text>
        <!-- 搜索图标 -->
        <search_image>search_red_icon</search_image>
        <!-- 标题左边文字大小(单位sp) -->
        <left_text_size>16</left_text_size>
        <!-- 标题左边文字颜色 -->
        <left_text_color>000000</left_text_color>
        <!-- 标题文字大小(单位sp) -->
        <title_text_size>18</title_text_size>
        <!-- 标题文字颜色 -->
        <title_text_color>000000</title_text_color>
        <!-- 标题高度(单位dp) -->
        <title_height>50</title_height>
    </title>

    <tabs>
        <!-- 导航栏背景颜色 -->
        <background>ff0000</background>
        <!-- 导航栏上边距(单位dp) -->
        <paddingTop>3</paddingTop>
        <!-- 导航栏下边据 (单位dp)-->
        <paddingBottom>3</paddingBottom>
        <tab>

            <!-- tab类型 -->
            <type>base</type>
            <!-- tab打开的Activity -->
            <file>com.junrui.fragment.TabFragment</file>
            <!-- tab图片名字(默认tab选择图片名字为tab图片名字后面加_select) -->
            <icon>tab1</icon>
            <icon_select>tab1_press</icon_select>
            <!-- tab显示的text -->
            <name>咨询</name>
            <!-- tab背景颜色 -->
            <color>fbfbfb</color>
            <!-- 选择tab时的背景颜色 -->
                <select_color>FFCCCC</select_color>
            <!-- text颜色 -->
            <text_color>646464</text_color>
            <!-- 选择tab时的text颜色 -->
            <text_color_select>fd9800</text_color_select>
            <!-- text的大小(单位sp) -->
            <text_size>12</text_size>
            <url>index</url>
        </tab>
            <tab>
            <type>base</type>
            <file>com.junrui.fragment.TabTwoFragment</file>
            <icon>tab2</icon>
            <icon_select>tab2_press</icon_select>
            <name>患者</name>
            <color>fbfbfb</color>
                <select_color>FFCCCC</select_color>
            <text_color>646464</text_color>
            <text_color_select>fd9800</text_color_select>
            <text_size>12</text_size>
            <url>index2</url>
        </tab>
        <tab>
            <type>base</type>
            <file>com.junrui.fragment.TabThreeFragment</file>
            <icon>tab3</icon>
            <icon_select>tab3_press</icon_select>
            <name>我的</name>
            <color>fbfbfb</color>
            <select_color>FFCCCC</select_color>
            <text_color>646464</text_color>
            <text_color_select>fd9800</text_color_select>
            <text_size>12</text_size>
            <url>index3</url>
        </tab>
    </tabs>
</skin>
时间: 2024-10-12 15:27:29

SAXpraseHelper的相关文章