/*
 * 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 apostrophe character
 * (the SQL quote character) by doubling it.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @see OutputFilter
 * @see HTMLChecker#addOutputFilter
 */
public class SQLEncodingFilter implements OutputFilter
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

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

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

  /**
   * Checks and possibly outputs a character.  If the character <CODE>ch</CODE> is
   * an apostrophe, it appends two apostrophes 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)
  {
    if (ch=='\'')
    { // convert one apostrophe into two
      buf.append("\'\'");
      return true;

    } // end if

    return false;

  } // end tryOutputCharacter

  /**
   * Returns <CODE>true</CODE> if the character <CODE>ch</CODE> is an apostrophe,
   * <CODE>false</CODE> if not.
   *
   * @param ch The character to be tested.
   * @return <CODE>true</CODE> if the character <CODE>ch</CODE> is an apostrophe,
   * <CODE>false</CODE> if not.
   * @see #tryOutputCharacter
   * @see #lengthNoMatch
   * @see HTMLOutputFilter#matchCharacter
   */
  public boolean matchCharacter(char ch)
  {
    return (ch=='\'');

  } // 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.indexOf('\'');
    if (rc<0)
      rc = s.length();
    return rc;

  } // end lengthNoMatch

} // end class SQLEncodingFilter
