HierarchicalMap Tutorial - Intermediate Level
HierarchicalMap is an interface. So anyone can implement it. This tutorial is based on BasicHierarchicalmap, a reference implementation of HierarchicalMap.
Following subjects are covered in this tutorial:
(Javascript must be enabled to allow syntax highlighting for source code snippets in this tutorial)
Advanced Level
11. Template FillingTemplate containing static content can be merged with HierarchicalMap to produce a new text document. There are two classes to handle template: org.dhmp.util.stylesheet.Script and org.dhmp.util.stylesheet.ScriptCollection.
A very simple language was defined to interact with HierarchicalMap.
Substitution
First, write down a text file with following content and name it "greetingTemplate.html" then place it on "/temp" directory:
The following snippet of code will generate an html output "greeting.html" on "/temp" directory.
The object ScriptCollection compiles the template into Script and keep it cached inside its structure. The constructor can receive a java.io.File object from where it will fetch the template. On the above example it will search for the template on "/temp" directory. There are another constructor which will accept an array of File or a java.net.URL.
Then the method get() will interprete the template language and compile it into Script Object. Note that compilation is not thread safe. Once Script is obtained, you can use it to generate a document replacing some tags with the content of HierarchicalMap and write down the result on writer object.
On the example above, substitution is a self-closing tag. But any text between opening and closing tag will be ignored.
Create a template with above content and save it as "/temp/greetingTemplate2.html". Then execute the code below:
The result should be:
Condition If-Then-Else
Now, let's see the conditions. Create a document "/temp/conditionTemplate.html" with following content:
Now, execute the code below:
The result should be:
For the condition to met, the existence of node or leaf with key name same as condition name is enough regardless of its content.
If the same template is executed without setting the flag as in code below:.
The result should be:
Iterarion
Nodes or leaves mapped to same key can be iterated with the following structure. Create a document "/temp/iterationTemplate.html" with following content:
Now, execute the code below:
The result should be:
ISBN | Title |
---|---|
978-1861005618 | Professional Java Servlets 2.3 |
978-0130655677 | Definitive XML Schema |
Condition Switch
Here is another type of condition. Create a document "/temp/switchTemplate.html" with following content:
Note that "</*" indicates the start and "/*>" the end of the script comment. Any text between them are striped out by script during evaluation. This can be used to html author to check its page before runing java code, without disturbing the result after script evaluation.
Try opening the "/temp/switchTemplate.html" with some browser and the result would be like following:
ISBN | Title |
---|---|
Now, execute the code below:
The result should be:
ISBN | Title |
---|---|
978-1861005618 | Professional Java Servlets 2.3 |
978-0130655677 | Definitive XML Schema |
978-1441408488 | Treasure Island |
978-0060229351 | Harold and the Purple Crayon |
Continue to Advanced Level - .NET Interoperability