1 58 59 package org.oddjob.arooa; 60 61 import java.io.Serializable ; 62 import java.util.Enumeration ; 63 import java.util.Vector ; 64 65 import org.oddjob.arooa.reflect.IntrospectionHelper; 66 import org.oddjob.arooa.reflect.RegistryLookup; 67 68 69 74 75 public class PropertyHelper implements Serializable { 76 77 Vector fragments = new Vector (); 78 Vector propertyRefs = new Vector (); 79 private final String value; 80 81 84 public PropertyHelper(String value) { 85 this.value = value; 86 if (value == null) { 87 return; 88 } 89 parsePropertyString(value, fragments, propertyRefs); 90 } 91 92 97 public String getValue() { 98 return value; 99 } 100 101 118 public Object replaceProperties(RegistryLookup registry, boolean strictSubstitution) 119 throws ArooaException { 120 121 if (propertyRefs.size() == 1 && fragments.size() == 1) { 123 String propertyName = (String )propertyRefs.elementAt(0); 124 Object replacement = registry.getProperty(propertyName); 125 if (replacement == null && strictSubstitution) { 126 throw new NullPointerException ("Value must not be null [" + propertyName + "]" ); 127 } 128 return replacement; 129 } 130 131 StringBuffer sb = new StringBuffer (); 132 Enumeration i = fragments.elements(); 133 Enumeration j = propertyRefs.elements(); 134 135 while (i.hasMoreElements()) { 136 String fragment = (String ) i.nextElement(); 137 if (fragment == null) { 138 String propertyName = (String ) j.nextElement(); 139 Object possibleProxy = registry.getProperty(propertyName); 140 Object replacement = IntrospectionHelper.valueFor(possibleProxy, 141 String .class); 142 if (replacement == null) { 143 if (strictSubstitution) { 144 throw new NullPointerException ("Value must not be null [" + propertyName + "] in [" + value + "]" ); 145 } 146 else { 147 replacement = ""; 148 } 149 } 150 151 fragment = (replacement != null) 152 ? replacement.toString() 153 : "${" + propertyName + "}"; 154 } 155 sb.append(fragment); 156 } 157 158 return sb.toString(); 159 } 160 161 180 181 private void parsePropertyString(String value, Vector fragments, 182 Vector propertyRefs) 183 throws ArooaException { 184 int prev = 0; 185 int pos; 186 while ((pos = value.indexOf("$", prev)) >= 0) { 188 189 if (pos > 0) { 194 fragments.addElement(value.substring(prev, pos)); 195 } 196 if (pos == (value.length() - 1)) { 199 fragments.addElement("$"); 200 prev = pos + 1; 201 } else if (value.charAt(pos + 1) != '{') { 202 208 if (value.charAt(pos + 1) == '$') { 209 fragments.addElement("$"); 211 prev = pos + 2; 212 } else { 213 fragments.addElement(value.substring(pos, pos + 2)); 215 prev = pos + 2; 216 } 217 218 } else { 219 int endName = value.indexOf('}', pos); 221 if (endName < 0) { 222 throw new ArooaException("Syntax error in property: " 223 + value); 224 } 225 String propertyName = value.substring(pos + 2, endName); 226 fragments.addElement(null); 227 propertyRefs.addElement(propertyName); 228 prev = endName + 1; 229 } 230 } 231 if (prev < value.length()) { 234 fragments.addElement(value.substring(prev)); 235 } 236 } 237 238 243 public boolean isConstant() { 244 245 return propertyRefs.size() == 0; 246 } 247 248 public String toString() { 249 return value; 250 } 251 } 252 | Popular Tags |