/*
 * 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 org.apache.commons.collections.*;
import com.silverwrist.dynamo.db.NamespaceCache;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class ProfileObject implements HTMLCheckerProfile
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final HTMLOutputFilter[] OF_NULL = new HTMLOutputFilter[0];
  private static final HTMLRewriter[] R_NULL = new HTMLRewriter[0];

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

  private ProfileOps m_ops;                         // database operations object
  private NamespaceCache m_ns_cache;                // namespace cache object
  private int m_index;                              // index of this profile
  private ReferenceMap m_properties;                // property cache
  private Vector m_output_filters = null;           // output filters
  private Vector m_raw_output_filters = null;       // raw output filters
  private Vector m_string_rewriters = null;         // string rewriters
  private Vector m_word_rewriters = null;           // word rewriters
  private Vector m_tag_rewriters = null;            // tag rewriters
  private Vector m_paren_rewriters = null;          // parenthesis rewriters
  private Vector m_bracket_rewriters = null;        // bracket rewriters
  private Vector m_brace_rewriters = null;          // brace rewriters
  private Set m_allowed_tag_set = null;             // allowed tag set

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

  ProfileObject(ProfileOps ops, NamespaceCache nscache, int ndx)
  {
    m_ops = ops;
    m_ns_cache = nscache;
    m_index = ndx;
    m_properties = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ObjectProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves an object from this <CODE>ObjectProvider</CODE>.
   *
   * @param namespace The namespace to interpret the name relative to.
   * @param name The name of the object to be retrieved.
   * @return The object reference specified.
   */
  public Object getObject(String namespace, String name)
  {
    try
    { // convert the namespace name to an ID here
      PropertyKey key = new PropertyKey(m_ns_cache.namespaceNameToId(namespace),name);
      Object rc = null;
      synchronized (this)
      { // start by looking in the properties map
	rc = m_properties.get(key);
	if (rc==null)
	{ // no use - need to try the database
	  rc = m_ops.getProperty(m_index,key);
	  if (rc!=null)
	    m_properties.put(key,rc);

	} // end if

      } // end synchronized block

      if (rc==null)
	throw new NoSuchObjectException(this.toString(),namespace,name);
      return rc;

    } // end try
    catch (DatabaseException e)
    { // translate into our NoSuchObjectException but retain the DatabaseException
      throw new NoSuchObjectException(this.toString(),namespace,name,e);

    } // end catch

  } // end getObject

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLCheckerProfile
   *--------------------------------------------------------------------------------
   */

  public void addOutputFilter(HTMLOutputFilter filter)
  {
    if (m_output_filters==null)
      m_output_filters = new Vector();
    m_output_filters.add(filter);

  } // end addOutputFilter

  public void addRawOutputFilter(HTMLOutputFilter filter)
  {
    if (m_raw_output_filters==null)
      m_raw_output_filters = new Vector();
    m_raw_output_filters.add(filter);

  } // end addRawOutputFilter

  public void addStringRewriter(HTMLRewriter rewriter)
  {
    if (m_string_rewriters==null)
      m_string_rewriters = new Vector();
    m_string_rewriters.add(rewriter);

  } // end addStringRewriter

  public void addWordRewriter(HTMLRewriter rewriter)
  {
    if (m_word_rewriters==null)
      m_word_rewriters = new Vector();
    m_word_rewriters.add(rewriter);

  } // end addWordRewriter

  public void addTagRewriter(HTMLRewriter rewriter)
  {
    if (m_tag_rewriters==null)
      m_tag_rewriters = new Vector();
    m_tag_rewriters.add(rewriter);

  } // end addTagRewriter

  public void addParenRewriter(HTMLRewriter rewriter)
  {
    if (m_paren_rewriters==null)
      m_paren_rewriters = new Vector();
    m_paren_rewriters.add(rewriter);

  } // end addParenRewriter

  public void addBracketRewriter(HTMLRewriter rewriter)
  {
    if (m_bracket_rewriters==null)
      m_bracket_rewriters = new Vector();
    m_bracket_rewriters.add(rewriter);

  } // end addBracketRewriter

  public void addBraceRewriter(HTMLRewriter rewriter)
  {
    if (m_brace_rewriters==null)
      m_brace_rewriters = new Vector();
    m_brace_rewriters.add(rewriter);

  } // end addBraceRewriter

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

  HTMLOutputFilter[] getOutputFilters()
  {
    return (m_output_filters==null) ? OF_NULL : (HTMLOutputFilter[])(m_output_filters.toArray(OF_NULL));

  } // end getOutputFilters

  HTMLOutputFilter[] getRawOutputFilters()
  {
    return (m_raw_output_filters==null) ? OF_NULL : (HTMLOutputFilter[])(m_raw_output_filters.toArray(OF_NULL));

  } // end getOutputFilters

  HTMLRewriter[] getStringRewriters()
  {
    return (m_string_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_string_rewriters.toArray(R_NULL));

  } // end getStringRewriters

  HTMLRewriter[] getWordRewriters()
  {
    return (m_word_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_word_rewriters.toArray(R_NULL));

  } // end getWordRewriters

  HTMLRewriter[] getTagRewriters()
  {
    return (m_tag_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_tag_rewriters.toArray(R_NULL));

  } // end getTagRewriters

  HTMLRewriter[] getParenRewriters()
  {
    return (m_paren_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_paren_rewriters.toArray(R_NULL));

  } // end getParenRewriters

  HTMLRewriter[] getBracketRewriters()
  {
    return (m_bracket_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_bracket_rewriters.toArray(R_NULL));

  } // end getBracketRewriters

  HTMLRewriter[] getBraceRewriters()
  {
    return (m_brace_rewriters==null) ? R_NULL : (HTMLRewriter[])(m_brace_rewriters.toArray(R_NULL));

  } // end getBraceRewriters

  Set getAllowedTagSet() throws DatabaseException
  {
    if (m_allowed_tag_set==null)
      m_allowed_tag_set = m_ops.getAllowedTagSet(m_index);
    return m_allowed_tag_set;

  } // end getAllowedTagSet

} // end class ProfileObject
