Java String Format formatTreeLines(String tree)

Here you can find the source of formatTreeLines(String tree)

Description

format Tree Lines

License

Open Source License

Declaration

public static String formatTreeLines(String tree) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final String SPAN_INS = "<span class=\"line_ins\">";
    private static final String SPAN_DEL = "<span class=\"line_del\">";
    private static final String SPAN_CLOSE = "</span>";
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static String formatTreeLines(String tree) {
        if (tree == null || tree.equals(""))
            return tree;

        StringBuilder reconstructedTree = new StringBuilder();
        boolean isOpen = false;
        for (String s : tree.split(LINE_SEPARATOR)) {

            List<Integer> insStartIndexes = getIndexesOf(s, "<ins>");
            List<Integer> insEndIndexes = getIndexesOf(s, "</ins>");
            List<Integer> delStartIndexes = getIndexesOf(s, "<del>");
            List<Integer> delEndIndexes = getIndexesOf(s, "</del");
            int closingTags = insEndIndexes.size() + delEndIndexes.size();
            int startTags = insStartIndexes.size() + delStartIndexes.size();

            if (insStartIndexes.size() > 0 && delStartIndexes.size() > 0) {

                if (isOpen) {
                    if (closingTags > startTags) {
                        isOpen = false;//from   w w w . j  av a 2 s. c  o m
                        s += SPAN_CLOSE;
                    }
                } else {
                    if (insStartIndexes.get(0) > delStartIndexes.get(0))
                        s = SPAN_DEL + s;
                    else
                        s = SPAN_INS + s;

                    if (closingTags == startTags)
                        s += SPAN_CLOSE;
                    else
                        isOpen = true;
                }
            } else if (insStartIndexes.size() > 0 && delStartIndexes.size() == 0) {

                if (isOpen) {
                    if (closingTags > startTags) {
                        isOpen = false;
                        s += SPAN_CLOSE;
                    }
                } else {
                    s = SPAN_INS + s;
                    if (closingTags == startTags)
                        s += SPAN_CLOSE;
                    else
                        isOpen = true;
                }

            } else if (insStartIndexes.size() == 0 && delStartIndexes.size() > 0) {

                if (isOpen) {
                    if (closingTags > startTags) {
                        isOpen = false;
                        s += SPAN_CLOSE;
                    }
                } else {
                    s = SPAN_DEL + s;
                    if (closingTags == startTags)
                        s += SPAN_CLOSE;
                    else
                        isOpen = true;
                }
            } else if (closingTags > 0) {
                s += SPAN_CLOSE;
                isOpen = false;
            }

            reconstructedTree.append(s);
            reconstructedTree.append(LINE_SEPARATOR);
        }

        return reconstructedTree.toString().substring(0, reconstructedTree.length() - 1);
    }

    public static List<Integer> getIndexesOf(String word, String value) {
        List<Integer> indexes = new ArrayList<Integer>();
        for (int index = word.indexOf(value); index != -1; index = word.indexOf(value, index + 1)) {
            indexes.add(index);
        }
        return indexes;
    }
}

Related

  1. formatParam(String param)
  2. formatProtein(String seq)
  3. formatString(String stringFormat, String... args)
  4. formatStringTo80Columns(String str)
  5. formatTooltip(String tooltip)
  6. formatVarname(StringBuilder buffer, Object varname)
  7. formatWords(String words, String delimiter, boolean isHeaderUpperCase)
  8. getAnswerKey(String itemFormat)
  9. getCodeFileName(String fileNameFormat, String randomCode)