KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > js > JavaScript


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.js;
22
23 import java.util.WeakHashMap JavaDoc;
24 import java.lang.ref.*;
25 import org.mozilla.javascript.*;
26 import org.lobobrowser.util.*;
27
28 public class JavaScript {
29     private static JavaScript instance = new JavaScript();
30     // objectMap must be a map that uses weak keys
31
// and refers to values using weak references.
32
// Keys are java objects other than ScriptableDelegate instances.
33
private final WeakHashMap JavaDoc javaObjectToWrapper = new WeakHashMap JavaDoc();
34     
35     public static JavaScript getInstance() {
36         return instance;
37     }
38     
39     /**
40      * Returns an object that may be used by
41      * the Javascript engine.
42      * @param raw
43      * @return
44      */

45     public Object JavaDoc getJavascriptObject(Object JavaDoc raw, Scriptable scope) {
46         if(raw instanceof String JavaDoc || raw instanceof Scriptable) {
47             return raw;
48         }
49         else if(raw == null) {
50             return null;
51         }
52         else if(raw.getClass().isPrimitive()) {
53             return raw;
54         }
55         else if(raw instanceof ScriptableDelegate) {
56             // Classes that implement ScriptableDelegate retain
57
// the JavaScript object. Reciprocal linking cannot
58
// be done with weak hash maps and without leaking.
59
synchronized(this) {
60                 Object JavaDoc javascriptObject = ((ScriptableDelegate) raw).getScriptable();
61                 if(javascriptObject == null) {
62                     JavaObjectWrapper jow = new JavaObjectWrapper(JavaClassWrapperFactory.getInstance().getClassWrapper(raw.getClass()), raw);
63                     javascriptObject = jow;
64                     jow.setParentScope(scope);
65                     ((ScriptableDelegate) raw).setScriptable(jow);
66                 }
67                 return javascriptObject;
68             }
69         }
70         else if(Objects.isBoxClass(raw.getClass())) {
71             return raw;
72         }
73         else {
74             synchronized(this.javaObjectToWrapper) {
75                 //WeakHashMaps will retain keys if the value refers to the key.
76
//That's why we need to refer to the value weakly too.
77
WeakReference valueRef = (WeakReference) this.javaObjectToWrapper.get(raw);
78                 JavaObjectWrapper jow = null;
79                 if(valueRef != null) {
80                     jow = (JavaObjectWrapper) valueRef.get();
81                 }
82                 if(jow == null) {
83                     Class JavaDoc javaClass = raw.getClass();
84                     JavaClassWrapper wrapper = JavaClassWrapperFactory.getInstance().getClassWrapper(javaClass);
85                     jow = new JavaObjectWrapper(wrapper, raw);
86                     jow.setParentScope(scope);
87                     this.javaObjectToWrapper.put(raw, new WeakReference(jow));
88                 }
89                 return jow;
90             }
91         }
92     }
93     
94     public Object JavaDoc getJavaObject(Object JavaDoc javascriptObject, Class JavaDoc type) {
95         if(javascriptObject instanceof JavaObjectWrapper) {
96             return ((JavaObjectWrapper) javascriptObject).getJavaObject();
97         }
98         else if(javascriptObject == null) {
99             return null;
100         }
101         else if(type == String JavaDoc.class) {
102             if(javascriptObject instanceof String JavaDoc) {
103                 return javascriptObject;
104             }
105             else if(javascriptObject instanceof Double JavaDoc) {
106                 String JavaDoc text = String.valueOf(javascriptObject);
107                 if(text.endsWith(".0")) {
108                     return text.substring(0, text.length() - 2);
109                 }
110                 else {
111                     return text;
112                 }
113             }
114 // else if(javascriptObject instanceof Function) {
115
// return ((Function) javascriptObject).getDefaultValue(String.class);
116
// }
117
else {
118                 return String.valueOf(javascriptObject);
119             }
120         }
121         else if(type == int.class || type == Integer JavaDoc.class) {
122             if(javascriptObject instanceof Double JavaDoc) {
123                 return new Integer JavaDoc(((Double JavaDoc) javascriptObject).intValue());
124             }
125             else if(javascriptObject instanceof Integer JavaDoc) {
126                 return javascriptObject;
127             }
128             else if(javascriptObject instanceof String JavaDoc) {
129                 return Integer.valueOf((String JavaDoc) javascriptObject);
130             }
131             else if(javascriptObject instanceof Short JavaDoc) {
132                 return new Integer JavaDoc(((Short JavaDoc) javascriptObject).shortValue());
133             }
134             else if(javascriptObject instanceof Long JavaDoc) {
135                 return new Integer JavaDoc(((Long JavaDoc) javascriptObject).intValue());
136             }
137             else if(javascriptObject instanceof Float JavaDoc) {
138                 return new Integer JavaDoc(((Float JavaDoc) javascriptObject).intValue());
139             }
140             else {
141                 return javascriptObject;
142             }
143         }
144         else {
145             return javascriptObject;
146         }
147     }
148 }
149
Popular Tags