KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > AttributeSupportHelper


1 /**
2  * Jun 24, 2005
3  *
4  * Copyright 2004 uitags
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 package net.sf.uitags.tagutil;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import net.sf.uitags.util.UiString;
25
26 /**
27  * A helper for tags that support arbitrary HTML tag attributes.
28  *
29  * @author hgani
30  * @version $Id: AttributeSupportHelper.java,v 1.1.1.1 2006/02/26 11:39:21 hgani Exp $
31  */

32 public class AttributeSupportHelper implements AttributeSupport {
33   /**
34    * HTML tag attribute values keyed on attribute names.
35    */

36   private Map JavaDoc attributeMap;
37
38   /**
39    * Constructs a new helper.
40    */

41   public AttributeSupportHelper() {
42     this.attributeMap = new HashMap JavaDoc();
43   }
44
45   /** {@inheritDoc} */
46   public void addAttribute(String JavaDoc name, String JavaDoc value) {
47     this.attributeMap.put(name, value);
48   }
49
50   /**
51    * Returns HTML code representing the specified attributes preceeded by
52    * a space, therefore the generated code can be appended to an existing
53    * HTML tag without any need to modify the code.
54    *
55    * @return the HTML code
56    */

57   public String JavaDoc eval() {
58     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59     for (
60         Iterator JavaDoc iter = this.attributeMap.entrySet().iterator();
61         iter.hasNext(); ) {
62       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
63       buffer.append(UiString.simpleConstruct(" {0}=\"{1}\"", new String JavaDoc[] {
64           (String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue() }));
65     }
66     return buffer.toString();
67   }
68 }
69
Popular Tags