001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.taskdefs.Ant;
025: import org.apache.tools.ant.taskdefs.optional.extension.Extension;
026: import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
027:
028: /**
029: * Resolver that just returns s specified location.
030: *
031: */
032: public class AntResolver implements ExtensionResolver {
033: private File antfile;
034: private File destfile;
035: private String target;
036:
037: /**
038: * Sets the ant file
039: * @param antfile the ant file to set
040: */
041: public void setAntfile(final File antfile) {
042: this .antfile = antfile;
043: }
044:
045: /**
046: * Sets the destination file
047: * @param destfile the destination file
048: */
049: public void setDestfile(final File destfile) {
050: this .destfile = destfile;
051: }
052:
053: /**
054: * Sets the target
055: * @param target the target
056: */
057: public void setTarget(final String target) {
058: this .target = target;
059: }
060:
061: /**
062: * Returns the resolved file
063: * @param extension the extension
064: * @param project the project
065: * @return the file resolved
066: * @throws BuildException if the file cannot be resolved
067: */
068: public File resolve(final Extension extension, final Project project)
069: throws BuildException {
070: validate();
071:
072: final Ant ant = new Ant();
073: ant.setProject(project);
074: ant.setInheritAll(false);
075: ant.setAntfile(antfile.getName());
076:
077: try {
078: final File dir = antfile.getParentFile().getCanonicalFile();
079: ant.setDir(dir);
080: } catch (final IOException ioe) {
081: throw new BuildException(ioe.getMessage(), ioe);
082: }
083:
084: if (null != target) {
085: ant.setTarget(target);
086: }
087:
088: ant.execute();
089:
090: return destfile;
091: }
092:
093: /*
094: * Validates URL
095: */
096: private void validate() {
097: if (null == antfile) {
098: final String message = "Must specify Buildfile";
099: throw new BuildException(message);
100: }
101:
102: if (null == destfile) {
103: final String message = "Must specify destination file";
104: throw new BuildException(message);
105: }
106: }
107:
108: /**
109: * Returns a string representation
110: * @return the string representation
111: */
112: public String toString() {
113: return "Ant[" + antfile + "==>" + destfile + "]";
114: }
115: }
|