001: /* Out.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Sep 6 16:10:51 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.servlet.dsp.action;
020:
021: import java.io.IOException;
022:
023: import org.zkoss.web.mesg.MWeb;
024: import org.zkoss.web.servlet.ServletException;
025: import org.zkoss.xml.XMLs;
026:
027: /**
028: * Generates the specified value into a string.
029: *
030: * @author tomyeh
031: */
032: public class Out extends AbstractAction {
033: private String _value = null;
034: private int _maxlength;
035: private boolean _escapeXML = true;
036: private boolean _nbsp;
037:
038: /** Returns whether to escape XML.
039: * Default: true.
040: */
041: public boolean getEscapeXML() {
042: return _escapeXML;
043: }
044:
045: /** Sets whether to escape XML.
046: */
047: public void setEscapeXML(boolean escapeXML) {
048: _escapeXML = escapeXML;
049: }
050:
051: /** Returns whether to generate   if the content is empty.
052: * Default: false.
053: */
054: public boolean getNbsp() {
055: return _escapeXML;
056: }
057:
058: /** Sets whether to generate   if the content is empty.
059: */
060: public void setNbsp(boolean nbsp) {
061: _nbsp = nbsp;
062: }
063:
064: /** Returns the value.
065: * Default: null.
066: */
067: public String getValue() {
068: return _value;
069: }
070:
071: /** Sets the value.
072: */
073: public void setValue(String value) {
074: _value = value;
075: }
076:
077: /** Returns the maxlength of bytes to output.
078: * <p>Default: 0 (no limit).
079: */
080: public int getMaxlength() {
081: return _maxlength;
082: }
083:
084: /** Sets the maxlength to output.
085: */
086: public void setMaxlength(int maxlength) {
087: _maxlength = maxlength;
088: }
089:
090: //-- Action --//
091: public void render(ActionContext ac, boolean nested)
092: throws javax.servlet.ServletException, IOException {
093: if (!isEffective())
094: return;
095: if (nested)
096: throw new ServletException(
097: MWeb.DSP_NESTED_ACTION_NOT_ALLOWED, new Object[] {
098: this , new Integer(ac.getLineNumber()) });
099:
100: int len = _value != null ? _value.length() : 0;
101: if (len == 0 || (_nbsp && _value.trim().length() == 0)) {
102: if (_nbsp)
103: ac.getOut().write(" ");
104: return;
105: }
106:
107: String value = _value;
108: if (_maxlength > 0 && len > _maxlength) {
109: int j = _maxlength;
110: while (j > 0 && Character.isWhitespace(value.charAt(j - 1)))
111: --j;
112: value = value.substring(0, j) + "...";
113: }
114:
115: if (_escapeXML) {
116: StringBuffer sb = null;
117: len = value.length();
118: for (int j = 0; j < len; ++j) {
119: final char cc = value.charAt(j);
120: final String replace = _escapeXML ? XMLs.escapeXML(cc)
121: : null;
122:
123: if (replace != null) {
124: if (sb == null) {
125: sb = new StringBuffer(value.length() + 10);
126: sb.append(value.substring(0, j));
127: }
128: sb.append(replace);
129: } else if (sb != null) {
130: sb.append(cc);
131: }
132: }
133: if (sb != null)
134: value = sb.toString();
135: }
136:
137: ac.getOut().write(value);
138: }
139:
140: //-- Object --//
141: public String toString() {
142: return "out";
143: }
144: }
|