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 public class Dom4jXmlMapConfigElement extends Dom4jXmlStringConfigElement
13 {
14 public Dom4jXmlMapConfigElement(String name, Element context, AbstractDom4jXmlConfigurationPersister config)
15 {
16 super(name, context, config);
17 }
18
19 public Class getObjectClass()
20 {
21 return Map.class;
22 }
23
24 public void saveConfig(Object object) throws ConfigurationException
25 {
26 Map map = (Map) object;
27 Element node = DocumentHelper.makeElement(context, getPropertyName());
28 Map.Entry entry;
29 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
30 {
31 entry = (Map.Entry) iterator.next();
32 getConfiguration().addConfigElement(entry, "property", node);
33 }
34 }
35
36 public Object loadConfig() throws ConfigurationException
37 {
38 XPath xpath = DocumentHelper.createXPath("/" + context.getName() + "/" + getPropertyName());
39 Element element = (Element) xpath.selectSingleNode(context);
40 Map.Entry entry;
41 Map map = new HashMap();
42
43 for (Iterator iterator = element.elementIterator(); iterator.hasNext();)
44 {
45 entry = (Map.Entry) getConfiguration().getConfigElement(Map.Entry.class, "property", (Element) iterator.next());
46 map.put(entry.getKey(), entry.getValue());
47 }
48 return map;
49 }
50 }