cmshtmltagremovefactory.java

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

JAVA
672
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsHtmlTagRemoveFactory.java,v $
 * Date   : $Date: 2007-08-13 16:29:56 $
 * Version: $Revision: 1.5 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * 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 Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project 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 org.opencms.util;

import org.opencms.main.CmsLog;

import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import org.apache.commons.logging.Log;

import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.PrototypicalNodeFactory;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Page;
import org.htmlparser.scanners.Scanner;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
import org.htmlparser.visitors.NodeVisitor;

/**
 * 
 * A tag factory for htmlparser that is able to "remove tags".<p>
 * 
 * Create an instance, add the {@link org.htmlparser.Tag} instances to remove and assign this
 * factory to the {@link org.htmlparser.Parser} before starting a visit. A demo usage is shown in
 * <code>CmsTagReplaceParser</code>.<p>
 * 
 * The tags are not actually removed: They are linked in the document object model tree of the HTML
 * that the parser generates. They just will not accept any {@link NodeVisitor} instances and
 * therefore be invisible in any output a visitor will generate from the visited tree.<p> 
 * 
 * The decision whether a tag is removed can be controlled in two ways: 
 * <ol>
 *  <li>
 *   <code>{@link #addTagRemoval(Tag)}</code><br/>
 *   <p>
 *   The given tag will be removed ("invisible in the DOM"). 
 *   </p> 
 *  </li>
 *  <li>
 *   <code>{@link #addTagPreserve(Tag)}</code><br/>
 *   <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>
 *  </li>
 * </ol>
 * 
 * @author Achim Westermann
 * 
 * @version $Revision: 1.5 $
 * 
 * @since 6.1.8
 */
public final class CmsHtmlTagRemoveFactory extends PrototypicalNodeFactory {

    /**
     * A Tag implementation that will not accept any {@link NodeVisitor} stopping by.<p>
     * 
     * When visiting the corresponding tree of tags, this tag will be there but the visitor will not
     * see it as it is not accepted. This allows "elimination" of this tag in the output the visitor
     * generates from the document object model (e.g. HTML code again).<p>
     * 
     * Potential child tags will be visible to visitors (unless they are instances of this class).<p>
     * 
     * @author Achim Westermann
     * 
     * @version $Revision: 1.5 $
     * 
     * @since 6.1.8
     */
    private static final class CmsInvisibleTag implements Tag {

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

        /** The real underlying tag. */
        private Tag m_decorated;

        /**
         * Constructor with the delegate to wrap.
         * <p>
         * 
         * Every property is accessed transparently from the delegate, except that visitors are not
         * welcome.
         * <p>
         * 
         * @param delegate the tag to hide.
         */
        CmsInvisibleTag(Tag delegate) {

            m_decorated = delegate;
        }

        /**
         * @see org.htmlparser.Tag#accept(org.htmlparser.visitors.NodeVisitor)
         */
        public void accept(NodeVisitor visitor) {

            // be invisible but show the children (if they like visits)
            NodeList children = m_decorated.getChildren();
            if (children == null) {
                return;
            }
            SimpleNodeIterator itChildren = children.elements();
            while (itChildren.hasMoreNodes()) {
                itChildren.nextNode().accept(visitor);
            }
        }

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

            return m_decorated.breaksFlow();
        }

        /**
         * @see org.htmlparser.Node#clone()
         */
        public Object clone() throws CloneNotSupportedException {

            return m_decorated.clone();
        }

        /**
         * @see org.htmlparser.Node#collectInto(org.htmlparser.util.NodeList,
         *      org.htmlparser.NodeFilter)
         */
        public void collectInto(NodeList arg0, NodeFilter arg1) {

            m_decorated.collectInto(arg0, arg1);
        }

        /**
         * @see org.htmlparser.Node#doSemanticAction()
         */
        public void doSemanticAction() throws ParserException {

            m_decorated.doSemanticAction();
        }

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

            return m_decorated.getAttribute(arg0);
        }

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

            return m_decorated.getAttributeEx(arg0);
        }

        /**
         * @see org.htmlparser.Tag#getAttributesEx()
         */
        public Vector getAttributesEx() {

            return m_decorated.getAttributesEx();
        }

        /**
         * @see org.htmlparser.Node#getChildren()
         */
        public NodeList getChildren() {

            return m_decorated.getChildren();
        }

        /**
         * @see org.htmlparser.Tag#getEnders()
         */
        public String[] getEnders() {

            return m_decorated.getEnders();
        }

        /**
         * @see org.htmlparser.Tag#getEndingLineNumber()
         */
        public int getEndingLineNumber() {

            return m_decorated.getEndingLineNumber();
        }

        /**
         * @see org.htmlparser.Node#getEndPosition()
         */
        public int getEndPosition() {

            return m_decorated.getEndPosition();
        }

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

            return m_decorated.getEndTag();
        }

        /**
         * @see org.htmlparser.Tag#getEndTagEnders()
         */
        public String[] getEndTagEnders() {

            return m_decorated.getEndTagEnders();
        }

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

            return m_decorated.getFirstChild();
        }

        /**
         * @see org.htmlparser.Tag#getIds()
         */
        public String[] getIds() {

            return m_decorated.getIds();
        }

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

            return m_decorated.getLastChild();
        }

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

            return m_decorated.getNextSibling();
        }

        /**
         * @see org.htmlparser.Node#getPage()
         */
        public Page getPage() {

            return m_decorated.getPage();
        }

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

            return m_decorated.getParent();
        }

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

            return m_decorated.getPreviousSibling();
        }

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

            return m_decorated.getRawTagName();
        }

        /**
         * @see org.htmlparser.Tag#getStartingLineNumber()
         */
        public int getStartingLineNumber() {

            return m_decorated.getStartingLineNumber();
        }

        /**
         * @see org.htmlparser.Node#getStartPosition()
         */
        public int getStartPosition() {

⌨️ 快捷键说明

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