|
Ant example source code file (Target.java)
The Target.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.tools.ant.util.CollectionUtils;
/**
* Class to implement a target object with required parameters.
*
*/
public class Target implements TaskContainer {
/** Name of this target. */
private String name;
/** The "if" condition to test on execution. */
private String ifCondition = "";
/** The "unless" condition to test on execution. */
private String unlessCondition = "";
/** List of targets this target is dependent on. */
private List dependencies = null;
/** Children of this target (tasks and data types). */
private List children = new ArrayList();
/** Since Ant 1.6.2 */
private Location location = Location.UNKNOWN_LOCATION;
/** Project this target belongs to. */
private Project project;
/** Description of this target, if any. */
private String description = null;
/** Default constructor. */
public Target() {
//empty
}
/**
* Cloning constructor.
* @param other the Target to clone.
*/
public Target(Target other) {
this.name = other.name;
this.ifCondition = other.ifCondition;
this.unlessCondition = other.unlessCondition;
this.dependencies = other.dependencies;
this.location = other.location;
this.project = other.project;
this.description = other.description;
// The children are added to after this cloning
this.children = other.children;
}
/**
* Sets the project this target belongs to.
*
* @param project The project this target belongs to.
* Must not be <code>null.
*/
public void setProject(Project project) {
this.project = project;
}
/**
* Returns the project this target belongs to.
*
* @return The project this target belongs to, or <code>null if
* the project has not been set yet.
*/
public Project getProject() {
return project;
}
/**
* Sets the location of this target's definition.
*
* @param location <code>Location
* @since 1.6.2
*/
public void setLocation(Location location) {
this.location = location;
}
/**
* Get the location of this target's definition.
*
* @return <code>Location
* @since 1.6.2
*/
public Location getLocation() {
return location;
}
/**
* Sets the list of targets this target is dependent on.
* The targets themselves are not resolved at this time.
*
* @param depS A comma-separated list of targets this target
* depends on. Must not be <code>null.
*/
public void setDepends(String depS) {
if (depS.length() > 0) {
StringTokenizer tok =
new StringTokenizer(depS, ",", true);
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();
// Make sure the dependency is not empty string
if ("".equals(token) || ",".equals(token)) {
throw new BuildException("Syntax Error: depends "
+ "attribute of target \"" + getName()
+ "\" has an empty string as dependency.");
}
addDependency(token);
// Make sure that depends attribute does not
// end in a ,
if (tok.hasMoreTokens()) {
token = tok.nextToken();
if (!tok.hasMoreTokens() || !",".equals(token)) {
throw new BuildException("Syntax Error: Depend "
+ "attribute for target \"" + getName()
+ "\" ends with a , character");
}
}
}
}
}
/**
* Sets the name of this target.
*
* @param name The name of this target. Should not be <code>null.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the name of this target.
*
* @return the name of this target, or <code>null if the
* name has not been set yet.
*/
public String getName() {
return name;
}
/**
* Adds a task to this target.
*
* @param task The task to be added. Must not be <code>null.
*/
public void addTask(Task task) {
children.add(task);
}
/**
* Adds the wrapper for a data type element to this target.
*
* @param r The wrapper for the data type element to be added.
* Must not be <code>null.
*/
public void addDataType(RuntimeConfigurable r) {
children.add(r);
}
/**
* Returns the current set of tasks to be executed by this target.
*
* @return an array of the tasks currently within this target
*/
public Task[] getTasks() {
List tasks = new ArrayList(children.size());
Iterator it = children.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Task) {
tasks.add(o);
}
}
return (Task[]) tasks.toArray(new Task[tasks.size()]);
}
/**
* Adds a dependency to this target.
*
* @param dependency The name of a target this target is dependent on.
* Must not be <code>null.
*/
public void addDependency(String dependency) {
if (dependencies == null) {
dependencies = new ArrayList(2);
}
dependencies.add(dependency);
}
/**
* Returns an enumeration of the dependencies of this target.
*
* @return an enumeration of the dependencies of this target
*/
public Enumeration getDependencies() {
return (dependencies != null ? Collections.enumeration(dependencies)
: new CollectionUtils.EmptyEnumeration());
}
/**
* Does this target depend on the named target?
* @param other the other named target.
* @return true if the target does depend on the named target
* @since Ant 1.6
*/
public boolean dependsOn(String other) {
Project p = getProject();
Hashtable t = (p == null) ? null : p.getTargets();
return (p != null
&& p.topoSort(getName(), t, false).contains(t.get(other)));
}
/**
* Sets the "if" condition to test on execution. This is the
* name of a property to test for existence - if the property
* is not set, the task will not execute. The property goes
* through property substitution once before testing, so if
* property <code>foo has value
Other Ant examples (source code examples)Here is a short list of links related to this Ant Target.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.