/*
 * 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 Community 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.htmlcheck.filters;

import com.silverwrist.venice.htmlcheck.OutputFilter;

/**
 * An output filter for <CODE>HTMLChecker</CODE> that escapes the three basic characters
 * used by HTML formatting.  It translates the following characters:
 * <UL>
 *  <LI>&lt; - &amp;lt;</LI>
 *  <LI>&gt; - &amp;gt;</LI>
 *  <LI>&amp; - &amp;amp;</LI>
 * </UL>
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @see HTMLOutputFilter
 * @see HTMLChecker#addOutputFilter
 */
public class HTMLEncodingFilter implements OutputFilter
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

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

  /**
   * Creates a new instance of <CODE>HTMLEncodingFilter.</CODE>
   */
  public HTMLEncodingFilter()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface OutputFilter
   *--------------------------------------------------------------------------------
   */

  /**
   * 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.
   * @see #matchCharacter
   * @see HTMLOutputFilter#tryOutputCharacter
   */
  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.
   * @see #tryOutputCharacter
   * @see #lengthNoMatch
   * @see HTMLOutputFilter#matchCharacter
   */
  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.
   * @see #matchCharacter
   * @see HTMLOutputFilter#lengthNoMatch
   */
  public int lengthNoMatch(String s)
  {
    int rc = s.length();
    int i;
    for (i=0; i<ESCAPED_CHARACTERS.length(); i++)
    { // look for one of the escaped characters in the string...
      int tmp = s.indexOf(ESCAPED_CHARACTERS.charAt(i));
      if ((tmp>=0) && (tmp<rc))
      { // we have a new return value
	rc = tmp;
	if (rc==0)
	  break;  // can't get any lower!

      } // end if

    } // end for

    return rc;

  } // end lengthNoMatch

} // end class HTMLEncodingFilter
