| Constructor: |
public HtmlResponseWriter(Writer writer,
String contentType,
String encoding) throws FacesException {
// At the beginning of a line, match // followed by any amount of
// whitespace, followed by < ![CDATA[
CDATA_START_SLASH_SLASH = Pattern.compile("^//\\s*\\Q< ![CDATA[\\E");
// At the end of a line, match // followed by any amout of whitespace,
// followed by ]] >
CDATA_END_SLASH_SLASH = Pattern.compile("//\\s*\\Q]] >\\E$");
// At the beginning of a line, match /* followed by any amout of
// whitespace, followed by < ![CDATA[, followed by any amount of whitespace,
// followed by */
CDATA_START_SLASH_STAR = Pattern.compile("^/\\*\\s*\\Q< ![CDATA[\\E\\s*\\*/");
// At the end of a line, match /* followed by any amount of whitespace,
// followed by ]] > followed by any amount of whitespace, followed by */
CDATA_END_SLASH_STAR = Pattern.compile("/\\*\\s*\\Q]] >\\E\\s*\\*/$");
this(writer, contentType, encoding, null, null, null);
}
Constructor sets the ResponseWriter and
encoding, and enables script hiding by default. Parameters:
writer - the ResponseWriter
contentType - the content type.
encoding - the character encoding.
Throws:
javax.faces.FacesException - the encoding is not recognized.
|
public HtmlResponseWriter(Writer writer,
String contentType,
String encoding,
Boolean isScriptHidingEnabled,
Boolean isScriptInAttributeValueEnabled,
WebConfiguration.DisableUnicodeEscaping disableUnicodeEscaping) throws FacesException {
this.writer = writer;
if (null != contentType) {
this.contentType = contentType;
}
this.encoding = encoding;
// init those configuration parameters not yet initialized
WebConfiguration webConfig = null;
if (isScriptHidingEnabled == null) {
webConfig = getWebConfiguration(webConfig);
isScriptHidingEnabled = (null == webConfig) ? BooleanWebContextInitParameter.EnableJSStyleHiding.getDefaultValue() :
webConfig.isOptionEnabled(
BooleanWebContextInitParameter.EnableJSStyleHiding);
}
if (isScriptInAttributeValueEnabled == null) {
webConfig = getWebConfiguration(webConfig);
isScriptInAttributeValueEnabled = (null == webConfig) ? BooleanWebContextInitParameter.EnableScriptInAttributeValue.getDefaultValue() :
webConfig.isOptionEnabled(
BooleanWebContextInitParameter.EnableScriptInAttributeValue);
}
if (disableUnicodeEscaping == null) {
webConfig = getWebConfiguration(webConfig);
disableUnicodeEscaping =
WebConfiguration.DisableUnicodeEscaping.getByValue(
(null == webConfig) ? WebConfiguration.WebContextInitParameter.DisableUnicodeEscaping.getDefaultValue() :
webConfig.getOptionValue(
WebConfiguration.WebContextInitParameter.DisableUnicodeEscaping));
if (disableUnicodeEscaping == null) {
disableUnicodeEscaping = WebConfiguration.DisableUnicodeEscaping.False;
}
}
// and store them for later use
this.isScriptHidingEnabled = isScriptHidingEnabled;
this.isScriptInAttributeValueEnabled = isScriptInAttributeValueEnabled;
this.disableUnicodeEscaping = disableUnicodeEscaping;
this.attributesBuffer = new FastStringWriter(128);
// Check the character encoding
if (!HtmlUtils.validateEncoding(encoding)) {
throw new IllegalArgumentException(MessageUtils.getExceptionMessageString(
MessageUtils.ENCODING_ERROR_MESSAGE_ID));
}
String charsetName = encoding.toUpperCase();
switch (disableUnicodeEscaping)
{
case True:
// html escape noting (except the dangerous characters like "< >'" etc
escapeUnicode = false;
escapeIso = false;
break;
case False:
// html escape any non-ascii character
escapeUnicode = true;
escapeIso = true;
break;
case Auto:
// is stream capable of rendering unicode, do not escape
escapeUnicode = !HtmlUtils.isUTFencoding(charsetName);
// is stream capable of rendering unicode or iso-8859-1, do not escape
escapeIso = !HtmlUtils.isISO8859_1encoding(charsetName) && !HtmlUtils.isUTFencoding(charsetName);
break;
}
}
Constructor sets the ResponseWriter and
encoding.
The argument configPrefs is a map of configurable prefs that affect
this instance's behavior. Supported keys are:
BooleanWebContextInitParameter.EnableJSStyleHiding: true
if the writer should attempt to hide JS from older browsers
Parameters:
writer - the ResponseWriter
contentType - the content type.
encoding - the character encoding.
Throws:
javax.faces.FacesException - the encoding is not recognized.
|
| Method from com.sun.faces.renderkit.html_basic.HtmlResponseWriter Detail: |
public ResponseWriter cloneWithWriter(Writer writer) {
try {
return new HtmlResponseWriter(writer,
getContentType(),
getCharacterEncoding(),
isScriptHidingEnabled,
isScriptInAttributeValueEnabled,
disableUnicodeEscaping);
} catch (FacesException e) {
// This should never happen
throw new IllegalStateException();
}
}
|
public void close() throws IOException {
closeStartIfNecessary();
writer.close();
}
Methods From java.io.Writer |
public void endDocument() throws IOException {
writer.flush();
}
Output the text for the end of a document. |
public void endElement(String name) throws IOException {
if (name == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name"));
}
if (!writingCdata) {
// always turn escaping back on once an element ends unless we're
// still writing cdata content
dontEscape = false;
}
isXhtml = getContentType().equals(
RIConstants.XHTML_CONTENT_TYPE);
if (isScriptOrStyle(name)
&& !scriptOrStyleSrc
&& writer instanceof FastStringWriter) {
String result = ((FastStringWriter) writer).getBuffer().toString();
writer = origWriter;
if (result != null) {
String trim = result.trim();
if (isXhtml) {
if (isScript) {
Matcher
cdataStartSlashSlash =
CDATA_START_SLASH_SLASH.matcher(trim),
cdataEndSlashSlash =
CDATA_END_SLASH_SLASH.matcher(trim),
cdataStartSlashStar =
CDATA_START_SLASH_STAR.matcher(trim),
cdataEndSlashStar =
CDATA_END_SLASH_STAR.matcher(trim);
int trimLen = trim.length(), start, end;
// case 1 start is // end is //
if (cdataStartSlashSlash.find() &&
cdataEndSlashSlash.find()) {
start = cdataStartSlashSlash.end() - cdataStartSlashSlash.start();
end = trimLen - (cdataEndSlashSlash.end() - cdataEndSlashSlash.start());
writer.write(trim.substring(start, end));
}
// case 2 start is // end is /* */
else if ((null != cdataStartSlashSlash.reset() && cdataStartSlashSlash.find()) &&
cdataEndSlashStar.find()) {
start = cdataStartSlashSlash.end() - cdataStartSlashSlash.start();
end = trimLen - (cdataEndSlashStar.end() - cdataEndSlashStar.start());
writer.write(trim.substring(start, end));
}
// case 3 start is /* */ end is /* */
else if (cdataStartSlashStar.find() &&
(null != cdataEndSlashStar.reset() && cdataEndSlashStar.find())) {
start = cdataStartSlashStar.end() - cdataStartSlashStar.start();
end = trimLen - (cdataEndSlashStar.end() - cdataEndSlashStar.start());
writer.write(trim.substring(start, end));
}
// case 4 start is /* */ end is //
else if ((null != cdataStartSlashStar.reset() && cdataStartSlashStar.find()) &&
(null != cdataEndSlashStar.reset() && cdataEndSlashSlash.find())) {
start = cdataStartSlashStar.end() - cdataStartSlashStar.start();
end = trimLen - (cdataEndSlashSlash.end() - cdataEndSlashSlash.start());
writer.write(trim.substring(start, end));
}
// case 5 no commented out cdata present.
else {
writer.write(result);
}
} else {
if (trim.startsWith("< ![CDATA[") && trim.endsWith("]] >")) {
writer.write(trim.substring(9, trim.length() - 3));
} else {
writer.write(result);
}
}
} else {
if (trim.startsWith("< !--") && trim.endsWith("//-- >")) {
writer.write(trim.substring(4, trim.length() - 5));
} else {
writer.write(result);
}
}
}
if (isXhtml) {
if (!writingCdata) {
if (isScript) {
writer.write("\n//]] >\n");
} else {
writer.write("\n]] >\n");
}
}
} else {
if (isScriptHidingEnabled) {
writer.write("\n//-- >\n");
}
}
}
isScript = false;
isStyle = false;
if ("cdata".equalsIgnoreCase(name)) {
writer.write("]] >");
writingCdata = false;
isCdata = false;
dontEscape = false;
return;
}
// See if we need to close the start of the last element
if (closeStart) {
boolean isEmptyElement = HtmlUtils.isEmptyElement(name);
// Tricky: we need to use the writer ivar here, rather than the
// one from the FacesContext because we don't want
// spurious / > characters to appear in the output.
if (isEmptyElement) {
flushAttributes();
writer.write(" / >");
closeStart = false;
return;
}
flushAttributes();
writer.write(' >");
closeStart = false;
}
writer.write("< /");
writer.write(name);
writer.write(' >");
}
Write the end of an element. This method will first
close any open element created by a call to
startElement().
|
public void flush() throws IOException {
// NOTE: Internal buffer's contents (the ivar "buffer") is
// written to the contained writer in the HtmlUtils class - see
// HtmlUtils.flushBuffer method; Buffering is done during
// writeAttribute/writeText - otherwise, output is written
// directly to the writer (ex: writer.write(....)..
//
// close any previously started element, if necessary
closeStartIfNecessary();
}
Flush any buffered output to the contained writer. |
public String getCharacterEncoding() {
return encoding;
}
|
public String getContentType() {
return contentType;
}
|
public void startDocument() throws IOException {
// do nothing;
}
|
public void startElement(String name,
UIComponent componentForElement) throws IOException {
if (name == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name"));
}
closeStartIfNecessary();
isScriptOrStyle(name);
scriptOrStyleSrc = false;
if ("cdata".equalsIgnoreCase(name)) {
isCdata = true;
writingCdata = true;
dontEscape = true;
writer.write("< ![CDATA[");
closeStart = false;
return;
} else if (writingCdata) {
// starting an element within a cdata section,
// keep escaping disabled
isCdata = false;
writingCdata = true;
dontEscape = true;
}
writer.write('< ");
writer.write(name);
closeStart = true;
}
Write the start of an element, up to and including the
element name. Clients call writeAttribute() or
writeURIAttribute() methods to add attributes after
calling this method.
|
public void write(char[] cbuf) throws IOException {
closeStartIfNecessary();
writer.write(cbuf);
}
|
public void write(int c) throws IOException {
closeStartIfNecessary();
writer.write(c);
}
|
public void write(String str) throws IOException {
closeStartIfNecessary();
writer.write(str);
}
|
public void write(char[] cbuf,
int off,
int len) throws IOException {
closeStartIfNecessary();
writer.write(cbuf, off, len);
}
|
public void write(String str,
int off,
int len) throws IOException {
closeStartIfNecessary();
writer.write(str, off, len);
}
|
public void writeAttribute(String name,
Object value,
String componentPropertyName) throws IOException {
if (name == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name"));
}
if (value == null) {
return;
}
if (isCdata) {
return;
}
if (name.equalsIgnoreCase("src") && isScriptOrStyle()) {
scriptOrStyleSrc = true;
}
Class valueClass = value.getClass();
// Output Boolean values specially
if (valueClass == Boolean.class) {
if (Boolean.TRUE.equals(value)) {
// NOTE: HTML 4.01 states that boolean attributes
// may legally take a single value which is the
// name of the attribute itself or appear using
// minimization.
// http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4.2
attributesBuffer.write(' ");
attributesBuffer.write(name);
attributesBuffer.write("=\"");
attributesBuffer.write(name);
attributesBuffer.write('"");
}
} else {
attributesBuffer.write(' ");
attributesBuffer.write(name);
attributesBuffer.write("=\"");
// write the attribute value
String val = value.toString();
ensureTextBufferCapacity(val);
HtmlUtils.writeAttribute(attributesBuffer,
escapeUnicode,
escapeIso,
buffer,
val,
textBuffer,
isScriptInAttributeValueEnabled);
attributesBuffer.write('"");
}
}
Write a properly escaped attribute name and the corresponding
value. The value text will be converted to a String if
necessary. This method may only be called after a call to
startElement(), and before the opened element has been
closed.
|
public void writeComment(Object comment) throws IOException {
if (comment == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID));
}
if (writingCdata) {
return;
}
closeStartIfNecessary();
// Don't include a trailing space after the '< !--'
// or a leading space before the '-- >' to support
// IE conditional commentsoth
writer.write("< !--");
writer.write(comment.toString());
writer.write("-- >");
}
Write a comment string containing the specified text.
The text will be converted to a String if necessary.
If there is an open element that has been created by a call
to startElement(), that element will be closed
first.
|
public void writeText(char text) throws IOException {
closeStartIfNecessary();
if (dontEscape) {
writer.write(text);
} else {
charHolder[0] = text;
HtmlUtils.writeText(writer, escapeUnicode, escapeIso, buffer, charHolder);
}
}
Write a properly escaped single character, If there
is an open element that has been created by a call to
startElement(), that element will be closed first.
All angle bracket occurrences in the argument must be escaped
using the > < syntax.
|
public void writeText(char[] text) throws IOException {
if (text == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "text"));
}
closeStartIfNecessary();
if (dontEscape) {
writer.write(text);
} else {
HtmlUtils.writeText(writer, escapeUnicode, escapeIso, buffer, text);
}
}
Write properly escaped text from a character array.
The output from this command is identical to the invocation:
writeText(c, 0, c.length).
If there is an open element that has been created by a call to
startElement(), that element will be closed first.
All angle bracket occurrences in the argument must be escaped
using the > < syntax.
|
public void writeText(Object text,
String componentPropertyName) throws IOException {
if (text == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "text"));
}
closeStartIfNecessary();
if (dontEscape) {
writer.write(text.toString());
} else {
String val = text.toString();
ensureTextBufferCapacity(val);
HtmlUtils.writeText(writer,
escapeUnicode,
escapeIso,
buffer,
val,
textBuffer);
}
}
Write a properly escaped object. The object will be converted
to a String if necessary. If there is an open element
that has been created by a call to startElement(),
that element will be closed first.
|
public void writeText(char[] text,
int off,
int len) throws IOException {
if (text == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "text"));
}
if (off < 0 || off > text.length || len < 0 || len > text.length) {
throw new IndexOutOfBoundsException();
}
closeStartIfNecessary();
if (dontEscape) {
writer.write(text, off, len);
} else {
HtmlUtils.writeText(writer, escapeUnicode, escapeIso, buffer, text, off, len);
}
}
Write properly escaped text from a character array.
If there is an open element that has been created by a call
to startElement(), that element will be closed
first.
All angle bracket occurrences in the argument must be escaped
using the > < syntax.
|
public void writeURIAttribute(String name,
Object value,
String componentPropertyName) throws IOException {
if (name == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name"));
}
if (value == null) {
throw new NullPointerException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "value"));
}
if (isCdata) {
return;
}
if (name.equalsIgnoreCase("src") && isScriptOrStyle()) {
scriptOrStyleSrc = true;
}
attributesBuffer.write(' ");
attributesBuffer.write(name);
attributesBuffer.write("=\"");
String stringValue = value.toString();
ensureTextBufferCapacity(stringValue);
// Javascript URLs should not be URL-encoded
if (stringValue.startsWith("javascript:")) {
HtmlUtils.writeAttribute(attributesBuffer,
escapeUnicode,
escapeIso,
buffer,
stringValue,
textBuffer,
isScriptInAttributeValueEnabled);
} else {
HtmlUtils.writeURL(attributesBuffer,
stringValue,
textBuffer,
encoding,
getContentType());
}
attributesBuffer.write('"");
}
Write a properly encoded URI attribute name and the corresponding
value. The value text will be converted to a String if necessary).
This method may only be called after a call to
startElement(), and before the opened element has been
closed.
|