📄 htmlbuilder.java
字号:
/*
* Copyright 2004 original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ecside.util;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ecside.common.log.LogHandler;
/**
* Will build up an Html String piece by piece.
*
* Note: unless otherwise noted if the parameter used for any of the methods is
* null or empty then that html element will not be appended. The advantage of this
* is you do not have to do any null or empty string checking to use this class.
*
* @author Jeff Johnston
*/
public class HtmlBuilder {
private Log logger = LogFactory.getLog(HtmlBuilder.class);
private Writer writer;
/**
* Default constructor using a StringWriter,
* which is really just a StringBuffer.
*/
public HtmlBuilder() {
this.writer = new StringWriter();
}
/**
* A builder with the specified Writer.
*
* @param writer The Writer to use.
*/
public HtmlBuilder(Writer writer) {
this.writer = writer;
}
/**
* Flush the writer.
*/
public void flushBuilder() {
try {
writer.flush();
} catch (IOException e) {
LogHandler.errorLog(logger, e);
}
}
/**
* Close the writer.
*/
public void closeBuilder() {
try {
writer.close();
} catch (IOException e) {
LogHandler.errorLog(logger, e);
}
}
/**
* Write out the content to the internal writer.
*/
private HtmlBuilder write(String text) {
try {
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* The length of the internal Writer.
*/
public int length() {
return writer.toString().length();
}
/**
* The Object to append. Will call the Object's toString() method.
*/
public HtmlBuilder append(Object text) {
if (text != null) {
write(text.toString());
}
return this;
}
/**
* The String to append.
*/
public HtmlBuilder append(String text) {
write(text);
return this;
}
/**
* <p>Append tabs [\t] and newlines [\n].</p>
*
* @param tabs The number of tab spaces \t to put in.
* @param newlines The number of newlines \n to put in.
*/
public HtmlBuilder format(int tabs, int newlines) {
tabs(tabs);
newlines(newlines);
return this;
}
/**
* <p>Append tabs.</p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
public HtmlBuilder tabs(int tabs) {
for (int i = 0; i < tabs; i++) {
tab();
}
return this;
}
/**
* <p>Append newlines [\n].</p>
*
* @param newlines The number of newlines \n to put in.
*/
public HtmlBuilder newlines(int newlines) {
for (int i = 0; i < newlines; i++) {
newline();
}
return this;
}
/**
* <p>Append tab [\t].</p>
*/
public HtmlBuilder tab() {
write("\t");
return this;
}
/**
* <p>Append newline [\n].</p>
*/
public HtmlBuilder newline() {
write("\n");
return this;
}
/**
* <p>Close the element [>]</p>
*/
public HtmlBuilder close() {
write(">");
return this;
}
/**
* <p>Close the element with a slash to comply with xhtml [/>]</p>
*/
public HtmlBuilder xclose() {
write("/>");
return this;
}
/**
* <p>The start of the table element [<table].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the table.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
public HtmlBuilder table(int tabs) {
newline();
tabs(tabs);
write("<table");
return this;
}
/**
* <p>The close tag of the table element [</table>].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the table ends.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
public HtmlBuilder tableEnd(int tabs) {
newline();
tabs(tabs);
write("</table>");
return this;
}
/**
* <p>The start of the button element [<button].</p>
*/
public HtmlBuilder button() {
write("<button");
return this;
}
/**
* <p>The close tag of the button element [</button>].</p>
*/
public HtmlBuilder buttonEnd() {
write("</button>");
return this;
}
/**
* <p>The start of the tr element [<tr].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the tr.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
// public HtmlBuilder tr(int tabs) {
// newline();
// tabs(tabs);
// write("<tr");
//
// return this;
// }
public HtmlBuilder tr(int tabs) {
// newline();
// tabs(tabs);
write("<tr");
return this;
}
/**
* <p>The close tag of the tr element [</tr>].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the tr ends.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
// public HtmlBuilder trEnd(int tabs) {
// newline();
// tabs(tabs);
// write("</tr>");
//
// return this;
// }
public HtmlBuilder trEnd(int tabs) {
write("</tr>");
return this;
}
/**
* <p>The start of the th element [<th].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the th.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
public HtmlBuilder th(int tabs) {
newline();
tabs(tabs);
write("<th");
return this;
}
/**
* <p>The close tag of the th element [</th>].</p>
*/
public HtmlBuilder thEnd() {
write("</th>");
return this;
}
/**
* <p>The start of the td element [<td].</p>
*
* <p>
* Also appends a newline [\n] and the specified number of tabs [\t]
* before the td.
* </p>
*
* @param tabs The number of tab spaces [\t] to put in.
*/
// public HtmlBuilder td(int tabs) {
// newline();
// tabs(tabs);
// write("<td");
//
// return this;
// }
public HtmlBuilder td(int tabs) {
write("<td");
return this;
}
/**
* <p>The close tag of the td element [</td>].</p>
*/
public HtmlBuilder tdEnd() {
write("</td>");
return this;
}
/**
* <p>The start of the input element [<input].</p>
*/
public HtmlBuilder input() {
write("<input");
return this;
}
/**
* <p>The type attribute [type=].</p>
*/
public HtmlBuilder type(String type) {
if (StringUtils.isNotBlank(type)) {
write(" type=\"").write(type).write("\" ");
}
return this;
}
/**
* <p>Combines the start of the input element and the
* type attribute [<input type=].
* </p>
*/
public HtmlBuilder input(String type) {
write("<input type=\"").write(type).write("\" ");
return this;
}
/**
* <p>The start of the select element [<select].</p>
*/
public HtmlBuilder select() {
write("<select");
return this;
}
/**
* <p>The close tag of the select element [</select>].</p>
*/
public HtmlBuilder selectEnd() {
write("</select>");
return this;
}
/**
* <p>The start of the option element [<option].</p>
*/
public HtmlBuilder option() {
write("<option");
return this;
}
/**
* <p>The close tag of the option element [</option>].</p>
*/
public HtmlBuilder optionEnd() {
write("</option>");
return this;
}
/**
* <p>The start of the form element [<form].</p>
*
* <p>Also appends a newline [\n] before the form.</p>
*/
public HtmlBuilder form() {
newline();
write("<form");
return this;
}
/**
* <p>The close tag of the form element [</form>].</p>
*
* <p>Also appends a newline [\n] before the end.</p>
*/
public HtmlBuilder formEnd() {
newline();
write("</form>");
return this;
}
/**
* <p>The name attribute [name=].</p>
*/
public HtmlBuilder name(String name) {
if (StringUtils.isNotBlank(name)) {
write(" name=\"").write(name).write("\" ");
}
return this;
}
/**
* <p>The value attribute [value=].</p>
*
* <p>If the value parameter is null or empty then
* will append a empty value element.</p>
*/
public HtmlBuilder value(String value) {
if (StringUtils.isNotBlank(value)) {
write(" value=\"").write(value).write("\" ");
} else {
write(" value=\"").write("\" ");
}
return this;
}
/**
* <p>The title attribute [title=].</p>
*/
public HtmlBuilder title(String title) {
if (StringUtils.isNotBlank(title)) {
write(" title=\"").write(title).write("\" ");
}
return this;
}
/**
* <p>The action attribute [action=].</p>
*/
public HtmlBuilder action(String action) {
write(" action=\"");
if (StringUtils.isNotBlank(action)) {
write(action);
}
write("\" ");
return this;
}
/**
* <p>The method attribute [method=].</p>
*/
public HtmlBuilder method(String method) {
if (StringUtils.isNotBlank(method)) {
write(" method=\"").write(method).write("\" ");
}
return this;
}
/**
* <p>The enctype attribute [enctype=].</p>
*/
public HtmlBuilder enctype(String enctype) {
if (StringUtils.isNotBlank(enctype)) {
write(" enctype=\"").write(enctype).write("\" ");
}
return this;
}
/**
* <p>The onchange attribute [onchange=].</p>
*/
public HtmlBuilder onchange(String onchange) {
if (StringUtils.isNotBlank(onchange)) {
write(" onchange=\"").write(onchange).write("\" ");
}
return this;
}
/**
* <p>The onsubmit attribute [onsubmit=].</p>
*/
public HtmlBuilder onsubmit(String onsubmit) {
if (StringUtils.isNotBlank(onsubmit)) {
write(" onsubmit=\"").write(onsubmit).write("\" ");
}
return this;
}
/**
* <p>The onclick attribute [onclick=].</p>
*/
public HtmlBuilder onclick(String onclick) {
if (StringUtils.isNotBlank(onclick)) {
write(" onclick=\"").write(onclick).write("\" ");
}
return this;
}
/**
* <p>The ondblclick attribute [ondblclick=].</p>
*/
public HtmlBuilder ondblclick(String ondblclick) {
if (StringUtils.isNotBlank(ondblclick)) {
write(" ondblclick=\"").write(ondblclick).write("\" ");
}
return this;
}
/**
* <p>The onmouseover attribute [onmouseover=].</p>
*/
public HtmlBuilder onmouseover(String onmouseover) {
if (StringUtils.isNotBlank(onmouseover)) {
write(" onmouseover=\"").write(onmouseover).write("\" ");
}
return this;
}
/**
* <p>The onmouseout attribute [onmouseout=].</p>
*/
public HtmlBuilder onmouseout(String onmouseout) {
if (StringUtils.isNotBlank(onmouseout)) {
write(" onmouseout=\"").write(onmouseout).write("\" ");
}
return this;
}
public HtmlBuilder tagAttributes(String tagAttributes) {
if (StringUtils.isNotBlank(tagAttributes)) {
write(" ").write(tagAttributes).write(" ");
}
return this;
}
/**
* <p>The onkeypress attribute [onkeypress=].</p>
*/
public HtmlBuilder onkeypress(String onkeypress) {
if (StringUtils.isNotBlank(onkeypress)) {
write(" onkeypress=\"").write(onkeypress).write("\" ");
}
return this;
}
/**
* <p>The id attribute [id=].</p>
*/
public HtmlBuilder id(String id) {
if (StringUtils.isNotBlank(id)) {
write(" id=\"").write(id).write("\" ");
}
return this;
}
/**
* <p>The class attribute [class=].</p>
*/
public HtmlBuilder styleClass(String styleClass) {
if (StringUtils.isNotBlank(styleClass)) {
write(" class=\"").write(styleClass).write("\" ");
}
return this;
}
/**
* <p>The style attribute [style=].</p>
*/
public HtmlBuilder style(String style) {
if (StringUtils.isNotBlank(style)) {
write(" style=\"").write(style).write("\" ");
}
return this;
}
/**
* <p>The width attribute [width=].</p>
*/
public HtmlBuilder width(String width) {
if (StringUtils.isNotBlank(width)) {
write(" width=\"").write(width).write("\" ");
}
return this;
}
public HtmlBuilder height(String height) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -