BrightSide Workbench Full Report + Source Code
HTMLGenerator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.html;
19 
20 import java.io.IOException;
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.util.Stack;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.string.Strings;
27 import org.turro.elephant.context.ElephantContext;
28 
33 public class HTMLGenerator {
34  protected Stack tags = new Stack();
35  protected Writer out = null;
36 
37  private String extraAttributes;
38 
40  public HTMLGenerator() {
41  this.out = new StringWriter();
42  }
43 
45  public HTMLGenerator(Writer out) {
46  this.out = out;
47  }
48 
49  @Override
50  public String toString() {
51  return out.toString().replaceAll("&(?![a-z]+;)", "&amp;");
52  }
53 
54  public void setOut(Writer out) {
55  this.out = out;
56  }
57 
58  public void setExtraAttributes(String extraAttributes) {
59  this.extraAttributes = extraAttributes;
60  }
61 
62  public HTMLGenerator doTag(String tag) {
63  return doTag(tag, (String[]) null);
64  }
65 
66  public HTMLGenerator doTag(String tag, String attributes) {
67  return doTag(tag, new String[] { attributes });
68  }
69 
70  public HTMLGenerator doTag(String tag, String[] attributes) {
71  if(attributes != null) {
72  write("<" + tag);
73  for(int i = 0; i < attributes.length; i++) {
74  if(!Strings.isBlank(attributes[i])) {
75  write(" " + attributes[i]);
76  }
77  }
78  return write(" />");
79  }
80  else {
81  return write("<" + tag + " />");
82  }
83  }
84 
85  public HTMLGenerator startTag(String tag) {
86  return startTag(tag, (String[]) null);
87  }
88 
89  public HTMLGenerator startTag(String tag, String attributes) {
90  return startTag(tag, new String[] { attributes });
91  }
92 
93  public HTMLGenerator startTag(String tag, String[] attributes) {
94  tags.push(tag);
95  if(attributes != null) {
96  write("<" + tag);
97  for(int i = 0; i < attributes.length; i++) {
98  if(!Strings.isBlank(attributes[i])) {
99  write(" " + attributes[i]);
100  }
101  }
102  if(!Strings.isBlank(extraAttributes)) {
103  write(" " + extraAttributes);
104  extraAttributes = null;
105  }
106  return write(">");
107  }
108  else {
109  return write("<" + tag + ">");
110  }
111  }
112 
113  public HTMLGenerator startExtAnchor(String url, String hint) {
114  return startTargetAnchor(url, "_blank", hint);
115  }
116 
117  public HTMLGenerator startTargetAnchor(String url, String target, String hint) {
118  return startTag("a", new String[] {
119  "href='" + url + "'",
120  (hint != null ? "title=\"" + hint + "\"" : ""),
121  (hint != null ? "alt=\"" + hint + "\"" : ""),
122  "target='" + target + "'",
123  "onclick='event.stopPropagation();'",
124  "class='newwindow'"
125  });
126  }
127 
128  public HTMLGenerator startAnchor(String url, String hint) {
129  return startAnchor(url, hint, null, null);
130  }
131 
132  public HTMLGenerator startAnchor(String url, String hint, String cssClass) {
133  return startAnchor(url, hint, cssClass, null);
134  }
135 
136  public HTMLGenerator startAnchor(String url, String hint, String cssClass, String onClick) {
137  return startTag("a", new String[] {
138  "href='" + url + "'",
139  (hint != null ? "title=\"" + hint + "\"" : ""),
140  (hint != null ? "alt=\"" + hint + "\"" : ""),
141  (cssClass != null ? "class='" + cssClass + "'" : ""),
142  (onClick != null ? "onclick=\"" + onClick + "\"" : "")
143  });
144  }
145 
146  public HTMLGenerator startTable(String attributes) {
147  return startTable(new String[] { attributes });
148  }
149 
150  public HTMLGenerator startTable(String[] attributes) {
151  return startTag("table", attributes);
152  }
153 
155  return endTag("table");
156  }
157 
158  public HTMLGenerator startTableRow(String attributes) {
159  return startTableRow(new String[] { attributes });
160  }
161 
162  public HTMLGenerator startTableRow(String[] attributes) {
163  if(!checkTag("table")) {
164  startTable("");
165  }
166  return startTag("tr", attributes);
167  }
168 
170  return endTag("tr");
171  }
172 
173  public HTMLGenerator startTableCol(String attributes) {
174  return startTableCol(new String[] { attributes });
175  }
176 
177  public HTMLGenerator startTableCol(String[] attributes) {
178  if(!checkTag("tr")) {
179  startTableRow("");
180  }
181  return startTag("td", attributes);
182  }
183 
185  return endTag("td");
186  }
187 
189  String tag = (String) tags.pop();
190  return write("</" + tag + ">");
191  }
192 
193  public HTMLGenerator endTag(String tag) {
194  if(!checkTag(tag)) return this;
195  String tmp = null;
196  while(!tag.equals(tmp)) {
197  tmp = (String) tags.pop();
198  write("</" + tmp + ">");
199  }
200  return this;
201  }
202 
203  public HTMLGenerator endBeforeTag(String tag) {
204  if(!checkTag(tag)) return this;
205  String tmp = null;
206  while(!tag.equals(tags.peek())) {
207  tmp = (String) tags.pop();
208  write("</" + tmp + ">");
209  }
210  return this;
211  }
212 
214  while(!tags.empty()) {
215  endTag();
216  }
217  return write("\n");
218  }
219 
220  public boolean checkTag(String tag) {
221  return tags.contains(tag);
222  }
223 
224  public HTMLGenerator write(String value) {
225  try {
226  if (value == null) {
227  return this;
228  }
229  out.write(value);
230  } catch (IOException ex) {
231  Logger.getLogger(HTMLGenerator.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
232  }
233  return this;
234  }
235 
237  return doTag("br");
238  }
239 
241  return doTag("hr");
242  }
243 
244  public HTMLGenerator writeHorizontalStrut(int pixels) {
245  startTag("div", "style='height:" + pixels + "px'");
246  write("&nbsp;");
247  return endTag();
248  }
249 
251  return write("\n<script language='JavaScript' type='text/javascript'>\n");
252  }
253 
255  return write("\n</script>\n");
256  }
257 
259  return write("\n<style type='text/css'>\n");
260  }
261 
263  return write("\n</style>\n");
264  }
265 
266  public HTMLGenerator addCSSLink(String cssFile) {
267  return write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
268  cssFile +
269  "\">");
270  }
271 
272  public HTMLGenerator addJavaScriptLink(String jsFile) {
273  return write("<script type=\"text/javascript\" src=\"" +
274  jsFile +
275  "\"></script>");
276  }
277 
278 }
HTMLGenerator write(String value)
HTMLGenerator startTag(String tag, String attributes)
HTMLGenerator doTag(String tag, String[] attributes)
HTMLGenerator doTag(String tag)
void setExtraAttributes(String extraAttributes)
HTMLGenerator startAnchor(String url, String hint, String cssClass, String onClick)
HTMLGenerator writeHorizontalStrut(int pixels)
HTMLGenerator startTableCol(String[] attributes)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableRow(String[] attributes)
HTMLGenerator startTag(String tag, String[] attributes)
HTMLGenerator startTableRow(String attributes)
HTMLGenerator startAnchor(String url, String hint, String cssClass)
boolean checkTag(String tag)
HTMLGenerator endTag(String tag)
HTMLGenerator doTag(String tag, String attributes)
HTMLGenerator addJavaScriptLink(String jsFile)
HTMLGenerator startTable(String[] attributes)
HTMLGenerator endBeforeTag(String tag)
HTMLGenerator startTargetAnchor(String url, String target, String hint)
HTMLGenerator startExtAnchor(String url, String hint)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator addCSSLink(String cssFile)
HTMLGenerator startTable(String attributes)