1. 程式人生 > >Android 實現自定義Html解析器

Android 實現自定義Html解析器

浪費了“黃金五年”的Java程式設計師,還有救嗎? >>>   

一、需求簡介

Android中TextView可以實現簡單的HTML解析,將Html文字封裝為Spannable資料實現圖文混排等富文字效果,但是同樣問題很多。

1、SDK中提供的解析能力不夠強,提供的樣式支援不足,對於css屬性的解析很弱。

2、不支援多個css樣式同時解析。

3、SDK中提供的Html.TagHandler無法獲取到標籤屬性。

4、可擴充套件性不夠強,無法自定義解析器。

 

二、解決方案

方案1: 自定義一套HTML解析器,其實很簡單,複製一份android.text.Html,替換其中SDK隱藏的XmlReader即可

方案2:移花接木,通過Html.TagHandler奪取解析流程控制權,然後獲得攔截解析tag的能力。

 

這兩種方案實質上都是可行的,第一種的話要實現自己的SaxParse解析,但工作量不小,因此這裡我們主要提供方案二的實現方式。

 

三、移花接木

之所以可以移花接木,是因為TagHandler會被作為Html中標籤解析的最後一個流程語句,當遇到自定義的或者Html類無法解析的標籤,標籤呼叫TagHandler的handleTag方法會被回撥,同時可以獲得TagName,Editable,XmlReader,然後我們便可移花接木。

 

package com.example.myapplication;

import android.graphics.drawable.Drawable;
import android.support.v4.util.ArrayMap;
import android.text.Editable;
import android.text.Html;
import android.util.Log;

import com.example.myapplication.tags.HtmlTag;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class HtmlTagHandler implements Html.TagHandler,Html.ImageGetter, ContentHandler {

    private static final String LOG_TAG  =  "HtmlTagHandler";

    private final String  H5_TAG = "html";  //自定義標籤,該標籤無法在原Html類中解析
    private volatile ContentHandler orginalContentHandler;
    private int count = 0;  //防止自定義的相互巢狀的情況 如:<html><html></html></html>
    //設定標籤計數器,防止自定義標籤巢狀自定義標籤
    private XMLReader originalXmlReader;
    private Editable originlaEditableText;  //該物件是SpannableStringBuilder
    private List<String> orginalTags = null;

   //自定義解析器集合
    private final Map<String,HtmlTag> tagHandlerMap;


    public HtmlTagHandler( ) {
        String orginalContentHandlerTag = "br|p|ul|li|div|span|strong|b|em|cite|dnf|i|big|small|font|blockquote|tt|a|u|del|s|strike|sup|sub|h1|h2|h3|h4|h5|h6|img";  
//原android.text.Html類中可以解析的標籤
        orginalTags = Arrays.asList(orginalContentHandlerTag.split("|"));
        tagHandlerMap = new ArrayMap<>();
    }
    //註冊解析器
    public void registerTag(String tagName,HtmlTag tagHandler){
        tagHandlerMap.put(tagName,tagHandler);
    }

    public HtmlTag unregisterTag(String tagName){
        return tagHandlerMap.remove(tagName);
    }

    @Override
    public Drawable getDrawable(String source) {
        return null;
    }
   //處理原Html中無法識別的標籤
    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if(opening){
            startHandleTag(tag,output,xmlReader);
        }else{
            endHandleTag(tag,output,xmlReader);
        }

    }
    private void startHandleTag( String tag, Editable output, XMLReader xmlReader) {

        if (tag.equalsIgnoreCase(H5_TAG)){
            if(orginalContentHandler==null) {
                orginalContentHandler = xmlReader.getContentHandler();
                this.originalXmlReader = xmlReader; //獲取XmlReader
                this.originalXmlReader.setContentHandler(this);//獲取控制權,讓本類監聽解析流程
                this.originlaEditableText = output;  //獲取到SpannableStringBuilder
              
            }
            count++;
        }

    }

    private void endHandleTag( String tag, Editable output, XMLReader xmlReader) {
        if(tag.equalsIgnoreCase(tag)){
            count--;
            if(count==0 ){
                this.originalXmlReader.setContentHandler(this.orginalContentHandler);
                //將原始的handler交還
                this.originalXmlReader = null;
                this.originlaEditableText = null;
                this.orginalContentHandler = null;
              //還原控制權
            }
        }

    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {

        if (localName.equalsIgnoreCase(H5_TAG)){
            handleTag(true,localName,this.originlaEditableText,this.originalXmlReader);
        }else if(canHandleTag(localName)){  //攔截,判斷是否可以解析該標籤
             
           final HtmlTag htmlTag = tagHandlerMap.get(localName);  //讀取自定義解析器開始解析
            htmlTag.startHandleTag(this.originlaEditableText,atts);

        }else if(orginalTags.contains(localName)){ //無法解析的優先讓原Html類解析
            this.orginalContentHandler.startElement(uri,localName,qName,atts);
        }else{
            Log.e(LOG_TAG,"無法解析的標籤<"+localName+">");
        }

    }

    private boolean canHandleTag(String tagName) {
        if(!tagHandlerMap.containsKey(tagName)){
            return false;
        }
        final HtmlTag htmlTag = tagHandlerMap.get(tagName);
        return htmlTag!=null;

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (localName.equalsIgnoreCase(H5_TAG)){
            handleTag(false,localName,this.originlaEditableText,this.originalXmlReader);
        }else if(canHandleTag(localName)){
            final HtmlTag htmlTag = tagHandlerMap.get(localName); //讀取自定義解析器結束解析
            htmlTag.endHandleTag(this.originlaEditableText);
        }else if(orginalTags.contains(localName)){
            this.orginalContentHandler.endElement(uri,localName,qName);
        }else{
            Log.e(LOG_TAG,"無法解析的標籤</"+localName+">");
        }
    }
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        orginalContentHandler.characters(ch,start,length); 
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        orginalContentHandler.ignorableWhitespace(ch,start,length);
    }

    @Override
    public void processingInstruction(String target, String data) throws SAXException {
        orginalContentHandler.processingInstruction(target,data);
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
        orginalContentHandler.skippedEntity(name);
    }


    @Override
    public void setDocumentLocator(Locator locator) {
        orginalContentHandler.setDocumentLocator(locator);
    }

    @Override
    public void startDocument() throws SAXException {
        orginalContentHandler.startDocument();
    }

    @Override
    public void endDocument() throws SAXException {
        orginalContentHandler.endDocument();
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        orginalContentHandler.startPrefixMapping(prefix,uri);
    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
        orginalContentHandler.endPrefixMapping(prefix);
    }



}

 

