summaryrefslogtreecommitdiffstats
path: root/src/java/net/sf/antcontrib/logic/AntFetch.java
blob: 177bb9e37980b80b89b58d517a5c96d805f88d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 *
 * Licensed 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 net.sf.antcontrib.logic;

import java.util.StringTokenizer;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Ant;

/**
 * Subclass of CallTarget which allows us to fetch
 * properties which are set in the scope of the called
 * target, and set them in the scope of the calling target.
 * Normally, these properties are thrown away as soon as the
 * called target completes execution.
 *
 * @author     inger
 * @author     Dale Anson, danson@germane-software.com
 */
public class AntFetch extends Ant {
	/** the name of the property to fetch from the new project */
	private String returnName = null;
	
	private ProjectDelegate fakeProject = null;

	public void setProject(Project realProject) {
		fakeProject = new ProjectDelegate(realProject);
		super.setProject(fakeProject);
	}

	/**
	 * Do the execution.
	 *
	 * @exception BuildException  Description of the Exception
	 */
	public void execute() throws BuildException {
		super.execute();

		// copy back the props if possible
		if ( returnName != null ) {
			StringTokenizer st = new StringTokenizer( returnName, "," );
			while ( st.hasMoreTokens() ) {
				String name = st.nextToken().trim();
				String value = fakeProject.getSubproject().getUserProperty( name );
				if ( value != null ) {
					getProject().setUserProperty( name, value );
				}
				else {
					value = fakeProject.getSubproject().getProperty( name );
					if ( value != null ) {
						getProject().setProperty( name, value );
					}
				}
			}
		}
	}

	/**
	 * Set the property or properties that are set in the new project to be
	 * transfered back to the original project. As with all properties, if the
	 * property already exists in the original project, it will not be overridden
	 * by a different value from the new project.
	 *
	 * @param r  the name of a property in the new project to set in the original
	 *      project. This may be a comma separate list of properties.
	 */
	public void setReturn( String r ) {
		returnName = r;
	}	
}