001: /* AbstractAction.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Sep 7 14:55:38 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.util.Map;
022:
023: import org.zkoss.xml.XMLs;
024:
025: /**
026: * A skeletal implementation to simplify the implementation of actions.
027: *
028: * @author tomyeh
029: */
030: abstract public class AbstractAction implements Action {
031: /** A special integer to denote no attribute is assigned. */
032: protected static final int NULL_INT = Integer.MIN_VALUE + 0x68;
033: /** A special short to denote no attribute is assigned. */
034: protected static final short NULL_SHORT = Short.MIN_VALUE + 0x68;
035:
036: private boolean _if = true;
037: private boolean _unless = false;
038:
039: /** Returns the if condition.
040: * If the if condition is false, this tag is ignored.
041: * If the unless condition ({@link #getUnless}) is true, this tag is
042: * ignored, too.
043: * Tags deriving from this class shall invoke {@link #isEffective} to
044: * know the result.
045: *
046: * <p>Default: true.
047: */
048: public boolean getIf() {
049: return _if;
050: }
051:
052: /** Sets the if condition.
053: */
054: public void setIf(boolean ifcond) {
055: _if = ifcond;
056: }
057:
058: /** Returns the unless condition.
059: * If the unless condition is true, this tag is ignored.
060: * If the if condition ({@link #getIf}) is true, this tag is ignored, too.
061: * Tags deriving from this class shall invoke {@link #isEffective} to
062: * know the result.
063: *
064: * <p>Default: false.
065: */
066: public boolean getUnless() {
067: return _unless;
068: }
069:
070: /** Sets the unless condition.
071: */
072: public void setUnless(boolean unless) {
073: _unless = unless;
074: }
075:
076: /** Returns whether this tag is effecive. If false, this tag does nothing
077: * (as if not specified at all).
078: */
079: public boolean isEffective() {
080: return _if && !_unless;
081: }
082:
083: //-- Utilities --//
084: /** Returns one of {@link ActionContext#PAGE_SCOPE}, {@link ActionContext#REQUEST_SCOPE},
085: * {@link ActionContext#SESSION_SCOPE} and {@link ActionContext#APPLICATION_SCOPE}
086: * of the specified scope name.
087: *
088: * @param scope one of "page", "request", "session" and "application".
089: */
090: protected static final int toScope(String scope) {
091: return "request".equals(scope) ? ActionContext.REQUEST_SCOPE
092: : "session".equals(scope) ? ActionContext.SESSION_SCOPE
093: : "application".equals(scope) ? ActionContext.APPLICATION_SCOPE
094: : ActionContext.PAGE_SCOPE;
095: }
096:
097: /** Puts an attribute to the map,
098: * if <code>attrValue</code> is not null.
099: */
100: protected static final void put(Map params, String attrName,
101: String attrValue) {
102: if (attrValue != null)
103: params.put(attrName, attrValue);
104: }
105:
106: /** Puts an attribute to the map.
107: * Unlike othere put(...), it always puts the specified attribute.
108: */
109: protected static final void put(Map params, String attrName,
110: boolean avail) {
111: params.put(attrName, Boolean.valueOf(avail));
112: }
113:
114: /** Puts an attribute to the map,
115: * if <code>val</code> is not NULL_INT.
116: */
117: protected static final void put(Map params, String attrName, int val) {
118: if (val != NULL_INT)
119: params.put(attrName, new Integer(val));
120: }
121:
122: /** Puts an object if it is not null.
123: */
124: protected static final void put(Map params, String attrName,
125: Object attrValue) {
126: if (attrValue != null)
127: params.put(attrName, attrValue);
128: }
129:
130: /** Appends an attribute to the string buffer,
131: * if <code>attrValue</code> is not null.
132: */
133: protected static final void append(StringBuffer sb,
134: String attrName, String attrValue) {
135: if (attrValue != null)
136: sb.append(' ').append(attrName).append("=\"").append(
137: XMLs.encodeAttribute(attrValue)).append('"');
138: //it might contain " or other special characters
139: }
140:
141: /** Appends an attribute to the string buffer,
142: * if <code>avail</code> is true.
143: */
144: protected static//not final, e.g., xul has different format
145: void append(StringBuffer sb, String attrName, boolean avail) {
146: if (avail)
147: sb.append(' ').append(attrName);
148: }
149:
150: /** Appends an attribute to the string buffer,
151: * if <code>val</code> is not NULL_INT.
152: */
153: protected static final void append(StringBuffer sb,
154: String attrName, int val) {
155: if (val != NULL_INT)
156: sb.append(' ').append(attrName).append("=\"").append(
157: Integer.toString(val)).append('"');
158: }
159: }
|