📄 jspresponsewriter.java
字号:
package com.pegasus.framework.component.taglib.util;
import com.pegasus.framework.component.taglib.xml.Xml;
import java.io.StringWriter;
import java.io.Writer;
public class JspResponseWriter {
/** The Constant LT. */
private static final String LT = "<";
/** The Constant GT. */
private static final String GT = ">";
/** The Constant COLON. */
private static final String COLON = ":";
/** The Constant SPACE. */
private static final String SPACE = " ";
/** The Constant HTML_SPACE. */
private static final String HTML_SPACE = " ";
/** The Constant EQUALS. */
private static final String EQUALS = "=";
/** The Constant TAB_STOP. */
private static final String TAB_STOP = "\t";
/** The Constant LINE_BREAK. */
private static final String LINE_BREAK = "\n";
/** The Constant SINGLE_QUOTE. */
private static final String SINGLE_QUOTE = "'";
/** The Constant DOUBLE_QUOTE. */
private static final String DOUBLE_QUOTE = "\"";
/** The Constant OPEN_COMMENT. */
private static final String OPEN_COMMENT = "<!-- ";
/** The Constant CLOSE_COMMENT. */
private static final String CLOSE_COMMENT = " -->";
/** The Constant FORWARD_SLASH. */
private static final String FORWARD_SLASH = "/";
/** The out. */
private StringWriter out = new StringWriter();
/** The pretty printing. */
private boolean prettyPrinting = true;
/** The tag depth. */
private int tagDepth;
/** The element tag is open. */
private volatile boolean elementTagIsOpen;
/** The element is empty. */
private volatile boolean elementIsEmpty;
/**
* Defualt constructor -- Sets pretty printing to 'on'.
*/
public JspResponseWriter() {
this(true);
}
/**
* Allows instanciating this writer with pretty printing turned off.设置输出有换行
*
* @param prettyPrinting the pretty printing
*/
public JspResponseWriter(final boolean prettyPrinting) {
setPrettyPrinting(prettyPrinting);
}
/**
* Set this writer to include line breaks and tabstops in the output for easier human-readable markup.
*
* @param prettyPrinting the pretty printing
*/
public void setPrettyPrinting(final boolean prettyPrinting) {
this.prettyPrinting = prettyPrinting;
}
/**
* Check for pretty printing setting.
*
* @return true, if is pretty printing
*/
public boolean isPrettyPrinting() {
return prettyPrinting;
}
/**
* Pretty print.格式化的输出
*/
private void prettyPrint() {
if (!isPrettyPrinting()) {
return;
}
lineBreak();
for (int i = 0; i < tagDepth; i++) {
tabStop();
}
tagDepth++;
}
/**
* Tab stop.输出tab
*/
public void tabStop() {
closeOpenTagIfNecessary();
out.write(TAB_STOP);
elementIsEmpty = false;
}
/**
* Start an XML element with a qualified name, up to and including the element name. Once this method has
* been called, clients can call the <code>writeAttribute()</code> or methods to add attributes and
* corresponding values. The starting element will be closed (that is, the trailing '>' character added)
* on any subsequent call to <code>startElement()</code>, <code>comment()</code>, lineBreak(),
* <code>text()</code>, <code>endElement()</code>
*
* @param qName the q name
*/
public void startElement(Xml.Html qName) {
startElement(qName.toString());
}
/**
* Start an XML element with a namespace prefix and a localName.
*
* @param prefix the prefix
* @param localName the local name
*/
public void startElement(String prefix, Xml.Html localName) {
startElement(prefix + COLON + localName);
}
/**
* start element.
*
* @param name the name
*/
private void startElement(final String name) {
closeOpenTagIfNecessary();
prettyPrint();
out.write(LT);
out.write(name);
elementTagIsOpen = true;
elementIsEmpty = true;
}
/**
* End an XML element with a qualified name.
*
* @param qName the q name
*/
public void endElement(Xml.Html qName) {
endElement(qName.toString());
}
/**
* End an XML element with a namespace prefix and a localName.
*
* @param prefix the prefix
* @param localName the local name
*/
public void endElement(String prefix, Xml.Html localName) {
endElement(prefix + COLON + localName);
}
/**
* _end element.
*
* @param name the name
*/
private void endElement(final String name) {
if (elementIsEmpty) {
out.write(SPACE);
out.write(FORWARD_SLASH);
out.write(GT);
elementIsEmpty = false;
}
else {
if (isPrettyPrinting()) {
lineBreak();
}
out.write(LT);
out.write(FORWARD_SLASH);
out.write(name);
out.write(GT);
}
elementTagIsOpen = false;
tagDepth--;
}
/**
* This method exists for the convenience of the JSP author in manually controlling the pretty printing of
* the buffered output.
*/
public void lineBreak() {
closeOpenTagIfNecessary();
out.write(LINE_BREAK);
elementIsEmpty = false;
}
/**
* Write an attribute with qualified name and corresponding value. This method may only be called after a
* call to <code>startElement()</code>, and before the element has been closed.
*
* @param qName the q name
* @param value the value
*/
public void attribute(Xml.Attr qName, String value) {
attribute(qName.toString(), value);
}
/**
* Write an attribute with this namespace prefix, and local name corresponding values. This method may
* only be called after a call to <code>startElement()</code>, and before the element has been closed.
*
* @param prefix the prefix
* @param localName the local name
* @param value the value
*/
public void attribute(String prefix, Xml.Attr localName, String value) {
attribute(prefix + localName.toString(), value);
}
/**
* attribute.
*
* @param qName the q name
* @param value the value
*/
private void attribute(String qName, String value) {
if (!elementTagIsOpen) {
throw new IllegalStateException("Attempting to write attribute " + " with no open element tag");
}
if (null == qName) {
throw new NullPointerException("Attempting to write attribute " + " with no name");
}
out.write(SPACE);
out.write(qName);
out.write(EQUALS);
out.write(DOUBLE_QUOTE);
if (value == null) {
value = " ";
}
out.write(value);
out.write(DOUBLE_QUOTE);
}
/**
* <p>
* Write an object, after converting it to a String (if necessary), and after performing any escaping
* appropriate for the markup language being rendered. If there is an open element that has been created
* by a call to <code>startElement()</code>, that element will be closed first.
* </p>
*
* @param text Text to be written
*
* @exception NullPointerException if <code>text</code> is <code>null</code>
*/
public void text(Object text) {
if (null == text) {
throw new NullPointerException("Attempting to write null text");
}
closeOpenTagIfNecessary();
out.write(text.toString());
elementIsEmpty = false;
}
/**
* Text blank.输出一个空格
*/
public void textBlank() {
text(HTML_SPACE);
}
/**
* Text blank.输出N个空格
*
* @param number the number
*/
public void textBlank(int number) {
for (int i = 0; i < number; i++) {
text(HTML_SPACE);
}
}
/**
* Text forward slash.
*/
public void textForwardSlash() {
text(FORWARD_SLASH);
}
/**
* Text line break.
*/
public void textLineBreak() {
text(LINE_BREAK);
}
/**
* <p>
* Write a comment containing the specified text, after converting that text to a String (if necessary),
* and after performing any escaping appropriate for the markup language being rendered. If there is an
* open element that has been created by a call to <code>startElement()</code>, that element will be
* closed first.
* </p>
*
* @param comment Text content of the comment
*/
public void comment(Object comment) {
if (null == comment) {
throw new NullPointerException("Attempting to write null comment");
}
closeOpenTagIfNecessary();
out.write(OPEN_COMMENT);
out.write(comment.toString());
out.write(CLOSE_COMMENT);
}
/**
* Close open tag if necessary.
*/
private void closeOpenTagIfNecessary() {
if (elementTagIsOpen) {
out.write(GT);
elementTagIsOpen = false;
}
}
/**
* Gets the buffer.
*
* @return the buffer
*/
public StringBuffer getBuffer() {
return out.getBuffer();
}
/**
* Gets the writer.
*
* @return the writer
*/
public Writer getWriter() {
return out;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -