jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Whats the best way ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Whats the best way ??" Watch "Whats the best way ??" New topic

Whats the best way ??

Jason Hankins

Joined: Oct 17, 2003
Posts: 3
Is there any design pattern that can be used in a scenario, where a class has to handle String manipulations. The requirement is that for every type of html field, I have to insert a default value for it. The default value will be retrieved form a map based on the parameter. I will have a class StringWorker which will have a method, workOnString(String main, Map map, String type). The StringWorker class has to first find out the key in the main string, and the get the value from the Map based on the key, and then replace the key with the value. For eg lets say the main string looks like this �<input type=�text� name=�first� constant=�key�>�, so in the StringWorker class I will parse the main string to retrieve �constant=key�, then I will retrieve the �key�, and then based on this key I will retrieve the value from the map, and then I will replace the �constant=key� with the value=valuefrommap in the main String. So finally the main string will look like this �<input type=�text� name=�first� value=�valuefromMap�>�
Second scenario which is bit complex is the handling of the string in cases of list, for eg in case of list, the main string will look like this �<select name=�menu�><option value=�item1� constant=�key�><option value=�item2� constant=�key� ><option value=�item3� constant=�key�>�. In this case I have to replace constant=�key� with selected attribute but I don�t know which option is selected, so based on the key I will get the value from the Map, and the compare the value returned from map with the value in the option tag, and wherever value matches, I will replace the constant=�key� with selected, and leave other option tag as it is. For eg if the selected option is item 2, then the after all the manipulations, the string will look like this �<select name=�menu�><option value=�item1� constant=�key�><option value=�item2� selected ><option value=�item3� constant=�key�>�
Is there any better way to handle this sort of requirement.
Is there any java design pattern that can be used for such sort of implementation.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your first looks like pretty simple string substitution. Look into regular expressions - java.util.Pattern is a starting point. That can help you find something like constant="key" and replace it.
Your second example is trickier. Unless you know the exact number of options you're going to have, you need something that can plug in the whole option tag, or a bunch of option tags. You might change your syntax to specify only one option in the input, then replace the whole option with one or more new ones.
But since you're in HTML, you're in an area where many tools exist for plugging in dynamic values on the server. Can you use JSP?
If you're not doing this on the server, but say preparing the HTML on your workstation and then uploading the HTML to a server for static delivery, you might look into actually parsing the HTML into a tree. I like the Quiotix Parser with its support for the Visitor Pattern for modifying the tree and regenerating the HTML. I do exactly this to build page navigation maps, table of contents and other goodies for pages that are finally served as static HTML.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jason Hankins

Joined: Oct 17, 2003
Posts: 3
Thanks for the reply.
I am working on Java1.3, so I guess the java.util.regex.Pattern is not available. If I go for my customized string manipulations, will it hamper the performance ?
Just wanted to know if there is any better approach for this requirement
I agree. Here's the link: http://aspose.com/file-tools
subject: Whats the best way ??
It's not a secret anymore!