以上TagHandler就實現了,接下來實現自己的解析器,為了更好的約束定義規則,我們這裡實現一個抽象類,並提供一些解析工具。

public abstract class HtmlTag {

    private Context context;

    public HtmlTag(Context context) {
        this.context = context;
    }

    public Context getContext() {
        return context;
    }

    private static final Map<String, Integer> sColorNameMap;

    static {
        sColorNameMap = new ArrayMap<String, Integer>();
        sColorNameMap.put("black", Color.BLACK);
        sColorNameMap.put("darkgray", Color.DKGRAY);
        sColorNameMap.put("gray", Color.GRAY);
        sColorNameMap.put("lightgray", Color.LTGRAY);
        sColorNameMap.put("white", Color.WHITE);
        sColorNameMap.put("red", Color.RED);
        sColorNameMap.put("green", Color.GREEN);
        sColorNameMap.put("blue", Color.BLUE);
        sColorNameMap.put("yellow", Color.YELLOW);
        sColorNameMap.put("cyan", Color.CYAN);
        sColorNameMap.put("magenta", Color.MAGENTA);
        sColorNameMap.put("aqua", 0xFF00FFFF);
        sColorNameMap.put("fuchsia", 0xFFFF00FF);
        sColorNameMap.put("darkgrey", Color.DKGRAY);
        sColorNameMap.put("grey", Color.GRAY);
        sColorNameMap.put("lightgrey", Color.LTGRAY);
        sColorNameMap.put("lime", 0xFF00FF00);
        sColorNameMap.put("maroon", 0xFF800000);
        sColorNameMap.put("navy", 0xFF000080);
        sColorNameMap.put("olive", 0xFF808000);
        sColorNameMap.put("purple", 0xFF800080);
        sColorNameMap.put("silver", 0xFFC0C0C0);
        sColorNameMap.put("teal", 0xFF008080);
        sColorNameMap.put("white", Color.WHITE);
        sColorNameMap.put("transparent", Color.TRANSPARENT);

    }

