001: /* SimpleNamespace.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue May 8 12:56:25 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 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.scripting.util;
020:
021: import java.util.Iterator;
022: import java.util.Set;
023: import java.util.Map;
024: import java.util.HashMap;
025:
026: import org.zkoss.zk.ui.Page;
027: import org.zkoss.zk.ui.Component;
028: import org.zkoss.zk.scripting.Namespace;
029:
030: /**
031: * A simple implementation of {@link Namespace}.
032: *
033: * @author tomyeh
034: */
035: public class SimpleNamespace extends AbstractNamespace {
036: private Namespace _parent;
037: private final Map _vars;
038: private final Component _owner;
039:
040: public SimpleNamespace() {
041: _vars = new HashMap(8);
042: _owner = null;
043: }
044:
045: /**
046: * @param owner the owner of this namespace.
047: * If not null, the fellow of the owner is considered as part of this namespace.
048: * In other words, {@link #containsVariable} and {@link #getVariable}
049: * will check {@link Component#getFellow}.
050: * @since 3.0.0
051: */
052: public SimpleNamespace(Component owner) {
053: _vars = new HashMap(8);
054: _owner = owner;
055: }
056:
057: /** Copies all variables from the specified namespace.
058: * Note: only variables local to the specified namespace is copied.
059: */
060: public void copy(Namespace ns) {
061: for (Iterator it = ns.getVariableNames().iterator(); it
062: .hasNext();) {
063: final String name = (String) it.next();
064: final Object value = ns.getVariable(name, true);
065: _vars.put(name, value);
066: notifyAdd(name, value);
067: }
068: }
069:
070: //Namespace//
071: public Component getOwner() {
072: return _owner;
073: }
074:
075: public Page getOwnerPage() {
076: return _owner != null ? _owner.getPage() : null;
077: }
078:
079: public Set getVariableNames() {
080: return _vars.keySet();
081: }
082:
083: public boolean containsVariable(String name, boolean local) {
084: return _vars.containsKey(name)
085: || (_owner != null && _owner.getFellowIfAny(name) != null)
086: || (!local && _parent != null && _parent
087: .containsVariable(name, false));
088: }
089:
090: public Object getVariable(String name, boolean local) {
091: Object val = _vars.get(name);
092: if (val != null || _vars.containsKey(name))
093: return val;
094:
095: if (_owner != null) {
096: val = _owner.getFellowIfAny(name);
097: if (val != null)
098: return val;
099: }
100: return local || _parent == null ? null : _parent.getVariable(
101: name, false);
102: }
103:
104: public void setVariable(String name, Object value, boolean local) {
105: if (!local && _parent != null && !_vars.containsKey(name)) {
106: for (Namespace p = _parent;;) {
107: if (p.getVariableNames().contains(name)) {
108: p.setVariable(name, value, true);
109: return; //done;
110: }
111: if ((p = p.getParent()) == null)
112: break;
113: }
114: }
115:
116: _vars.put(name, value);
117: notifyAdd(name, value);
118: }
119:
120: public void unsetVariable(String name, boolean local) {
121: if (_vars.remove(name) != null || _vars.containsKey(name)) {
122: notifyRemove(name);
123: } else if (!local && _parent != null) {
124: for (Namespace p = _parent; p != null; p = p.getParent()) {
125: if (p.getVariableNames().contains(name)) {
126: p.unsetVariable(name, true);
127: break;
128: }
129: if ((p = p.getParent()) == null)
130: break;
131: }
132: }
133: }
134:
135: public Namespace getParent() {
136: return _parent;
137: }
138:
139: public void setParent(Namespace parent) {
140: if (_parent != parent) {
141: for (Namespace p = parent; p != null; p = p.getParent())
142: if (p == this )
143: throw new IllegalArgumentException(
144: "Recursive namespace: " + this + " with "
145: + parent);
146: _parent = parent;
147: notifyParentChanged(parent);
148: }
149: }
150: }
|