| 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.property; |
| 17 | |
| 18 | import org.apache.tools.ant.BuildException; |
| 19 | |
| 20 | /*** |
| 21 | * Task definition for the propertycopy task, which copies the value of a |
| 22 | * named property to another property. This is useful when you need to |
| 23 | * plug in the value of another property in order to get a property name |
| 24 | * and then want to get the value of that property name. |
| 25 | * |
| 26 | * <pre> |
| 27 | * Usage: |
| 28 | * |
| 29 | * Task declaration in the project: |
| 30 | * <code> |
| 31 | * <taskdef name="propertycopy" classname="net.sf.antcontrib.property.PropertyCopy" /> |
| 32 | * </code> |
| 33 | * |
| 34 | * Call Syntax: |
| 35 | * <code> |
| 36 | * <propertycopy name="propname" from="copyfrom" (silent="true|false")? /> |
| 37 | * </code> |
| 38 | * |
| 39 | * Attributes: |
| 40 | * name --> The name of the property you wish to set with the value |
| 41 | * from --> The name of the property you wish to copy the value from |
| 42 | * silent --> Do you want to suppress the error if the "from" property |
| 43 | * does not exist, and just not set the property "name". Default |
| 44 | * is false. |
| 45 | * |
| 46 | * Example: |
| 47 | * <property name="org" value="MyOrg" /> |
| 48 | * <property name="org.MyOrg.DisplayName" value="My Organiziation" /> |
| 49 | * <propertycopy name="displayName" from="org.${org}.DisplayName" /> |
| 50 | * <echo message="${displayName}" /> |
| 51 | * </pre> |
| 52 | * |
| 53 | * @author <a href="mailto:[email protected]">Matthew Inger</a> |
| 54 | */ |
| 55 | public class PropertyCopy |
| 56 | extends AbstractPropertySetterTask |
| 57 | { |
| 58 | private String from; |
| 59 | private boolean silent; |
| 60 | |
| 61 | /*** |
| 62 | * Default Constructor |
| 63 | */ |
| 64 | public PropertyCopy() |
| 65 | { |
| 66 | super(); |
| 67 | this.from = null; |
| 68 | this.silent = false; |
| 69 | } |
| 70 | |
| 71 | public void setName(String name) |
| 72 | { |
| 73 | setProperty(name); |
| 74 | } |
| 75 | |
| 76 | public void setFrom(String from) |
| 77 | { |
| 78 | this.from = from; |
| 79 | } |
| 80 | |
| 81 | public void setSilent(boolean silent) |
| 82 | { |
| 83 | this.silent = silent; |
| 84 | } |
| 85 | |
| 86 | protected void validate() |
| 87 | { |
| 88 | super.validate(); |
| 89 | if (from == null) |
| 90 | throw new BuildException("Missing the 'from' attribute."); |
| 91 | } |
| 92 | |
| 93 | public void execute() |
| 94 | throws BuildException |
| 95 | { |
| 96 | validate(); |
| 97 | |
| 98 | String value = getProject().getProperty(from); |
| 99 | |
| 100 | if (value == null && ! silent) |
| 101 | throw new BuildException("Property '" + from + "' is not defined."); |
| 102 | |
| 103 | if (value != null) |
| 104 | setPropertyValue(value); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 |