/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.htmlcheck;

import com.silverwrist.util.*;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class EncodeHTML implements HTMLCheckerConfigurator, HTMLOutputFilter
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String ESCAPED_CHARACTERS = "<>&";

  private static final AnyCharMatcher MATCHER = new AnyCharMatcher(ESCAPED_CHARACTERS);

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  EncodeHTML()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLCheckerConfigurator
   *--------------------------------------------------------------------------------
   */

  public void configure(HTMLCheckerProfile profile)
  {
    boolean enable = ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
								 "enable.HTML.filter",Boolean.FALSE))).booleanValue();
    if (enable)  // add myself as an output filter
      profile.addOutputFilter(this);

  } // end configure

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLOutputFilter
   *--------------------------------------------------------------------------------
   */

  /**
   * Checks and possibly outputs a character.  If the character <CODE>ch</CODE> is '<',
   * '>', or '&', it appends the appropriate string to the string buffer
   * <CODE>buf</CODE> and returns <CODE>true</CODE>.  If not, it returns <CODE>false</CODE>
   * and leaves the buffer alone.
   *
   * @param buf The string buffer to append the character equivalent to.
   * @param ch The character to be checked and possibly output.
   * @return <CODE>true</CODE> if the character matches and was output, <CODE>false</CODE> if not.
   */
  public boolean tryOutputCharacter(StringBuffer buf, char ch)
  {
    switch (ch)
    { // turn the HTML flag characters into appropriate entity escapes
      case '<':
	buf.append("&lt;");
	return true;

      case '>':
	buf.append("&gt;");
	return true;

      case '&':
	buf.append("&amp;");
	return true;

      default:
	break;

    } // end switch

    return false;  // bail out!  not one we handle!

  } // end tryOutputCharacter

  /**
   * Returns <CODE>true</CODE> if the character <CODE>ch</CODE> is '<', '>', or '&', <CODE>false</CODE> if not.
   *
   * @param ch The character to be tested.
   * @return <CODE>true</CODE> if the character <CODE>ch</CODE> is '<', '>', or '&', <CODE>false</CODE> if not.
   */
  public boolean matchCharacter(char ch)
  {
    return (ESCAPED_CHARACTERS.indexOf(ch)>=0);

  } // end matchCharacter

  /**
   * Given a character string, returns the total number of characters in that string, starting from the beginning,
   * that are <I>not</I> handled by this filter before one that <I>is</I> appears.  If none of the characters in this
   * string are handled by the filter, the length of the string is returned.
   *
   * @param s The string to scan through.
   * @return The length of the initial sequence of non-matching characters in this string.
   */
  public int lengthNoMatch(String s)
  {
    int rc = MATCHER.get(s);
    if (rc==-1)
      rc = s.length();
    return rc;

  } // end lengthNoMatch

} // end class EncodeHTML
