/* ======================================================================
   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;

import java.net.URLConnection;
import java.util.Vector;

import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.ParserException;

/**
 * Whenever we parse something we need to have a containing node that always
 * exisits so that we can delete top level elements from the NodeList.
 * This class always adds a containing node around any content it is asked to
 * parse.
 * @see org.bodington.util.html.nodes.RootTag
 * @author buckett
 */
public class RootedLexer extends Lexer
{
    private static Vector rootTag;
    static {
        rootTag = new Vector(1);
        rootTag.add(new Attribute("root", null, null, '0'));
    }
    
    private boolean returnedRoot = false;

    public RootedLexer ()
    {
        super();
    }
    
    public RootedLexer ( Page page )
    {
        super(page);
    }
    
    public RootedLexer ( String string )
    {
        super(string);
    }
    
    public RootedLexer (URLConnection connection ) throws ParserException
    {
        super(connection);
    }
    
    public Node nextNode() throws ParserException
    {
        return nextNode(false);
    }
    
    public Node nextNode(boolean quotesmart) throws ParserException
    {
        if (!returnedRoot)
        {
            returnedRoot=true;
            return getNodeFactory ().createTagNode (getPage (), 0, 0, (Vector)rootTag.clone());
        }
        return super.nextNode(quotesmart);
    }
    
    public void reset ()
    {
        returnedRoot = false;
        super.reset();
    }
    
}
