KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > MapPropertyContainer


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.users.filesystem;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.dom4j.DocumentFactory;
22 import org.dom4j.Element;
23
24 import com.openedit.users.PropertyContainer;
25 import com.openedit.users.UserManagerException;
26
27 /**
28  * Implementation of the <code>PropertyContainer</code> interface that retains
29  * a map of properties in memory.
30  *
31  * <p>Created on Dec. 26, 2003, to resolve issue OE-33
32  *
33  * @author Dennis Brown
34  */

35 public class MapPropertyContainer implements PropertyContainer
36 {
37     private static transient Log log;
38     private Log getLog()
39     {
40         if( log == null)
41         {
42             log = LogFactory.getLog(FileSystemObject.class);
43         }
44         return log;
45     }
46     protected Map JavaDoc fieldProperties;
47
48     /**
49      * Get all the properties on this user -- the real, live collection that is not read-only.
50      *
51      * @return Map
52      */

53     protected Map JavaDoc getRealProperties()
54     {
55         if (fieldProperties == null)
56         {
57             fieldProperties = new HashMap JavaDoc();
58         }
59
60         return fieldProperties;
61     }
62
63     /**
64      * @see com.openedit.users.User#getProperties()
65      */

66     public Map JavaDoc getProperties()
67     {
68         return getRealProperties();
69     }
70
71     /**
72      * @see com.openedit.users.User#get(java.lang.String)
73      */

74     public Object JavaDoc get(String JavaDoc inPropertyName)
75     {
76         return getRealProperties().get(inPropertyName);
77     }
78
79     /**
80      * @see com.openedit.users.User#put(java.lang.String, java.lang.Object)
81      */

82     public void put(String JavaDoc inPropertyName, Object JavaDoc inPropertyValue)
83         throws UserManagerException
84     {
85         //validateProperty(inPropertyName, inPropertyValue);
86
getRealProperties().put(inPropertyName, inPropertyValue);
87     }
88
89     /**
90      * @see com.openedit.users.User#put(java.util.Map)
91      */

92     public void putAll(Map JavaDoc inProperties) throws UserManagerException
93     {
94         for (Iterator JavaDoc iter = inProperties.entrySet().iterator(); iter.hasNext();)
95         {
96             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
97             //validateProperty((String) entry.getKey(), entry.getValue());
98
}
99
100         getRealProperties().putAll(inProperties);
101     }
102
103     /**
104      * @see com.openedit.users.User#remove(java.lang.String)
105      */

106     public void remove(String JavaDoc inPropertyName) throws UserManagerException
107     {
108         getRealProperties().remove(inPropertyName);
109     }
110
111     /* (non-Javadoc)
112      * @see com.openedit.users.PropertyContainer#removeAll(java.lang.String[])
113      */

114     public void removeAll(String JavaDoc[] inProperties) throws UserManagerException
115     {
116         if (inProperties == null)
117         {
118             return;
119         }
120         for (int i = 0; i < inProperties.length; i++)
121         {
122             remove(inProperties[i]);
123         }
124     }
125
126     protected Element createPropertiesElement(String JavaDoc inElementName)
127     {
128         Element propertiesElem = DocumentFactory.getInstance().createElement(inElementName);
129
130         for (Iterator JavaDoc iter = getRealProperties().entrySet().iterator(); iter.hasNext();)
131         {
132             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
133             if ( entry.getValue() != null)
134             {
135                 Element propertyElem = propertiesElem.addElement("property");
136                 propertyElem.addAttribute("name", entry.getKey().toString());
137                 propertyElem.addAttribute("value", entry.getValue().toString());
138             }
139         }
140
141         return propertiesElem;
142     }
143
144     /**
145      * Load the properties from the given element.
146      *
147      * @param inPropertiesElement The element (hopefully created via
148      * <code>createPropertiesElement</code>) from which to load the properties
149      */

150     protected void loadProperties(Element inPropertiesElement)
151     {
152         Map JavaDoc properties = new HashMap JavaDoc();
153
154         if (inPropertiesElement != null)
155         {
156             for (Iterator JavaDoc iter = inPropertiesElement.elementIterator(); iter.hasNext();)
157             {
158                 Element elem = (Element) iter.next();
159
160                 if (elem.getName().equals("property"))
161                 {
162                     String JavaDoc name = elem.attributeValue("name");
163                     String JavaDoc value = elem.attributeValue("value");
164
165                     if ((name != null) && (value != null))
166                     {
167                         properties.put(name, value);
168                     }
169                 }
170             }
171         }
172
173         fieldProperties = properties;
174     }
175
176     /**
177      * Determine whether the given name is valid. In order to be a valid name, the first character
178      * must be a letter or underscore, and each subsequent character must be a letter, digit,
179      * underscore, or period.
180      *
181      * @param inName The name to query
182      *
183      * @return <code>true</code> if the name is valid, <code>false</code> if not
184      */

185     protected boolean isValidName(String JavaDoc inName)
186     {
187         if ((inName == null) || (inName.length() == 0))
188         {
189             return false;
190         }
191
192         char c = inName.charAt(0);
193
194         if ((c == '_') || Character.isLetter(c))
195         {
196             for (int i = 1; i < inName.length(); i++)
197             {
198                 c = inName.charAt(i);
199
200                 if ((c != '_') && (c != '.') && !Character.isLetter(c) && !Character.isDigit(c))
201                 {
202                     return false;
203                 }
204             }
205         }
206
207         return true;
208     }
209
210
211     /* (non-javadoc)
212      * @see com.openedit.users.PropertyContainer#getBoolean(java.lang.String)
213      */

214     public boolean getBoolean(String JavaDoc inKey)
215     {
216         return Boolean.valueOf(getString(inKey)).booleanValue();
217     }
218
219     /* (non-javadoc)
220      * @see com.openedit.users.PropertyContainer#getString(java.lang.String)
221      */

222     public String JavaDoc getString(String JavaDoc inKey)
223     {
224         return (String JavaDoc)get(inKey);
225     }
226
227     /* (non-javadoc)
228      * @see com.openedit.users.PropertyContainer#safePut(java.lang.String, java.lang.Object)
229      */

230     public void safePut(String JavaDoc inPropertyName, Object JavaDoc inPropertyValue)
231     {
232         try
233         {
234             if ( inPropertyValue == null )
235             {
236                 remove( inPropertyName );
237             }
238             else
239             {
240                 put( inPropertyName, inPropertyValue );
241             }
242         }
243         catch ( UserManagerException ex)
244         {
245             getLog().error( ex );
246         }
247     }
248
249 }
250
Popular Tags