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

import java.util.regex.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.HTMLTagSet;
import com.silverwrist.dynamo.Namespaces;

class A_Tag extends BalancedTag
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

  private static final String DEFAULT_TARGET = "Wander";

  private static Pattern TARGET_PATTERN;

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

  A_Tag()
  {
    super("A",false,HTMLTagSet.ANCHOR);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class HTMLTag
   *--------------------------------------------------------------------------------
   */

  public String rewrite(String tag_data, boolean is_closing, HTMLTagSite site)
  {
    if (is_closing)
      return tag_data;
    if (TARGET_PATTERN.matcher(tag_data).matches())
      return tag_data;
    String target_window = site.getAttribute(Namespaces.HTMLCHECKER_PROPERTIES_NAMESPACE,"default.A.target");
    if (target_window==null)
      target_window = DEFAULT_TARGET;
    return tag_data + " target=\"" + target_window + "\"";

  } // end rewrite

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

  static
  {
    try
    { // pre-compile all our regex patterns
      TARGET_PATTERN = Pattern.compile("\\s+target\\s*=\\s*([\"']?)\\w+\\1(?:\\s+.*)?$",Pattern.CASE_INSENSITIVE);

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

    } // end catch

  } // end static initializer

} // end class A_Tag
