2008年10月16日木曜日

HtmlStringBufferを使いやすくする

Click FrameworkのHtmlStringBufferを使うと行数が増えてつかいづらかったので以下のようにしてみました。
以下のCustomHtmlStringBufferを利用するとコードの記述量がへります。

改善前
CustomHtmlStringBuffer customHtmlStringBuffer = new CustomHtmlStringBuffer();
customHtmlStringBuffer.elementStart("div");
customHtmlStringBuffer.appendAttribute("id", "test_id");
customHtmlStringBuffer.closeTag();
customHtmlStringBuffer.append("これはテストです。");
customHtmlStringBuffer.elementEnd("div");

改善後
CustomHtmlStringBuffer customHtmlStringBuffer = new CustomHtmlStringBuffer();
customHtmlStringBuffer.elementStart("div").appendAttribute("id", "test_id").closeTag().append("これはテストです。").elementEnd("div");

ね、記述しやすくなったでしょ。




import java.util.Map;

import net.sf.click.util.HtmlStringBuffer;

public class CustomHtmlStringBuffer {

protected HtmlStringBuffer htmlStringBuffer;

public CustomHtmlStringBuffer() {

htmlStringBuffer = new HtmlStringBuffer();

}

public CustomHtmlStringBuffer append(char value) {
htmlStringBuffer.append(value);
return this;
}

public CustomHtmlStringBuffer append(int value) {
htmlStringBuffer.append(value);
return this;
}

public CustomHtmlStringBuffer append(long value) {
htmlStringBuffer.append(value);
return this;
}

public CustomHtmlStringBuffer append(Object value) {
htmlStringBuffer.append(value);
return this;
}

public CustomHtmlStringBuffer append(String value) {
htmlStringBuffer.append(value);
return this;
}

public CustomHtmlStringBuffer appendAttribute(String name, int value) {
htmlStringBuffer.appendAttribute(name, value);
return this;
}

public CustomHtmlStringBuffer appendAttribute(String name, Object value) {
htmlStringBuffer.appendAttribute(name, value);
return this;
}

public CustomHtmlStringBuffer appendAttributeDisabled() {
htmlStringBuffer.appendAttributeDisabled();
return this;
}

public CustomHtmlStringBuffer appendAttributeReadonly() {
htmlStringBuffer.appendAttributeReadonly();
return this;
}

public CustomHtmlStringBuffer appendAttributes(Map attributes) {
htmlStringBuffer.appendAttributes(attributes);
return this;
}

public CustomHtmlStringBuffer appendEscaped(Object value) {
htmlStringBuffer.appendEscaped(value);
return this;
}

public CustomHtmlStringBuffer appendStyleAttributes(Map attributes) {
htmlStringBuffer.appendStyleAttributes(attributes);
return this;
}

public CustomHtmlStringBuffer closeTag() {
htmlStringBuffer.closeTag();
return this;
}

public CustomHtmlStringBuffer elementEnd() {
htmlStringBuffer.elementEnd();
return this;
}

public CustomHtmlStringBuffer elementEnd(String name) {
htmlStringBuffer.elementEnd(name);
return this;
}

public CustomHtmlStringBuffer elementStart(String name) {
htmlStringBuffer.elementStart(name);
return this;
}

public boolean equals(Object obj) {
return htmlStringBuffer.equals(obj);
}

public int hashCode() {
return htmlStringBuffer.hashCode();
}

public boolean isJavaScriptAttribute(String name) {
return htmlStringBuffer.isJavaScriptAttribute(name);
}

public int length() {
return htmlStringBuffer.length();
}

public String toString() {
return htmlStringBuffer.toString();
}

}

0 件のコメント:

コメントを投稿