KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > javascript > ScriptablePointer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.flow.javascript;
17
18 import java.util.Locale JavaDoc;
19
20 import org.apache.commons.jxpath.ri.QName;
21 import org.apache.commons.jxpath.ri.model.NodePointer;
22 import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
23 import org.apache.commons.jxpath.ri.model.dynamic.DynamicPointer;
24 import org.mozilla.javascript.NativeArray;
25 import org.mozilla.javascript.Scriptable;
26 import org.mozilla.javascript.ScriptableObject;
27 import org.mozilla.javascript.Wrapper;
28
29 /**
30  *
31  * @version CVS $Id: ScriptablePointer.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public class ScriptablePointer extends DynamicPointer {
34
35     Scriptable node;
36
37     final static ScriptablePropertyHandler handler =
38         new ScriptablePropertyHandler();
39
40     public ScriptablePointer(NodePointer parent,
41                              QName name,
42                              Scriptable object) {
43         super(parent, name, object, handler);
44         node = object;
45     }
46
47     public ScriptablePointer(QName name,
48                              Scriptable object,
49                              Locale JavaDoc locale) {
50         super(name, object, handler, locale);
51         node = object;
52     }
53
54     public PropertyPointer getPropertyPointer(){
55         return new ScriptablePropertyPointer(this, handler);
56     }
57
58     public int getLength() {
59         Object JavaDoc obj = getBaseValue();
60         if (obj instanceof Scriptable) {
61             Scriptable node = (Scriptable)obj;
62             if (node instanceof NativeArray) {
63                 return (int)((NativeArray)node).jsGet_length();
64             }
65             if (ScriptableObject.hasProperty(node, "length")) {
66                 Object JavaDoc val = ScriptableObject.getProperty(node, "length");
67                 if (val instanceof Number JavaDoc) {
68                     return ((Number JavaDoc)val).intValue();
69                 }
70             }
71         }
72         return super.getLength();
73     }
74
75     public Object JavaDoc getImmediateNode() {
76         Object JavaDoc value;
77         if (index == WHOLE_COLLECTION) {
78             value = node;
79         } else {
80             value = ScriptableObject.getProperty(node, index);
81             if (value == Scriptable.NOT_FOUND) {
82                 value = node; // hack: same behavior as ValueUtils.getValue()
83
}
84         }
85         if (value instanceof Wrapper) {
86             value = ((Wrapper)value).unwrap();
87         }
88         return value;
89     }
90
91     public void setValue(Object JavaDoc value){
92         if (getParent() != null) {
93             getParent().setValue(value);
94         }
95     }
96
97 }
98
Popular Tags