/*
 * 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 java.util.*;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.htmlcheck.SpellingDictionary;

public class SpellingRewriter implements Rewriter
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String DEFAULT_BEGIN_ERROR = "<FONT COLOR=\"red\"><B>";
  private static final String DEFAULT_END_ERROR = "</B></FONT>";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Vector dictionaries = new Vector();
  private String begin_error = DEFAULT_BEGIN_ERROR;
  private String end_error = DEFAULT_END_ERROR;

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

  public SpellingRewriter()
  { // do nothing here
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Rewriter
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return "spelling";

  } // end getName

  public MarkupData rewrite(String data, RewriterServices svc)
  {
    Iterator it = dictionaries.iterator();
    while (it.hasNext())
    { // check each stored dictionary in turn
      SpellingDictionary dict = (SpellingDictionary)(it.next());
      if (dict.checkWord(data))
	return null;  // it's in a dictionary, that means it's spelled right

    } // end while

    // not found - it's spelled wrong
    return new MarkupData(begin_error,data,end_error);

  } // end rewrite

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void addDictionary(SpellingDictionary dict)
  {
    dictionaries.add(dict);

  } // end addDictionary

  public String getBeginError()
  {
    return begin_error;

  } // end getBeginError

  public void setBeginError(String val)
  {
    begin_error = val;

  } // end setBeginError

  public String getEndError()
  {
    return end_error;

  } // end getEndError

  public void setEndError(String val)
  {
    end_error = val;

  } // end setEndError

} // end class SpellingRewriter


