1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jetspeed.serializer.objects;
19
20 /***
21 * Serialized Name Value Pairs <info> <name>user.first.name</name> <value>Paul</value>
22 * </info>
23 *
24 * @author <a href="mailto:hajo@bluesunrsie.com">Hajo Birthelmer</a>
25 * @version $Id: $
26 */
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.prefs.Preferences;
30
31 import javolution.xml.XMLFormat;
32 import javolution.xml.stream.XMLStreamException;
33
34 public class JSNVPElements
35 {
36
37 private HashMap myMap = new HashMap();
38
39 public int size()
40 {
41 return myMap.size();
42
43 }
44 public JSNVPElements()
45 {
46 }
47
48
49 public HashMap getMyMap()
50 {
51 return myMap;
52 }
53
54 public void add(String key, String value)
55 {
56 myMap.put(key,value);
57 }
58
59 /***
60 * @param arg0
61 */
62 public JSNVPElements(Preferences preferences)
63 {
64 try
65 {
66 String[] strings = preferences.keys();
67 if ((strings != null) && (strings.length > 0))
68 {
69 int i = strings.length;
70 for (int j = 0; j < i; j++)
71 myMap.put(strings[j], preferences.get(strings[j], "?????"));
72 }
73
74 } catch (Exception e)
75 {
76 e.printStackTrace();
77 }
78 }
79
80 /****************************************************************************
81 * SERIALIZER
82 */
83 private static final XMLFormat XML = new XMLFormat(JSNVPElements.class)
84 {
85
86 public void write(Object o, OutputElement xml)
87 throws XMLStreamException
88 {
89 try
90 {
91 JSNVPElements g = (JSNVPElements) o;
92 Iterator _it = g.myMap.keySet().iterator();
93 while (_it.hasNext())
94 {
95 String _key = (String) _it.next();
96 JSNVPElement elem = new JSNVPElement(_key,(String)g.myMap.get(_key));
97 xml.add(elem,"preference",JSNVPElement.class);
98 }
99 } catch (Exception e)
100 {
101 e.printStackTrace();
102 }
103 }
104
105 public void read(InputElement xml, Object o)
106 {
107
108 try
109 {
110 JSNVPElements g = (JSNVPElements) o;
111 while (xml.hasNext())
112 {
113 JSNVPElement elem = (JSNVPElement)xml.get("preference",JSNVPElement.class);
114 g.myMap.put(elem.getKey(), elem.getValue());
115 }
116 } catch (Exception e)
117 {
118 /***
119 * while annoying invalid entries in the file should be
120 * just disregarded
121 */
122 e.printStackTrace();
123 }
124 }
125 };
126
127
128
129 }