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)

Intermediate Level

10. Accessing Preferences

The org.dhmp.io.MapPreferences is a very simple class which can interact with java.util.prefs.Preferences. It has the following methods:

Basically, the class can transfer the content of a HierarchicalMap into Preferences and vice versa.

Unfortunately, due to the Preferences restriction, only String values are supported. Actually Preferences can accept other types like Boolean, Integer etc. However using Preferences api, there is no indication about which type of data are stored, on the other hand, appropriate method must be used to retrieve them. So, MapPreferences store HierarchicalMap values converting them to String. Therefore most of Objects can not be stored, unless split them into their attributes and save them individually, or convert them into Base64. The org.dhmp.util.xml.Schema's replace() method can be used to simplify conversion between String and other type of Objects like Boolean, Integer etc.

Check below how to use this class.

Note that although comparison returned true, the data held by each of HierarchicalMap are totally different. The node "test/integer1" is Integer on original map and it is String on retrieved map.

Continue to Advanced Level - Template Filling