cmshtmltagremovefactory.java

来自「找了很久才找到到源代码」· Java 代码 · 共 672 行 · 第 1/2 页

JAVA
672
字号
            return m_decorated.getStartPosition();
        }

        /**
         * @see org.htmlparser.Tag#getTagName()
         */
        public String getTagName() {

            return m_decorated.getTagName();
        }

        /**
         * @see org.htmlparser.Node#getText()
         */
        public String getText() {

            return m_decorated.getText();
        }

        /**
         * @see org.htmlparser.Tag#getThisScanner()
         */
        public Scanner getThisScanner() {

            return m_decorated.getThisScanner();
        }

        /**
         * @see org.htmlparser.Tag#isEmptyXmlTag()
         */
        public boolean isEmptyXmlTag() {

            return m_decorated.isEmptyXmlTag();
        }

        /**
         * @see org.htmlparser.Tag#isEndTag()
         */
        public boolean isEndTag() {

            return m_decorated.isEndTag();
        }

        /**
         * @see org.htmlparser.Tag#removeAttribute(java.lang.String)
         */
        public void removeAttribute(String arg0) {

            m_decorated.removeAttribute(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setAttribute(java.lang.String, java.lang.String)
         */
        public void setAttribute(String arg0, String arg1) {

            m_decorated.setAttribute(arg0, arg1);
        }

        /**
         * @see org.htmlparser.Tag#setAttribute(java.lang.String, java.lang.String, char)
         */
        public void setAttribute(String arg0, String arg1, char arg2) {

            m_decorated.setAttribute(arg0, arg1, arg2);
        }

        /**
         * @see org.htmlparser.Tag#setAttributeEx(org.htmlparser.Attribute)
         */
        public void setAttributeEx(Attribute arg0) {

            m_decorated.setAttributeEx(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setAttributesEx(java.util.Vector)
         */
        public void setAttributesEx(Vector arg0) {

            m_decorated.setAttributesEx(arg0);
        }

        /**
         * @see org.htmlparser.Node#setChildren(org.htmlparser.util.NodeList)
         */
        public void setChildren(NodeList arg0) {

            m_decorated.setChildren(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setEmptyXmlTag(boolean)
         */
        public void setEmptyXmlTag(boolean arg0) {

            m_decorated.setEmptyXmlTag(arg0);
        }

        /**
         * @see org.htmlparser.Node#setEndPosition(int)
         */
        public void setEndPosition(int arg0) {

            m_decorated.setEndPosition(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setEndTag(org.htmlparser.Tag)
         */
        public void setEndTag(Tag arg0) {

            m_decorated.setEndTag(arg0);
        }

        /**
         * @see org.htmlparser.Node#setPage(org.htmlparser.lexer.Page)
         */
        public void setPage(Page arg0) {

            m_decorated.setPage(arg0);
        }

        /**
         * @see org.htmlparser.Node#setParent(org.htmlparser.Node)
         */
        public void setParent(Node arg0) {

            m_decorated.setParent(arg0);
        }

        /**
         * @see org.htmlparser.Node#setStartPosition(int)
         */
        public void setStartPosition(int arg0) {

            m_decorated.setStartPosition(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setTagName(java.lang.String)
         */
        public void setTagName(String arg0) {

            m_decorated.setTagName(arg0);
        }

        /**
         * @see org.htmlparser.Node#setText(java.lang.String)
         */
        public void setText(String arg0) {

            m_decorated.setText(arg0);
        }

        /**
         * @see org.htmlparser.Tag#setThisScanner(org.htmlparser.scanners.Scanner)
         */
        public void setThisScanner(Scanner arg0) {

            m_decorated.setThisScanner(arg0);
        }

        /**
         * @see org.htmlparser.Node#toHtml()
         */
        public String toHtml() {

            return m_decorated.toHtml();
        }

        /**
         * @see org.htmlparser.Node#toHtml(boolean)
         */
        public String toHtml(boolean value) {

            return m_decorated.toHtml(value);
        }

        /**
         * @see org.htmlparser.Node#toPlainTextString()
         */
        public String toPlainTextString() {

            return m_decorated.toPlainTextString();
        }

        /**
         * @see org.htmlparser.Node#toString()
         */
        public String toString() {

            return m_decorated.toString();
        }
    }

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

    /** Generated serial version UID. */
    private static final long serialVersionUID = 6961158563666656633L;

    /** The tags to hide tothe node visitors. */
    private Set m_invisibleTags;

    /** The tags to show to the node visitors. */
    private Set m_visibleTags;

    /**
     * Create a new factory with all tags registered.
     * <p>
     * 
     */
    public CmsHtmlTagRemoveFactory() {

        super();
        m_invisibleTags = new TreeSet();
        m_visibleTags = new TreeSet();
    }

    /**
     * Add a tag that will be visible for {@link NodeVisitor} instances.
     * <p>
     * 
     * Not only "this" tag will be visible but all parsed Tags that have the same name (case
     * insensitive).
     * <p>
     * 
     * The given tag will be kept as-is. The following behaviour happens if this method is used: 
     * <ol>
     *  <li>
     *   Once <code>{@link #addTagPreserve(Tag)}</code> has been called all Tags that are not added 
     *   to this method will be removed. <strong>We are in include mode then</strong>. 
     *  </li>
     *  <li>
     *   The Tags provided to <code>{@link #addTagRemoval(Tag)}</code> will only have the 
     *   power to hide exactly the same tags that are given to <code>{@link #addTagPreserve(Tag)}</code>: 
     *   <strong>Deny is stronger than allow.</strong>
     *  </li>
     * </ol>
     * <p>
     * 
     * 
     * @param tag the tag that will be visible for all {@link NodeVisitor} instances.
     * 
     * @return true if the tag was added to the internal set of tags to keep, false if not (was
     *         contained before, has no name,...).
     */
    public boolean addTagPreserve(final Tag tag) {

        boolean result = false;
        String tagName = tag.getTagName();
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tagName)) {
            result = m_visibleTags.add(tagName.toLowerCase());
        }
        return result;

    }

    /**
     * Add a tag that will be invisible for {@link NodeVisitor} instances.
     * <p>
     * 
     * Not only "this" tag will be invisible but all parsed Tags that have the same name (case
     * insensitive).
     * <p>
     * 
     * @param tag the tag that will be visible for all {@link NodeVisitor} instances.
     * 
     * @return true if the tag was added to the internal set of tags to remove, false if not (was
     *         contained before, has no name,...).
     */
    public boolean addTagRemoval(final Tag tag) {

        boolean result = false;
        String tagName = tag.getTagName();
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tagName)) {
            result = m_invisibleTags.add(tagName.toLowerCase());
        }
        return result;
    }

    /**
     * @see org.htmlparser.PrototypicalNodeFactory#createTagNode(org.htmlparser.lexer.Page, int,
     *      int, java.util.Vector)
     */
    public Tag createTagNode(Page arg0, int arg1, int arg2, Vector arg3) {

        try {
            String tagName = ((Attribute)arg3.get(0)).getName().toLowerCase();
            // end tags have names like "/a"....
            if (tagName.charAt(0) == '/') {
                tagName = tagName.substring(1);
            }
            Tag result = super.createTagNode(arg0, arg1, arg2, arg3);
            if (!keepTag(tagName)) {
                result = new CmsInvisibleTag(result);
            }
            return result;
        } catch (RuntimeException rte) {
            if (LOG.isErrorEnabled()) {
                // log here, as htmlparser 1.5 did swallow exceptions from here and threw NPEs from
                // other places
                LOG.error(rte);
            }
            throw rte;
        }
    }

    /**
     * Encapsulation of the "preserve / remove" logic.<p>
     * 
     * @param tagName the lower case name of the tag to keep or hide 
     * 
     * @return if true the given Tag will be kept, if false it will be removed
     */
    private boolean keepTag(final String tagName) {

        boolean result = true;
        // include mode: 
        if (m_visibleTags.size() > 0) {
            if (m_visibleTags.contains(tagName)) {
                result = true;
            } else {
                result = false;
            }
        }
        // Power of hide: if no visible tags configured this works as a normal remove, 
        // if visible tags are configured this can change a visible tag to be invisible 
        if (m_invisibleTags.contains(tagName)) {
            result = false;
        }

        return result;
    }
}

⌨️ 快捷键说明

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