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

class TagA extends BalancedTag
{
  /*--------------------------------------------------------------------------------
   * Internal static data
   *--------------------------------------------------------------------------------
   */

  private static final String TARGET_ATTR = "TARGET";

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

  TagA()
  {
    super("A",false);

  } // end constructor

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

  public String rewriteTagContents(String contents, boolean is_closing, HTMLCheckerBackend context)
  {
    if (is_closing)
      return contents;  // don't bother checking close tags

    // Skip over the initial word of the tag data, as that's the tag name.
    int i = 0;
    while ((i!=contents.length()) && !(Character.isWhitespace(contents.charAt(i))))
      i++;

    // Search for the TARGET= attribute in the tag data.
    boolean target_seen = false;
    while (i!=contents.length())
    { // skip over any whitespace between one attribute (or the name) and the next one
      while ((i!=contents.length()) && Character.isWhitespace(contents.charAt(i)))
	i++;
      if (i==contents.length())
	break;  // reached end of string, all done searching

      // Mark the start of this attribute name and start skipping over it.
      int start_name = i;
      while ((i!=contents.length()) && !(Character.isWhitespace(contents.charAt(i)))
	     && (contents.charAt(i)!='='))
	i++;

      // We now know where the attribute name is, see if it's "TARGET".
      if ((i-start_name)==TARGET_ATTR.length())
      { // compare the substring to see if it's right
	String name = contents.substring(start_name,i);
	if (name.equalsIgnoreCase(TARGET_ATTR))
	{ // OK, we saw the TARGET tag in the list!  Bail out!
	  target_seen = true;
	  break;

	} // end if

      } // end if

      while ((i!=contents.length()) && Character.isWhitespace(contents.charAt(i)))
	i++;  // skip over whitespace at end of name but before the = sign

      if ((i<contents.length()) && (contents.charAt(i)=='='))
      { // skip over the = sign first
	i++;
	while ((i!=contents.length()) && Character.isWhitespace(contents.charAt(i)))
	  i++;  // skip over whitespace after the = sign

	if (i==contents.length())
	  break;  // reached end of string, all done searching

	if ((contents.charAt(i)=='\'') || (contents.charAt(i)=='\"'))
	{ // this is a quoted string - swallow it
	  char quote_char = contents.charAt(i++); // skip the quote part
	  while ((i!=contents.length()) && (contents.charAt(i)!=quote_char))
	    i++;  // skip over data between quotes
	  if (i!=contents.length())
	    i++;  // skip over last quote

	} // end if
	else
	{ // skip over a single word
	  while ((i!=contents.length()) && !(Character.isWhitespace(contents.charAt(i))))
	    i++;

	} // end else

      } // end if
      // else this tag had no value - just go on to the next one

    } // end while

    if (target_seen)
      return contents;  // no need to alter the string

    String tail = (String)(context.getCheckerAttrValue("ANCHORTAIL"));
    return new String(contents + " " + tail);

  } // end rewriteTagContents

} // end class TagA
