add tag to all word in content HTML which is occur in list Word. - Java java.lang

Java examples for java.lang:String HTML

Description

add tag to all word in content HTML which is occur in list Word.

Demo Code


//package com.java2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        String content = "java2s.com";
        List listWord = java.util.Arrays.asList("asdf", "java2s.com");
        String beginTag = "java2s.com";
        String endTag = "java2s.com";
        System.out.println(colorWord(content, listWord, beginTag, endTag));
    }/*  w w  w  .  j a va  2 s. c o m*/

    private static final String TAGBEGIN = "<";
    private static final String TAGEND = ">";

    /**
     * this method will add tag to all word in contentHTML which is occur in listWord.
     * @param listWord
     * @param beginTag
     * @param endTag
     * @return String of contentHTML with added tag
     */
    public static String colorWord(String content, List<String> listWord,
            String beginTag, String endTag) {
        //String tempContent = contentHTML.toUpperCase();
        StringBuilder contentHTML = new StringBuilder(content);
        StringBuilder tempContent = new StringBuilder(content.toUpperCase());
        try {
            int wordIndex = 0;
            int beginTagIndex = 0;
            int endTagIndex = 0;
            String temp = "";
            String wordFind = "";
            for (String word : listWord) {
                do {
                    // upper case the word
                    wordFind = word.toUpperCase();
                    // get next index of word from the previous index
                    wordIndex = tempContent.indexOf(wordFind, wordIndex);
                    if (wordIndex > 0) {
                        char charAfterWord = tempContent.charAt(wordIndex
                                + word.length());
                        char charBeforeWord = tempContent
                                .charAt(wordIndex - 1);
                        if (!Character.isLetter(charAfterWord)
                                && !Character.isLetter(charBeforeWord)) {
                            // get the next begin tag from the word
                            beginTagIndex = tempContent.indexOf(TAGBEGIN,
                                    wordIndex);
                            // get the next end tag from the word
                            endTagIndex = tempContent.indexOf(TAGEND,
                                    wordIndex);
                            // if the word is in the content then change word

                            if ((beginTagIndex < endTagIndex)
                                    && (beginTagIndex > 0)) {
                                // add tag to tempContent
                                temp = beginTag + word + endTag;
                                // tempContent = tempContent.substring(0, wordIndex) + temp
                                //         + tempContent.substring(wordIndex + word.length(), tempContent.length());
                                tempContent.replace(wordIndex, wordIndex
                                        + word.length(), temp);
                                // add tag to contentHTML- the return result
                                temp = beginTag
                                        + contentHTML.substring(wordIndex,
                                                wordIndex + word.length())
                                        + endTag;
                                // contentHTML = contentHTML.substring(0, wordIndex) + temp
                                //        + contentHTML.substring(wordIndex + word.length(), contentHTML.length());
                                contentHTML.replace(wordIndex, wordIndex
                                        + word.length(), temp);
                                // position for find next occur of the word
                                wordIndex += (beginTag.length()
                                        + endTag.length() + word.length());
                            } else {
                                wordIndex = endTagIndex;
                            }
                        } else {
                            wordIndex += word.length();
                        }
                    }

                } while (wordIndex > 0);
            }

        } catch (Exception e) {

        }
        return contentHTML.toString();
    }
}

Related Tutorials