1 12 13 package com.openedit.users.filesystem; 14 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 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 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 fieldProperties; 47 48 53 protected Map getRealProperties() 54 { 55 if (fieldProperties == null) 56 { 57 fieldProperties = new HashMap (); 58 } 59 60 return fieldProperties; 61 } 62 63 66 public Map getProperties() 67 { 68 return getRealProperties(); 69 } 70 71 74 public Object get(String inPropertyName) 75 { 76 return getRealProperties().get(inPropertyName); 77 } 78 79 82 public void put(String inPropertyName, Object inPropertyValue) 83 throws UserManagerException 84 { 85 getRealProperties().put(inPropertyName, inPropertyValue); 87 } 88 89 92 public void putAll(Map inProperties) throws UserManagerException 93 { 94 for (Iterator iter = inProperties.entrySet().iterator(); iter.hasNext();) 95 { 96 Map.Entry entry = (Map.Entry ) iter.next(); 97 } 99 100 getRealProperties().putAll(inProperties); 101 } 102 103 106 public void remove(String inPropertyName) throws UserManagerException 107 { 108 getRealProperties().remove(inPropertyName); 109 } 110 111 114 public void removeAll(String [] 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 inElementName) 127 { 128 Element propertiesElem = DocumentFactory.getInstance().createElement(inElementName); 129 130 for (Iterator iter = getRealProperties().entrySet().iterator(); iter.hasNext();) 131 { 132 Map.Entry entry = (Map.Entry ) 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 150 protected void loadProperties(Element inPropertiesElement) 151 { 152 Map properties = new HashMap (); 153 154 if (inPropertiesElement != null) 155 { 156 for (Iterator iter = inPropertiesElement.elementIterator(); iter.hasNext();) 157 { 158 Element elem = (Element) iter.next(); 159 160 if (elem.getName().equals("property")) 161 { 162 String name = elem.attributeValue("name"); 163 String 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 185 protected boolean isValidName(String 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 214 public boolean getBoolean(String inKey) 215 { 216 return Boolean.valueOf(getString(inKey)).booleanValue(); 217 } 218 219 222 public String getString(String inKey) 223 { 224 return (String )get(inKey); 225 } 226 227 230 public void safePut(String inPropertyName, Object 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 |