001: /* ExecutionResolver.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Fri Jun 24 12:22:23 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.zk.xel.impl;
020:
021: import java.util.Collections;
022:
023: import org.zkoss.xel.VariableResolver;
024: import org.zkoss.xel.XelException;
025:
026: import org.zkoss.zk.ui.Execution;
027: import org.zkoss.zk.ui.Page;
028: import org.zkoss.zk.ui.Component;
029: import org.zkoss.zk.ui.UiException;
030: import org.zkoss.zk.ui.sys.ExecutionCtrl;
031:
032: /**
033: * A variable resolver that is based on the specified execution.
034: *
035: * @author tomyeh
036: * @since 3.0.0
037: */
038: public class ExecutionResolver implements VariableResolver {
039: /** The parent resolver. */
040: private final VariableResolver _parent;
041: private final Execution _exec;
042: private Object _self;
043:
044: /** Constructs a resolver with a parent and a page.
045: * @param parent the parent resolver (null means ignored).
046: * @param exec the current execution
047: */
048: public ExecutionResolver(Execution exec, VariableResolver parent) {
049: if (exec == null)
050: throw new NullPointerException();
051: _exec = exec;
052: _parent = parent;
053: }
054:
055: /** Sets the self variable.
056: * The self variable also acts as the context to resolve other variables.
057: */
058: public void setSelf(Object self) {
059: _self = self;
060: }
061:
062: /** Returns the self variable.
063: */
064: public Object getSelf() {
065: return _self;
066: }
067:
068: //-- VariableResolver --//
069: public Object resolveVariable(String name) throws XelException {
070: if (name == null || name.length() == 0) //just in case
071: return null;
072:
073: switch (name.charAt(0)) {
074: case 'a':
075: if ("arg".equals(name))
076: return _exec.getArg();
077: break;
078: case 'c':
079: if ("componentScope".equals(name)) {
080: if (_self instanceof Component)
081: return ((Component) _self)
082: .getAttributes(Component.COMPONENT_SCOPE);
083: return Collections.EMPTY_MAP;
084: }
085: break;
086: case 'd':
087: if ("desktopScope".equals(name))
088: return _exec.getDesktop().getAttributes();
089: if ("desktop".equals(name))
090: return _exec.getDesktop();
091: break;
092: case 'p':
093: if ("pageScope".equals(name)) {
094: if (_self instanceof Component)
095: return ((Component) _self)
096: .getAttributes(Component.PAGE_SCOPE);
097: if (_self instanceof Page)
098: return ((Page) _self).getAttributes();
099: final Page page = ((ExecutionCtrl) _exec)
100: .getCurrentPage();
101: return page != null ? page.getAttributes()
102: : Collections.EMPTY_MAP;
103: }
104: if ("page".equals(name)) {
105: if (_self instanceof Component)
106: return ((Component) _self).getPage();
107: if (_self instanceof Page)
108: return (Page) _self;
109: return ((ExecutionCtrl) _exec).getCurrentPage();
110: }
111: break;
112: case 'r':
113: if ("requestScope".equals(name))
114: return _exec.getAttributes();
115: break;
116: case 's':
117: if ("self".equals(name))
118: return _self;
119: if ("sessionScope".equals(name))
120: return _exec.getDesktop().getSession().getAttributes();
121: if ("session".equals(name))
122: return _exec.getDesktop().getSession();
123: if ("spaceOwner".equals(name)) {
124: if (_self instanceof Component)
125: return ((Component) _self).getSpaceOwner();
126: if (_self instanceof Page)
127: return (Page) _self;
128: return null;
129: }
130: if ("spaceScope".equals(name)) {
131: if (_self instanceof Component)
132: return ((Component) _self)
133: .getAttributes(Component.SPACE_SCOPE);
134: if (_self instanceof Page)
135: return ((Page) _self).getAttributes();
136: return Collections.EMPTY_MAP;
137: }
138: break;
139: }
140:
141: if (_self instanceof Component) {
142: final Component comp = (Component) _self;
143:
144: //We have to look getZScriptVariable first and then namespace
145: //so it is in the same order of interpreter
146: final Page page = comp.getPage();
147: if (page != null) {
148: final Object o = page.getZScriptVariable(comp
149: .getNamespace(), name);
150: if (o != null)
151: return o;
152: }
153:
154: final Object o = comp.getVariable(name, false);
155: if (o != null)
156: return o;
157: } else {
158: Page page;
159: if (_self instanceof Page) {
160: page = (Page) _self;
161: } else {
162: page = ((ExecutionCtrl) _exec).getCurrentPage();
163: }
164:
165: if (page != null) {
166: Object o = page.getZScriptVariable(name);
167: if (o != null)
168: return o;
169:
170: o = page.getVariable(name);
171: if (o != null)
172: return o;
173: }
174: }
175:
176: return _parent != null ? _parent.resolveVariable(name) : null;
177: }
178:
179: //Object//
180: public String toString() {
181: return "[ExecutionResolver: " + _self + ']';
182: }
183: }
|