1 package com.atlassian.config.xml;
2
3 import com.atlassian.config.ConfigurationException;
4 import org.dom4j.Element;
5
6 import java.util.Map;
7
8 public class Dom4jXmlMapEntryConfigElement extends Dom4jXmlStringConfigElement
9 {
10 public Dom4jXmlMapEntryConfigElement(String name, Element context, AbstractDom4jXmlConfigurationPersister config)
11 {
12 super(name, context, config);
13 }
14
15 public Class getObjectClass()
16 {
17 return Map.Entry.class;
18 }
19
20 public void saveConfig(Object object) throws ConfigurationException
21 {
22 Map.Entry entry = (Map.Entry) object;
23 String name = entry.getKey().toString();
24
25 if (entry.getValue() == null)
26 return;
27
28 Element element = (Element) context.selectSingleNode(getPropertyName() + "[@name='" + name + "']");
29 if (element == null)
30 {
31 element = context.addElement(getPropertyName());
32 element.addAttribute("name", name);
33 }
34 else
35 {
36 element.clearContent();
37 }
38 if (useCData)
39 {
40 element.addCDATA(entry.getValue().toString());
41 }
42 else
43 {
44 element.setText(entry.getValue().toString());
45 }
46 }
47
48 public Object loadConfig() throws ConfigurationException
49 {
50 String key = context.attribute("name").getValue();
51 if (key == null)
52 {
53 throw new ConfigurationException("The attribute 'name' must be specified for element: " + getPropertyName());
54 }
55 return new Entry(key, context.getText());
56
57 }
58
59 private class Entry implements Map.Entry
60 {
61 private Object key;
62 private Object value;
63
64 public Entry(Object key, Object value)
65 {
66 this.key = key;
67 this.value = value;
68 }
69
70 public Object getKey()
71 {
72 return key;
73 }
74
75 public Object getValue()
76 {
77 return value;
78 }
79
80 public Object setValue(Object value)
81 {
82 this.value = value;
83 return this.value;
84 }
85 }
86 }