01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.flow.javascript;
18:
19: import java.util.Locale;
20:
21: import org.apache.commons.jxpath.ri.QName;
22: import org.apache.commons.jxpath.ri.model.NodePointer;
23: import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
24: import org.apache.commons.jxpath.ri.model.dynamic.DynamicPointer;
25: import org.mozilla.javascript.NativeArray;
26: import org.mozilla.javascript.Scriptable;
27: import org.mozilla.javascript.ScriptableObject;
28: import org.mozilla.javascript.Wrapper;
29:
30: /**
31: *
32: * @version CVS $Id: ScriptablePointer.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34: public class ScriptablePointer extends DynamicPointer {
35:
36: Scriptable node;
37:
38: final static ScriptablePropertyHandler handler = new ScriptablePropertyHandler();
39:
40: public ScriptablePointer(NodePointer parent, QName name,
41: Scriptable object) {
42: super (parent, name, object, handler);
43: node = object;
44: }
45:
46: public ScriptablePointer(QName name, Scriptable object,
47: Locale locale) {
48: super (name, object, handler, locale);
49: node = object;
50: }
51:
52: public PropertyPointer getPropertyPointer() {
53: return new ScriptablePropertyPointer(this , handler);
54: }
55:
56: public int getLength() {
57: Object obj = getBaseValue();
58: if (obj instanceof Scriptable) {
59: Scriptable node = (Scriptable) obj;
60: if (node instanceof NativeArray) {
61: return (int) ((NativeArray) node).jsGet_length();
62: }
63: if (ScriptableObject.hasProperty(node, "length")) {
64: Object val = ScriptableObject.getProperty(node,
65: "length");
66: if (val instanceof Number) {
67: return ((Number) val).intValue();
68: }
69: }
70: }
71: return super .getLength();
72: }
73:
74: public Object getImmediateNode() {
75: Object value;
76: if (index == WHOLE_COLLECTION) {
77: value = node;
78: } else {
79: value = ScriptableObject.getProperty(node, index);
80: if (value == Scriptable.NOT_FOUND) {
81: value = node; // hack: same behavior as ValueUtils.getValue()
82: }
83: }
84: if (value instanceof Wrapper) {
85: value = ((Wrapper) value).unwrap();
86: }
87: return value;
88: }
89:
90: public void setValue(Object value) {
91: if (getParent() != null) {
92: getParent().setValue(value);
93: }
94: }
95:
96: }
|