1 21 24 package org.lobobrowser.html.domimpl; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.UserDataHandler ; 28 import org.w3c.dom.html2.HTMLScriptElement; 29 import org.lobobrowser.html.*; 30 import org.lobobrowser.html.js.Executor; 31 import org.mozilla.javascript.*; 32 33 import java.security.AccessController ; 34 import java.security.PrivilegedAction ; 35 import java.util.logging.*; 36 37 public class HTMLScriptElementImpl extends HTMLElementImpl implements 38 HTMLScriptElement { 39 private static final Logger logger = Logger.getLogger(HTMLScriptElementImpl.class.getName()); 40 private static final boolean loggableInfo = logger.isLoggable(Level.INFO); 41 42 public HTMLScriptElementImpl() { 43 super("SCRIPT", true); 44 } 45 46 public HTMLScriptElementImpl(String name) { 47 super(name, true); 48 } 49 50 private String text; 51 52 public String getText() { 53 String t = this.text; 54 if(t == null) { 55 return this.getRawInnerText(true); 56 } 57 else { 58 return t; 59 } 60 } 61 62 public void setText(String text) { 63 this.text = text; 64 } 65 66 public String getHtmlFor() { 67 return this.getAttribute("htmlFor"); 68 } 69 70 public void setHtmlFor(String htmlFor) { 71 this.setAttribute("htmlFor", htmlFor); 72 } 73 74 public String getEvent() { 75 return this.getAttribute("event"); 76 } 77 78 public void setEvent(String event) { 79 this.setAttribute("event", event); 80 } 81 82 private boolean defer; 83 84 public boolean getDefer() { 85 return this.defer; 86 } 87 88 public void setDefer(boolean defer) { 89 this.defer = defer; 90 } 91 92 public String getSrc() { 93 return this.getAttribute("src"); 94 } 95 96 public void setSrc(String src) { 97 this.setAttribute("src", src); 98 } 99 100 public String getType() { 101 return this.getAttribute("type"); 102 } 103 104 public void setType(String type) { 105 this.setAttribute("type", type); 106 } 107 108 public Object setUserData(String key, Object data, UserDataHandler handler) { 109 if(org.lobobrowser.html.parser.HtmlParser.MODIFYING_KEY.equals(key) && data != Boolean.TRUE) { 110 this.processScript(); 111 } 112 return super.setUserData(key, data, handler); 113 } 114 115 protected final void processScript() { 116 UserAgentContext bcontext = this.getUserAgentContext(); 117 if(bcontext == null) { 118 throw new IllegalStateException ("No user agent context."); 119 } 120 if(bcontext.isScriptingEnabled()) { 121 String text; 122 final String scriptURI; 123 int baseLineNumber; 124 String src = this.getSrc(); 125 Document doc = this.document; 126 if(!(doc instanceof HTMLDocumentImpl)) { 127 throw new IllegalStateException ("no valid document"); 128 } 129 boolean liflag = loggableInfo; 130 if(src == null) { 131 text = this.getText(); 132 scriptURI = doc.getBaseURI(); 133 baseLineNumber = 1; } 135 else { 136 this.informExternalScriptLoading(); 137 java.net.URL scriptURL = ((HTMLDocumentImpl) doc).getFullURL(src); 138 scriptURI = scriptURL == null ? src : scriptURL.toExternalForm(); 139 long time1 = liflag ? System.currentTimeMillis() : 0; 140 try { 141 final HttpRequest request = bcontext.createHttpRequest(); 142 SecurityManager sm = System.getSecurityManager(); 144 if(sm == null) { 145 request.open("GET", scriptURI, false); 146 } 147 else { 148 AccessController.doPrivileged(new PrivilegedAction () { 149 public Object run() { 150 request.open("GET", scriptURI, false); 153 return null; 154 } 155 }); 156 } 157 int status = request.getStatus(); 158 if(status != 200 && status != 0) { 159 this.warn("Script at [" + scriptURI + "] failed to load; HTTP status: " + status + "."); 160 return; 161 } 162 text = request.getResponseText(); 163 } finally { 164 if(liflag) { 165 long time2 = System.currentTimeMillis(); 166 logger.info("processScript(): Loaded external Javascript from URI=[" + scriptURI + "] in " + (time2 - time1) + " ms."); 167 } 168 } 169 baseLineNumber = 1; 170 } 171 Context ctx = Executor.createContext(this.getDocumentURL(), bcontext); 172 try { 173 Scriptable scope = (Scriptable) doc.getUserData(Executor.SCOPE_KEY); 174 if(scope == null) { 175 throw new IllegalStateException ("Scriptable (scope) instance was expected to be keyed as UserData to document using " + Executor.SCOPE_KEY); 176 } 177 try { 178 long time1 = liflag ? System.currentTimeMillis() : 0; 179 ctx.evaluateString(scope, text, scriptURI, baseLineNumber, null); 180 if(liflag) { 181 long time2 = System.currentTimeMillis(); 182 logger.info("addNotify(): Evaluated (or attempted to evaluate) Javascript in " + (time2 - time1) + " ms."); 183 } 184 } catch(EcmaError ecmaError) { 185 this.warn("Javascript error at " + ecmaError.getSourceName() + ":" + ecmaError.getLineNumber() + ": " + ecmaError.getMessage()); 186 } catch(Throwable err) { 187 this.warn("Unable to evaluate Javascript code", err); 188 } 189 } finally { 190 Context.exit(); 191 } 192 } 193 } 194 } 195 | Popular Tags |