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

📄 cmshtmlconverter.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        return m_tidy.getParseErrors() != 0;
    }

    /**
     * Returns number of found errors in last parsed html code.<p>
     * 
     * @return int with number of errors
     */
    public int getNumberErrors() {

        return m_tidy.getParseErrors();
    }

    /**
     * Checks if HTML code has errors and lists errors.<p>
     * 
     * @param inString String with HTML code
     * @return String with detected errors
     */
    public String showErrors(String inString) {

        InputStream in = new ByteArrayInputStream(inString.getBytes());
        OutputStream out = new ByteArrayOutputStream();
        this.showErrors(in, out);
        return out.toString();
    }

    /**
     * Checks if HTML code has errors and lists errors.<p>
     * 
     * @param input InputStream with HTML code
     * @param output OutputStream with detected errors
     */
    public void showErrors(InputStream input, OutputStream output) {

        /* initialise JTidy */
        m_tidy.setOnlyErrors(true);
        m_tidy.setQuiet(true);
        m_tidy.setShowWarnings(false);
        InputStream in = new BufferedInputStream(input);
        PrintWriter errorLog = new PrintWriter(output);
        m_tidy.setErrout(errorLog);
        m_tidy.parse(in, null);
        if (m_tidy.getParseErrors() == 0) {
            errorLog.println("HTML code ok!\nNo errors detected.");
        }
        errorLog.close();
    }

    /**
     * Transforms HTML code into user defined output.<p>
     * 
     * @param inString String with HTML code
     * @return String with transformed code
     */
    public String convertHTML(String inString) {

        Reader in = new StringReader(inString);
        Writer out = new StringWriter();
        this.convertHTML(in, out);
        return out.toString();
    }

    /**
     * Transforms HTML code into user defined output.<p>
     * 
     * @param input Reader with HTML code
     * @param output Writer with transformed code
     */
    public void convertHTML(Reader input, Writer output) {

        /* local variables */
        StringBuffer htmlString = new StringBuffer();
        Node node;
        String outString = "";
        /* initialise JTidy */
        m_tidy.setShowWarnings(false);
        m_tidy.setQuiet(true);
        if (!m_tidyConfFileDefined) {
            m_tidy.setOnlyErrors(false);
            m_tidy.setTidyMark(false);
        }
        /* print errorlog in ByteArray */
        PrintWriter errorLog = new PrintWriter(new ByteArrayOutputStream(), true);
        m_tidy.setErrout(errorLog);
        try {
            /* write InputStream input in StringBuffer htmlString */
            int c;
            while ((c = input.read()) != -1) {
                htmlString.append((char)c);
            }
        } catch (IOException e) {
            System.err.println("Conversion error: " + e.toString());
            return;
        }
        outString = htmlString.toString();
        /* first step: replace subStrings in htmlString run #1*/
        outString = m_tools.scanContent(outString, m_configuration.getReplaceContent());
        /* convert htmlString in InputStream for parseDOM */
        InputStream in;
        try {
            in = new ByteArrayInputStream(outString.getBytes(CmsEncoder.ENCODING_UTF_8));
            //m_tidy.setCharEncoding(org.w3c.tidy.Configuration.UTF8);
            m_tidy.setOutputEncoding(CmsEncoder.ENCODING_UTF_8);
            m_tidy.setInputEncoding(CmsEncoder.ENCODING_UTF_8);
        } catch (UnsupportedEncodingException e) {
            in = new ByteArrayInputStream(outString.getBytes());
            //m_tidy.setCharEncoding(org.w3c.tidy.Configuration.LATIN1);
            m_tidy.setOutputEncoding("LATIN1");
            m_tidy.setInputEncoding("LATIN1");
        }
        node = m_tidy.parseDOM(in, null);
        /* check if html code has errors */
        if (m_tidy.getParseErrors() != 0) {
            System.err.println("Conversion error: HTML code has errors!");
        }
        /* second step: create transformed output with printDocument from DOM */
        this.printDocument(node);
        /* third step: replace Strings run #2 */
        outString = m_tools.scanString(m_tempString.toString(), m_configuration.getReplaceStrings());
        outString = this.cleanOutput(outString);
        try {
            output.write(outString);
            output.close();
        } catch (IOException e) {
            System.err.println("Conversion error: " + e.toString());
            return;
        }
    }

    /**
     * Private method to parse DOM and create user defined output.<p>
     * 
     * @param node Node of DOM from HTML code
     */
    private void printDocument(Node node) {

        // if node is empty do nothing... (Recursion) 
        if (node == null) {
            return;
        }
        // initialise local variables 
        int type = node.getNodeType();
        int replaceTag = -1;
        int replaceBlock = -1;
        // detect node type 
        switch (type) {
            case Node.DOCUMENT_NODE:
                // initialise m_tempString and add global prefix 
                m_tempString = new StringBuffer(m_configuration.getGlobalPrefix());
                this.printDocument(((Document)node).getDocumentElement());
                break;
            case Node.ELEMENT_NODE:
                // analyse element node and transform it 
                replaceBlock = this.indexReplaceBlock(node);
                replaceTag = this.indexReplaceTag(node);
                // scan element node; if a block has to be removed or replaced,
                // break and discard child nodes 
                if (this.transformStartElement(node, replaceBlock, replaceTag)) {
                    break;
                }
                // test if node has children 
                NodeList children = node.getChildNodes();
                if (children != null) {
                    int len = children.getLength();
                    for (int i = 0; i < len; i++) {
                        // recursively call printDocument with all child nodes 
                        this.printDocument(children.item(i));
                    }
                }
                break;
            case Node.TEXT_NODE:
                // replace subStrings in text nodes 
                this.transformTextNode(node);
                break;
            default:
                // TODO: check what to do if node type is unknown
                break;
        }
        // end of recursion, add eventual endtags and suffixes 
        switch (type) {
            case Node.ELEMENT_NODE:
                // analyse endtags and add them to output 
                this.transformEndElement(node, replaceBlock, replaceTag);
                break;
            case Node.DOCUMENT_NODE:
                // add suffix to end of output 
                this.transformEndDocument();
                break;
            default:
                // TODO: check what to do if node type is unknown
                break;
        }
    }

    /**
     * Private method to transform element nodes and create start tags in output.<p>
     * 
     * @param node actual element node
     * @param replaceBlock index of object m_replaceBlocks
     * @param replaceTag index of object m_replaceTags
     * @return true if recursion has to be interrupted, otherwise false
     */
    private boolean transformStartElement(Node node, int replaceBlock, int replaceTag) {

        String tempReplaceString, valueParam;
        /* remove complete block, interrupt recursion in printDocument */
        if (m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveBlocks())) {
            return true;
        }
        /* if tag has to be removed return, otherwise test other cases */
        if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveTags())) {
            /* test if a block has to be replaced */
            if (replaceBlock != -1) {
                m_blockObject = (CmsHtmlConverterObjectReplaceBlocks)m_configuration.getReplaceBlocks().get(
                    replaceBlock);
                /* if a parameter is used, get it from node attribute value,
                 insert it into replaceString */
                tempReplaceString = m_blockObject.getReplaceString();
                if (!m_blockObject.getParameter().equals("")) {
                    valueParam = m_tools.scanNodeAttrs(node, m_blockObject.getParameter());
                    tempReplaceString = m_tools.replaceString(tempReplaceString, "$parameter$", valueParam);
                }
                m_tempString.append(tempReplaceString);
                /* remove temporary object from ArrayList replaceBlocks */
                if (replaceBlock > (m_numberReplaceBlocks - 1)) {
                    m_configuration.removeObjectReplaceBlock(replaceBlock);
                }
                /* ignore child elements in block, interrupt recursion in printDocument */
                return true;
            } else {
                /* test if actual element (start tag) has to be replaced */
                if (replaceTag != -1) {
                    m_tagObject = (CmsHtmlConverterObjectReplaceTags)m_configuration.getReplaceTags().get(replaceTag);
                    tempReplaceString = m_tagObject.getReplaceStartTag();
                    /* if a parameter is used, get it from node attribute value,
                     insert it into replaceString */
                    if (!m_tagObject.getParameter().equals("")) {
                        valueParam = m_tools.scanNodeAttrs(node, m_tagObject.getParameter());
                        // HACK: only replace attribute value of parameter attribute!
                        if (m_tagObject.getReplaceParamAttr()) {
                            if (!m_tools.shouldReplaceUrl(m_url, valueParam, m_servletPrefix)) {
                                tempReplaceString = "$parameter$";
                            } else {
                                valueParam = m_tools.modifyParameter(m_url, valueParam, m_servletPrefix, m_relativeRoot);
                            }
                            tempReplaceString = m_tools.reconstructTag(
                                tempReplaceString,
                                node,
                                m_tagObject.getParameter(),
                                m_configuration.getQuotationmark());
                        }
                        tempReplaceString = m_tools.replaceString(tempReplaceString, "$parameter$", valueParam);
                    }
                    m_tempString.append(tempReplaceString);
                } else {
                    /* no replacement needed: append original element to output */
                    m_tempString.append("<");
                    m_tempString.append(node.getNodeName());
                    NamedNodeMap attrs = node.getAttributes();
                    for (int i = attrs.getLength() - 1; i >= 0; i--) {
                        m_tempString.append(" "
                            + attrs.item(i).getNodeName()
                            + "="
                            + m_configuration.getQuotationmark());
                        /* scan attribute values and replace subStrings */
                        String helpString = attrs.item(i).getNodeValue();
                        helpString = m_tools.scanString(helpString, m_configuration.getReplaceStrings());
                        m_tempString.append(helpString + m_configuration.getQuotationmark());
                    }
                    if (m_configuration.getXhtmlOutput()
                        && m_tools.checkTag(node.getNodeName(), m_configuration.getInlineTags())) {
                        m_tempString.append("/");
                    }
                    m_tempString.append(">");
                }
            } /* OPTION: Here I can add a "\n" after every starttag */
        }
        return false;
    }

    /**
     * Private method to transform element nodes and create end tags in output.<p>
     * 
     * @param node actual element node
     * @param replaceBlock index of object m_replaceBlocks
     * @param replaceTag index of object m_replaceTags
     */
    private void transformEndElement(Node node, int replaceBlock, int replaceTag) {

        /* test if block has to be removed */
        if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveBlocks())) {
            /* test if tag has to be removed */
            if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveTags())) {

⌨️ 快捷键说明

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