J F W

making web application programming easier is possible

HOW TO : CORE

JFW generic parser

The JFW generic parser can be used as stand alone or in a jsp. Using this parser, you can substitute variables in a string. Let's see some use cases of the parser.

Use case 1: I want to create the text of an email that has two variables, the name and the surname of a client.

Solution:

The first idea is to substitute the variables with the "<?...?>". Between the two question marks, without spaces, must be a single word that will be the name of the variable. In our example the variables are two, the name and the surname.
Then we have to set the variables in an HashMap. The key is the name of the variable and the value, for this kind of substitution, is a String.
All we have to do now is to create a Parser object and use the substitute method.

Remember that if a variable is not set in the HashMap, then the value will not be substituted in the string.

Use case 2: I want to create a string based on a value of a variable.

Solution:

In this case we want to have a different value based on the value of the gender. For that reason we use the "if-then-else" tag. In the if we use the name of a variable and the value a string. Only the equal condition is available.

If the variable used in the if condition is not set in the HashMap, then the entire "if-then-else" tag will be deleted from the result string.

Use case 3: I want to create a csv file with the values of a database table.

Solution:

In this case we use the repeat tag. After the word repeat we leave a space and write the name of a variable saved in the HashMap. Note that the value of the variable is not a string but an ArrayList of HashMaps.

See also how to read the data of a table and save it in a HashMap, in a format compatible with the parser.

Use case 4: I don't want to parse a portion of the string.

Solution:

The portion of the string between the start and the end of the noparse tag will be not evaluated and will be left as is in the result string.

Tip: between the start and the end of the tag repeat we can use all the other tags: the single variable ("<?...?>"), the "if-then-else" tag and even another repeat tag. The same is valid also for the tag noparse and for the text between the start and the end of tags then and else.

Use case 5: I want to use the parser in a jsp.

Solution:

The values to substitute in the jsp must be added in the HashMap passed as parameter of method myExecute , in the same way we did it in the previous examples with the HashMap data.