001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: AttributesWriter.java,v $
031: * Revision 1.8 2005/10/02 14:06:32 colinmacleod
032: * Added/improved log4j logging.
033: *
034: * Revision 1.7 2005/04/09 18:04:17 colinmacleod
035: * Changed copyright text to GPL v2 explicitly.
036: *
037: * Revision 1.6 2005/01/19 12:51:02 colinmacleod
038: * Changed Id --> Name.
039: *
040: * Revision 1.5 2005/01/07 08:08:24 colinmacleod
041: * Moved up a version number.
042: * Changed copyright notices to 2005.
043: * Updated the documentation:
044: * - started working on multiproject:site docu.
045: * - changed the logo.
046: * Added checkstyle and fixed LOADS of style issues.
047: * Added separate thirdparty subproject.
048: * Added struts (in web), util and webgui (in webtheme) from ivata op.
049: *
050: * Revision 1.4 2004/12/30 20:27:28 colinmacleod
051: * Added appendAttribute method.
052: *
053: * Revision 1.3 2004/12/23 21:28:31 colinmacleod
054: * Modifications to add ivata op compatibility.
055: *
056: * Revision 1.2 2004/11/11 13:42:10 colinmacleod
057: * Added new field based constructor.
058: *
059: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
060: * Ready for 0.1 release
061: * -----------------------------------------------------------------------------
062: */
063: package com.ivata.mask.web.field;
064:
065: import org.apache.log4j.Logger;
066:
067: import java.util.Iterator;
068: import java.util.Properties;
069: import java.util.Set;
070: import org.apache.struts.taglib.TagUtils;
071: import com.ivata.mask.field.Field;
072: import com.ivata.mask.util.StringHandling;
073:
074: /**
075: * <p>
076: * Stores and displays the attributes for an <code>HTML</code> field.
077: * </p>
078: *
079: * @since ivata masks 0.4 (2005-05-14)
080: * @author Colin MacLeod
081: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
082: * @version $Revision: 1.8 $
083: */
084: public class AttributesWriter {
085: /**
086: * Logger for this class.
087: */
088: private static final Logger logger = Logger
089: .getLogger(AttributesWriter.class);
090:
091: /**
092: * <p>
093: * This stores all the attributes and their values.
094: * <p>
095: */
096: private Properties attributes = new Properties();
097:
098: /**
099: * <p>
100: * Construct an attributes writer for the given field.
101: * </p>
102: *
103: * @param fieldParam
104: * definition of the field being written.
105: */
106: public AttributesWriter(final Field fieldParam) {
107: this (fieldParam, "");
108: }
109:
110: /**
111: * <p>
112: * Construct an attributes writer for the given field.
113: * </p>
114: *
115: * @param fieldParam
116: * definition of the field being written.
117: * @param propertyNameSuffixParam
118: * used to build the property name of the field written, by the
119: * combination <code>"valueObject." +
120: * fieldParam.getFullId() + propertyNameSuffixParam</code>.
121: */
122: public AttributesWriter(final Field fieldParam,
123: final String propertyNameSuffixParam) {
124: if (fieldParam != null) {
125: String fullName = fieldParam.getPath()
126: + propertyNameSuffixParam;
127: // replace dots with dashes in the id - makes the JavaScript easier
128: setAttribute("id", fullName.replaceAll("\\.", "_"));
129: setAttribute("name", "valueObject." + fullName);
130: String type = fieldParam.getType();
131: if (!StringHandling.isNullOrEmpty(type)) {
132: appendAttribute("class", type);
133: }
134: }
135: }
136:
137: /**
138: * <p>
139: * Append an attribute value, such as <strong>CSS </strong> class. It the
140: * attribute value is set already, a space is added followed by the new
141: * value.
142: * </p>
143: *
144: * @param name
145: * name of the attribute to set - usually <code>class</code>.
146: * @param value
147: * value to add to this attribute.
148: * @see #setAttribute
149: */
150: public final void appendAttribute(final String name,
151: final String value) {
152: if (logger.isDebugEnabled()) {
153: logger.debug("appendAttribute(String name = " + name
154: + ", String value = " + value + ") - start");
155: }
156:
157: String valueNow = attributes.getProperty(name);
158: StringBuffer newValue = new StringBuffer();
159: if (!StringHandling.isNullOrEmpty(valueNow)) {
160: newValue.append(valueNow);
161: newValue.append(" ");
162: }
163: newValue.append(value);
164: attributes.setProperty(name, newValue.toString());
165:
166: if (logger.isDebugEnabled()) {
167: logger.debug("appendAttribute(String, String) - end");
168: }
169: }
170:
171: /**
172: * <p>
173: * Get an attribute value.
174: * </p>
175: *
176: * @param name
177: * name of the attribute to set.
178: * @return current value of this attribute.
179: */
180: public final String getAttribute(final String name) {
181: if (logger.isDebugEnabled()) {
182: logger.debug("getAttribute(String name = " + name
183: + ") - start");
184: }
185:
186: String returnString = (String) attributes.getProperty(name);
187: if (logger.isDebugEnabled()) {
188: logger.debug("getAttribute(String) - end - return value = "
189: + returnString);
190: }
191: return returnString;
192: }
193:
194: /**
195: * <p>
196: * Clear an attribute.
197: * </p>
198: *
199: * @param name
200: * name of the attribute to remove.
201: */
202: public final void remove(final String name) {
203: if (logger.isDebugEnabled()) {
204: logger.debug("remove(String name = " + name + ") - start");
205: }
206:
207: attributes.remove(name);
208:
209: if (logger.isDebugEnabled()) {
210: logger.debug("remove(String) - end");
211: }
212: }
213:
214: /**
215: * <p>
216: * Set an attribute value.
217: * </p>
218: *
219: * @param name
220: * name of the attribute to set.
221: * @param value
222: * current value of this attribute.
223: */
224: public final void setAttribute(final String name, final String value) {
225: if (logger.isDebugEnabled()) {
226: logger.debug("setAttribute(String name = " + name
227: + ", String value = " + value + ") - start");
228: }
229:
230: attributes.setProperty(name, value);
231:
232: if (logger.isDebugEnabled()) {
233: logger.debug("setAttribute(String, String) - end");
234: }
235: }
236:
237: /**
238: * <p>
239: * Output all the attribute names and values.
240: * </p>
241: *
242: * @return all the attribute names and values.
243: */
244: public String toString() {
245: if (logger.isDebugEnabled()) {
246: logger.debug("toString() - start");
247: }
248:
249: Set keys = attributes.keySet();
250: StringBuffer buffer = new StringBuffer();
251: for (Iterator keysIterator = keys.iterator(); keysIterator
252: .hasNext();) {
253: String key = (String) keysIterator.next();
254: buffer.append(" ");
255: buffer.append(key);
256: buffer.append("='");
257: buffer.append(TagUtils.getInstance().filter(
258: attributes.getProperty(key)));
259: buffer.append("'");
260: }
261: String returnString = buffer.toString();
262: if (logger.isDebugEnabled()) {
263: logger.debug("toString() - end - return value = "
264: + returnString);
265: }
266: return returnString;
267: }
268:
269: }
|