View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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  
17  package org.apache.commons.jelly.tags.ant;
18  
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.MissingAttributeException;
21  import org.apache.commons.jelly.TagSupport;
22  import org.apache.commons.jelly.XMLOutput;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * Tag which sets an attribute on the parent Ant Task if the given value is not null.
29   * This can be useful when setting parameters on Ant tasks, only if they have been specified
30   * via some well defined property, otherwise allowing the inbuilt default to be used.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   */
34  public class SetPropertyTag extends TagSupport {
35  
36      /*** The Log to which logging calls will be made. */
37      private static final Log log = LogFactory.getLog(SetPropertyTag.class);
38  
39      private String name;
40      private Object value;
41      private Object defaultValue;
42  
43      public SetPropertyTag() {
44      }
45  
46      // Tag interface
47      //-------------------------------------------------------------------------
48      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
49          if (name == null) {
50              throw new MissingAttributeException("name");
51          }
52          TaskSource tag = (TaskSource) findAncestorWithClass( TaskSource.class );
53          if ( tag == null ) {
54              throw new JellyTagException( "This tag must be nested within an Ant task tag" );
55          }
56          Object value = getValue();
57          if (value == null) {
58              value = getDefault();
59          }
60          if (value != null) {
61              tag.setTaskProperty(name, value);
62          }
63      }
64  
65  
66      // Properties
67      //-------------------------------------------------------------------------
68  
69      /***
70       * Returns the name.
71       * @return String
72       */
73      public String getName() {
74          return name;
75      }
76  
77      /***
78       * Returns the value.
79       * @return Object
80       */
81      public Object getValue() {
82          return value;
83      }
84  
85      /***
86       * Sets the name of the Ant task property to set.
87       * @param name The name of the Ant task property to set
88       */
89      public void setName(String name) {
90          this.name = name;
91      }
92  
93      /***
94       * Sets the value of the Ant task property to set.
95       * @param value The value of the Ant task property to set
96       */
97      public void setValue(Object value) {
98          this.value = value;
99      }
100 
101     /***
102      * Returns the defaultValue.
103      * @return Object
104      */
105     public Object getDefault() {
106         return defaultValue;
107     }
108 
109     /***
110      * Sets the default value to be used if the specified value is empty.
111      */
112     public void setDefault(Object defaultValue) {
113         this.defaultValue = defaultValue;
114     }
115 
116 }