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

📄 cmshtmlimportconverter.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        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.add(new String(T.nextToken()));
        }
    }

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

        // if node is empty do nothing... (Recursion)
        if (node == null) {
            return;
        }
        // initialise local variables
        int type = node.getNodeType();
        String name = node.getNodeName();

        // detect node type
        switch (type) {
            case Node.DOCUMENT_NODE:

                this.printDocument(((Document)node).getDocumentElement(), properties);
                break;
            case Node.ELEMENT_NODE:

                // check if its the <head> node. Nothing inside the <head> node
                // must be
                // part of the output, but we must scan the content of this
                // node to get all
                // <meta> tags
                if (name.equals(NODE_HEAD)) {
                    m_write = false;
                }
                // scan element node; if a block has to be removed or replaced,
                // break and discard child nodes
                transformStartElement(node, properties);

                // 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), properties);
                    }
                }
                break;
            case Node.TEXT_NODE:

                // replace subStrings in text nodes
                transformTextNode(node);
                break;
            default:

                break;
        }
        // end of recursion, add eventual endtags and suffixes
        switch (type) {
            case Node.ELEMENT_NODE:
                // analyse endtags and add them to output
                transformEndElement(node);
                if (node.getNodeName().equals(NODE_HEAD)) {
                    m_write = true;
                }
                break;
            case Node.DOCUMENT_NODE:
                break;
            default:
                break;
        }
    }

    /**
     * Transform element nodes and create end tags in output.<p>
     * 
     * @param node actual element node
     */
    private void transformEndElement(Node node) {

        // check hat kind of node we have
        String nodeName = node.getNodeName();

        // the <HTML> and <BODY> node must be skipped
        if (nodeName.equals(NODE_HTML) || nodeName.equals(NODE_BODY)) {
            // do nothing here
        } else {
            // only do some output if we are in writing mode
            if (m_write) {
                m_tempString.append("</");
                m_tempString.append(nodeName);
                m_tempString.append(">");

                // append a "\n" to output String if possible
                if (m_enterTags.contains(node.getNodeName())) {
                    m_tempString.append("\n");
                }
            }
        }
    }

    /**
     * Transforms element nodes and create start tags in output. <p>
     * 
     * @param node actual element node
     * @param properties the file properties
     */
    private void transformStartElement(Node node, Hashtable properties) {

        // check hat kind of node we have
        String nodeName = node.getNodeName();

        // the <HTML> and <BODY> node must be skipped
        if (nodeName.equals(NODE_HTML) || nodeName.equals(NODE_BODY)) {
            // the <TITLE> node must be read and its value set as properties to
            // the imported file

        } else if (nodeName.equals(NODE_TITLE)) {

            writeTitleProperty(node, properties);

        } else if (nodeName.equals(NODE_META)) {

            writeMetaTagProperty(node, properties);

        } else if (nodeName.equals(NODE_HREF)) {

            // only do some output if we are in writing mode
            if (m_write) {
                m_tempString.append("<");
                m_tempString.append(nodeName);
                NamedNodeMap attrs = node.getAttributes();
                // look through all attribs to find the reference
                for (int i = attrs.getLength() - 1; i >= 0; i--) {
                    String name = attrs.item(i).getNodeName();
                    String value = attrs.item(i).getNodeValue();

                    if (name.equals(ATTRIB_HREF)) {

                        // check if this is an external link
                        if (value.indexOf("://") > 0) {
                            // store it for later creation of an entry in the
                            // link gallery
                            m_htmlImport.storeExternalLink(value);
                        } else if (!value.startsWith("mailto:") && !value.startsWith("javascript:")) {

                            // save an existing anchor link for later use
                            //                            if (value.indexOf("#") > 0) {
                            //                                String anchor = value.substring(value.indexOf("#"), value.length());
                            //                            }
                            // get the new link into the VFS
                            String internalUri = m_htmlImport.getAbsoluteUri(value, m_filename.substring(
                                0,
                                m_filename.lastIndexOf("/") + 1));

                            value = m_htmlImport.translateLink(internalUri);
                        }
                    }

                    m_tempString.append(" ");
                    m_tempString.append(name);
                    m_tempString.append("=\"");
                    m_tempString.append(value);
                    m_tempString.append("\"");
                }
                m_tempString.append(">");
            }

            // this is a imasge, its reference must be converted
        } else if (nodeName.equals(NODE_IMG)) {

            // only do some output if we are in writing mode
            if (m_write) {
                m_tempString.append("<");
                m_tempString.append(nodeName);
                NamedNodeMap attrs = node.getAttributes();
                // look through all attribs to find the src and alt attributes
                String imagename = "";
                String altText = "";
                for (int i = attrs.getLength() - 1; i >= 0; i--) {
                    String name = attrs.item(i).getNodeName();
                    String value = attrs.item(i).getNodeValue();
                    if (name.equals(ATTRIB_SRC)) {
                        // we found the src. now check if it refers to an
                        // external image.
                        // if not, we must get the correct location in the VFS
                        if (value.indexOf("://") <= 0) {
                            imagename = m_htmlImport.getAbsoluteUri(value, m_filename.substring(
                                0,
                                m_filename.lastIndexOf("/") + 1));
                            value = m_htmlImport.translateLink(imagename);
                        }
                    } else if (name.equals(ATTRIB_ALT)) {
                        altText = value;
                    }

                    m_tempString.append(" ");
                    m_tempString.append(name);
                    m_tempString.append("=\"");
                    m_tempString.append(value);
                    m_tempString.append("\"");
                }

                //store the alt tag of this image for later use
                m_htmlImport.storeImageInfo(imagename, altText);

                m_tempString.append(">");
            }
        } else {

            // only do some output if we are in writing mode
            if (m_write) {

                m_tempString.append("<");
                m_tempString.append(nodeName);
                NamedNodeMap attrs = node.getAttributes();
                for (int i = attrs.getLength() - 1; i >= 0; i--) {
                    m_tempString.append(" " + attrs.item(i).getNodeName() + "=" + "\"");
                    /* scan attribute values and replace subStrings */
                    m_tempString.append(attrs.item(i).getNodeValue() + "\"");
                }
                m_tempString.append(">");
            }
        }
    }

    /**
     * Private method to transform text nodes.<p>
     * 
     * @param node actual text node
     */
    private void transformTextNode(Node node) {

        // only do some output if we are in writing mode
        if (m_write) {
            String helpString = node.getNodeValue();
            m_tempString.append(helpString);
        }
    }

    /**
     * Writes meta tags as cms properties by analyzing the meta tags nodes.<p>
     * 
     * @param node the meta tag node in html document
     * @param properties the properties hashtable
     */
    private void writeMetaTagProperty(Node node, Hashtable properties) {

        NamedNodeMap attrs = node.getAttributes();
        String metaName = "";
        String metaContent = "";
        // look through all attribs to find the name and content attributes
        for (int i = attrs.getLength() - 1; i >= 0; i--) {
            String name = attrs.item(i).getNodeName();
            String value = attrs.item(i).getNodeValue();
            if (name.equals(ATTRIB_NAME)) {
                metaName = value;
            } else if (name.equals(ATTRIB_CONTENT)) {
                metaContent = value;
            }
        }
        // check if we have valid entries for this <META> node, store them
        // in the properties
        if (metaName.length() > 0 && metaContent.length() > 0) {
            properties.put(metaName, metaContent);
        }
    }

    /**
     * Sets the Property title by analyzing the title node.<p>
     * 
     * @param node the title node in html document
     * @param properties the properties hashtable
     */
    private void writeTitleProperty(Node node, Hashtable properties) {

        String title = "";
        // the title string is stored in the first child node
        NodeList children = node.getChildNodes();
        if (children != null) {
            Node titleNode = children.item(0);
            if (titleNode != null) {
                title = titleNode.getNodeValue();
            }
        }
        // add the title property if we have one
        if ((title != null) && (title.length() > 0)) {

            properties.put(CmsPropertyDefinition.PROPERTY_TITLE, title);
            // the title will be used as navtext if no other navtext is
            // given
            if (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) == null) {
                properties.put(CmsPropertyDefinition.PROPERTY_NAVTEXT, title);
            }
        }

    }

}

⌨️ 快捷键说明

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