View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 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   * Java representation of an XHTML tag.
28   * <p>
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   * </p>
36   * <em>Note</em>:
37   * <ul>
38   * <li>Attributes are not ordered.</li>
39   * <li>If you want to avoid self-closing tags without supplying a value, set
40   * {@link #SPACE} as the tag's value.</li>
41   * </ul>
42   * 
43   * @version $Id: XHtmlTagTool.java 4785 2009-12-21 20:10:28Z 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;
55  
56  	/** Indicator to uses non self-closing tag. */
57  	public static final String SPACE = " ";
58  
59  	/**
60  	 * Class constructor with tag name.
61  	 * 
62  	 * @param name
63  	 *            the tag name of the new XHtmlTagTool
64  	 * @throws IllegalArgumentException
65  	 *             if <code>name</code> is empty
66  	 */
67  	public XHtmlTagTool(final String name) {
68  		if (Utils.isEmpty(name))
69  			throw new IllegalArgumentException(
70  					"Parameter 'name' shouldn't be empty!");
71  		this.name = name;
72  	}
73  
74  	/**
75  	 * Class constructor with name and value.
76  	 * 
77  	 * @param name
78  	 *             the tag name of the new XHtmlTagTool
79  	 * @param value
80  	 *            the tag value of the new XHtmlTagTool which is the tag body
81  	 * @throws IllegalArgumentException
82  	 *             if <code>name</code> is empty
83  	 */
84  	public XHtmlTagTool(final String name, final String value) {
85  		this(name);
86  		this.value = value;
87  	}
88  
89  	/**
90  	 * Sets the tag value.
91  	 * 
92  	 * @param value
93  	 *            the tag value which is the tag body
94  	 */
95  	public void setValue(final String value) {
96  		this.value = value;
97  	}
98  
99  	/**
100 	 * Adds an attribute to this tag.
101 	 * 
102 	 * @param name
103 	 *            attribute name
104 	 * @param value
105 	 *            attribute value
106 	 * @throws IllegalArgumentException
107 	 *             if <code>name</code> is empty
108 	 */
109 	public void addAttribute(final String name, final String value) {
110 		if (Utils.isEmpty(name))
111 			throw new IllegalArgumentException("Name is null or empty");
112 		attributes.put(name, value);
113 	}
114 
115 	/**
116 	 * Creates the HTML representation of this tag. It follows the XHTML
117 	 * standard.
118 	 * 
119 	 * @return HTML representation of this tag
120 	 */
121 	@Override
122 	public String toString() {
123 		StringBuffer tag = new StringBuffer();
124 
125 		// open tag
126 		tag.append("<").append(name);
127 
128 		// add attributes
129 		for (String key : attributes.keySet()) {
130 			String val = attributes.get(key);
131 			tag.append(' ').append(key).append('=').append('\"').append(val)
132 					.append('\"');
133 		}
134 
135 		// close the tag
136 		if (Utils.isNotEmpty(value)) {
137 			tag.append(">").append(value).append("</").append(name).append('>');
138 		} else
139 			tag.append(" />");
140 
141 		return tag.toString();
142 	}
143 
144 }