Java search and replace?

B

bugnthecode

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
 
O

Oliver Wong

bugnthecode said:
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
 
S

Stefan Ram

bugnthecode said:
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.
 
A

amitgupti

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

Stefan said:
bugnthecode said:
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.
 
S

Stefan Ram

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top