KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import com.openedit.users.InvalidPropertyNameException;
23 import com.openedit.users.PropertyContainer;
24 import com.openedit.users.UnsupportedPropertyTypeException;
25 import com.openedit.users.UserManagerException;
26
27
28 /**
29  * This class represents an object which is backed by an XML file. It implements {@link
30  * PropertyContainer} as a simple <code>Map</code> which only permits String-valued properties.
31  *
32  * @author Matt Avery
33  */

34 public abstract class FileSystemObject implements PropertyContainer, Serializable JavaDoc
35 {
36     private transient static Log log = null;
37     private Log getLog()
38     {
39         if( log == null)
40         {
41             log = LogFactory.getLog(FileSystemObject.class);
42         }
43         return log;
44     }
45     
46     protected PropertyContainer fieldPropertyContainer;
47     //protected Element fieldRootElement;
48
//protected String fieldFile;
49
protected Date JavaDoc fieldCreationDate;
50
51     
52     public FileSystemObject()
53     {
54     }
55     
56     public PropertyContainer getPropertyContainer()
57     {
58         if( fieldPropertyContainer == null)
59         {
60             fieldPropertyContainer = new MapPropertyContainer();
61         }
62         return fieldPropertyContainer;
63     }
64     public void setPropertyContainer(PropertyContainer inData )
65     {
66         fieldPropertyContainer = inData;
67     }
68     /**
69      * Get all the properties on this object as a read-only map.
70      *
71      * @return A read-only map of all the properties on this object
72      */

73     public Map JavaDoc getProperties()
74     {
75         return getPropertyContainer().getProperties();
76     }
77
78     /**
79      * Get the value of the given property of this object.
80      *
81      * @param inPropertyName The property name
82      *
83      * @return The property value, or <code>null</code> if the property does not exist on this
84      * object
85      */

86     public Object JavaDoc get( String JavaDoc inPropertyName )
87     {
88         return getPropertyContainer().get( inPropertyName );
89     }
90     
91     public Date JavaDoc getCreationDate()
92     {
93         return fieldCreationDate;
94     }
95     protected void setCreationDate(Date JavaDoc inDate)
96     {
97         fieldCreationDate = inDate;
98     }
99     /**
100      * Set the given property of this object. Note that depending on the implementation, only
101      * certain types may be supported for the property value. All implementations must support at
102      * least String. However, it is recommended that implementations support at least the
103      * following types:
104      *
105      * <ul>
106      * <li>
107      * Boolean
108      * </li>
109      * <li>
110      * Double
111      * </li>
112      * <li>
113      * Integer
114      * </li>
115      * <li>
116      * String
117      * </li>
118      * <li>
119      * Object[]
120      * </li>
121      * </ul>
122      *
123      * Property names must conform to the regular expression <code>[A-Za-z_][A-Za-z0-9_.]</code>.
124      * In other words, the first character must be an underscore or letter; and each subsequent
125      * character must be an underscore, period, letter, or digit.
126      *
127      * @param inPropertyName The property name
128      * @param inPropertyValue The property value
129      *
130      * @throws InvalidPropertyNameException If the property name was invalid
131      * @throws UnsupportedPropertyTypeException If the property value was of an unsupported type
132      * @throws UserManagerException If the property could not be set
133      */

134     public void put( String JavaDoc inPropertyName, Object JavaDoc inPropertyValue )
135         throws UserManagerException
136     {
137         getPropertyContainer().put( inPropertyName, inPropertyValue );
138     }
139
140     /**
141      * Add all the specified properties to this object. Note that the keys in the given map must
142      * be strings (property names), and that the values must satisfy the implementation's
143      * requirements for property values.
144      *
145      * @param inProperties The properties to set
146      *
147      * @throws UserManagerException If any of the properties could not be set
148      *
149      * @see #put(String, Object)
150      */

151     public void putAll( Map JavaDoc inProperties ) throws UserManagerException
152     {
153         getPropertyContainer().putAll( inProperties );
154     }
155
156     /**
157      * Remove the given property from this object. If no such property exists, this method will do
158      * nothing.
159      *
160      * @param inPropertyName The name of the property to remove
161      *
162      * @throws UserManagerException If the property exists and could not be removed
163      */

164     public void remove( String JavaDoc inPropertyName ) throws UserManagerException
165     {
166         getPropertyContainer().remove( inPropertyName );
167     }
168     
169     /**
170      * Remove the given properties from this object. If no such property exists, this method will do
171      * nothing.
172      *
173      * @param inProperties The names of the properties to remove
174      *
175      * @throws UserManagerException If the property exists and could not be removed
176      */

177     public void removeAll( String JavaDoc[] inProperties ) throws UserManagerException
178     {
179         getPropertyContainer().removeAll( inProperties );
180     }
181     
182     public boolean getBoolean( String JavaDoc inPropertyName )
183     {
184         return Boolean.valueOf( getString( inPropertyName ) ).booleanValue();
185     }
186     
187     public String JavaDoc getString( String JavaDoc inPropertyName )
188     {
189         return (String JavaDoc) get( inPropertyName );
190     }
191     
192     public void safePut( String JavaDoc inKey, Object JavaDoc inValue )
193     {
194         try
195         {
196             if ( inValue == null)
197             {
198                 getPropertyContainer().remove( inKey );
199             }
200             else
201             {
202                 Object JavaDoc value = inValue;
203                 if ( inValue instanceof String JavaDoc )
204                 {
205                     value = ( (String JavaDoc) inValue ).trim();
206                 }
207                 getPropertyContainer().put( inKey, value );
208             }
209         }
210         catch ( UserManagerException ex)
211         {
212             getLog().error( ex );
213         }
214     }
215     
216 }
217
Popular Tags