    @ColorInt
    public static   int getHtmlColor(String colorString){

        if(sColorNameMap.containsKey(colorString.toLowerCase())){
            Integer colorInt = sColorNameMap.get(colorString);
            if(colorInt!=null) return colorInt;
        }

        return parseHtmlColor(colorString.toLowerCase());
    }

    @ColorInt
    public static int parseHtmlColor( String colorString) {

        if (colorString.charAt(0) == '#') {
            if(colorString.length()==4){
                StringBuilder sb = new StringBuilder("#");
                for (int i=1;i<colorString.length();i++){
                    char c = colorString.charAt(i);
                    sb.append(c).append(c);
                }
                colorString  = sb.toString();
            }
            long color = Long.parseLong(colorString.substring(1), 16);
            if (colorString.length() == 7) {
                // Set the alpha value
                color |= 0x00000000ff000000;
            } else if (colorString.length() != 9) {
                throw new IllegalArgumentException("Unknown color");
            }
            return (int)color;
        } else if(colorString.startsWith("rgb(") || colorString.startsWith("rgba(") && colorString.endsWith(")")){
            colorString = colorString.substring(colorString.indexOf("("),colorString.indexOf(")"));
            colorString = colorString.replaceAll(" ","");
            String[] colorArray = colorString.split(",");
            if(colorArray.length==3){
                return Color.argb(255,Integer.parseInt(colorArray[0]),Integer.parseInt(colorArray[1]),Integer.parseInt(colorArray[2]));
            }
            else if (colorArray.length==4){
                return Color.argb(Integer.parseInt(colorArray[3]),Integer.parseInt(colorArray[0]),Integer.parseInt(colorArray[1]),Integer.parseInt(colorArray[2]));
            }

        }
        throw new IllegalArgumentException("Unknown color");
    }


    public static <T> T getLast(Spanned text, Class<T> kind) {

        T[] objs = text.getSpans(0, text.length(), kind);
        if (objs.length == 0) {
            return null;
        } else {
            return objs[objs.length - 1];
        }
    }
    public abstract void startHandleTag(Editable text, Attributes attributes);  //開始解析
    public abstract void endHandleTag(Editable text);  //結束解析
}

實際上,到這裡我們的任務已經完成了,按照規則實現解析即可。startHandleTag和endHandleTag因為引數Editable本質上就是SpannableStringBuilder類,同時提供了attributes,接下來的工作無非就是Editable.setSpan的操作,接下來看一個案例。

 

四、案例:改寫span標籤的解析規則


public class SpanTag  extends HtmlTag{

    private static Pattern sTextDecorationPattern;
    private static Pattern sBackgroundColorPattern;
    private static Pattern sForegroundColorPattern;

    public SpanTag(Context context) {
        super(context);
    }


