001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.javascript.fom;
018:
019: import org.mozilla.javascript.Context;
020: import org.mozilla.javascript.Scriptable;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: /**
027: * @version CVS $Id: PageLocalScopeImpl.java 433543 2006-08-22 06:22:54Z crossley $
028: */
029: public class PageLocalScopeImpl implements PageLocalScope {
030:
031: private Map locals;
032: private Scriptable scope;
033:
034: public PageLocalScopeImpl(Scriptable scope) {
035: locals = new HashMap();
036: this .scope = scope;
037: }
038:
039: private Scriptable newObject() {
040: try {
041: return Context.getCurrentContext().newObject(scope);
042: } catch (Exception ignored) {
043: // can't happen here
044: ignored.printStackTrace();
045: throw new Error("error: " + ignored);
046: }
047: }
048:
049: private PageLocalScopeImpl(PageLocalScopeImpl toBeCloned) {
050: this .scope = toBeCloned.scope;
051: locals = new HashMap();
052: Iterator iter = toBeCloned.locals.entrySet().iterator();
053: while (iter.hasNext()) {
054: Map.Entry e = (Map.Entry) iter.next();
055: Object key = e.getKey();
056: Object value = e.getValue();
057: // clone it
058: Scriptable obj = (Scriptable) value;
059: Scriptable newObj = newObject();
060: Object[] ids = obj.getIds();
061: for (int i = 0; i < ids.length; i++) {
062: String name = ids[i].toString();
063: newObj.put(name, newObj, obj.get(name, obj));
064: }
065: value = newObj;
066: locals.put(key, value);
067: }
068: }
069:
070: private Scriptable resolve(PageLocal local) {
071: final Object id = local.getId();
072: Scriptable result = (Scriptable) locals.get(id);
073: if (result == null) {
074: locals.put(id, result = newObject());
075: }
076: return result;
077: }
078:
079: public boolean has(PageLocal local, String name) {
080: Scriptable obj = resolve(local);
081: return obj.has(name, obj);
082: }
083:
084: public boolean has(PageLocal local, int index) {
085: Scriptable obj = resolve(local);
086: return obj.has(index, obj);
087: }
088:
089: public Object get(PageLocal local, String name) {
090: Scriptable obj = resolve(local);
091: return obj.get(name, obj);
092: }
093:
094: public Object get(PageLocal local, int index) {
095: Scriptable obj = resolve(local);
096: return obj.get(index, obj);
097: }
098:
099: public void put(PageLocal local, String name, Object value) {
100: Scriptable obj = resolve(local);
101: obj.put(name, obj, value);
102: }
103:
104: public void put(PageLocal local, int index, Object value) {
105: Scriptable obj = resolve(local);
106: obj.put(index, obj, value);
107: }
108:
109: public void delete(PageLocal local, String name) {
110: Scriptable obj = resolve(local);
111: obj.delete(name);
112: }
113:
114: public void delete(PageLocal local, int index) {
115: Scriptable obj = resolve(local);
116: obj.delete(index);
117: }
118:
119: public Object[] getIds(PageLocal local) {
120: Scriptable obj = resolve(local);
121: return obj.getIds();
122: }
123:
124: public Object getDefaultValue(PageLocal local, Class hint) {
125: Scriptable obj = resolve(local);
126: return obj.getDefaultValue(hint);
127: }
128:
129: public PageLocalScopeImpl duplicate() {
130: return new PageLocalScopeImpl(this );
131: }
132:
133: public PageLocal createPageLocal() {
134: // not used
135: return null;
136: }
137: }
|