View Javadoc

1   package com.atlassian.config.xml;
2   
3   import com.atlassian.config.ConfigurationException;
4   import org.dom4j.DocumentHelper;
5   import org.dom4j.Element;
6   import org.dom4j.XPath;
7   
8   import java.util.HashMap;
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  /**
13   * Created by IntelliJ IDEA.
14   * User: ROSS
15   * Date: 16/03/2004
16   * Time: 11:29:26
17   * To change this template use File | Settings | File Templates.
18   */
19  public class Dom4jXmlMapConfigElement extends Dom4jXmlStringConfigElement
20  {
21      public Dom4jXmlMapConfigElement(String name, Element context, AbstractDom4jXmlConfigurationPersister config)
22      {
23          super(name, context, config);
24      }
25  
26      public Class getObjectClass()
27      {
28          return Map.class;
29      }
30  
31      public void saveConfig(Object object) throws ConfigurationException
32      {
33          Map map = (Map) object;
34          Element node = DocumentHelper.makeElement(context, getPropertyName());
35          Map.Entry entry;
36          for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
37          {
38              entry = (Map.Entry) iterator.next();
39              getConfiguration().addConfigElement(entry, "property", node);
40          }
41      }
42  
43      public Object loadConfig() throws ConfigurationException
44      {
45          XPath xpath = DocumentHelper.createXPath("/" + context.getName() + "/" + getPropertyName());
46          Element element = (Element) xpath.selectSingleNode(context);
47          Map.Entry entry;
48          Map map = new HashMap();
49  
50          for (Iterator iterator = element.elementIterator(); iterator.hasNext();)
51          {
52              entry = (Map.Entry) getConfiguration().getConfigElement(Map.Entry.class, "property", (Element) iterator.next());
53              map.put(entry.getKey(), entry.getValue());
54          }
55          return map;
56      }
57  }