View Javadoc

1   /*
2    * $Id: XmlAttribute.java 421151 2006-07-12 06:07:14Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.tiles.xmlDefinition;
20  
21  import org.apache.struts.tiles.DefinitionNameAttribute;
22  import org.apache.struts.tiles.DirectStringAttribute;
23  import org.apache.struts.tiles.PathAttribute;
24  import org.apache.struts.tiles.UntypedAttribute;
25  
26  /***
27   * A property key-value pair.  This class is used to read configuration files.
28   */
29  public class XmlAttribute {
30  
31      /***
32       * Attribute name or key.
33       */
34      private String name = null;
35  
36      /***
37       * Attribute value.
38       * Value read from description file.
39       */
40      private Object value = null;
41  
42      /***
43       * Attribute value.
44       */
45      private String direct = null;
46  
47      /***
48       * Attribute value.
49       */
50      private String valueType = null;
51  
52      /***
53       * Attribute value.
54       */
55      private String role = null;
56  
57      /***
58       * Real attribute value.
59       * Real value is the value after processing of valueType.
60       * I.e. if a type is defined, realValue contains wrapper for this type.
61       */
62      private Object realValue = null;
63  
64      /***
65       * Constructor.
66       */
67      public XmlAttribute() {
68          super();
69      }
70  
71      /***
72       * Constructor.
73       */
74      public XmlAttribute(String name, Object value) {
75          this.name = name;
76          this.value = value;
77      }
78  
79      /***
80       * Access method for the name property.
81       *
82       * @return The current value of the name property.
83       */
84      public String getName() {
85          return name;
86      }
87  
88      /***
89       * Sets the value of the name property.
90       *
91       * @param role the new value of the name property
92       */
93      public void setRole(String role) {
94          this.role = role;
95      }
96  
97      /***
98       * Access method for the name property.
99       *
100      * @return The current value of the name property.
101      */
102     public String getRole() {
103         return role;
104     }
105 
106     /***
107      * Sets the value of the name property.
108      *
109      * @param aName the new value of the name property.
110      */
111     public void setName(String aName) {
112         name = aName;
113     }
114 
115     /***
116      * Another access method for the name property.
117      *
118      * @return   the current value of the name property
119      */
120     public String getAttribute() {
121         return name;
122     }
123 
124     /***
125      * Sets the value of the name property.
126      *
127      * @param aName the new value of the name property
128      */
129     public void setAttribute(String aName) {
130         name = aName;
131     }
132 
133     /***
134      * Access method for the value property. Return the value or a
135      * QualifiedAttribute containing the value if 'direct' is set.
136      *
137      * @return The current value of the value property.
138      */
139     public Object getValue() {
140         // Compatibility with JSP Template
141         if (this.realValue == null) {
142             this.realValue = this.computeRealValue();
143         }
144 
145         return this.realValue;
146     }
147 
148     /***
149      * Sets the value of the value property.
150      *
151      * @param aValue the new value of the value property
152      */
153     public void setValue(Object aValue) {
154         realValue = null;
155         value = aValue;
156     }
157 
158     /***
159      * Sets the value of the value property.
160      *
161      * @param aValue the new value of the value property
162      */
163     public void setContent(Object aValue) {
164         setValue(aValue);
165     }
166 
167     /***
168      * Sets the value of the value property.
169      *
170      * @param body the new value of the value property
171      */
172     public void setBody(String body) {
173         if (body.length() == 0) {
174             return;
175         }
176 
177         setValue(body);
178     }
179 
180     /***
181      * Sets the value of the value property.
182      *
183      * @param value the new value of the value property
184      */
185     public void setDirect(String value) {
186         this.direct = value;
187     }
188 
189     /***
190      * Sets the value of the value property.
191      *
192      * @param value the new value of the value property
193      */
194     public void setType(String value) {
195         this.valueType = value;
196     }
197 
198     /***
199      * Compute  real value from attributes setting.
200      */
201     protected Object computeRealValue() {
202         Object realValue = value;
203         // Is there a type set ?
204         // First check direct attribute, and translate it to a valueType.
205         // Then, evaluate valueType, and create requested typed attribute.
206         if (direct != null) {
207             this.valueType =
208                 Boolean.valueOf(direct).booleanValue() ? "string" : "path";
209         }
210 
211         if (value != null && valueType != null) {
212             String strValue = value.toString();
213 
214             if (valueType.equalsIgnoreCase("string")) {
215                 realValue = new DirectStringAttribute(strValue);
216 
217             } else if (valueType.equalsIgnoreCase("page")) {
218                 realValue = new PathAttribute(strValue);
219 
220             } else if (valueType.equalsIgnoreCase("template")) {
221                 realValue = new PathAttribute(strValue);
222 
223             } else if (valueType.equalsIgnoreCase("instance")) {
224                 realValue = new DefinitionNameAttribute(strValue);
225             }
226 
227             // Set realValue's role value if needed
228             if (role != null) {
229                 ((UntypedAttribute) realValue).setRole(role);
230             }
231         }
232 
233         // Create attribute wrapper to hold role if role is set and no type 
234         // specified
235         if (role != null && value != null && valueType == null) {
236             realValue = new UntypedAttribute(value.toString(), role);
237         }
238 
239         return realValue;
240     }
241 }