Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Any design patter for this ??

 
Jason Hankins
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
Regards
escaped < and > so that the post can be read in a browser
[ October 17, 2003: Message edited by: Frank Carver ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Design patterns generally introduce abstraction/indirection to solve a structural problem. Yours doesn't qualify, in my opinion, so there probably isn't a "design pattern" for your situation. I might be wrong, of course.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
I would probably use regular expressions for the parsing of the strings - not a design pattern, but might help nevertheless.
But I wonder: where are the original strings coming from?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Just noticed that this is crossposted in the Java in General (intermediate) forum: http://www.coderanch.com/t/372010/java/java/Whats-best-way
Please continue discussion there. I will close this thread.
Please don't crosspost in the future. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic