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:

    Basic Level
  1. Getting Started
  2. Creating Structure
  3. Recovering the Data
  4. Interacting with Collections
  5. Restructuring the Map

  6. Intermediate Level
  7. Working with Stream
  8. Working with XML
  9. Handling Large XML
  10. Accessing Data Base
  11. Accessing Preferences

  12. Advanced Level
  13. Template Filling
  14. .NET Interoperability
  15. Runtime schema validation
  16. Writing Application

  17. Planned for Future
  18. IDE's plugin for coding-time schema validation and code completion

(Javascript must be enabled to allow syntax highlighting for source code snippets in this tutorial)

Advanced Level

11. Template Filling

Template 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.

  • Simple Substitution: Substitution
  • Conditional Substitution: If-Then-Else and Switch
  • Iteration: Foreach
  • Others: Comment and Include
  • 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:

    Hello my friends!

    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:

    Hello, testing simple condition!
    Flag exists

    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:

    Hello, testing simple condition without flag!
    Flag does not exist

    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:

    Hello, testing iteration!
    ISBNTitle
    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:

    Hello, world!
    ISBNTitle
    978-1423101475 The Last Olympian
    978-0470114872 Beginning XML
    978-0596008734 Learning Java
    978-1441412850 Think and Grow Rich

    Now, execute the code below:

    The result should be:

    Hello, testing switch case!
    ISBNTitle
    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