1 | /*
|
---|
2 | * GetPropertyValue.java
|
---|
3 | * A task to get the value of a given property from a given properties file
|
---|
4 | *
|
---|
5 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 | package org.greenstone.anttasks;
|
---|
22 |
|
---|
23 | import org.apache.tools.ant.*;
|
---|
24 | import org.apache.tools.ant.taskdefs.*;
|
---|
25 | import java.util.*;
|
---|
26 | import java.io.*;
|
---|
27 | import java.util.regex.*;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Gets looks up a property in a properties file and returns its value
|
---|
31 | */
|
---|
32 | public class GetPropertyValue extends Task {
|
---|
33 |
|
---|
34 | private File propertiesFile = null;
|
---|
35 | private String propertyName = null;
|
---|
36 | private String outputProperty = null;
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * for testing
|
---|
41 |
|
---|
42 | public static void main( String[] args ) {
|
---|
43 |
|
---|
44 | GetPropertyValue gpv = new GetPropertyValue();
|
---|
45 | gpv.setPropertiesFile( new File(args[0]) );
|
---|
46 | gpv.setPropertyName( args[1] );
|
---|
47 | gpv.setOutputProperty( args[2] );
|
---|
48 | gpv.execute();
|
---|
49 |
|
---|
50 | }
|
---|
51 | */
|
---|
52 |
|
---|
53 | public void execute() {
|
---|
54 |
|
---|
55 | if ( propertiesFile == null ) {
|
---|
56 | throw new BuildException( "Error - No properties file specified !!" );
|
---|
57 | }
|
---|
58 |
|
---|
59 | if ( propertyName == null ) {
|
---|
60 | throw new BuildException( "Error - No property name specified !!" );
|
---|
61 | }
|
---|
62 |
|
---|
63 | if ( outputProperty == null ) {
|
---|
64 | throw new BuildException( "Error - No output property specified !!" );
|
---|
65 | }
|
---|
66 |
|
---|
67 | //compile the regex we're looking for
|
---|
68 | //System.err.println( "compile the regex we're looking for" );
|
---|
69 | String regex = "^" + propertyName.replaceAll("\\.","\\\\.") + "[:=]\\s*(.*)$";
|
---|
70 | //System.err.println( "regex: " + regex );
|
---|
71 | Pattern wantedLine = Pattern.compile( regex );
|
---|
72 |
|
---|
73 | //create the input stream
|
---|
74 | //System.err.println( "create the input stream" );
|
---|
75 | BufferedReader in = null;
|
---|
76 | try {
|
---|
77 | in = new BufferedReader(new FileReader( propertiesFile ));
|
---|
78 | } catch ( Exception e ) {
|
---|
79 | throw new BuildException( "Error - couldn't open the properties file " + propertiesFile );
|
---|
80 | }
|
---|
81 |
|
---|
82 | //System.err.println( "pass over the file" );
|
---|
83 | //pass over the file
|
---|
84 | String line, value = null;
|
---|
85 |
|
---|
86 | try {
|
---|
87 |
|
---|
88 | boolean found = false;
|
---|
89 | while ( (line = in.readLine()) != null && !found ) {
|
---|
90 |
|
---|
91 | Matcher matcher = wantedLine.matcher( line );
|
---|
92 | if ( matcher.matches() ) {
|
---|
93 | //found the property
|
---|
94 | //System.err.println( "found the property" );
|
---|
95 | value = matcher.group(1);
|
---|
96 | value = value.replaceAll( "#.*", "" ).trim();
|
---|
97 | found = true;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | //close the input stream
|
---|
104 | //System.err.println( "close the input stream" );
|
---|
105 | in.close();
|
---|
106 |
|
---|
107 | } catch ( Exception e ) {
|
---|
108 | throw new BuildException( "Error - couldn't read from the properties file" );
|
---|
109 | }
|
---|
110 |
|
---|
111 | //set the property in ant
|
---|
112 | if ( value != null ) {
|
---|
113 | //System.err.println( "set the property in ant" );
|
---|
114 | this.getProject().setProperty(outputProperty, value);
|
---|
115 | }
|
---|
116 |
|
---|
117 | //System.err.println( "done" );
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | public void setPropertiesFile(File file) {
|
---|
123 | this.propertiesFile = file;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void setPropertyName(String name) {
|
---|
127 | this.propertyName = name;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void setOutputProperty(String property) {
|
---|
131 | this.outputProperty = property;
|
---|
132 | }
|
---|
133 |
|
---|
134 | }
|
---|