View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }