com.jfw.util
Class Parser
java.lang.Object
com.jfw.util.Parser
public final class Parser
- extends java.lang.Object
Parse a string and elaborate special tags.
The special tags are:
<?key_value?>
<?if key_value=consant_value?>
<then>
...
</then>
<else>
...
</else>
</if>
<?repeat key_value?>
...
</repeat>
<?noparse?>
...
</noparse>
Is possibile to use this class alone for elaborate a string or in a jsp. For the
use in a jsp the tag defined in class GenericTag
must be used.
Example of use in a jsp
<%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mtl" %>
...
<mtl:generic>
<repeat repeat_this>
<?if key_1=value_1?>
<then>
found it: <?key_1?>
</then>
<else>
not found it: <?key_1?>
</else>
</if>
key_2 value: <?key_2?>
</repeat>
show this once: <?not_in_repeat?>
</mtl:generic>
To use outside a jsp the taglib line and the tag mtl:generic
mustn't be used.
To elaborate a String as the previous one you use the method substitute(java.lang.String, java.util.HashMap)
.
The data are in the HashMap. The parse substitute the keys with the values.
Here a java code example
HashMap outputdata = new HashMap();
ArrayList arrayList = new ArrayList();
outputdata.put("repeat_this", arrayList);
HashMap h1 = new HashMap();
arrayList.put(h1);
h1.put("key_1", "value_1");
h2.put("key_2", "value_2");
HashMap h2 = new HashMap();
arrayList.put(h2);
h2.put("key_1", "value_3");
h2.put("key_2", "value_4");
outputdata.put("not_in_repeat", "value_0");
Parser parser = new Parser();
String string2Elaborate = ...;
String elaboratedString = parser.substitute(string2Elaborate, outputdata);
For the repeat tag the value is an ArrayList of HashMaps. All the others
keys are associated with a String or with an object that implement the
toString method.
All the tags can be used "inside" other tags. So, is possibile to have a
repeat tag inside an if-then or if-else, an if
tag inside an other if-then-else tag, a repeat in a repeat
and so on.
The possible values in the if condition are:
- a constant value
- the blank space is indicate by ""
- <#KEY_4_STRING#> indicate the value of another variable. If inside
a repeat tag the variable are first taken from the current HashMap
(h1 or h2 in previous example) and if not found from the main HashMap (outputdata).
The if condition is evaluate using the equals method of String.
All text inside the noparse tag will be returned without elaboration.
If a variable not found in HashMap then is deleted from the parsed string. That
is valid also for if and repeat tags: the entire tag will not be
elaborated.
Method Summary |
java.lang.String |
substitute(java.lang.String stringToBeParsed,
java.util.HashMap variables)
Elaborate a String. |
void |
substitute(java.lang.String stringToBeParsed,
java.util.HashMap variables,
java.io.PrintWriter output)
Elaborate a String. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Parser
public Parser()
substitute
public java.lang.String substitute(java.lang.String stringToBeParsed,
java.util.HashMap variables)
- Elaborate a String.
- Parameters:
stringToBeParsed
- the String to elaborate.variables
- the data to substitute in the String.
- Returns:
- the elaborated String.
substitute
public void substitute(java.lang.String stringToBeParsed,
java.util.HashMap variables,
java.io.PrintWriter output)
- Elaborate a String.
- Parameters:
stringToBeParsed
- the String to elaborate.variables
- the data to substitute in the String.output
- the output stream where the elaborated String will be write.