| 1 | /* |
| 2 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package net.sf.antcontrib.logic; |
| 17 | |
| 18 | import java.util.StringTokenizer; |
| 19 | |
| 20 | import org.apache.tools.ant.BuildException; |
| 21 | import org.apache.tools.ant.Project; |
| 22 | import org.apache.tools.ant.taskdefs.Ant; |
| 23 | |
| 24 | /** |
| 25 | * Subclass of CallTarget which allows us to fetch |
| 26 | * properties which are set in the scope of the called |
| 27 | * target, and set them in the scope of the calling target. |
| 28 | * Normally, these properties are thrown away as soon as the |
| 29 | * called target completes execution. |
| 30 | * |
| 31 | * @author inger |
| 32 | * @author Dale Anson, [email protected] |
| 33 | */ |
| 34 | public class AntFetch extends Ant { |
| 35 | /** the name of the property to fetch from the new project */ |
| 36 | private String returnName = null; |
| 37 | |
| 38 | private ProjectDelegate fakeProject = null; |
| 39 | |
| 40 | public void setProject(Project realProject) { |
| 41 | fakeProject = new ProjectDelegate(realProject); |
| 42 | super.setProject(fakeProject); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Do the execution. |
| 47 | * |
| 48 | * @exception BuildException Description of the Exception |
| 49 | */ |
| 50 | public void execute() throws BuildException { |
| 51 | super.execute(); |
| 52 | |
| 53 | // copy back the props if possible |
| 54 | if ( returnName != null ) { |
| 55 | StringTokenizer st = new StringTokenizer( returnName, "," ); |
| 56 | while ( st.hasMoreTokens() ) { |
| 57 | String name = st.nextToken().trim(); |
| 58 | String value = fakeProject.getSubproject().getUserProperty( name ); |
| 59 | if ( value != null ) { |
| 60 | getProject().setUserProperty( name, value ); |
| 61 | } |
| 62 | else { |
| 63 | value = fakeProject.getSubproject().getProperty( name ); |
| 64 | if ( value != null ) { |
| 65 | getProject().setProperty( name, value ); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set the property or properties that are set in the new project to be |
| 74 | * transfered back to the original project. As with all properties, if the |
| 75 | * property already exists in the original project, it will not be overridden |
| 76 | * by a different value from the new project. |
| 77 | * |
| 78 | * @param r the name of a property in the new project to set in the original |
| 79 | * project. This may be a comma separate list of properties. |
| 80 | */ |
| 81 | public void setReturn( String r ) { |
| 82 | returnName = r; |
| 83 | } |
| 84 | } |