001: /*
002: * $Id: XmlAttribute.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles.xmlDefinition;
023:
024: import org.apache.struts.tiles.DefinitionNameAttribute;
025: import org.apache.struts.tiles.DirectStringAttribute;
026: import org.apache.struts.tiles.PathAttribute;
027: import org.apache.struts.tiles.UntypedAttribute;
028:
029: /**
030: * A property key-value pair. This class is used to read configuration files.
031: */
032: public class XmlAttribute {
033:
034: /**
035: * Attribute name or key.
036: */
037: private String name = null;
038:
039: /**
040: * Attribute value.
041: * Value read from description file.
042: */
043: private Object value = null;
044:
045: /**
046: * Attribute value.
047: */
048: private String direct = null;
049:
050: /**
051: * Attribute value.
052: */
053: private String valueType = null;
054:
055: /**
056: * Attribute value.
057: */
058: private String role = null;
059:
060: /**
061: * Real attribute value.
062: * Real value is the value after processing of valueType.
063: * I.e. if a type is defined, realValue contains wrapper for this type.
064: */
065: private Object realValue = null;
066:
067: /**
068: * Constructor.
069: */
070: public XmlAttribute() {
071: super ();
072: }
073:
074: /**
075: * Constructor.
076: */
077: public XmlAttribute(String name, Object value) {
078: this .name = name;
079: this .value = value;
080: }
081:
082: /**
083: * Access method for the name property.
084: *
085: * @return The current value of the name property.
086: */
087: public String getName() {
088: return name;
089: }
090:
091: /**
092: * Sets the value of the name property.
093: *
094: * @param role the new value of the name property
095: */
096: public void setRole(String role) {
097: this .role = role;
098: }
099:
100: /**
101: * Access method for the name property.
102: *
103: * @return The current value of the name property.
104: */
105: public String getRole() {
106: return role;
107: }
108:
109: /**
110: * Sets the value of the name property.
111: *
112: * @param aName the new value of the name property.
113: */
114: public void setName(String aName) {
115: name = aName;
116: }
117:
118: /**
119: * Another access method for the name property.
120: *
121: * @return the current value of the name property
122: */
123: public String getAttribute() {
124: return name;
125: }
126:
127: /**
128: * Sets the value of the name property.
129: *
130: * @param aName the new value of the name property
131: */
132: public void setAttribute(String aName) {
133: name = aName;
134: }
135:
136: /**
137: * Access method for the value property. Return the value or a
138: * QualifiedAttribute containing the value if 'direct' is set.
139: *
140: * @return The current value of the value property.
141: */
142: public Object getValue() {
143: // Compatibility with JSP Template
144: if (this .realValue == null) {
145: this .realValue = this .computeRealValue();
146: }
147:
148: return this .realValue;
149: }
150:
151: /**
152: * Sets the value of the value property.
153: *
154: * @param aValue the new value of the value property
155: */
156: public void setValue(Object aValue) {
157: realValue = null;
158: value = aValue;
159: }
160:
161: /**
162: * Sets the value of the value property.
163: *
164: * @param aValue the new value of the value property
165: */
166: public void setContent(Object aValue) {
167: setValue(aValue);
168: }
169:
170: /**
171: * Sets the value of the value property.
172: *
173: * @param body the new value of the value property
174: */
175: public void setBody(String body) {
176: if (body.length() == 0) {
177: return;
178: }
179:
180: setValue(body);
181: }
182:
183: /**
184: * Sets the value of the value property.
185: *
186: * @param value the new value of the value property
187: */
188: public void setDirect(String value) {
189: this .direct = value;
190: }
191:
192: /**
193: * Sets the value of the value property.
194: *
195: * @param value the new value of the value property
196: */
197: public void setType(String value) {
198: this .valueType = value;
199: }
200:
201: /**
202: * Compute real value from attributes setting.
203: */
204: protected Object computeRealValue() {
205: Object realValue = value;
206: // Is there a type set ?
207: // First check direct attribute, and translate it to a valueType.
208: // Then, evaluate valueType, and create requested typed attribute.
209: if (direct != null) {
210: this .valueType = Boolean.valueOf(direct).booleanValue() ? "string"
211: : "path";
212: }
213:
214: if (value != null && valueType != null) {
215: String strValue = value.toString();
216:
217: if (valueType.equalsIgnoreCase("string")) {
218: realValue = new DirectStringAttribute(strValue);
219:
220: } else if (valueType.equalsIgnoreCase("page")) {
221: realValue = new PathAttribute(strValue);
222:
223: } else if (valueType.equalsIgnoreCase("template")) {
224: realValue = new PathAttribute(strValue);
225:
226: } else if (valueType.equalsIgnoreCase("instance")) {
227: realValue = new DefinitionNameAttribute(strValue);
228: }
229:
230: // Set realValue's role value if needed
231: if (role != null) {
232: ((UntypedAttribute) realValue).setRole(role);
233: }
234: }
235:
236: // Create attribute wrapper to hold role if role is set and no type
237: // specified
238: if (role != null && value != null && valueType == null) {
239: realValue = new UntypedAttribute(value.toString(), role);
240: }
241:
242: return realValue;
243: }
244: }
|