1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 package com.ivata.mask.web.field;
61 import java.util.Iterator;
62 import java.util.Properties;
63 import java.util.Set;
64 import org.apache.struts.taglib.TagUtils;
65 import com.ivata.mask.field.Field;
66 import com.ivata.mask.util.StringHandling;
67 /***
68 * <p>
69 * Stores and displays the attributes for an <code>HTML</code> field.
70 * </p>
71 *
72 * @since ivata masks 0.4 (2005-05-14)
73 * @author Colin MacLeod
74 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
75 * @version $Revision: 1.7 $
76 */
77 public class AttributesWriter {
78 /***
79 * <p>
80 * This stores all the attributes and their values.
81 * <p>
82 */
83 private Properties attributes = new Properties();
84 /***
85 * <p>
86 * Construct an attributes writer for the given field.
87 * </p>
88 *
89 * @param fieldParam
90 * definition of the field being written.
91 */
92 public AttributesWriter(final Field fieldParam) {
93 this(fieldParam, "");
94 }
95 /***
96 * <p>
97 * Construct an attributes writer for the given field.
98 * </p>
99 *
100 * @param fieldParam
101 * definition of the field being written.
102 * @param propertyNameSuffixParam
103 * used to build the property name of the field written, by the
104 * combination <code>"valueObject." +
105 * fieldParam.getFullId() + propertyNameSuffixParam</code>.
106 */
107 public AttributesWriter(final Field fieldParam,
108 final String propertyNameSuffixParam) {
109 if (fieldParam != null) {
110 String fullName = fieldParam.getPath() + propertyNameSuffixParam;
111
112 setAttribute("id", fullName.replaceAll("//.", "_"));
113 setAttribute("name", "valueObject." + fullName);
114 String type = fieldParam.getType();
115 if (!StringHandling.isNullOrEmpty(type)) {
116 appendAttribute("class", type);
117 }
118 }
119 }
120 /***
121 * <p>
122 * Append an attribute value, such as <strong>CSS </strong> class. It the
123 * attribute value is set already, a space is added followed by the new
124 * value.
125 * </p>
126 *
127 * @param name
128 * name of the attribute to set - usually <code>class</code>.
129 * @param value
130 * value to add to this attribute.
131 * @see #setAttribute
132 */
133 public final void appendAttribute(final String name, final String value) {
134 String valueNow = attributes.getProperty(name);
135 StringBuffer newValue = new StringBuffer();
136 if (!StringHandling.isNullOrEmpty(valueNow)) {
137 newValue.append(valueNow);
138 newValue.append(" ");
139 }
140 newValue.append(value);
141 attributes.setProperty(name, newValue.toString());
142 }
143 /***
144 * <p>
145 * Get an attribute value.
146 * </p>
147 *
148 * @param name
149 * name of the attribute to set.
150 * @return current value of this attribute.
151 */
152 public final String getAttribute(final String name) {
153 return (String) attributes.getProperty(name);
154 }
155 /***
156 * <p>
157 * Clear an attribute.
158 * </p>
159 *
160 * @param name
161 * name of the attribute to remove.
162 */
163 public final void remove(final String name) {
164 attributes.remove(name);
165 }
166 /***
167 * <p>
168 * Set an attribute value.
169 * </p>
170 *
171 * @param name
172 * name of the attribute to set.
173 * @param value
174 * current value of this attribute.
175 */
176 public final void setAttribute(final String name, final String value) {
177 attributes.setProperty(name, value);
178 }
179 /***
180 * <p>
181 * Output all the attribute names and values.
182 * </p>
183 *
184 * @return all the attribute names and values.
185 */
186 public String toString() {
187 Set keys = attributes.keySet();
188 StringBuffer buffer = new StringBuffer();
189 for (Iterator keysIterator = keys.iterator(); keysIterator.hasNext();) {
190 String key = (String) keysIterator.next();
191 buffer.append(" ");
192 buffer.append(key);
193 buffer.append("='");
194 buffer.append(TagUtils.getInstance().filter(
195 attributes.getProperty(key)));
196 buffer.append("'");
197 }
198 return buffer.toString();
199 }
200
201 }
202