/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.htmlcheck.filters;

import java.util.*;
import org.apache.log4j.*;
import org.apache.regexp.*;
import org.w3c.dom.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.util.XMLLoader;

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

  private static Logger logger = Logger.getLogger(EmoticonTagHandler.class);

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

  private Set m_valid_icons;

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

  public EmoticonTagHandler(Element root) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();
    HashSet tmp = new HashSet();

    // Look for all associated icons.
    NodeList nl = root.getChildNodes();
    for (int i=0; i<nl.getLength(); i++)
    { // look through all the child nodes...
      Node n = nl.item(i);
      if ((n.getNodeType()==Node.ELEMENT_NODE) && n.getNodeName().equals("icon"))
      { // load the icon name
	String icon_name = loader.configGetAttribute((Element)n,"name").intern();
	tmp.add(icon_name);
	
      } // end if
      // else skip this element

    } // end for

    if (tmp.isEmpty())
      m_valid_icons = Collections.EMPTY_SET;
    else
      m_valid_icons = Collections.unmodifiableSet(tmp);
    if (logger.isDebugEnabled())
      logger.debug("EmoticonTagHandler recognizes " + m_valid_icons.size() + " valid tag names");

  } // end constructor

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

  public String getName()
  {
    return "emoticon_tag";

  } // end getName

  public MarkupData rewrite(String data, RewriterServices svc)
  {
    logger.debug("EmoticonTagHandler looking at: " + data);
    RE r = null;
    try
    { // compile regular expression
      r = new RE("ei:\\s*(\\w+)(?:\\s*/)?\\s*");

    } // end try
    catch (RESyntaxException e)
    { // something wicked this way comes
      logger.fatal("WTF?!?!?!?",e);

    } // end catch
    
    if (!(r.match(data)))
      return null;
    String icon_name = r.getParen(1);
    logger.debug("EmoticonTagHandler match icon name: " + icon_name);
    if (!(m_valid_icons.contains(icon_name)))
      return null;
    return new MarkupData("<ei:" + icon_name + "/>",null,null);

  } // end rewrite

} // end class EmoticonTagHandler
