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.sax.Attributes;
33  import javolution.xml.stream.XMLStreamException;
34  
35  import org.apache.commons.lang.StringEscapeUtils;
36  
37  public class JSNameValuePairs
38  {
39  
40      private HashMap myMap = new HashMap();
41  
42      public int size()
43      {
44      	return myMap.size();
45      	
46      }
47      public JSNameValuePairs()
48      {
49      }
50      
51   
52      public HashMap getMyMap()
53  	{
54  		return myMap;
55  	}
56  
57      public void add(String key, String value)
58      {
59      	myMap.put(key,value);
60      }
61  
62  	/***
63       * @param arg0
64       */
65      public JSNameValuePairs(Preferences preferences)
66      {
67          try
68          {
69              String[] strings = preferences.keys();
70              if ((strings != null) && (strings.length > 0))
71              {
72                  int i = strings.length;
73                  for (int j = 0; j < i; j++)
74                      myMap.put(strings[j], preferences.get(strings[j], "?????"));
75              }
76  
77          } catch (Exception e)
78          {
79              e.printStackTrace();
80          }
81      }
82  
83      /****************************************************************************
84       * SERIALIZER
85       */
86      private static final XMLFormat XML = new XMLFormat(JSNameValuePairs.class)
87      {
88  
89          public void write(Object o, OutputElement xml)
90                  throws XMLStreamException
91          {
92              try
93              {
94                  JSNameValuePairs g = (JSNameValuePairs) o;
95                  Iterator _it = g.myMap.keySet().iterator();
96                  while (_it.hasNext())
97                  {
98                      String _key = (String) _it.next();
99                      // xml.add((String) g.get(_key), _key, String.class);
100                     xml.setAttribute(_key, (String) g.myMap.get(_key));
101                 }
102             } catch (Exception e)
103             {
104                 e.printStackTrace();
105             }
106         }
107 
108         public void read(InputElement xml, Object o)
109         {
110 
111             try
112             {
113                 JSNameValuePairs g = (JSNameValuePairs) o;
114                 Attributes attribs = xml.getAttributes();
115                 int len = attribs.getLength();
116 
117                 for (int i = 0; i < len; i++)
118                 {
119                     try
120                     {
121                         String _key = StringEscapeUtils.unescapeHtml(attribs.getLocalName(i).toString());
122                         String _value = StringEscapeUtils.unescapeHtml(attribs.getValue(i).toString());
123                         g.myMap.put(_key, _value);
124                     } catch (Exception e)
125                     {
126                         /***
127                          * while annoying invalid entries in the file should be
128                          * just disregarded
129                          */
130                         e.printStackTrace();
131                     }
132                 }
133             } catch (Exception e)
134             {
135                 e.printStackTrace();
136             }
137         }
138     };
139 
140 }