Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Java search and replace?

Thread view: 
bugnthecode - 20 Sep 2006 19:55 GMT
Hi, I've got something I'd like to do, but I'm not exactly sure what it
would be called. Basically it is a search and replace. I'll have a
"template" file setup with tokens on the lines like $name &
$phone_number. What I want to do is parse the file and replace each
token with a member from a specified object.

Psuedo code:
open file in read mode // test file defined below code block
while (line = readline()) not EOF
 while (word = nextword()) not null
   if word equals any predefined token // $user, $name, $phone_number,
$email etc...
     send matching member variable to string builder //
tokenHolder.user, tokenHolder.name etc...
   else
     send word to string builder
   endif
close file
end psuedo code

//test file
Hello $user, my name is $name.
My phone number is $phone_number
Please email me at $email
//end test file

This is a basic example of what I'm trying do. Basically this will
allow users of my program define their own template and wording without
the need of diving into any actual code.

My problem is not implementing this. I would probably end up using
regular expressions to do the matching. What I am asking is if there is
a term for this so that I may do google searching for libraries, or do
you know of any java libraries that already do this so I don't have to
roll my own?

Thanks,
Will
Larry - 20 Sep 2006 20:35 GMT
I think Java's StringTokenizer class would get you off to an excellent
start:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Larry

> Hi, I've got something I'd like to do, but I'm not exactly sure what it
> would be called. Basically it is a search and replace. I'll have a
[quoted text clipped - 34 lines]
> Thanks,
> Will
Oliver Wong - 20 Sep 2006 21:01 GMT
> Hi, I've got something I'd like to do, but I'm not exactly sure what it
> would be called. Basically it is a search and replace. I'll have a
> "template" file setup with tokens on the lines like $name &
> $phone_number. What I want to do is parse the file and replace each
> token with a member from a specified object.
[...]
> My problem is not implementing this. I would probably end up using
> regular expressions to do the matching. What I am asking is if there is
> a term for this so that I may do google searching for libraries, or do
> you know of any java libraries that already do this so I don't have to
> roll my own?

   I'd search for "templating library" or "template engine". JSTL seems
like a good fit, if you can be flexibile about the template language (i.e.
it doesn't use $identifier_name). Eclipse's JET also does something similar.

   - Oliver
Stefan Ram - 20 Sep 2006 22:00 GMT
>Hello $user, my name is $name.

public class Main
{
 public static java.lang.String replace
 ( final java.lang.String template,
   final java.util.Map<java.lang.String,java.lang.String> map )
 { final java.lang.StringBuilder list =
   new java.lang.StringBuilder( "\\$(" );
   for( final java.lang.String key: map.keySet() )
   { list.append( key ); list.append( "|" ); }
   list.append( "[^\\s\\S])" );
   java.util.regex.Pattern pattern =
   java.util.regex.Pattern.compile( list.toString() );
   java.util.regex.Matcher matcher = pattern.matcher( template );
   final java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer();
   while( matcher.find() )
   { final java.lang.String string = matcher.group( 1 );
     matcher.appendReplacement
     ( stringBuffer, map.get( string )); }
   matcher.appendTail( stringBuffer );
   return stringBuffer.toString(); }

 public static void main( final java.lang.String[] args )
 { final java.util.Map<java.lang.String,java.lang.String> map =
   new java.util.HashMap<java.lang.String,java.lang.String>();
   map.put( "user", "Mary" );
   map.put( "name", "Patricia" );
   java.lang.System.out.println
   ( replace( "Hello $user, my name is $name.", map ) ); }}

Hello Mary, my name is Patricia.
amitgupti@gmail.com - 20 Sep 2006 22:47 GMT
Hello You can use Velocity for this purpose
Recenlty we had a such case and using Velocity solved the whole problem

If you need any more info reply over here and i can put some sample
code

Amit

> >Hello $user, my name is $name.
>
[quoted text clipped - 28 lines]
>
> Hello Mary, my name is Patricia.
Stefan Ram - 20 Sep 2006 23:35 GMT
>Hello You can use Velocity for this purpose

 You respond to the wrong post(er) (addressing him with "you"),
 there also is no need to quote a complete program without any
 reference to it in the body of your message, full-quoting an
 X-No-Archive message is especially impolite in such a case.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.