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;
018:
019: import org.apache.commons.jxpath.DynamicPropertyHandler;
020: import org.mozilla.javascript.Context;
021: import org.mozilla.javascript.Function;
022: import org.mozilla.javascript.JavaScriptException;
023: import org.mozilla.javascript.Scriptable;
024: import org.mozilla.javascript.ScriptableObject;
025: import org.mozilla.javascript.Undefined;
026: import org.mozilla.javascript.Wrapper;
027:
028: /**
029: *
030: * @version CVS $Id: ScriptablePropertyHandler.java 433543 2006-08-22 06:22:54Z crossley $
031: */
032: public class ScriptablePropertyHandler implements
033: DynamicPropertyHandler {
034:
035: public Object getProperty(Object obj, String propertyName) {
036: Context cx = null;
037: try {
038: cx = Context.enter();
039: Scriptable s = (Scriptable) obj;
040: Object result = ScriptableObject.getProperty(s,
041: propertyName);
042: if (result == Scriptable.NOT_FOUND) {
043: result = ScriptableObject.getProperty(s, "get"
044: + propertyName.substring(0, 1).toUpperCase()
045: + (propertyName.length() > 1 ? propertyName
046: .substring(1) : ""));
047: if (result != Scriptable.NOT_FOUND
048: && result instanceof Function) {
049: try {
050: result = ((Function) result).call(cx,
051: ScriptableObject.getTopLevelScope(s),
052: s, new Object[] {});
053: } catch (JavaScriptException exc) {
054: exc.printStackTrace();
055: result = Undefined.instance;
056: }
057: }
058: if (result == Undefined.instance
059: || result == Scriptable.NOT_FOUND) {
060: result = null;
061: }
062: } else if (result instanceof Wrapper) {
063: result = ((Wrapper) result).unwrap();
064: } else if (result == Undefined.instance) {
065: result = null;
066: }
067: return result;
068: } finally {
069: Context.exit();
070: }
071: }
072:
073: public String[] getPropertyNames(Object obj) {
074: Context.enter();
075: try {
076: Object[] ids;
077: if (obj instanceof ScriptableObject) {
078: ids = ((ScriptableObject) obj).getAllIds();
079: } else {
080: ids = ((Scriptable) obj).getIds();
081: }
082: String[] result = new String[ids.length];
083: for (int i = 0; i < result.length; i++) {
084: result[i] = (String) ids[i];
085: }
086: return result;
087: } finally {
088: Context.exit();
089: }
090: }
091:
092: public void setProperty(Object obj, String propertyName,
093: Object value) {
094: Context.enter();
095: try {
096: if (!(value == null || value instanceof String
097: || value instanceof Number || value instanceof Boolean)) {
098: value = Context.toObject(value, (Scriptable) obj);
099: }
100: ScriptableObject.putProperty((Scriptable) obj,
101: propertyName, value);
102: } finally {
103: Context.exit();
104: }
105: }
106: }
|