View Javadoc

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   /**
9    * Created by IntelliJ IDEA.
10   * User: ROSS
11   * Date: 16/03/2004
12   * Time: 13:00:44
13   * To change this template use File | Settings | File Templates.
14   */
15  public class Dom4jXmlMapEntryConfigElement extends Dom4jXmlStringConfigElement
16  {
17      public Dom4jXmlMapEntryConfigElement(String name, Element context, AbstractDom4jXmlConfigurationPersister config)
18      {
19          super(name, context, config);
20      }
21  
22      public Class getObjectClass()
23      {
24          return Map.Entry.class;
25      }
26  
27      public void saveConfig(Object object) throws ConfigurationException
28      {
29          Map.Entry entry = (Map.Entry) object;
30          String name = entry.getKey().toString();
31  
32          if (entry.getValue() == null)
33              return;
34  
35          Element element = (Element) context.selectSingleNode(getPropertyName() + "[@name='" + name + "']");
36          if (element == null)
37          {
38              element = context.addElement(getPropertyName());
39              element.addAttribute("name", name);
40          }
41          else
42          {
43              element.clearContent();
44          }
45          if (useCData)
46          {
47              element.addCDATA(entry.getValue().toString());
48          }
49          else
50          {
51              element.setText(entry.getValue().toString());
52          }
53      }
54  
55      public Object loadConfig() throws ConfigurationException
56      {
57          String key = context.attribute("name").getValue();
58          if (key == null)
59          {
60              throw new ConfigurationException("The attribute 'name' must be specified for element: " + getPropertyName());
61          }
62          return new Entry(key, context.getText());
63  
64      }
65  
66      private class Entry implements Map.Entry
67      {
68          private Object key;
69          private Object value;
70  
71          public Entry(Object key, Object value)
72          {
73              this.key = key;
74              this.value = value;
75          }
76  
77          public Object getKey()
78          {
79              return key;
80          }
81  
82          public Object getValue()
83          {
84              return value;
85          }
86  
87          public Object setValue(Object value)
88          {
89              this.value = value;
90              return this.value;
91          }
92      }
93  }