~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/KeySubst.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer. 
 
13
 *
 
14
 * 2. Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in
 
16
 *    the documentation and/or other materials provided with the
 
17
 *    distribution.
 
18
 *
 
19
 * 3. The end-user documentation included with the redistribution, if
 
20
 *    any, must include the following acknowlegement:  
 
21
 *       "This product includes software developed by the 
 
22
 *        Apache Software Foundation (http://www.apache.org/)."
 
23
 *    Alternately, this acknowlegement may appear in the software itself,
 
24
 *    if and wherever such third-party acknowlegements normally appear.
 
25
 *
 
26
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 
27
 *    Foundation" must not be used to endorse or promote products derived
 
28
 *    from this software without prior written permission. For written 
 
29
 *    permission, please contact apache@apache.org.
 
30
 *
 
31
 * 5. Products derived from this software may not be called "Apache"
 
32
 *    nor may "Apache" appear in their names without prior written
 
33
 *    permission of the Apache Group.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
36
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
37
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
38
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
41
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
42
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
43
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
44
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
45
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
49
 * This software consists of voluntary contributions made by many
 
50
 * individuals on behalf of the Apache Software Foundation.  For more
 
51
 * information on the Apache Software Foundation, please see
 
52
 * <http://www.apache.org/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant.taskdefs;
 
56
 
 
57
import org.apache.tools.ant.*;
 
58
import java.io.*;
 
59
import java.util.*;
 
60
 
 
61
/**
 
62
 * Keyword substitution. Input file is written to output file.
 
63
 * Do not make input file same as output file.
 
64
 * Keywords in input files look like this: @foo@. See the docs for the 
 
65
 * setKeys method to understand how to do the substitutions.
 
66
 *
 
67
 * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
 
68
 *
 
69
 * @deprecated KeySubst is deprecated. Use Filter + CopyDir instead.
 
70
 */
 
71
public class KeySubst extends Task {
 
72
    private File source = null;
 
73
    private File dest = null;
 
74
    private String sep = "*";
 
75
    private Hashtable replacements = new Hashtable();
 
76
    
 
77
    /**
 
78
        Do the execution.
 
79
    */
 
80
    public void execute() throws BuildException {
 
81
        log("!! KeySubst is deprecated. Use Filter + CopyDir instead. !!");
 
82
        log("Performing Substitions");
 
83
        if ( source == null || dest == null ) {
 
84
            log("Source and destinations must not be null");
 
85
            return;            
 
86
        }
 
87
        BufferedReader br = null;
 
88
        BufferedWriter bw = null;
 
89
        try {
 
90
            br = new BufferedReader(new FileReader(source));
 
91
            dest.delete();
 
92
            bw = new BufferedWriter(new FileWriter(dest));
 
93
 
 
94
            String line = null;
 
95
            String newline = null;
 
96
            int length;
 
97
            line = br.readLine();
 
98
            while (line != null) {
 
99
                if ( line.length() == 0 ) {
 
100
                    bw.newLine();
 
101
                } else {
 
102
                    newline = KeySubst.replace ( line, replacements );
 
103
                    bw.write ( newline );
 
104
                    bw.newLine();
 
105
                }
 
106
                line = br.readLine();
 
107
            }
 
108
            bw.flush();
 
109
            bw.close();
 
110
            br.close();
 
111
        } catch (IOException ioe) {
 
112
            ioe.printStackTrace();
 
113
        }       
 
114
    }
 
115
    /**
 
116
        Set the source file.
 
117
    */
 
118
    public void setSrc(File s) {
 
119
        this.source = s;
 
120
    }
 
121
 
 
122
    /**
 
123
        Set the destination file.
 
124
    */
 
125
    public void setDest(File dest) {
 
126
        this.dest = dest;
 
127
    }
 
128
 
 
129
    /**
 
130
        Sets the seperator between name=value arguments
 
131
        in setKeys(). By default it is "*".
 
132
    */
 
133
    public void setSep(String sep) {
 
134
        this.sep = sep;
 
135
    }
 
136
    /**
 
137
        Format string is like this:
 
138
        <p>
 
139
        name=value*name2=value
 
140
        <p>
 
141
        Names are case sensitive.
 
142
        <p>
 
143
        Use the setSep() method to change the * to something else
 
144
        if you need to use * as a name or value.
 
145
    */
 
146
    public void setKeys(String keys) {
 
147
        if (keys != null && keys.length() > 0) {
 
148
            StringTokenizer tok =
 
149
            new StringTokenizer(keys, this.sep, false);
 
150
            while (tok.hasMoreTokens()) {
 
151
                String token = tok.nextToken().trim();
 
152
                StringTokenizer itok =
 
153
                new StringTokenizer(token, "=", false);
 
154
                
 
155
                String name = itok.nextToken();
 
156
                String value = itok.nextToken();
 
157
//                log ( "Name: " + name );
 
158
//                log ( "Value: " + value );
 
159
                replacements.put ( name, value );
 
160
            }
 
161
        }
 
162
    }
 
163
        
 
164
 
 
165
    public static void main(String[] args)
 
166
    {
 
167
        try{
 
168
        Hashtable hash = new Hashtable();
 
169
        hash.put ( "VERSION", "1.0.3" );
 
170
        hash.put ( "b", "ffff" );
 
171
        System.out.println ( KeySubst.replace ( "$f ${VERSION} f ${b} jj $", hash ) );
 
172
        }catch ( Exception e)
 
173
        {
 
174
            e.printStackTrace();
 
175
        }
 
176
    }
 
177
 
 
178
    /**
 
179
        Does replacement on text using the hashtable of keys.
 
180
        
 
181
        @returns the string with the replacements in it.
 
182
    */
 
183
    public static String replace ( String origString, Hashtable keys )
 
184
        throws BuildException
 
185
    {
 
186
        StringBuffer finalString=new StringBuffer();
 
187
        int index=0;
 
188
        int i = 0;
 
189
        String key = null;
 
190
        while ((index = origString.indexOf("${", i)) > -1) {
 
191
            key = origString.substring(index + 2, origString.indexOf("}", index+3));
 
192
            finalString.append (origString.substring(i, index));
 
193
            if ( keys.containsKey ( key ) ) {
 
194
                finalString.append (keys.get(key));
 
195
            } else {
 
196
                finalString.append ( "${" );
 
197
                finalString.append ( key );
 
198
                finalString.append ( "}" );
 
199
            }
 
200
            i = index + 3 + key.length();
 
201
        }
 
202
        finalString.append (origString.substring(i));
 
203
        return finalString.toString();
 
204
    }
 
205
}