/* ======================================================================
   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.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();
    }
    

}
