1 21 package org.lobobrowser.js; 22 23 import java.util.WeakHashMap ; 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 private final WeakHashMap javaObjectToWrapper = new WeakHashMap (); 34 35 public static JavaScript getInstance() { 36 return instance; 37 } 38 39 45 public Object getJavascriptObject(Object raw, Scriptable scope) { 46 if(raw instanceof String || 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 synchronized(this) { 60 Object 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 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 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 getJavaObject(Object javascriptObject, Class 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 .class) { 102 if(javascriptObject instanceof String ) { 103 return javascriptObject; 104 } 105 else if(javascriptObject instanceof Double ) { 106 String 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 { 118 return String.valueOf(javascriptObject); 119 } 120 } 121 else if(type == int.class || type == Integer .class) { 122 if(javascriptObject instanceof Double ) { 123 return new Integer (((Double ) javascriptObject).intValue()); 124 } 125 else if(javascriptObject instanceof Integer ) { 126 return javascriptObject; 127 } 128 else if(javascriptObject instanceof String ) { 129 return Integer.valueOf((String ) javascriptObject); 130 } 131 else if(javascriptObject instanceof Short ) { 132 return new Integer (((Short ) javascriptObject).shortValue()); 133 } 134 else if(javascriptObject instanceof Long ) { 135 return new Integer (((Long ) javascriptObject).intValue()); 136 } 137 else if(javascriptObject instanceof Float ) { 138 return new Integer (((Float ) javascriptObject).intValue()); 139 } 140 else { 141 return javascriptObject; 142 } 143 } 144 else { 145 return javascriptObject; 146 } 147 } 148 } 149 | Popular Tags |