Parameters XML serialisation_124354657 - Gumtree/gumtree GitHub Wiki

Gumtree : Parameters XML serialisation

Created by Tony Lam, last modified on Jul 08, 2009
GumTree provides an useful API to handling parameters or properties in XML format. Assume you want to configure your system using key-value pairs, and make them serialisable, XStream + org.gumtree.core.util.IParameters is your answer. To use this API, you need to import from the org.gumtree.core bundle:
IParameters parameters = new Parameters();
parameters.put("some_key", "value");
parameters.put("some_more_key", new Object());
To serialise or deserialise using XStream, we need to configure it to support customised converter:
XStream xStream = new XStream();
xStream.alias("parameters", IParameters.class, Parameters.class);
xStream.registerConverter(new ParametersConverter(xStream.getMapper()));

Example 1 - String properties

Say we have three key value paris as shown:
Key Value title Big Title visible false count 3
Equivalent XML using the parameters API will be:
<parameters>
  <title>Big Title</title>
  <visible>false</visible>
  <count>30</count>
</parameters>
In the Java API, you can retrieve the content by:
String title = parameters.getString("title");
boolean visible = parameters.get("visible", boolean.class);
int count = parameters.get("visible", int.class);

Example 2 - Object properties

Notice in example 1 all properties are stored in String. This is true that by default all properties are String in XML format. However, the parameters API can store any arbitrary object as value:
<parameters>
  <title>Big Title</title>
  <count>
    <int>30<int>
  </count>
  <myObject>
    <org.gumtree.core.util.xml.SampleModel>
      <id>id2</id>
      <value>30</value>
    </org.gumtree.core.util.xml.SampleModel>
  </myObject>
</parameters>
Retrieving the content is simple:
String title = parameters.getString("title");
int count = parameters.get("visible", int.class);
SampleModel model = parameters.get("myObject", SampleModel.class);
Document generated by Confluence on Apr 01, 2015 00:11
⚠️ **GitHub.com Fallback** ⚠️