View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: AttributesWriter.java,v $
31   * Revision 1.7  2005/04/09 18:04:17  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.6  2005/01/19 12:51:02  colinmacleod
35   * Changed Id --> Name.
36   *
37   * Revision 1.5  2005/01/07 08:08:24  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata op.
46   *
47   * Revision 1.4  2004/12/30 20:27:28  colinmacleod
48   * Added appendAttribute method.
49   *
50   * Revision 1.3  2004/12/23 21:28:31  colinmacleod
51   * Modifications to add ivata op compatibility.
52   *
53   * Revision 1.2  2004/11/11 13:42:10  colinmacleod
54   * Added new field based constructor.
55   *
56   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
57   * Ready for 0.1 release
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>&quot;valueObject.&quot; +
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             // replace dots with dashes in the id - makes the JavaScript easier
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