/*
 * 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.regex.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class RewriteEMailAddresses implements HTMLCheckerConfigurator, HTMLRewriter
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

  private static final String DEFAULT_TARGET = "Wander";

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

  private Pattern m_email_pattern;

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

  RewriteEMailAddresses()
  {
    try
    { // pre-compile all our regex patterns
      m_email_pattern = Pattern.compile("^[A-Za-z0-9!#$%*+-/=?^_`{|}~.]+@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$");

    } // end try
    catch (PatternSyntaxException pe)
    { // log an error, all we can do
      logger.fatal("Pattern compilation error in RewriteEMailAddresses",pe);

    } // end catch

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLCheckerConfigurator
   *--------------------------------------------------------------------------------
   */

  public void configure(HTMLCheckerProfile profile)
  {
    boolean enable =
        ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
						    "rewrite.email.string",Boolean.FALSE))).booleanValue();
    if (enable)
      profile.addStringRewriter(this);
    enable = ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
							 "rewrite.email.tag",Boolean.FALSE))).booleanValue();
    if (enable)
      profile.addTagRewriter(this);
    enable = ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
							 "rewrite.email.paren",Boolean.FALSE))).booleanValue();
    if (enable)
      profile.addParenRewriter(this);
    enable = ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
							 "rewrite.email.bracket",Boolean.FALSE))).booleanValue();
    if (enable)
      profile.addBracketRewriter(this);
    enable = ((Boolean)(PropertyUtils.getPropertyDefault(profile,Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,
							 "rewrite.email.brace",Boolean.FALSE))).booleanValue();
    if (enable)
      profile.addBraceRewriter(this);

  } // end configure

  /*--------------------------------------------------------------------------------
   * Implementations from interface HTMLRewriter
   *--------------------------------------------------------------------------------
   */

  public QualifiedNameKey getName()
  {
    return null;

  } // end getName

  public MarkupData rewrite(String data, String prefix, String suffix, HTMLRewriterSite site)
  {
    if (!(m_email_pattern.matcher(data).matches()))
      return null;

    // build the <A> tag (the gnarliest part)
    StringBuffer buf = new StringBuffer("<a href=\"mailto:");
    buf.append(data).append("\" target=\"");
    String target = site.getAttribute(Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,"default.A.target");
    if (target==null)
      target = DEFAULT_TARGET;
    buf.append(target).append("\">");

    return new MarkupData(prefix,buf.toString(),data,"</a>",suffix);

  } // end rewrite

} // end class RewriteEMailAddresses
