/*
 * 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 java.util.*;
import com.silverwrist.dynamo.HTMLTagSet;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class FrozenProfileObject
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String NAMESPACE = Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE;

  private static final Short DEFAULT_WORDWRAP = new Short((short)0);

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

  private ProfileObject m_profile;                   // backreference
  private boolean m_rewrap_lines;                    // re-word-wrap lines?
  private boolean m_process_angles;                  // process angle-bracketed strings?
  private boolean m_process_parens;                  // process parenthesized strings?
  private boolean m_process_brackets;                // process bracketed strings?
  private boolean m_process_braces;                  // process braced strings?
  private boolean m_discard_html_tags;               // discard all HTML tags?
  private boolean m_discard_rejected_html;           // discard HTML tags that are rejected?
  private boolean m_discard_html_comments;           // discard HTML comments?
  private boolean m_discard_xml_constructs;          // discard XML constructs (namespaced tags)?
  private short m_word_wrap_length;                  // word wrap length
  private HTMLOutputFilter[] m_output_filters;       // the output filters
  private HTMLOutputFilter[] m_raw_output_filters;   // the raw output filters
  private HTMLRewriter[] m_string_rewriters;         // the string rewriters
  private HTMLRewriter[] m_word_rewriters;           // the word rewriters
  private HTMLRewriter[] m_tag_rewriters;            // the tag rewriters
  private HTMLRewriter[] m_paren_rewriters;          // the parenthesis rewriters
  private HTMLRewriter[] m_bracket_rewriters;        // the bracket rewriters
  private HTMLRewriter[] m_brace_rewriters;          // the brace rewriters
  private BitSet m_allowed_tag_set;                  // the allowed tag set
  private String m_openers;                          // the set of recognized "opener" characters
  private Hashtable m_attrs_cache = new Hashtable(); // cached attributes from the underlying profile

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

  FrozenProfileObject(ProfileObject prof) throws DatabaseException
  {
    m_profile = prof;
    m_rewrap_lines =
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"rewrap.lines",Boolean.FALSE))).booleanValue();
    m_process_angles =
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"process.angles",Boolean.TRUE))).booleanValue();
    m_process_parens =
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"process.parens",Boolean.TRUE))).booleanValue();
    m_process_brackets =
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"process.brackets",Boolean.FALSE))).booleanValue();
    m_process_braces =
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"process.braces",Boolean.FALSE))).booleanValue();
    m_discard_html_tags = 
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"discard.html",Boolean.FALSE))).booleanValue();
    m_discard_rejected_html = 
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"discard.html.rejected",
						    Boolean.FALSE))).booleanValue();
    m_discard_html_comments = 
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"discard.html.comments",
						    Boolean.FALSE))).booleanValue();
    m_discard_xml_constructs = 
        ((Boolean)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"discard.xml",Boolean.FALSE))).booleanValue();
    m_word_wrap_length =
        ((Short)(PropertyUtils.getPropertyDefault(prof,NAMESPACE,"word.wrap",DEFAULT_WORDWRAP))).shortValue();
    if (m_word_wrap_length<0)
      m_word_wrap_length = 0;
    m_output_filters = prof.getOutputFilters();
    m_raw_output_filters = prof.getRawOutputFilters();
    m_string_rewriters = prof.getStringRewriters();
    m_word_rewriters = prof.getWordRewriters();
    m_tag_rewriters = prof.getTagRewriters();
    m_paren_rewriters = prof.getParenRewriters();
    m_bracket_rewriters = prof.getBracketRewriters();
    m_brace_rewriters = prof.getBraceRewriters();

    // Compute the allowed tag set.
    Set set1 = prof.getAllowedTagSet();
    m_allowed_tag_set = new BitSet(HTMLTagSet._LAST.getValue());
    Iterator it = set1.iterator();
    while (it.hasNext())
    { // convert the set of objects into a set of bits
      HTMLTagSet ts = (HTMLTagSet)(it.next());
      m_allowed_tag_set.set(ts.getValue()-1);

    } // end while

    // Construct the string full of "opener" characters.
    StringBuffer buf = new StringBuffer(4);
    if (m_process_angles)
      buf.append('<');
    if (m_process_parens)
      buf.append('(');
    if (m_process_brackets)
      buf.append('[');
    if (m_process_braces)
      buf.append('{');
    m_openers = buf.toString();

  } // end constructor

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

  boolean getRewrapLines()
  {
    return m_rewrap_lines;

  } // end getRewrapLines

  boolean getProcessAngles()
  {
    return m_process_angles;

  } // end getProcessAngles

  boolean getProcessParens()
  {
    return m_process_parens;

  } // end getProcessParens

  boolean getProcessBrackets()
  {
    return m_process_brackets;

  } // end getProcessBrackets

  boolean getProcessBraces()
  {
    return m_process_braces;

  } // end getProcessBraces

  boolean getDiscardHTMLTags()
  {
    return m_discard_html_tags;

  } // end getDiscardHTMLTags

  boolean getDiscardRejectedHTML()
  {
    return m_discard_rejected_html;

  } // end getDiscardRejectedHTML

  boolean getDiscardHTMLComments()
  {
    return m_discard_html_comments;

  } // end getDiscardHTMLComments

  boolean getDiscardXMLConstructs()
  {
    return m_discard_xml_constructs;

  } // end getDiscardXMLConstructs

  short getWordWrapLength()
  {
    return m_word_wrap_length;

  } // end getWordWrapLength

  HTMLOutputFilter[] getOutputFilters()
  {
    return m_output_filters;

  } // end getOutputFilters

  HTMLOutputFilter[] getRawOutputFilters()
  {
    return m_raw_output_filters;

  } // end getOutputFilters

  HTMLRewriter[] getStringRewriters()
  {
    return m_string_rewriters;

  } // end getStringRewriters

  HTMLRewriter[] getWordRewriters()
  {
    return m_word_rewriters;

  } // end getWordRewriters

  HTMLRewriter[] getTagRewriters()
  {
    return m_tag_rewriters;

  } // end getTagRewriters

  HTMLRewriter[] getParenRewriters()
  {
    return m_paren_rewriters;

  } // end getParenRewriters

  HTMLRewriter[] getBracketRewriters()
  {
    return m_bracket_rewriters;

  } // end getBracketRewriters

  HTMLRewriter[] getBraceRewriters()
  {
    return m_brace_rewriters;

  } // end getBraceRewriters

  BitSet getAllowedTagSet()
  {
    return m_allowed_tag_set;

  } // end getAllowedTagSet

  String getOpeners()
  {
    return m_openers;

  } // end getOpeners

  boolean isAllowed(HTMLTagSet ts)
  {
    return m_allowed_tag_set.get(ts.getValue()-1);

  } // end isAllowed

  String getAttribute(String namespace, String name)
  {
    QualifiedNameKey key = new QualifiedNameKey(namespace,name);
    String rc = (String)(m_attrs_cache.get(key));
    if (rc!=null)
      return rc;

    Object tmp = PropertyUtils.getPropertyNoErr(m_profile,namespace,name);
    if (tmp==null)
      return null;

    rc = tmp.toString();
    m_attrs_cache.put(key,rc);
    return rc;

  } // end getAttribute

} // end class FrozenProfileObject
