Android Open Source - RealTextView Html Tag Handler






From Project

Back to project page RealTextView.

License

The source code is released under:

Apache License

If you think the Android project RealTextView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013-2014 Dominik Schrmann <dominik@dominikschuermann.de>
 * Copyright (C) 2013 Mohammed Lakkadshaw
 */*w ww  . ja v a  2  s. c  om*/
 * 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 com.hardsoftstudio.real.textview.utils;

import android.text.Editable;
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
import android.text.style.AlignmentSpan;
import android.text.style.BulletSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.TypefaceSpan;
import android.util.Log;

import com.hardsoftstudio.real.textview.views.RealTextView;

import org.xml.sax.XMLReader;

import java.util.Vector;

/**
 * Some parts of this code are based on android.text.Html
 */
public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();

    private static class Code {
    }

    private static class Center {
    }

    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
        if (opening) {
            // opening tag
            if (RealTextView.DEBUG) {
                Log.d("MPB", "opening, output: " + output.toString());
            }

            if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
                mListParents.add(tag);
                mListItemCount = 0;
            } else if (tag.equalsIgnoreCase("code")) {
                start(output, new Code());
            } else if (tag.equalsIgnoreCase("center")) {
                start(output, new Center());
            }
        } else {
            // closing tag
            if (RealTextView.DEBUG) {
                Log.d("MPB", "closing, output: " + output.toString());
            }

            if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
                mListParents.remove(tag);
                mListItemCount = 0;
            } else if (tag.equalsIgnoreCase("li")) {
                handleListTag(output);
            } else if (tag.equalsIgnoreCase("code")) {
                end(output, Code.class, new TypefaceSpan("monospace"), false);
            } else if (tag.equalsIgnoreCase("center")) {
                end(output, Center.class, new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), true);
            }
        }
    }

    /**
     * Mark the opening tag by using private classes
     *
     * @param output
     * @param mark
     */
    private void start(Editable output, Object mark) {
        int len = output.length();
        output.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK);

        if (RealTextView.DEBUG) {
            Log.d("MPB", "len: " + len);
        }
    }

    private void end(Editable output, Class kind, Object repl, boolean paragraphStyle) {
        Object obj = getLast(output, kind);
        // start of the tag
        int where = output.getSpanStart(obj);
        // end of the tag
        int len = output.length();

        output.removeSpan(obj);

        if (where != len) {
            // paragraph styles like AlignmentSpan need to end with a new line!
            if (paragraphStyle) {
                output.append("\n");
                len++;
            }
            output.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        if (RealTextView.DEBUG) {
            Log.d("MPB", "where: " + where);
            Log.d("MPB", "len: " + len);
        }
    }

    /**
     * Get last marked position of a specific tag kind (private class)
     *
     * @param text
     * @param kind
     * @return
     */
    private Object getLast(Editable text, Class kind) {
        Object[] objs = text.getSpans(0, text.length(), kind);
        if (objs.length == 0) {
            return null;
        } else {
            for (int i = objs.length; i > 0; i--) {
                if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) {
                    return objs[i - 1];
                }
            }
            return null;
        }
    }

    private void handleListTag(Editable output) {
        if (mListParents.lastElement().equals("ul")) {
            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
        } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount++;

            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.insert(start, mListItemCount + ". ");
            output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
        }
    }
}




Java Source Code List

com.hardsoftstudio.real.textview.exceptions.RealHtmlTextViewException.java
com.hardsoftstudio.real.textview.utils.AutofitHelper.java
com.hardsoftstudio.real.textview.utils.FontManager.java
com.hardsoftstudio.real.textview.utils.HtmlTagHandler.java
com.hardsoftstudio.real.textview.utils.LocalImageGetter.java
com.hardsoftstudio.real.textview.utils.RealUrl.java
com.hardsoftstudio.real.textview.utils.SizeUtils.java
com.hardsoftstudio.real.textview.utils.UrlImageGetter.java
com.hardsoftstudio.real.textview.views.BaseTextView.java
com.hardsoftstudio.real.textview.views.RealButton.java
com.hardsoftstudio.real.textview.views.RealCheckBox.java
com.hardsoftstudio.real.textview.views.RealEditText.java
com.hardsoftstudio.real.textview.views.RealTextView.java
org.hardsoftstudio.real.example.ApplicationTest.java
org.hardsoftstudio.real.example.BaseActivity.java
org.hardsoftstudio.real.example.ExampleMainActivity.java
org.hardsoftstudio.real.example.HtmlExampleActivity.java
org.hardsoftstudio.real.example.MainActivity.java
org.hardsoftstudio.real.textview.ApplicationTest.java