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

📄 cmshtmlconverter.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/htmlconverter/CmsHtmlConverter.java,v $
 * Date   : $Date: 2006/03/27 14:53:04 $
 * Version: $Revision: 1.3 $
 *
 * 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 org.opencms.i18n.CmsEncoder;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;

/**
 * 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
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */
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;
    /** Filename for CmsHtmlConverter configuration. */
    private String m_converterConfFile = "";
    /** Checks if CmsHtmlConverter is configured from file. */
    private boolean m_converterConfFileDefined;
    /** Checks if CmsHtmlConverter is configured. */
    private boolean m_converterConfigDefined;
    /** stores number of predefined replaceTags. */
    private int m_numberReplaceTags;
    /** stores number of predefined replaceTags. */
    private int m_numberReplaceBlocks;
    /** 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;
    // replace String for relative uris 
    private String m_relativeRoot;
    //the url object for links that should not be replaced
    private URL m_url;
    /** Vector stores tag names, after the end-tag, a "\n" is added to the output. */
    private Vector m_enterTags = new Vector();

    /**
     * Default constructor.<p>
     */
    public CmsHtmlConverter() {

        m_tidy.setTidyMark(false);
        m_tidy.setShowWarnings(false);
        m_tidy.setQuiet(true);
        initialiseTags();
    }

    /**
     * Constructor with name of Tidy configuration file as parameter.<p>
     * 
     * @param tidyConfFileName String with Tidy configuration file name
     */
    public CmsHtmlConverter(String tidyConfFileName) {

        this.setTidyConfFile(tidyConfFileName);
        initialiseTags();
    }

    /**
     * Constructor with name of Tidy and Converter configuration files as parameters.<p>
     * 
     * @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);
        initialiseTags();
    }

    /** 
     * Initialises Vector m_enterTags with tag names.<p> 
     */
    private void initialiseTags() {

        StringTokenizer T = new StringTokenizer(
            "p,table,tr,td,body,head,script,pre,title,style,h1,h2,h3,h4,h5,h6,ul,ol,li",
            ",");
        while (T.hasMoreTokens()) {
            m_enterTags.addElement(new String(T.nextToken()));
        }
    }

    /**
     * Sets the prefix and the relative root.<p>
     *
     * @param prefix the servletprefix
     * @param relativeRoot the relative root
     */
    public void setServletPrefix(String prefix, String relativeRoot) {

        m_servletPrefix = prefix;
        m_relativeRoot = relativeRoot;
    }

    /**
     * Sets the url.<p>
     *
     * @param orgUrl object
     */
    public void setOriginalUrl(URL orgUrl) {

        m_url = orgUrl;
    }

    /**
     * Configures JTidy from file.<p>
     * 
     * @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.<p>
     * 
     * @return filename of JTidy configuration file
     */
    public String getTidyConfFile() {

        if (m_tidyConfFileDefined) {
            return m_tidyConfFile;
        } else {
            return "";
        }
    }

    /**
     * Checks whether JTidy is already configured or not.<p>
     * 
     * @return true if JTidy configuration file is set, otherwise false
     */
    public boolean tidyConfigured() {

        return m_tidyConfFileDefined;
    }

    /**
     * Configures CmsHtmlConverter from file.<p>
     * 
     * @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.<p>
     * 
     * @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.<p>
     * 
     * @return filename of configuration file
     */
    public String getConverterConfFile() {

        if (m_converterConfFileDefined) {
            return m_converterConfFile;
        } else {
            return "";
        }
    }

    /**
     * Checks whether CmsHtmlConverter is already configured or not.<p>
     * 
     * @return true if CmsHtmlConverter configuration is set, otherwise false
     */
    public boolean converterConfigured() {

        return m_converterConfigDefined;
    }

    /**
     * Checks if HTML code has errors.<p>
     * 
     * @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.<p>
     * 
     * @param input 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 */

⌨️ 快捷键说明

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