KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > property > PropertyCopy


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48 package net.sf.antcontrib.property;
49
50 import org.apache.tools.ant.BuildException;
51 import org.apache.tools.ant.Project;
52 import org.apache.tools.ant.Target;
53 import org.apache.tools.ant.Task;
54 import org.apache.tools.ant.taskdefs.CallTarget;
55 import org.apache.tools.ant.taskdefs.Property;
56 import org.apache.tools.ant.types.FileSet;
57 import java.io.File JavaDoc;
58
59 /***
60  * Task definition for the propertycopy task, which copies the value of a
61  * named property to another property. This is useful when you need to
62  * plug in the value of another property in order to get a property name
63  * and then want to get the value of that property name.
64  *
65  * <pre>
66  * Usage:
67  *
68  * Task declaration in the project:
69  * <code>
70  * &lt;taskdef name="propertycopy" classname="net.sf.antcontrib.property.PropertyCopy" /&gt;
71  * </code>
72  *
73  * Call Syntax:
74  * <code>
75  * &lt;propertycopy name="propname" from="copyfrom" (silent="true|false")? /&gt;
76  * </code>
77  *
78  * Attributes:
79  * name --&gt; The name of the property you wish to set with the value
80  * from --&gt; The name of the property you wish to copy the value from
81  * silent --&gt; Do you want to suppress the error if the "from" property
82  * does not exist, and just not set the property "name". Default
83  * is false.
84  *
85  * Example:
86  * &lt;property name="org" value="MyOrg" /&gt;
87  * &lt;property name="org.MyOrg.DisplayName" value="My Organiziation" /&gt;
88  * &lt;propertycopy name="displayName" from="org.${org}.DisplayName" /&gt;
89  * &lt;echo message="${displayName}" /&gt;
90  * </pre>
91  *
92  * @author <a HREF="mailto:mattinger@mindless.com">Matthew Inger</a>
93  */

94 public class PropertyCopy extends Task
95 {
96     private String JavaDoc name;
97     private String JavaDoc from;
98     private boolean silent;
99
100     /***
101      * Default Constructor
102      */

103     public PropertyCopy()
104     {
105         super();
106         this.name = null;
107         this.from = null;
108         this.silent = false;
109     }
110
111     public void setName(String JavaDoc name)
112     {
113         this.name = name;
114     }
115
116     public void setFrom(String JavaDoc from)
117     {
118         this.from = from;
119     }
120
121     public void setSilent(boolean silent)
122     {
123         this.silent = silent;
124     }
125
126     public void execute()
127         throws BuildException
128     {
129         if (name == null)
130             throw new BuildException("Missing the 'name' attribute.");
131
132         if (from == null)
133             throw new BuildException("Missing the 'from' attribute.");
134
135         String JavaDoc value = getProject().getProperty(from);
136
137         
138         if (value == null && ! silent)
139             throw new BuildException("Property '" + from + "' is not defined.");
140
141         if (value != null)
142         {
143             if (getProject().getUserProperty(name) == null)
144                 getProject().setProperty(name, value);
145             else
146                 getProject().setUserProperty(name, value);
147         }
148     }
149
150 }
151
152
153
Popular Tags