/*
 * 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.conf.jsp;

import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.silverwrist.venice.db.PostLinkRewriter;
import com.silverwrist.venice.db.UserNameRewriter;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.util.IDUtils;

public class PostRewriteTag extends VeniceConfBodyTagSupport
{
  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static void output(BodyContent body, String data) throws JspException
  {
    try
    { // write the escaped information
      JspWriter out = body.getEnclosingWriter();
      out.write(data);

    } // end try
    catch (IOException e)
    { // translate output exception
      throw new JspTagException("IO error writing rewritten post data: " + e.getMessage());

    } // end catch

  } // end output

  /*--------------------------------------------------------------------------------
   * Overrides from class BodyTagSupport
   *--------------------------------------------------------------------------------
   */

  public int doAfterBody() throws JspException
  {
    BodyContent body = getBodyContent();
    String data = body.getString();

    if ((data.indexOf(PostLinkRewriter.URI_PREFIX)<0) && (data.indexOf(UserNameRewriter.URI_PREFIX)<0))
    { // no post links or user links in here - output what we've got and call it
      output(body,data);
      return SKIP_BODY;

    } // end if

    StringBuffer buf = new StringBuffer();
    HTMLRendering html = (HTMLRendering)(getRequestInput().queryService(HTMLRendering.class));
    String t;
    int p;

    if (data.indexOf(PostLinkRewriter.URI_PREFIX)>=0)
    { // begin replacing the post links with real post links
      t = data;
      p = t.indexOf(PostLinkRewriter.URI_PREFIX);
      while (p>=0)
      { // break off the start of the string and append it
	if (p>0)
	  buf.append(t.substring(0,p));
	t = t.substring(p + PostLinkRewriter.URI_PREFIX.length());

	// find the end of the post link...
	p = 0;
	while (IDUtils.isValidPostLinkChar(t.charAt(p)))
	  p++;

	if (p>0)
	{ // append the post link to the "go" servlet path, and encode the lot
	  buf.append(html.formatURL("go/" + t.substring(0,p),html.SERVLET));
	  t = t.substring(p);

	} // end if
	else  // false alarm
	  buf.append(PostLinkRewriter.URI_PREFIX);

	// and now look again...
	p = t.indexOf(PostLinkRewriter.URI_PREFIX);

      } // end while

      // append the rest of the string and clean up
      buf.append(t);
      data = buf.toString();
      buf.setLength(0);

    } // end if
    // else there were no post links, just leave "data" the way it is

    if (data.indexOf(UserNameRewriter.URI_PREFIX)<0)
    { // no user links in here - output what we've got and call it
      output(body,data);
      return SKIP_BODY;

    } // end if

    // begin replacing the user links with real user links
    t = data;
    p = t.indexOf(UserNameRewriter.URI_PREFIX);
    while (p>=0)
    { // break off the start of the string and append it
      if (p>0)
	buf.append(t.substring(0,p));
      t = t.substring(p + UserNameRewriter.URI_PREFIX.length());

      // find the end of the user link...
      p = 0;
      while (IDUtils.isValidVeniceIDChar(t.charAt(p)))
	p++;
      
      if (p>0)
      { // append the post link to the "user" servlet path, and encode the lot
	buf.append(html.formatURL("user/" + t.substring(0,p),html.SERVLET));
	t = t.substring(p);

      } // end if
      else  // false alarm
	buf.append(UserNameRewriter.URI_PREFIX);

      // and now look again...
      p = t.indexOf(UserNameRewriter.URI_PREFIX);

    } // end while

    // Clean up and return the resulting output.
    buf.append(t);
    output(body,buf.toString());
    return SKIP_BODY;

  } // end doAfterBody

} // end class PostRewriteTag

