/* ======================================================================
   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 org.htmlparser.Attribute;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Page;

public class Utils
{
    /**
     * Limit the value to the bounds.
     * @param value The value to limit.
     * @param lower The lower limit.
     * @param upper The upper limit.
     * @return The value inside the bounds.
     */
    public static int bound(int value, int lower, int upper)
    {
        if (value < lower)
            return lower;
        if (value > upper)
            return upper;
        return value;
    }

    /**
     * Gets the texts surrounding the supplied tag.
     * @param tag The tag to get the text surrounding.
     * @return A string containing the tag and a little more.
     */
    public static String getInContext(Tag tag)
    {
        final int lower = 0;
        final int upper = tag.getPage().getSource().offset();
        return tag.getPage().getText(
            bound(tag.getStartPosition() - 5, lower, upper),
            bound(tag.getEndPosition() + 5, lower, upper));
    }
    
    /**
     * Get the line of the input that contains the start of the tag.
     * @param tag The tag to get the line for.
     * @return The line of text.
     */
    public static String getInLine(Tag tag)
    {
        return tag.getPage().getLine(tag.getStartPosition());
    }
    
    public static String formatLineColumn(Tag tag)
    {
        Page page = tag.getPage();
        int start = tag.getStartPosition();
        return "( line: " + (page.row(start) + 1) + ", column: "
            + (page.column(start) + 1) + " )";
    }
    
    public static String badTag(Tag tag)
    {
        return "Invalid tag <"+ tag.getTagName()+ "> at "+
            formatLineColumn(tag);
    }
    
    public static String badTagContent(Tag tag)
    {
        return "Invalid content for tag <"+ tag.getTagName()+ "> at "+
        formatLineColumn(tag);
    }
    
    
    public static String badAttribute(Tag tag, Attribute attribute)
    {
        return "Invalid attribute '"+ attribute.getName()+ "' found in tag <"+
        tag.getTagName()+ "> "+ formatLineColumn(tag);
    }
    
    public static String badAttributeValue(Tag tag, Attribute attribute)
    {
        return "Invalid content for attribute '"+ attribute.getName()+ "' found in tag <"+
        tag.getTagName()+ "> at "+ formatLineColumn(tag);
    }

}
