001: package clime.messadmin.taglib;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005:
006: import javax.servlet.jsp.JspException;
007: import javax.servlet.jsp.JspWriter;
008: import javax.servlet.jsp.PageContext;
009: import javax.servlet.jsp.tagext.BodyContent;
010:
011: /**
012: * Provides helper methods for JSP tags.
013: * Some methods from Struts 1.2
014: * @author Cédrik LIME
015: */
016: public class TagUtils {
017:
018: private TagUtils() {
019: super ();
020: }
021:
022: /**
023: * Copied and adapted from org.apache.struts.taglib.TagUtils v1.2.8
024: *
025: * Write the specified text as the response to the writer associated with
026: * this page. <strong>WARNING</strong> - If you are writing body content
027: * from the <code>doAfterBody()</code> method of a custom tag class that
028: * implements <code>BodyTag</code>, you should be calling
029: * <code>writePrevious()</code> instead.
030: *
031: * @param pageContext The PageContext object for this page
032: * @param value The text to be written
033: *
034: * @exception JspException if an input/output error occurs (already saved)
035: */
036: public static void write(PageContext pageContext,
037: boolean escapeXml, Object value) throws JspException {
038:
039: if (value == null) {
040: return;
041: }
042: JspWriter writer = pageContext.getOut();
043:
044: out(writer, escapeXml, value);
045: }
046:
047: /**
048: * Copied and adapted from org.apache.struts.taglib.TagUtils v1.2.8
049: *
050: * Write the specified text as the response to the writer associated with
051: * the body content for the tag within which we are currently nested.
052: *
053: * @param pageContext The PageContext object for this page
054: * @param value The text to be written
055: *
056: * @exception JspException if an input/output error occurs (already saved)
057: */
058: public static void writePrevious(PageContext pageContext,
059: boolean escapeXml, Object value) throws JspException {
060:
061: if (value == null) {
062: return;
063: }
064: JspWriter writer = pageContext.getOut();
065: if (writer instanceof BodyContent) {
066: writer = ((BodyContent) writer).getEnclosingWriter();
067: }
068:
069: out(writer, escapeXml, value);
070: }
071:
072: /**
073: * Copied and adapted from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2
074: *
075: * Outputs <tt>text</tt> to <tt>writer</tt> JspWriter.
076: * If <tt>escapeXml</tt> is true, performs the following substring
077: * replacements (to facilitate output to XML/HTML pages):
078: *
079: * & -> &
080: * < -> <
081: * > -> >
082: * " -> "
083: * ' -> '
084: *
085: * See also escapeXml().
086: */
087: protected static void out(JspWriter writer, boolean escapeXml,
088: Object value) throws JspException {
089:
090: if (value == null) {
091: return;
092: }
093:
094: try {
095: if (escapeXml) {
096: String text = value.toString();
097: writeEscapedXml(text.toCharArray(), text.length(),
098: writer);
099: } else {
100: writer.print(value.toString());
101: }
102: } catch (IOException e) {
103: throw new JspException(e.toString());
104: }
105: }
106:
107: /**
108: * Copied and adapted from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2
109: *
110: * Optimized to create no extra objects and write directly
111: * to the JspWriter using blocks of escaped and unescaped characters
112: *
113: */
114: protected static void writeEscapedXml(char[] buffer, int length,
115: Writer w) throws IOException {
116: if (null == buffer || null == w) {
117: return;
118: }
119: int start = 0;
120: //int length = buffer.length();
121:
122: for (int i = 0; i < length; ++i) {
123: char c = buffer[i];
124: if (c <= HIGHEST_SPECIAL) {
125: char[] escaped = specialCharactersRepresentation[c];
126: if (escaped != null) {
127: // add unescaped portion
128: if (start < i) {
129: w.write(buffer, start, i - start);
130: }
131: // add escaped xml
132: w.write(escaped);
133: start = i + 1;
134: }
135: }
136: }
137: // add rest of unescaped portion
138: if (start < length) {
139: w.write(buffer, start, length - start);
140: }
141: }
142:
143: private static final int HIGHEST_SPECIAL = '>';
144: private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
145: static {
146: specialCharactersRepresentation['&'] = "&".toCharArray();//$NON-NLS-1$
147: specialCharactersRepresentation['<'] = "<".toCharArray();//$NON-NLS-1$
148: specialCharactersRepresentation['>'] = ">".toCharArray();//$NON-NLS-1$
149: specialCharactersRepresentation['"'] = """.toCharArray();//$NON-NLS-1$
150: specialCharactersRepresentation['\''] = "'".toCharArray();//$NON-NLS-1$
151: }
152: }
|