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

import java.util.*;
import com.silverwrist.venice.htmlcheck.*;

public class HTMLCheckerConfigImpl implements HTMLCheckerConfig
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String DEFAULT_ANCHOR_TAIL = "TARGET=\"Wander\"";

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

  private boolean rewrap_lines = false;               // re-word-wrap lines?
  private boolean process_angles = true;              // process angle-bracketed strings?
  private boolean process_parens = true;              // process parenthesized strings?
  private boolean discard_html_tags = false;          // discard all HTML tags?
  private boolean discard_rejected_html = false;      // discard HTML tags that are rejected?
  private boolean discard_html_comments = false;      // discard HTML comments?
  private boolean discard_xml_constructs = false;     // discard XML constructs (namespaced tags)?
  private short word_wrap_length = 0;                 // word wrap length
  private String anchor_tail = DEFAULT_ANCHOR_TAIL;   // the tail end of the anchor
  private BitSet allowed_tagsets = new BitSet();      // which tagsets are allowed?
  private Vector output_filters = new Vector();       // output filters
  private Vector raw_output_filters = new Vector();   // output filters only applied in "raw mode"
  private Vector string_rewriters = new Vector();     // rewriters that operate on full "strings"
  private Vector word_rewriters = new Vector();       // rewriters that operate on "words" only
  private Vector tag_rewriters = new Vector();        // rewriters that operate on <tags> only
  private Vector paren_rewriters = new Vector();      // rewriters that operate on (parens) only

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

  HTMLCheckerConfigImpl()
  {
    TagRepository.configureNormalSet(allowed_tagsets);
    
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLCheckerConfig
   *--------------------------------------------------------------------------------
   */

  public short getWordWrapLength()
  {
    return word_wrap_length;

  } // end getWordWrapLength

  public void setWordWrapLength(short val)
  {
    if (val<0)
      word_wrap_length = 0;
    else
      word_wrap_length = val;

  } // end setWordWrapLength

  public boolean getRewrapLines()
  {
    return rewrap_lines;

  } // end getRewrapLines

  public void setRewrapLines(boolean val)
  {
    rewrap_lines = val;

  } // end setRewrapLines

  public boolean getProcessAngles()
  {
    return process_angles;

  } // end getProcessAngles

  public void setProcessAngles(boolean val)
  {
    process_angles = val;

  } // end setProcessAngles

  public boolean getProcessParens()
  {
    return process_parens;

  } // end getProcessParens

  public void setProcessParens(boolean val)
  {
    process_parens = val;

  } // end setProcessParens

  public boolean getDiscardHTMLTags()
  {
    return discard_html_tags;

  } // end getDiscardHTMLTags

  public void setDiscardHTMLTags(boolean val)
  {
    discard_html_tags = val;

  } // end setDiscardHTMLTags

  public boolean getDiscardRejectedHTML()
  {
    return discard_rejected_html;

  } // end getDiscardRejectedHTML

  public void setDiscardRejectedHTML(boolean val)
  {
    discard_rejected_html = val;

  } // end setDiscardRejectedHTML

  public boolean getDiscardHTMLComments()
  {
    return discard_html_comments;

  } // end getDiscardHTMLComments

  public void setDiscardHTMLComments(boolean val)
  {
    discard_html_comments = val;

  } // end setDiscardHTMLComments

  public boolean getDiscardXMLConstructs()
  {
    return discard_xml_constructs;

  } // end getDiscardXMLConstructs

  public void setDiscardXMLConstructs(boolean val)
  {
    discard_xml_constructs = val;

  } // end setDiscardXMLConstructs

  public String getAnchorTail()
  {
    return anchor_tail;

  } // end getAnchorTail

  public void setAnchorTail(String s)
  {
    anchor_tail = s;

  } // end setAnchorTail

  public void addOutputFilter(OutputFilter filter)
  {
    output_filters.add(filter);

  } // end addOutputFilter

  public void addRawOutputFilter(OutputFilter filter)
  {
    raw_output_filters.add(filter);

  } // end addRawOutputFilter

  public void addStringRewriter(Rewriter rewriter)
  {
    string_rewriters.add(rewriter);

  } // end addStringRewriter

  public void addWordRewriter(Rewriter rewriter)
  {
    word_rewriters.add(rewriter);

  } // end addWordRewriter

  public void addTagRewriter(Rewriter rewriter)
  {
    tag_rewriters.add(rewriter);

  } // end addTagRewriter

  public void addParenRewriter(Rewriter rewriter)
  {
    paren_rewriters.add(rewriter);

  } // end addParenRewriter

  public boolean isTagSetAllowed(int setid)
  {
    return allowed_tagsets.get(setid);

  } // end isTagSetAllowed

  public void allowTagSet(int setid)
  {
    allowed_tagsets.set(setid);

  } // end allowTagSet

  public void disallowTagSet(int setid)
  {
    allowed_tagsets.clear(setid);

  } // end disallowTagSet

  public void configureNormalTagSet()
  {
    TagRepository.configureNormalSet(allowed_tagsets);

  } // end configureNormalTagSet

  public void configureRestrictedTagSet()
  {
    TagRepository.configureRestrictedSet(allowed_tagsets);

  } // end configureRestrictedTagSet

  /*--------------------------------------------------------------------------------
   * Operations usable only from within the package
   *--------------------------------------------------------------------------------
   */

  List getOutputFilters()
  {
    return output_filters;

  } // end getOutputFilters

  List getRawOutputFilters()
  {
    return raw_output_filters;

  } // end getRawOutputFilters

  List getStringRewriters()
  {
    return string_rewriters;

  } // end getStringRewriters

  List getWordRewriters()
  {
    return word_rewriters;

  } // end getWordRewriters

  List getTagRewriters()
  {
    return tag_rewriters;

  } // end getTagRewriters

  List getParenRewriters()
  {
    return paren_rewriters;

  } // end getParenRewriters

  public HTMLChecker createHTMLChecker()
  {
    return new HTMLCheckerImpl(this);

  } // end createHTMLChecker

  /*--------------------------------------------------------------------------------
   * Static operations
   *--------------------------------------------------------------------------------
   */

  public static HTMLCheckerConfig createHTMLCheckerConfig()
  {
    return new HTMLCheckerConfigImpl();

  } // end createHTMLCheckerConfig

} // end class HTMLCheckerConfigImpl
