KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Filter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.Task;
25
26 /**
27  * Sets a token filter that is used by the file copy tasks
28  * to do token substitution. Sets multiple tokens by
29  * reading these from a file.
30  *
31  * @since Ant 1.1
32  *
33  * @ant.task category="filesystem"
34  */

35 public class Filter extends Task {
36
37     private String JavaDoc token;
38     private String JavaDoc value;
39     private File JavaDoc filtersFile;
40
41     /**
42      * The token string without @ delimiters.
43      * @param token token to set
44      */

45     public void setToken(String JavaDoc token) {
46         this.token = token;
47     }
48
49     /**
50      * The string that should replace the token during filtered copies.
51      * @param value token replace value
52      */

53     public void setValue(String JavaDoc value) {
54         this.value = value;
55     }
56
57     /**
58      * The file from which the filters must be read.
59      * This file must be a formatted as a property file.
60      *
61      * @param filtersFile filter file
62      */

63     public void setFiltersfile(File JavaDoc filtersFile) {
64         this.filtersFile = filtersFile;
65     }
66
67     /**
68      * Execute the task.
69      * @throws BuildException on error
70      */

71     public void execute() throws BuildException {
72         boolean isFiltersFromFile =
73             filtersFile != null && token == null && value == null;
74         boolean isSingleFilter =
75             filtersFile == null && token != null && value != null;
76
77         if (!isFiltersFromFile && !isSingleFilter) {
78             throw new BuildException("both token and value parameters, or "
79                                      + "only a filtersFile parameter is "
80                                      + "required", getLocation());
81         }
82
83         if (isSingleFilter) {
84             getProject().getGlobalFilterSet().addFilter(token, value);
85         }
86
87         if (isFiltersFromFile) {
88             readFilters();
89         }
90     }
91
92     /**
93      * Read the filters.
94      * @throws BuildException on error
95      */

96     protected void readFilters() throws BuildException {
97         log("Reading filters from " + filtersFile, Project.MSG_VERBOSE);
98         getProject().getGlobalFilterSet().readFiltersFromFile(filtersFile);
99     }
100 }
101
Popular Tags