    private int getHtmlSize(String fontSize) {
         fontSize = fontSize.toLowerCase();
         if(fontSize.endsWith("px")){
             return (int) Double.parseDouble(fontSize.substring(0,fontSize.indexOf("px")));
         }else if(fontSize.endsWith("sp") ){
              float sp = (float) Double.parseDouble(fontSize.substring(0,fontSize.indexOf("sp")));
              return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,getContext().getResources().getDisplayMetrics());
         }else if(TextUtils.isDigitsOnly(fontSize)){  //如果不帶單位,預設按照sp處理
             float sp = (float) Double.parseDouble(fontSize);
             return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,getContext().getResources().getDisplayMetrics());
         }
         return -1;
    }

    private static String getTextColorPattern(String style) {
        String cssName = "text-color";
        String cssVal = getHtmlCssValue(style, cssName);
        if(TextUtils.isEmpty(cssVal)){
             cssName = "color";
             cssVal = getHtmlCssValue(style, cssName);
        }
        return cssVal;
    }

    @Nullable
    private static String getHtmlCssValue(String style, String cssName) {
        final String[]  keyValueSet = style.split(";");
        if(keyValueSet==null) return null;
        for (int i=0;i<keyValueSet.length;i++){
            final String match = keyValueSet[i].replaceAll(" ","").toLowerCase();
            if(match.indexOf(cssName)==0){
                final String[] parts = match.split(":");
                if(parts==null || parts.length!=2) continue;
                return parts[1];
            }
        }
        return null;
    }

    private static String getBackgroundColorPattern(String style) {
        String cssName = "background-color";
        String cssVal = getHtmlCssValue(style, cssName);

        if(TextUtils.isEmpty(cssVal)){
            cssName = "bakground";
            cssVal = getHtmlCssValue(style, cssName);
        }

        return cssVal;
    }

    private static String getTextFontSizePattern(String style) {
        String cssName = "font-size";
        String cssVal = getHtmlCssValue(style, cssName);
        return cssVal;
    }


    public static  class Font{  //定義標記
        int textSize;

        public Font( int textSize) {
            this.textSize = textSize;
        }
    }

    public static class Background{ //定義標記
        int color;
        public Background(int color) {
            this.color = color;
        }
    }

    @Override
    public void startHandleTag(Editable text, Attributes attributes) {
        String style = attributes.getValue("", "style");
        if(TextUtils.isEmpty(style)) return;

        int  textSize = -1;

        String textColorPattern = getTextColorPattern(style);
        if (!TextUtils.isEmpty(textColorPattern)) {
            int c = getHtmlColor(textColorPattern);
            c = c | 0xFF000000;
            start(text,new ForegroundColorSpan(c));

        }
        String fontSizePattern = getTextFontSizePattern(style);
        if(!TextUtils.isEmpty(fontSizePattern)){
            textSize = getHtmlSize(fontSizePattern);
         }
        if(textSize!=-1){
            start(text,new Font(textSize));  //注意,第二個引數可以為任意Object型別,這裡起到標記的作用
        }

        String backgroundColorPattern = getBackgroundColorPattern(style);
        if (!TextUtils.isEmpty(backgroundColorPattern)) {
                int c = getHtmlColor(backgroundColorPattern);
                c = c | 0xFF000000;
                start(text,new Background(c));
          }

    }

    @Override
    public void endHandleTag(Editable text){


        Background b = getLast(text, Background.class); //讀取出最後標記型別
        if(b!=null){
            end(text,Background.class,new BackgroundColorSpan(b.color)); //設定為Android可以解析的24種ParcelableSpan基本分類,當然也可以自己定義,但需要整合原有的分類
        }

        final ForegroundColorSpan fc = getLast(text, ForegroundColorSpan.class);
        if(fc!=null){
            end(text,Font.class,new ForegroundColorSpan(fc.getForegroundColor()));
        }

        Font f = getLast(text, Font.class);
        if (f != null) {
            end(text,Font.class,new TextFontSpan(f.textSize)); //使用自定義的
        }
    }

    private static void start(Editable text, Object mark) {
        int len = text.length();
        text.setSpan(mark, len, len, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  //新增標記在最後一位,注意開始位置和結束位置
    }

    @SuppressWarnings("unchecked")
    private static void end(Editable text, Class kind, Object repl) {
        Object obj = getLast(text, kind); //讀取kind型別
        if (obj != null) {
            setSpanFromMark(text, obj, repl);
        }
    }


    private static void setSpanFromMark(Spannable text, Object mark, Object... spans) {
        int where = text.getSpanStart(mark);
        text.removeSpan(mark);
        //移除原有標記,因為原有標記不是預設的24種ParcelableSpan子類,因此無法渲染文字
        int len = text.length();
        if (where != len) {
            for (Object span : spans) {
                text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //注意:開始位置和結束位置,因為SpannableStringBuilder的append新增字元方法導致len已經大於where了
            }
        }
    }
}

 

關於TextFont實現很簡單,程式碼如下

public class TextFontSpan extends AbsoluteSizeSpan {
    private  int color;

    public TextFontSpan(int size) {
        this(size,false);
        //這裡我們以px作為單位,方便統一呼叫
    }

    /**
     * 保持構造方法無法被外部呼叫
     * @param size
     * @param dip
     */
    protected TextFontSpan(int size, boolean dip) {
        super(size, dip);
    }

    public TextFontSpan(Parcel src) {
        super(src);
    }


}

 

使用方法:

HtmlTagHandler htmlTagHandler = new HtmlTagHandler();
htmlTagHandler.registerTag("span",new SpanTag(targetFragment.getContext()));

String source = "<html>今天<span style='color:#FFE31335;font-size:16sp;background-color:white;'>星期三</span>,<span style='color:#fff;font-size:14sp;background-color:red;'>但是我還要加班</span><html>";


final Spanned spanned = Html.fromHtml(source, htmlTagHandler, htmlTagHandler);
textView.setText(spanned );

注意: <html>標籤必須加到要解析的文字段,否則Android系統仍然會走Html的解析流程。