1 16 package org.apache.cocoon.components.flow.javascript; 17 18 import java.util.LinkedList ; 19 import java.util.List ; 20 21 import org.apache.commons.jxpath.DynamicPropertyHandler; 22 import org.apache.commons.jxpath.ri.model.NodePointer; 23 import org.apache.commons.jxpath.ri.model.dynamic.DynamicPropertyPointer; 24 import org.mozilla.javascript.NativeArray; 25 import org.mozilla.javascript.Scriptable; 26 import org.mozilla.javascript.ScriptableObject; 27 import org.mozilla.javascript.Undefined; 28 import org.mozilla.javascript.Wrapper; 29 30 34 public class ScriptablePropertyPointer extends DynamicPropertyPointer { 35 36 DynamicPropertyHandler handler; 37 38 public ScriptablePropertyPointer(NodePointer parent, 39 DynamicPropertyHandler handler) { 40 super(parent, handler); 41 this.handler = handler; 42 } 43 44 public int getLength() { 45 Object obj = getBaseValue(); 46 if (obj instanceof Scriptable) { 47 Scriptable node = (Scriptable)obj; 48 if (node instanceof NativeArray) { 49 return (int)((NativeArray)node).jsGet_length(); 50 } 51 if (ScriptableObject.hasProperty(node, "length")) { 52 Object val = ScriptableObject.getProperty(node, "length"); 53 if (val instanceof Number ) { 54 return ((Number )val).intValue(); 55 } 56 } 57 } 58 return super.getLength(); 59 } 60 61 public Object getImmediateNode() { 62 Object value; 63 if (index == WHOLE_COLLECTION) { 64 value = getBaseValue(); 65 } 66 else { 67 value = getBaseValue(); 68 if (value instanceof Scriptable) { 69 Object property = 70 ScriptableObject.getProperty((Scriptable)value, index); 71 if (property != Scriptable.NOT_FOUND) { 72 value = property; } 74 } else { 75 return super.getImmediateNode(); 76 } 77 } 78 if (value instanceof Wrapper) { 79 value = ((Wrapper)value).unwrap(); 80 } 81 return value; 82 } 83 84 public Object getValue() { 85 Object val = getNode(); 86 if (val instanceof NativeArray) { 87 NativeArray arr = (NativeArray)val; 88 List list = new LinkedList (); 89 int len = (int)arr.jsGet_length(); 90 for (int i = 0; i < len; i++) { 91 Object obj = arr.get(i, arr); 92 if (obj == Undefined.instance) { 93 obj = null; 94 } 95 list.add(obj); 96 } 97 return list; 98 } 99 return super.getValue(); 100 } 101 102 public void setValue(Object value){ 103 if (index == WHOLE_COLLECTION){ 104 handler.setProperty(getBean(), getPropertyName(), value); 105 } else { 106 Object val = handler.getProperty(getBean(), getPropertyName()); 107 if (val instanceof Scriptable) { 108 ScriptableObject.putProperty((Scriptable)val, index, value); 109 } else { 110 super.setValue(value); 111 } 112 } 113 } 114 115 public void remove(){ 116 if (index == WHOLE_COLLECTION){ 117 handler.setProperty(getBean(), "length", new Integer (0)); 118 } else { 119 Object val = handler.getProperty(getBean(), getPropertyName()); 120 if (val instanceof Scriptable) { 121 try { 122 ScriptableObject.deleteProperty((Scriptable)val, index); 123 } catch (Exception e) { 124 e.printStackTrace(); 125 } 126 } else { 127 super.remove(); 128 } 129 } 130 } 131 132 public boolean isCollection() { 133 Object obj = getBaseValue(); 134 if (obj instanceof NativeArray) { 135 return true; 136 } 137 return super.isCollection(); 138 } 139 140 public String asPath(){ 141 Object obj = getBaseValue(); 142 if (!(obj instanceof Scriptable)) { 143 return super.asPath(); 144 } 145 StringBuffer buffer = new StringBuffer (); 146 buffer.append(getParent().asPath()); 147 if (buffer.length() == 0){ 148 buffer.append("/."); 149 } 150 else if (buffer.charAt(buffer.length() - 1) == '/'){ 151 buffer.append('.'); 152 } 153 buffer.append("[@name = '"); 154 buffer.append(escape(getPropertyName())); 155 buffer.append("']"); 156 if (index != WHOLE_COLLECTION && (obj instanceof NativeArray)) { 157 buffer.append('[').append(index+1).append(']'); 158 } 159 return buffer.toString(); 160 } 161 162 private String escape(String string){ 163 int index = string.indexOf('\''); 164 while (index != -1){ 165 string = string.substring(0, index) + "'" + string.substring(index + 1); 166 index = string.indexOf('\''); 167 } 168 index = string.indexOf('\"'); 169 while (index != -1){ 170 string = string.substring(0, index) + """ + string.substring(index + 1); 171 index = string.indexOf('\"'); 172 } 173 return string; 174 } 175 176 } 177 | Popular Tags |