1 | import org.apache.tools.ant.*;
|
---|
2 | import org.apache.tools.ant.taskdefs.*;
|
---|
3 | import java.util.*;
|
---|
4 | import java.io.*;
|
---|
5 | import java.util.regex.*;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Gets looks up a property in a properties file and returns its value
|
---|
9 | */
|
---|
10 | public class GetPropertyValue extends Task {
|
---|
11 |
|
---|
12 | private File propertiesFile = null;
|
---|
13 | private String propertyName = null;
|
---|
14 | private String outputProperty = null;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * for testing
|
---|
19 |
|
---|
20 | public static void main( String[] args ) {
|
---|
21 |
|
---|
22 | GetPropertyValue gpv = new GetPropertyValue();
|
---|
23 | gpv.setPropertiesFile( new File(args[0]) );
|
---|
24 | gpv.setPropertyName( args[1] );
|
---|
25 | gpv.setOutputProperty( args[2] );
|
---|
26 | gpv.execute();
|
---|
27 |
|
---|
28 | }
|
---|
29 | */
|
---|
30 |
|
---|
31 | public void execute() {
|
---|
32 |
|
---|
33 | if ( propertiesFile == null ) {
|
---|
34 | throw new BuildException( "Error - No properties file specified !!" );
|
---|
35 | }
|
---|
36 |
|
---|
37 | if ( propertyName == null ) {
|
---|
38 | throw new BuildException( "Error - No property name specified !!" );
|
---|
39 | }
|
---|
40 |
|
---|
41 | if ( outputProperty == null ) {
|
---|
42 | throw new BuildException( "Error - No output property specified !!" );
|
---|
43 | }
|
---|
44 |
|
---|
45 | //compile the regex we're looking for
|
---|
46 | //System.err.println( "compile the regex we're looking for" );
|
---|
47 | String regex = "^" + propertyName.replaceAll("\\.","\\\\.") + "[:=]\\s*(.*)$";
|
---|
48 | //System.err.println( "regex: " + regex );
|
---|
49 | Pattern wantedLine = Pattern.compile( regex );
|
---|
50 |
|
---|
51 | //create the input stream
|
---|
52 | //System.err.println( "create the input stream" );
|
---|
53 | BufferedReader in = null;
|
---|
54 | try {
|
---|
55 | in = new BufferedReader(new FileReader( propertiesFile ));
|
---|
56 | } catch ( Exception e ) {
|
---|
57 | throw new BuildException( "Error - couldn't open the properties file " + propertiesFile );
|
---|
58 | }
|
---|
59 |
|
---|
60 | //System.err.println( "pass over the file" );
|
---|
61 | //pass over the file
|
---|
62 | String line, value = null;
|
---|
63 |
|
---|
64 | try {
|
---|
65 |
|
---|
66 | boolean found = false;
|
---|
67 | while ( (line = in.readLine()) != null && !found ) {
|
---|
68 |
|
---|
69 | Matcher matcher = wantedLine.matcher( line );
|
---|
70 | if ( matcher.matches() ) {
|
---|
71 | //found the property
|
---|
72 | //System.err.println( "found the property" );
|
---|
73 | value = matcher.group(1);
|
---|
74 | value = value.replaceAll( "#.*", "" ).trim();
|
---|
75 | found = true;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | //close the input stream
|
---|
82 | //System.err.println( "close the input stream" );
|
---|
83 | in.close();
|
---|
84 |
|
---|
85 | } catch ( Exception e ) {
|
---|
86 | throw new BuildException( "Error - couldn't read from the properties file" );
|
---|
87 | }
|
---|
88 |
|
---|
89 | //set the property in ant
|
---|
90 | if ( value != null ) {
|
---|
91 | //System.err.println( "set the property in ant" );
|
---|
92 | this.getProject().setProperty(outputProperty, value);
|
---|
93 | }
|
---|
94 |
|
---|
95 | //System.err.println( "done" );
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | public void setPropertiesFile(File file) {
|
---|
101 | this.propertiesFile = file;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void setPropertyName(String name) {
|
---|
105 | this.propertyName = name;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setOutputProperty(String property) {
|
---|
109 | this.outputProperty = property;
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|