View Javadoc

1   /*
2    * Project Bimbo.
3    * Copyright 2008 Frank Cornelis.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package net.sf.bimbo.impl;
19  
20  import java.io.PrintWriter;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  /**
25   * HTML element generator.
26   * 
27   * @author fcorneli
28   * 
29   */
30  public class HtmlElement {
31  
32  	private final String elementName;
33  
34  	private String escapedBody;
35  
36  	/**
37  	 * Main constructor.
38  	 * 
39  	 * @param elementName
40  	 *            the name of the HTML element.
41  	 */
42  	public HtmlElement(String elementName) {
43  		this.elementName = elementName;
44  		this.elements = new LinkedList<Element>();
45  	}
46  
47  	/**
48  	 * Sets the body of this HTML element.
49  	 * 
50  	 * @param body
51  	 * @return this HTML element.
52  	 */
53  	public HtmlElement setBody(String body) {
54  		this.escapedBody = HtmlUtil.escape(body);
55  		return this;
56  	}
57  
58  	private static class Element {
59  		private final String name;
60  		private final String value;
61  
62  		public Element(String name, String value) {
63  			this.name = name;
64  			this.value = value;
65  		}
66  	}
67  
68  	private final List<Element> elements;
69  
70  	/**
71  	 * Adds an attribute to the HTML element.
72  	 * 
73  	 * @param name
74  	 *            the name of the attribute.
75  	 * @param value
76  	 *            the attribute value. This can be <code>null</code>.
77  	 * @return this HTML element.
78  	 */
79  	public HtmlElement addAttribute(String name, String value) {
80  		this.elements.add(new Element(name, value));
81  		return this;
82  	}
83  
84  	/**
85  	 * Outputs the HTML element to the given writer.
86  	 * 
87  	 * @param writer
88  	 */
89  	public void write(PrintWriter writer) {
90  		writer.print("<" + this.elementName);
91  		for (Element element : this.elements) {
92  			String value = element.value;
93  			if (null == value) {
94  				value = "";
95  			}
96  			writer.print(" " + element.name + "=\"" + HtmlUtil.escape(value)
97  					+ "\"");
98  		}
99  		if (null != this.escapedBody) {
100 			writer.println(">");
101 			writer.println(this.escapedBody);
102 			writer.println("</" + this.elementName + ">");
103 		} else {
104 			writer.println("/>");
105 		}
106 	}
107 
108 	/**
109 	 * Sets the HTML entity escaped body.
110 	 * 
111 	 * @param escapedBody
112 	 * @return
113 	 */
114 	public HtmlElement setEscapedBody(String escapedBody) {
115 		this.escapedBody = escapedBody;
116 		return this;
117 	}
118 }