/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util.html.nodevisitors;

import java.util.List;
import java.util.Vector;

import org.htmlparser.Remark;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.visitors.NodeVisitor;

/**
 * Class for applying the multiple visitors. This saves having to
 * run multiple visitors over the same document. I don't have
 * any proof that this is faster but it should mean less objects are
 * created (specifically NodeLists) which should be better.
 * @author buckett
 */
public class BroadcastVisitor extends NodeVisitor
    implements ReportingVisitor
{
    /**
     * The visitors to run. We use an array as performance really matters
     * as it will be enumerated for each node we hit.
     */
    private NodeVisitor visitors[];

    public BroadcastVisitor(NodeVisitor[] visitors)
    {
        this.visitors = visitors;
    }

    public void beginParsing()
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].beginParsing();
        }
    }

    public void finishedParsing()
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].finishedParsing();
        }
    }

    public void visitEndTag(Tag tag)
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].visitEndTag(tag);
        }
    }

    public void visitRemarkNode(Remark remark)
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].visitRemarkNode(remark);
        }
    }

    public void visitStringNode(Text string)
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].visitStringNode(string);
        }
    }

    public void visitTag(Tag tag)
    {
        for (int count = 0; count < visitors.length; count++)
        {
            visitors[count].visitTag(tag);
        }
    }

    public List getMessages()
    {
        List messages = new Vector();
        for (int count = 0; count < visitors.length; count++)
        {
            if (visitors[count] instanceof ReportingVisitor)
            {
                messages.addAll(((ReportingVisitor)visitors[count]).getMessages());
            }
        }
        return messages;
    }


}
