View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.tool;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * Tool to construct a XHTML tag.<br>
28   * <br>
29   * Usage:
30   * <pre>
31   * XHtmlTagTool tag = XHtmlTagTool(&quot;a&quot;, &quot;link&quot;);
32   * tag.addAttribute(&quot;href&quot;, &quot;http://google.com&quot;);
33   * tag.toString();: &lt;a href=&quot;http://google.com&quot;&gt;link&lt;/a&gt;
34   * </pre>
35   * 
36   * <em>Hint</em>:
37   * <ul>
38   * <li>Attributes are not ordered.</li>
39   * <li>If your tag shouldn't have a value but the tag has to close with '&lt;/[tagname]&gt;', set
40   * the value to {@link XHtmlTagTool#SPACE}.</li>
41   * </ul>
42   * 
43   * @version $Id: XHtmlTagTool.java 2151 2008-07-02 22:03:15Z mosipov $
44   */
45  public class XHtmlTagTool {
46  
47  	/** Name of the tag. */
48  	private String name;
49  
50  	/** Container for the attributes. */
51  	private Map<String, String> attributes = new HashMap<String, String>();
52  
53  	/** Value of the tag. */
54  	private String value = null;
55  	
56  	/** Indicator to uses non self-closing tag. */
57  	public static final String SPACE = " ";
58  
59  	public XHtmlTagTool(final String name, final String value) throws IllegalArgumentException {
60  		if (Utils.isEmpty(name))
61  			throw new IllegalArgumentException("Parameter 'name' shouldn't be empty!");
62  		this.name = name;
63  		this.value = value;
64  	}
65  
66  	public XHtmlTagTool(final String name) {
67  		this(name, null);
68  	}
69  
70  	/**
71  	 * Setter for the value of the tag.
72  	 * 
73  	 * @param value
74  	 */
75  	public void setValue(final String value) {
76  		this.value = value;
77  	}
78  
79  	/**
80  	 * Adds an attribute to the tag.
81  	 * 
82  	 * @param name
83  	 * @param value
84  	 * @throws IllegalArgumentException if 'key' is empty.
85  	 */
86  	public void addAttribute(final String name, final String value) {
87  		if (Utils.isEmpty(name))
88  			throw new IllegalArgumentException("Parameter 'name' shouldn't be empty!");
89  		attributes.put(name, value);
90  	}
91  
92  	/**
93  	 * Constructs the tag.
94  	 * 
95  	 * @see java.lang.Object#toString()
96  	 */
97  	@Override
98  	public String toString() {
99  		StringBuffer tag = new StringBuffer();
100 
101 		// open tag
102 		tag.append("<").append(name);
103 
104 		// add attributes
105 		for (String key : attributes.keySet()) {
106 			String val = attributes.get(key);
107 			tag.append(' ').append(key).append('=').append('\"').append(val).append('\"');
108 		}
109 
110 		// close the tag
111 		if (Utils.isNotEmpty(value)) {
112 			tag.append(">").append(value).append("</").append(name).append('>');
113 		} else
114 			tag.append(" />");
115 
116 		return tag.toString();
117 	}
118 
119 	@Override
120 	public boolean equals(Object obj) {
121 		if (obj == null)
122 			return false;
123 		try {
124 			XHtmlTagTool tag = (XHtmlTagTool) obj;
125 			return value.equals(tag.value) && name.equals(tag.name)
126 					&& attributes.equals(tag.attributes);
127 		} catch (ClassCastException e) {
128 			return false;
129 		}
130 	}
131 	
132 	@Override
133 	public int hashCode() {
134 		
135 		return name.hashCode() + value.hashCode() + attributes.hashCode();
136 	}
137 }