1   /*
2    * ========================================================================
3    *
4    * Copyright 2003-2008 The Apache Software Foundation. Code from this file
5    * was originally imported from the Jakarta Cactus project.
6    *
7    * Codehaus CARGO, copyright 2004-2010 Vincent Massol.
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   *
21   * ========================================================================
22   */
23  package com.atlassian.maven.plugins.amps.util.ant;
24  
25  import org.apache.tools.ant.Task;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.filters.ReplaceTokens;
28  import org.apache.tools.ant.types.Environment;
29  import org.apache.tools.ant.types.Path;
30  import org.apache.tools.ant.types.FilterChain;
31  
32  import java.io.File;
33  import java.net.URI;
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  /**
38   * Set of common Ant utility methods.
39   *
40   * @version $Id: AntUtils.java 2335 2010-07-01 23:35:19Z alitokmen $
41   */
42  public class AntUtils
43  {
44      /**
45       * The factory for creating ant tasks.
46       */
47      private AntTaskFactory antTaskFactory;
48  
49      /**
50       * Uses the {@link DefaultAntTaskFactory} class when creating Ant
51       * tasks.
52       */
53      public AntUtils()
54      {
55          this.antTaskFactory = new DefaultAntTaskFactory(createProject());
56      }
57  
58      /**
59       * @param factory Ant task factory class used when creating Ant tasks
60       */
61      public AntUtils(AntTaskFactory factory)
62      {
63          this.antTaskFactory = factory;
64      }
65  
66      /**
67       * Creates and returns a new instance of the Ant task mapped to the
68       * specified logical name.
69       *
70       * @param taskName The logical name of the task to create
71       * @return A new instance of the task
72       */
73      public final Task createAntTask(String taskName)
74      {
75          return this.antTaskFactory.createTask(taskName);
76      }
77  
78      /**
79       * Convenience method to create an Ant environment variable that points to
80       * a file.
81       *
82       * @param key The key or name of the variable
83       * @param file The file the variable should point to
84       * @return The created environment variable
85       */
86      public final Environment.Variable createSysProperty(String key, File file)
87      {
88          Environment.Variable var = new Environment.Variable();
89          var.setKey(key);
90          var.setFile(file);
91          return var;
92      }
93  
94      /**
95       * Convenience method to create an Ant environment variable that contains
96       * a path.
97       *
98       * @param key The key or name of the variable
99       * @param path The path
100      * @return The created environment variable
101      */
102     public final Environment.Variable createSysProperty(String key, Path path)
103     {
104         Environment.Variable var = new Environment.Variable();
105         var.setKey(key);
106         var.setPath(path);
107         return var;
108     }
109 
110     /**
111      * Convenience method to create an Ant environment variable that contains a
112      * string.
113      *
114      * @param key The key or name of the variable
115      * @param value The value
116      * @return The created environment variable
117      */
118     public final Environment.Variable createSysProperty(String key, String value)
119     {
120         Environment.Variable var = new Environment.Variable();
121         var.setKey(key);
122         var.setValue(value);
123         return var;
124     }
125 
126     /**
127      * <p>Convenience method to create an Ant environment variable that contains a
128      * string from an URI.
129      * <p><b>Note</b> that {@link java.net.URI#getPath()} will be used.
130      *
131      * @param key The key or name of the variable
132      * @param value The URI to take the value from; {@link java.net.URI#getPath()} will be used
133      * @return The created environment variable
134      */
135     public final Environment.Variable createSysProperty(String key, URI value)
136     {
137         Environment.Variable var = new Environment.Variable();
138         var.setKey(key);
139         var.setValue(value.getPath());
140         return var;
141     }
142 
143     /**
144      * @return a default empty Ant {@link org.apache.tools.ant.Project }
145      */
146     public Project createProject()
147     {
148         Project defaultProject = new Project();
149         defaultProject.init();
150 
151         return defaultProject;
152     }
153 
154     /**
155      * Add a token to an existing filter chain.
156      *
157      * @param filterChain the filter chain to augment
158      * @param key the token key
159      * @param value the token value
160      */
161     public void addTokenToFilterChain(FilterChain filterChain, String key,
162         String value)
163     {
164         ReplaceTokens replaceToken = new ReplaceTokens();
165         ReplaceTokens.Token token = new ReplaceTokens.Token();
166         token.setKey(key);
167         token.setValue(value);
168         replaceToken.addConfiguredToken(token);
169         filterChain.addReplaceTokens(replaceToken);
170     }
171   
172     /**
173      * Add the map of tokens to the filterChain.
174      * 
175      * @param filterChain The filterchain to use
176      * @param map The map
177      */
178     public void addTokensToFilterChain(FilterChain filterChain, Map map)
179     {
180         Iterator iterator = map.keySet().iterator();
181         while (iterator.hasNext())
182         {
183             String key = (String) iterator.next();
184             String value = (String) map.get(key);
185             addTokenToFilterChain(filterChain, key, value);
186         }
187     }
188 
189 }