KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > extension > resolvers > AntResolver


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 package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.taskdefs.Ant;
25 import org.apache.tools.ant.taskdefs.optional.extension.Extension;
26 import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
27
28 /**
29  * Resolver that just returns s specified location.
30  *
31  */

32 public class AntResolver implements ExtensionResolver {
33     private File JavaDoc antfile;
34     private File JavaDoc destfile;
35     private String JavaDoc target;
36
37     /**
38      * Sets the ant file
39      * @param antfile the ant file to set
40      */

41     public void setAntfile(final File JavaDoc antfile) {
42         this.antfile = antfile;
43     }
44
45     /**
46      * Sets the destination file
47      * @param destfile the destination file
48      */

49     public void setDestfile(final File JavaDoc destfile) {
50         this.destfile = destfile;
51     }
52
53     /**
54      * Sets the target
55      * @param target the target
56      */

57     public void setTarget(final String JavaDoc target) {
58         this.target = target;
59     }
60
61     /**
62      * Returns the resolved file
63      * @param extension the extension
64      * @param project the project
65      * @return the file resolved
66      * @throws BuildException if the file cannot be resolved
67      */

68     public File JavaDoc resolve(final Extension extension,
69                          final Project project) throws BuildException {
70         validate();
71
72         final Ant ant = new Ant();
73         ant.setProject(project);
74         ant.setInheritAll(false);
75         ant.setAntfile(antfile.getName());
76
77         try {
78             final File JavaDoc dir =
79                 antfile.getParentFile().getCanonicalFile();
80             ant.setDir(dir);
81         } catch (final IOException JavaDoc ioe) {
82             throw new BuildException(ioe.getMessage(), ioe);
83         }
84
85         if (null != target) {
86             ant.setTarget(target);
87         }
88
89         ant.execute();
90
91         return destfile;
92     }
93
94     /*
95      * Validates URL
96      */

97     private void validate() {
98         if (null == antfile) {
99             final String JavaDoc message = "Must specify Buildfile";
100             throw new BuildException(message);
101         }
102
103         if (null == destfile) {
104             final String JavaDoc message = "Must specify destination file";
105             throw new BuildException(message);
106         }
107     }
108
109     /**
110      * Returns a string representation
111      * @return the string representation
112      */

113     public String JavaDoc toString() {
114         return "Ant[" + antfile + "==>" + destfile + "]";
115     }
116 }
117
Popular Tags