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

📄 cmshtmlconverter.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/htmlconverter/CmsHtmlConverter.java,v $
* Date   : $Date: 2002/02/21 16:15:40 $
* Version: $Revision: 1.4 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* 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 OpenCms, please see the
* OpenCms 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 com.opencms.htmlconverter;

import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.tidy.Tidy;
import org.w3c.dom.*;
import com.opencms.htmlconverter.*;

/**
 * Implementation of interface I_CmsHtmlConverterInterface:
 * Tool to check and transform HTML input code
 * to user-defined output, for example JSP, JavaScript or other syntax.
 * @author Andreas Zahner
 * @version 1.0
 */
public final class CmsHtmlConverter implements I_CmsHtmlConverterInterface {

    /** Filename for JTidy configuration */
    private String m_tidyConfFile = "";
    /** Checks if JTidy is already defined */
    private boolean m_tidyConfFileDefined = false;
    /** Filename for CmsHtmlConverter configuration */
    private String m_converterConfFile = "";
    /** Checks if CmsHtmlConverter is configured from file */
    private boolean m_converterConfFileDefined = false;
    /** Checks if CmsHtmlConverter is configured */
    private boolean m_converterConfigDefined = false;
    /** stores number of predefined replaceTags */
    private int m_numberReplaceTags = 0;
    /** stores number of predefined replaceTags */
    private int m_numberReplaceBlocks = 0;
    /** temporary buffer used in transformation method */
    private StringBuffer m_tempString;
    /** instance of JTidy */
    private Tidy m_tidy = new Tidy();
    /** instance of CmsHtmlConverterTools */
    private CmsHtmlConverterTools m_tools = new CmsHtmlConverterTools();
    /** configuration object for CmsHtmlConverter */
    private CmsHtmlConverterConfig m_configuration = new CmsHtmlConverterConfig();
    /** object stores data for replacing tags */
    private CmsHtmlConverterObjectReplaceTags m_tagObject = new CmsHtmlConverterObjectReplaceTags();
    /** object stores data for replacing blocks */
    private CmsHtmlConverterObjectReplaceBlocks m_blockObject = new CmsHtmlConverterObjectReplaceBlocks();
    // replacestring for the modifyParameter methode. Used for the html editor replacement
    private String m_servletPrefix = null;
    //the url object for links that should not be replaced
    private URL m_url = null;

    /**
     * default constructor
     */
    public CmsHtmlConverter() {
        m_tidy.setTidyMark(false);
        m_tidy.setShowWarnings(false);
        m_tidy.setQuiet(true);
    }

    /**
     * constructor with name of Tidy configuration file as parameter
     * @param tidyConfFileName String with Tidy configuration file name
     */
    public CmsHtmlConverter(String tidyConfFileName) {
        this.setTidyConfFile(tidyConfFileName);
    }

    /**
     * constructor with name of Tidy and Converter configuration files as parameters
     * @param tidyConfFileName String with Tidy configuration file name
     * @param confFile String with Converter configuration file name
     */
    public CmsHtmlConverter(String tidyConfFileName, String confFile) {
        this.setTidyConfFile(tidyConfFileName);
        this.setConverterConfFile(confFile);
    }

    /**
     * sets the prefix.
     *
     * @param prefix The servletprefix.
     */
    public void setServletPrefix(String prefix){
        m_servletPrefix = prefix;
    }

    /**
     * sets the url.
     *
     * @param url object.
     */
    public void setOriginalUrl(URL orgUrl){
        m_url = orgUrl;
    }

    /**
     * Configures JTidy from file
     * @param filename filename of JTidy configuration file
     */
    public void setTidyConfFile(String fileName) {
        m_tidyConfFile = fileName;
        m_tidyConfFileDefined = true;
        m_tidy.setConfigurationFromFile(m_tidyConfFile);
    }

    /**
     * If defined, returns JTidy configuration filename
     * @return filename of JTidy configuration file
     */
    public String getTidyConfFile() {
        if (m_tidyConfFileDefined == true) {
            return m_tidyConfFile;
        }
        else {
            return "";
        }
    }

    /**
     * Checks whether JTidy is already configured or not
     * @return true if JTidy configuration file is set, otherwise false
     */
    public boolean tidyConfigured() {
        return m_tidyConfFileDefined;
    }

    /**
     * Configures CmsHtmlConverter from file
     * @param confFile filename of configuration file
     */
    public void setConverterConfFile(String confFile) {
        try {
            InputStream in = new FileInputStream(confFile);
            m_configuration.init(in);
        }
        catch (IOException e) {
            System.err.println("Configuration error: Configuration file no found!");
            return;
        }
        m_converterConfFileDefined = true;
        m_converterConfigDefined = true;
        m_numberReplaceTags = m_configuration.getReplaceTags().size();
        m_numberReplaceBlocks = m_configuration.getReplaceBlocks().size();
    }

    /**
     * Configures CmsHtmlConverter from string
     * @param configuration string with CmsHtmlConverter configuration
     */
    public void setConverterConfString(String configuration) {
        m_configuration.init(configuration);
        m_converterConfFileDefined = false;
        m_converterConfigDefined = true;
        m_numberReplaceTags = m_configuration.getReplaceTags().size();
        m_numberReplaceBlocks = m_configuration.getReplaceBlocks().size();
    }

    /**
     * If defined, returns filename of CmsHtmlConverter configuration file
     * @return filename of configuration file
     */
    public String getConverterConfFile() {
        if (m_converterConfFileDefined == true) {
            return m_converterConfFile;
        }
        else {
            return "";
        }
    }

    /**
     * Checks whether CmsHtmlConverter is already configured or not
     * @return true if CmsHtmlConverter configuration is set, otherwise false
     */
    public boolean converterConfigured() {
        return m_converterConfigDefined;
    }

    /**
     * Checks if HTML code has errors
     * @param inString String with HTML code
     * @return true if errors were detected, otherwise false
     */
    public boolean hasErrors (String inString) {
        InputStream in = new ByteArrayInputStream(inString.getBytes());
        return this.hasErrors(in);
    }

    /**
     * Checks if HTML code has errors
     * @param in InputStream with HTML code
     * @return true if errors were detected, otherwise false
     */
    public boolean hasErrors (InputStream input) {
        /* initialise JTidy */
        m_tidy.setOnlyErrors(true);
        m_tidy.setShowWarnings(false);
        m_tidy.setQuiet(true);
        m_tidy.setErrout(null);
        /* parse InputStream */
        m_tidy.parse(input,null);
        /* check number of errors */
        if (m_tidy.getParseErrors() == 0) {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * returns number of found errors in last parsed html code
     * @return int with number of errors
     */
    public int getNumberErrors() {
        return m_tidy.getParseErrors();
    }

    /**
     * Checks if HTML code has errors and lists errors
     * @param inString String with HTML code

⌨️ 快捷键说明

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