KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
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 /**
31  *
32  * @version CVS $Id: ScriptablePropertyPointer.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

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 JavaDoc 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 JavaDoc val = ScriptableObject.getProperty(node, "length");
53                 if (val instanceof Number JavaDoc) {
54                     return ((Number JavaDoc)val).intValue();
55                 }
56             }
57         }
58         return super.getLength();
59     }
60
61     public Object JavaDoc getImmediateNode() {
62         Object JavaDoc value;
63         if (index == WHOLE_COLLECTION) {
64             value = getBaseValue();
65         }
66         else {
67             value = getBaseValue();
68             if (value instanceof Scriptable) {
69                 Object JavaDoc property =
70                     ScriptableObject.getProperty((Scriptable)value, index);
71                 if (property != Scriptable.NOT_FOUND) {
72                     value = property; // hack?
73
}
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 JavaDoc getValue() {
85         Object JavaDoc val = getNode();
86         if (val instanceof NativeArray) {
87             NativeArray arr = (NativeArray)val;
88             List JavaDoc list = new LinkedList JavaDoc();
89             int len = (int)arr.jsGet_length();
90             for (int i = 0; i < len; i++) {
91                 Object JavaDoc 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 JavaDoc value){
103         if (index == WHOLE_COLLECTION){
104             handler.setProperty(getBean(), getPropertyName(), value);
105         } else {
106             Object JavaDoc 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 JavaDoc(0));
118         } else {
119             Object JavaDoc val = handler.getProperty(getBean(), getPropertyName());
120             if (val instanceof Scriptable) {
121                 try {
122                     ScriptableObject.deleteProperty((Scriptable)val, index);
123                 } catch (Exception JavaDoc e) {
124                     e.printStackTrace();
125                 }
126             } else {
127                 super.remove();
128             }
129         }
130     }
131
132     public boolean isCollection() {
133         Object JavaDoc obj = getBaseValue();
134         if (obj instanceof NativeArray) {
135             return true;
136         }
137         return super.isCollection();
138     }
139
140     public String JavaDoc asPath(){
141         Object JavaDoc obj = getBaseValue();
142         if (!(obj instanceof Scriptable)) {
143             return super.asPath();
144         }
145         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc escape(String JavaDoc string){
163         int index = string.indexOf('\'');
164         while (index != -1){
165             string = string.substring(0, index) + "&apos;" + string.substring(index + 1);
166             index = string.indexOf('\'');
167         }
168         index = string.indexOf('\"');
169         while (index != -1){
170             string = string.substring(0, index) + "&quot;" + string.substring(index + 1);
171             index = string.indexOf('\"');
172         }
173         return string;
174     }
175
176 }
177
Popular Tags