1 16 package org.apache.cocoon.components.flow.javascript; 17 18 import java.util.Locale ; 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 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 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 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 val = ScriptableObject.getProperty(node, "length"); 67 if (val instanceof Number ) { 68 return ((Number )val).intValue(); 69 } 70 } 71 } 72 return super.getLength(); 73 } 74 75 public Object getImmediateNode() { 76 Object 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; } 84 } 85 if (value instanceof Wrapper) { 86 value = ((Wrapper)value).unwrap(); 87 } 88 return value; 89 } 90 91 public void setValue(Object value){ 92 if (getParent() != null) { 93 getParent().setValue(value); 94 } 95 } 96 97 } 98 | Popular Tags |