/*
 * 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.HTMLTagSets;

class TagRepository implements HTMLTagSets
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static HashMap tagname_to_index = new HashMap(50,0.9F);
  private static ArrayList index_to_object = new ArrayList();
  private static ArrayList index_to_setid = new ArrayList();
  private static int maxlength = 0;

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

  private TagRepository()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal functions
   *--------------------------------------------------------------------------------
   */

  private static void enshrineTag(SimpleTag tag, int set)
  {
    int ndx = index_to_object.size();
    index_to_object.add(tag);
    index_to_setid.add(new Integer(set));
    tag.setIndex(ndx);
    tagname_to_index.put(tag.getTagName(),new Integer(ndx));
    int newlen = tag.getTagName().length();
    if (newlen>maxlength)
      maxlength = newlen;

  } // end enshrineTag

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

  public static int getMaxTagLength()
  {
    return maxlength;

  } // end getMaxTagLength

  public static int tagNameToIndex(String tag)
  { // look up tag name and get index
    Object obj = tagname_to_index.get(tag.toUpperCase());
    if (obj==null)
      return -1;
    else
      return ((Integer)obj).intValue();

  } // end tagNameToIndex

  public static SimpleTag tagIndexToObject(int ndx)
  {
    Object obj = index_to_object.get(ndx);
    if (obj==null)
      return null;
    else
      return (SimpleTag)obj;

  } // end tagIndexToObject

  public static int tagIndexToSet(int ndx)
  {
    Object obj = index_to_setid.get(ndx);
    if (obj==null)
      return -1;
    else
      return ((Integer)obj).intValue();

  } // end tagIndexToSet

  public static void configureNormalSet(BitSet bs)
  {
    bs.xor(bs); // clears all bits presently set in the bitset
    bs.set(INLINE_FORMAT);
    bs.set(ANCHOR);
    bs.set(BLOCK_FORMAT);
    bs.set(FONT_FORMAT);
    bs.set(IMAGES);
    
  } // end configureNormalSet

  public static void configureRestrictedSet(BitSet bs)
  {
    bs.xor(bs); // clears all bits presently set in the bitset
    bs.set(INLINE_FORMAT);
    
  } // end configureRestrictedSet

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  { // begin enshrining the tags!
    enshrineTag(new SimpleTag("!DOCTYPE",false),DOC_FORMAT);
    enshrineTag(new SimpleTag("%",false),SERVER_PAGE);
    enshrineTag(new SimpleTag("%=",false),SERVER_PAGE);
    enshrineTag(new SimpleTag("%@",false),SERVER_PAGE);
    enshrineTag(new TagA(),ANCHOR);
    enshrineTag(new BalancedTag("ABBR",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("ACRONYM",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("ADDRESS",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("APPLET",false),ACTIVE_CONTENT);
    enshrineTag(new SimpleTag("AREA",false),IMAGE_MAPS);
    enshrineTag(new BalancedTag("B",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("BASE",false),DOC_FORMAT);
    enshrineTag(new SimpleTag("BASEFONT",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("BDO",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("BEAN",false),JAVA_SERVER);
    enshrineTag(new SimpleTag("BGSOUND",false),MSFT_DOC_FORMAT);
    enshrineTag(new BalancedTag("BIG",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("BLINK",false),NSCP_INLINE_FORMAT);
    enshrineTag(new BalancedTag("BLOCKQUOTE",true),BLOCK_FORMAT);
    enshrineTag(new OpenCloseTag("BODY",false),DOC_FORMAT);
    enshrineTag(new SimpleTag("BR",true),BLOCK_FORMAT);
    enshrineTag(new OpenCloseTag("BUTTON",false),FORMS);
    enshrineTag(new BalancedTag("CAPTION",true),TABLES);
    enshrineTag(new BalancedTag("CENTER",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("CITE",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("CODE",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("COL",true),TABLES);
    enshrineTag(new OpenCloseTag("COLGROUP",true),TABLES);
    enshrineTag(new BalancedTag("COMMENT",false),MSFT_INLINE_FORMAT);
    enshrineTag(new ListElementTag("DD"),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("DEL",false),CHANGE_MARKUP);
    enshrineTag(new BalancedTag("DFN",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("DIR",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("DIV",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("DL",true),BLOCK_FORMAT);
    enshrineTag(new ListElementTag("DT"),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("EM",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("EMBED",false),ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("FIELDSET",false),FORMS);
    enshrineTag(new BalancedTag("FONT",false),FONT_FORMAT);
    enshrineTag(new BalancedTag("FORM",false),FORMS);
    enshrineTag(new SimpleTag("FRAME",true),FRAMES);
    enshrineTag(new BalancedTag("FRAMESET",false),FRAMES);
    enshrineTag(new BalancedTag("H1",true),FONT_FORMAT);
    enshrineTag(new BalancedTag("H2",true),FONT_FORMAT);
    enshrineTag(new BalancedTag("H3",true),FONT_FORMAT);
    enshrineTag(new BalancedTag("H4",true),FONT_FORMAT);
    enshrineTag(new BalancedTag("H5",true),FONT_FORMAT);
    enshrineTag(new BalancedTag("H6",true),FONT_FORMAT);
    enshrineTag(new OpenCloseTag("HEAD",false),DOC_FORMAT);
    enshrineTag(new SimpleTag("HR",true),BLOCK_FORMAT);
    enshrineTag(new OpenCloseTag("HTML",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("I",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("IFRAME",true),FRAMES);
    enshrineTag(new BalancedTag("ILAYER",true),NSCP_LAYERS);
    enshrineTag(new SimpleTag("IMG",false),IMAGES);
    enshrineTag(new SimpleTag("INPUT",false),FORMS);
    enshrineTag(new BalancedTag("INS",false),CHANGE_MARKUP);
    enshrineTag(new SimpleTag("ISINDEX",false),FORMS);
    enshrineTag(new BalancedTag("KBD",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("KEYGEN",false),NSCP_FORMS);
    enshrineTag(new BalancedTag("LABEL",false),FORMS);
    enshrineTag(new BalancedTag("LAYER",true),NSCP_LAYERS);
    enshrineTag(new BalancedTag("LEGEND",false),FORMS);
    enshrineTag(new ListElementTag("LI"),BLOCK_FORMAT);
    enshrineTag(new SimpleTag("LINK",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("LISTING",false),MSFT_INLINE_FORMAT);
    enshrineTag(new BalancedTag("MAP",false),IMAGE_MAPS);
    enshrineTag(new BalancedTag("MARQUEE",true),MSFT_BLOCK_FORMAT);
    enshrineTag(new BalancedTag("MENU",true),BLOCK_FORMAT);
    enshrineTag(new SimpleTag("META",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("MULTICOL",true),NSCP_BLOCK_FORMAT);
    enshrineTag(new TagNOBR(),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("NOEMBED",false),ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("NOFRAMES",false),FRAMES);
    enshrineTag(new BalancedTag("NOLAYER",false),NSCP_LAYERS);
    enshrineTag(new BalancedTag("NOSCRIPT",false),ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("OBJECT",false),ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("OL",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("OPTGROUP",false),FORMS);
    enshrineTag(new ListElementTag("OPTION"),FORMS);
    enshrineTag(new OpenCloseTag("P",true),BLOCK_FORMAT);
    enshrineTag(new SimpleTag("PARAM",false),ACTIVE_CONTENT);
    enshrineTag(new SimpleTag("PLAINTEXT",false),PREFORMAT);
    enshrineTag(new BalancedTag("PRE",false),PREFORMAT);
    enshrineTag(new BalancedTag("Q",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("RT",false),MSFT_ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("RUBY",false),MSFT_ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("S",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("SAMP",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("SCRIPT",false),ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("SELECT",false),FORMS);
    enshrineTag(new BalancedTag("SERVER",false),NSCP_SERVER);
    enshrineTag(new BalancedTag("SERVLET",false),JAVA_SERVER);
    enshrineTag(new BalancedTag("SMALL",false),INLINE_FORMAT);
    enshrineTag(new SimpleTag("SPACER",false),NSCP_INLINE_FORMAT);
    enshrineTag(new BalancedTag("SPAN",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("STRIKE",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("STRONG",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("STYLE",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("SUB",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("SUP",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("TABLE",true),TABLES);
    enshrineTag(new OpenCloseTag("TBODY",false),TABLES);
    enshrineTag(new BalancedTag("TD",true),TABLES);
    enshrineTag(new BalancedTag("TEXTAREA",true),FORMS);
    enshrineTag(new OpenCloseTag("TFOOT",false),TABLES);
    enshrineTag(new BalancedTag("TH",true),TABLES);
    enshrineTag(new OpenCloseTag("THEAD",false),TABLES);
    enshrineTag(new BalancedTag("TITLE",false),DOC_FORMAT);
    enshrineTag(new BalancedTag("TR",true),TABLES);
    enshrineTag(new BalancedTag("TT",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("U",false),INLINE_FORMAT);
    enshrineTag(new BalancedTag("UL",true),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("VAR",false),INLINE_FORMAT);
    enshrineTag(new TagWBR(),BLOCK_FORMAT);
    enshrineTag(new BalancedTag("XML",false),MSFT_ACTIVE_CONTENT);
    enshrineTag(new BalancedTag("XMP",false),NSCP_INLINE_FORMAT);

  } // end static initializer

} // end class TagRepository
