⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmshtmldecorator.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/decorator/CmsHtmlDecorator.java,v $
 * Date   : $Date: 2006/03/27 14:52:30 $
 * Version: $Revision: 1.2 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.jsp.decorator;

import org.opencms.file.CmsObject;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsHtmlParser;
import org.opencms.util.CmsStringUtil;

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

import org.apache.commons.logging.Log;

import org.htmlparser.Text;
import org.htmlparser.util.Translate;

/**
 * The CmsHtmlDecorator is the main object for processing the text decorations.<p>
 * 
 * It uses the information of a <code>{@link CmsDecoratorConfiguration}</code> to process the
 * text decorations.
 *
 * @author Michael Emmerich  
 * 
 * @version $Revision: 1.2 $ 
 * 
 * @since 6.1.3 
 */
public class CmsHtmlDecorator extends CmsHtmlParser {

    /** Delimiters for string seperation. */
    private static final String[] DELIMITERS = {
        " ",
        ",",
        ".",
        ";",
        ":",
        "!",
        "(",
        ")",
        "'",
        "?",
        "\"",
        "&nbsp;",
        "&quot;",
        "\r\n",
        "\n"};

    /** Delimiters for second level string seperation. */
    private static final String[] DELIMITERS_SECOND_LEVEL = {"-", "@", "/", ".", ","};

    /** Steps for forward lookup in workd list. */
    private static final int FORWARD_LOOKUP = 5;

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsHtmlDecorator.class);

    /** Non translators, strings starting with those values must not be translated. */
    private static final String[] NON_TRANSLATORS = {"&nbsp;", "&quot;"};

    /** The decoration configuration.<p> */
    CmsDecoratorConfiguration m_config;

    /** Decoration bundle to be used by the decorator. */
    CmsDecorationBundle m_decorations;

    /**
     * Constructor, creates a new CmsHtmlDecorator with a given configuration.<p>
     * 
     * @param config the configuration to be used
     */
    public CmsHtmlDecorator(CmsDecoratorConfiguration config) {

        m_config = config;
        m_decorations = config.getDecorations();
        m_result = new StringBuffer(512);
        m_echo = true;
    }

    /**
     * Constructor, creates a new, empty CmsHtmlDecorator.<p>
     * 
     * @param cms the CmsObject
     */
    public CmsHtmlDecorator(CmsObject cms) {

        m_config = new CmsDecoratorConfiguration(cms);
        m_decorations = m_config.getDecorations();
        m_result = new StringBuffer(512);
        m_echo = true;

    }

    /**
     * Processes a HTML string and adds text decorations according to the decoration configuration.<p>
     * 
     * @param html a string holding the HTML code that should be added with text decorations
     * @param config the decoration configuration
     * @param encoding the encoding to be used
     * @return a HTML string with the decorations added.
     * @throws Exception if something goes wrong
     */
    public static String doDecoration(String html, CmsDecoratorConfiguration config, String encoding) throws Exception {

        CmsHtmlDecorator processor = new CmsHtmlDecorator(config);
        // create the converter instance
        return process(html, encoding, processor);
    }

    /**
     * Processes a HTML string and adds text decorations according to the decoration configuration.<p>
     * 
     * @param html a string holding the HTML code that should be added with text decorations
     * @param processor an already configured 
     * @param encoding the encoding to be used
     * @return a HTML string with the decorations added.
     * @throws Exception if something goes wrong
     */
    public static String doDecoration(String html, CmsHtmlDecorator processor, String encoding) throws Exception {

        processor.m_result = new StringBuffer(512);
        // create the converter instance
        return process(html, encoding, processor);
    }

    /**
     * Splits a String into substrings along the provided delimiter list and returns
     * the result as a List of Substrings.<p>
     *
     * @param source the String to split
     * @param delimiters the delimiters to split at
     * @param trim flag to indicate if leading and trailing whitespaces should be omitted
     * @param includeDelimiters flag to indicate if the delimiters should be included as well
     *
     * @return the List of splitted Substrings
     */
    public static List splitAsList(String source, String[] delimiters, boolean trim, boolean includeDelimiters) {

        List result = new ArrayList();
        String delimiter = new String();
        int i = 0;
        int l = source.length();
        int n = -1;
        int max = Integer.MAX_VALUE;

        // find the next delimiter
        for (int j = 0; j < delimiters.length; j++) {
            if (source.indexOf(delimiters[j]) > -1) {
                if (source.indexOf(delimiters[j]) < max) {
                    max = source.indexOf(delimiters[j]);
                    n = source.indexOf(delimiters[j]);
                    delimiter = delimiters[j];
                }
            }
        }

        while (n != -1) {
            // zero - length items are not seen as tokens at start or end
            if ((i < n) || (i > 0) && (i < l)) {
                result.add(trim ? source.substring(i, n).trim() : source.substring(i, n));
                // add the delimiter to the list as well
                if (includeDelimiters && n + delimiter.length() <= l) {
                    result.add(source.substring(n, n + delimiter.length()));
                }
            } else {
                // add the delimiter to the list as well
                if (includeDelimiters && source.startsWith(delimiter)) {
                    result.add(delimiter);
                }
            }
            i = n + delimiter.length();

            // find the next delimiter
            max = Integer.MAX_VALUE;
            n = -1;
            for (int j = 0; j < delimiters.length; j++) {
                if (source.indexOf(delimiters[j], i) > -1) {
                    if (source.indexOf(delimiters[j], i) < max) {
                        max = source.indexOf(delimiters[j], i);
                        n = source.indexOf(delimiters[j], i);
                        delimiter = delimiters[j];
                    }
                }
            }

        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -