| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.property; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.io.FileInputStream; |
| 20 |
import java.io.IOException; |
| 21 |
import java.lang.reflect.Field; |
| 22 |
import java.util.Enumeration; |
| 23 |
import java.util.Hashtable; |
| 24 |
import java.util.Properties; |
| 25 |
import java.util.Vector; |
| 26 |
|
| 27 |
import org.apache.tools.ant.BuildException; |
| 28 |
import org.apache.tools.ant.Project; |
| 29 |
import org.apache.tools.ant.ProjectHelper; |
| 30 |
import org.apache.tools.ant.Task; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class Variable extends Task { |
| 50 |
|
| 51 |
|
| 52 |
private String value = ""; |
| 53 |
private String name = null; |
| 54 |
private File file = null; |
| 55 |
private boolean remove = false; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public void setName( String name ) { |
| 64 |
this.name = name; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void setValue( String value ) { |
| 74 |
this.value = value; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void setFile( File file ) { |
| 84 |
this.file = file; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public void setUnset( boolean b ) { |
| 95 |
remove = b; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void execute() throws BuildException { |
| 105 |
if ( remove ) { |
| 106 |
if ( name == null || name.equals( "" ) ) { |
| 107 |
throw new BuildException( "The 'name' attribute is required with 'unset'." ); |
| 108 |
} |
| 109 |
removeProperty( name ); |
| 110 |
return ; |
| 111 |
} |
| 112 |
if ( file == null ) { |
| 113 |
|
| 114 |
if ( name == null || name.equals( "" ) ) { |
| 115 |
throw new BuildException( "The 'name' attribute is required." ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
if ( value == null ) { |
| 120 |
value = ""; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
value = getProject().replaceProperties( value ); |
| 126 |
|
| 127 |
|
| 128 |
forceProperty( name, value ); |
| 129 |
} |
| 130 |
else { |
| 131 |
if ( !file.exists() ) { |
| 132 |
throw new BuildException( file.getAbsolutePath() + " does not exists." ); |
| 133 |
} |
| 134 |
loadFile( file ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private void removeProperty( String name ) { |
| 143 |
Hashtable properties = null; |
| 144 |
|
| 145 |
try { |
| 146 |
properties = ( Hashtable ) getValue( getProject(), "properties" ); |
| 147 |
if ( properties != null ) { |
| 148 |
properties.remove( name ); |
| 149 |
} |
| 150 |
} |
| 151 |
catch ( Exception e ) { |
| 152 |
|
| 153 |
} |
| 154 |
try { |
| 155 |
properties = ( Hashtable ) getValue( getProject(), "userProperties" ); |
| 156 |
if ( properties != null ) { |
| 157 |
properties.remove( name ); |
| 158 |
} |
| 159 |
} |
| 160 |
catch ( Exception e ) { |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
try { |
| 167 |
Object property_helper = getProject().getReference( "ant.PropertyHelper" ); |
| 168 |
if ( property_helper != null ) { |
| 169 |
try { |
| 170 |
properties = ( Hashtable ) getValue( property_helper, "properties" ); |
| 171 |
if ( properties != null ) { |
| 172 |
properties.remove( name ); |
| 173 |
} |
| 174 |
} |
| 175 |
catch ( Exception e ) { |
| 176 |
|
| 177 |
} |
| 178 |
try { |
| 179 |
properties = ( Hashtable ) getValue( property_helper, "userProperties" ); |
| 180 |
if ( properties != null ) { |
| 181 |
properties.remove( name ); |
| 182 |
} |
| 183 |
} |
| 184 |
catch ( Exception e ) { |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
catch ( Exception e ) { |
| 190 |
|
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
private void forceProperty( String name, String value ) { |
| 195 |
try { |
| 196 |
Hashtable properties = ( Hashtable ) getValue( getProject(), "properties" ); |
| 197 |
if ( properties == null ) { |
| 198 |
getProject().setUserProperty( name, value ); |
| 199 |
} |
| 200 |
else { |
| 201 |
properties.put( name, value ); |
| 202 |
} |
| 203 |
} |
| 204 |
catch ( Exception e ) { |
| 205 |
getProject().setUserProperty( name, value ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private Field getField( Class thisClass, String fieldName ) throws NoSuchFieldException { |
| 220 |
if ( thisClass == null ) { |
| 221 |
throw new NoSuchFieldException( "Invalid field : " + fieldName ); |
| 222 |
} |
| 223 |
try { |
| 224 |
return thisClass.getDeclaredField( fieldName ); |
| 225 |
} |
| 226 |
catch ( NoSuchFieldException e ) { |
| 227 |
return getField( thisClass.getSuperclass(), fieldName ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private Object getValue( Object instance, String fieldName ) |
| 244 |
throws IllegalAccessException, NoSuchFieldException { |
| 245 |
Field field = getField( instance.getClass(), fieldName ); |
| 246 |
field.setAccessible( true ); |
| 247 |
return field.get( instance ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private void loadFile( File file ) throws BuildException { |
| 258 |
Properties props = new Properties(); |
| 259 |
try { |
| 260 |
if ( file.exists() ) { |
| 261 |
FileInputStream fis = new FileInputStream( file ); |
| 262 |
try { |
| 263 |
props.load( fis ); |
| 264 |
} |
| 265 |
finally { |
| 266 |
if ( fis != null ) { |
| 267 |
fis.close(); |
| 268 |
} |
| 269 |
} |
| 270 |
addProperties( props ); |
| 271 |
} |
| 272 |
else { |
| 273 |
log( "Unable to find property file: " + file.getAbsolutePath(), |
| 274 |
Project.MSG_VERBOSE ); |
| 275 |
} |
| 276 |
} |
| 277 |
catch ( IOException ex ) { |
| 278 |
throw new BuildException( ex, location ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected void addProperties( Properties props ) { |
| 289 |
resolveAllProperties( props ); |
| 290 |
Enumeration e = props.keys(); |
| 291 |
while ( e.hasMoreElements() ) { |
| 292 |
String name = ( String ) e.nextElement(); |
| 293 |
String value = props.getProperty( name ); |
| 294 |
forceProperty( name, value ); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private void resolveAllProperties( Properties props ) throws BuildException { |
| 306 |
for ( Enumeration e = props.keys(); e.hasMoreElements(); ) { |
| 307 |
String name = ( String ) e.nextElement(); |
| 308 |
String value = props.getProperty( name ); |
| 309 |
|
| 310 |
boolean resolved = false; |
| 311 |
while ( !resolved ) { |
| 312 |
Vector fragments = new Vector(); |
| 313 |
Vector propertyRefs = new Vector(); |
| 314 |
ProjectHelper.parsePropertyString( value, fragments, |
| 315 |
propertyRefs ); |
| 316 |
|
| 317 |
resolved = true; |
| 318 |
if ( propertyRefs.size() != 0 ) { |
| 319 |
StringBuffer sb = new StringBuffer(); |
| 320 |
Enumeration i = fragments.elements(); |
| 321 |
Enumeration j = propertyRefs.elements(); |
| 322 |
while ( i.hasMoreElements() ) { |
| 323 |
String fragment = ( String ) i.nextElement(); |
| 324 |
if ( fragment == null ) { |
| 325 |
String propertyName = ( String ) j.nextElement(); |
| 326 |
if ( propertyName.equals( name ) ) { |
| 327 |
throw new BuildException( "Property " + name |
| 328 |
+ " was circularly " |
| 329 |
+ "defined." ); |
| 330 |
} |
| 331 |
fragment = getProject().getProperty( propertyName ); |
| 332 |
if ( fragment == null ) { |
| 333 |
if ( props.containsKey( propertyName ) ) { |
| 334 |
fragment = props.getProperty( propertyName ); |
| 335 |
resolved = false; |
| 336 |
} |
| 337 |
else { |
| 338 |
fragment = "${" + propertyName + "}"; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
sb.append( fragment ); |
| 343 |
} |
| 344 |
value = sb.toString(); |
| 345 |
props.put( name, value ); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|