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

import java.util.*;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;

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

  private static final String NULLSTRING = "";
  private static Hashtable prefix_list = null;
  private static boolean set_up = true;

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

  public URLRewriter()
  {
    setUpPrefixes();  // make sure the prefix data is set up

  } // end constructor

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

  private static void setUpPrefixes()
  {
    if (set_up)
    { // allocate the hash table
      set_up = false;
      prefix_list = new Hashtable(10,0.9F);

      // fill it with the proper URL prefixes
      prefix_list.put("http:",NULLSTRING);
      prefix_list.put("ftp:",NULLSTRING);
      prefix_list.put("gopher:",NULLSTRING);
      prefix_list.put("mailto:",NULLSTRING);
      prefix_list.put("news:",NULLSTRING);
      prefix_list.put("nntp:",NULLSTRING);
      prefix_list.put("telnet:",NULLSTRING);
      prefix_list.put("tn3270:",NULLSTRING);
      prefix_list.put("www.",new String("http://"));
      prefix_list.put("ftp.",new String("ftp://"));
      prefix_list.put("gopher.",new String("gopher://"));

    } // end if

  } // end setUpPrefixes

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

  public String getName()
  {
    return null;

  } // end getName

  public MarkupData rewrite(String data, RewriterServices svc)
  {
    Enumeration prefixes = prefix_list.keys();
    while (prefixes.hasMoreElements())
    { // get the next prefix and compare against the beginning of the string
      String pfx = (String)(prefixes.nextElement());
      if (data.regionMatches(true,0,pfx,0,pfx.length()))
      { // good enough!  build the open <A> tag (the gnarliest part of the markup)
	StringBuffer open_a = new StringBuffer("<A HREF=\"");
	String catenate = (String)(prefix_list.get(pfx));
	open_a.append(catenate).append(data).append("\"");
	catenate = svc.getRewriterAttrValue("ANCHORTAIL");
	if ((catenate!=null) && (catenate.length()>0))
	  open_a.append(' ').append(catenate);
	open_a.append('>');

	// here's how you mark it up!
	return new MarkupData(open_a.toString(),data,"</A>");

      } // end if

    } // end while

    return null;  // sorry, no can do

  } // end rewrite

} // end class URLRewriter
