Without really looking at this, isn't string replacement better done as
tag replacement using xsl, ie use tags in the body indicating header,
etc and just transform via xsl? Or even just use xml entities?

This is probably my favorite thing about xml... its like having a string
with variables/placeholders built into it that can programmatically be
replaced, thus preventing durdeehacking involving many string
concatenations. 

Brady Moritz


-----Original Message-----
From: Greg Bishop [mailto:[EMAIL PROTECTED]] 
Sent: Friday, February 01, 2002 5:05 PM
To: Tag Libraries Users List
Subject: Re: JavaScript Functions-like Taglib

OK, save, here is something better....

/** Takes a string, and replaces the replace text with new text.
   * @param  str     String to replace into
   * @param  pattern Pattern of text to replace.
   * @param  replace What to replace it with.
   * @return str with the replace text replaced with the new text.
   */
   protected static String replace(String str,String pattern, String
replace) {
      int s = 0;
      int e = 0;
      StringBuffer result = new StringBuffer();

      while ((e = str.indexOf(pattern, s)) >= 0) {
      result.append(str.substring(s, e));
      result.append(replace);
      s = e+pattern.length();
      }
      result.append(str.substring(s));
      return result.toString();
   }

Ok, here's a shorter one that works better.

Also, a faster implementation.

   private static String templateText;

   public javascriptJSPInteractiveTagThingy() {
      super();
      StringBuffer sb = new StringBuffer();
      try {
        BufferedReader br = new BufferedReader(new
FileReader("/home/gbishop/www/bmg/dev/run/ckie/jsp/charttabletag.templat
e"));
        String line;
        while( ((line=br.readLine())!=null)){
           sb.append(line);
           sb.append("\n");
        }
      } catch(IOException e) {
        System.err.println("IO Error: " + e.getMessage());
      }
      templateText = sb.toString();
   }

  /**
   * Reads a file from the disk.  Replaces XXXX_GOES_HERE text in the
file with
   * Strings that contain the things we want the tag to show.  Looks at
the page
   * attributes to control the appearance of the tag's generated HTML.
   */
   public int doStartTag() throws JspTagException
   {
...
      JspWriter wr = pageContext.getOut();
      PrintWriter writer = new PrintWriter(wr);
      String line = templateText;
      line = replace(line, "HEADER_GOES_HERE",               header );
      line = replace(line, "TABLE_CONTENTS_GOES_HERE",
tableContents );
      line = replace(line, "SELECTIONS_GOES_HERE",
pageContext.getRequest().getParameter("TableSelections") );
      line = replace(line, "BACKGROUND_COLOR_GOES_HERE",
backgroundColor );
      line = replace(line, "HILIGHTED_COLOR_GOES_HERE",
hilightedColor );
      line = replace(line, "DRAG_COLOR_GOES_HERE",           dragColor
);
      line = replace(line, "SELECTED_COLOR_GOES_HERE",
selectedColor );
      line = replace(line, "BORDER_COLOR_GOES_HERE",         borderColor
);
      line = replace(line, "TABLE_DATA_VIEW_NAME_GOES_HERE",
tableDataViewHiddenFieldName );
      line = replace(line, "HTCS_TO_INVOKE_GOES_HERE",       htcs );
      writer.println(line);

      return SKIP_BODY;
...

Dave Newton wrote:

> I know it's protocode :)
>
> On Friday 01 February 2002 15:22, Greg Bishop wrote:
> > protected static String replaceTextInMiddleOfStringIfItExists(String
str,
> >                                                         String
replace,
> >                                                         String
newtext){
>
> Well, seems to me the "IfItExists" is implied and it replaces text in
a string
> regardless of location (doesn't have to be in the middle) so why
bother
> with the novella function name? ;)
>
> Dave



--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to