package org.bodington.servlet;

import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class to check if the HTML is escaped and leave it alone if 
 * it is. This class is created because we need to escape some 
 * of the HTML comming back from the database but all new stuff 
 * should already be escaped so we should just leave it alone.
 * I couldn't manage todo this in one regexp.
 * @author buckett
 *
 */
public class AlreadyEscapedHtmlWriter extends EscapedHtmlWriter
{
    
    private static Pattern escapedEntity = Pattern.compile("&((\\p{Alpha}+)|(#\\d+);)");
    private static Pattern amp = Pattern.compile("&");
    private static Pattern others = Pattern.compile("[<>\"]");

    public AlreadyEscapedHtmlWriter( Writer out )
    {
        super( out );
    }
    
    protected boolean shouldEscape(String text)
    {
        if (others.matcher(text).find())
            return true;
        Matcher ampMatcher = amp.matcher(text);
        Matcher escapedEntityMatcher = escapedEntity.matcher(text);
        
        while (escapedEntityMatcher.find())
        {
            // This should never fail.
            if (!ampMatcher.find())
            {
                return true;
            }
        }
        return ampMatcher.find();
    }
    

}
