KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > HTMLScriptElementImpl


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 /*
22  * Created on Oct 8, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.UserDataHandler JavaDoc;
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 JavaDoc;
34 import java.security.PrivilegedAction JavaDoc;
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 JavaDoc name) {
47         super(name, true);
48     }
49
50     private String JavaDoc text;
51     
52     public String JavaDoc getText() {
53         String JavaDoc 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 JavaDoc text) {
63         this.text = text;
64     }
65
66     public String JavaDoc getHtmlFor() {
67         return this.getAttribute("htmlFor");
68     }
69
70     public void setHtmlFor(String JavaDoc htmlFor) {
71         this.setAttribute("htmlFor", htmlFor);
72     }
73
74     public String JavaDoc getEvent() {
75         return this.getAttribute("event");
76     }
77
78     public void setEvent(String JavaDoc 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 JavaDoc getSrc() {
93         return this.getAttribute("src");
94     }
95
96     public void setSrc(String JavaDoc src) {
97         this.setAttribute("src", src);
98     }
99
100     public String JavaDoc getType() {
101         return this.getAttribute("type");
102     }
103
104     public void setType(String JavaDoc type) {
105         this.setAttribute("type", type);
106     }
107     
108     public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc data, UserDataHandler JavaDoc 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 JavaDoc("No user agent context.");
119         }
120         if(bcontext.isScriptingEnabled()) {
121             String JavaDoc text;
122             final String JavaDoc scriptURI;
123             int baseLineNumber;
124             String JavaDoc src = this.getSrc();
125             Document JavaDoc doc = this.document;
126             if(!(doc instanceof HTMLDocumentImpl)) {
127                 throw new IllegalStateException JavaDoc("no valid document");
128             }
129             boolean liflag = loggableInfo;
130             if(src == null) {
131                 text = this.getText();
132                 scriptURI = doc.getBaseURI();
133                 baseLineNumber = 1; //TODO: Line number of inner text??
134
}
135             else {
136                 this.informExternalScriptLoading();
137                 java.net.URL JavaDoc 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                     // Perform a synchronous request
143
SecurityManager JavaDoc sm = System.getSecurityManager();
144                     if(sm == null) {
145                         request.open("GET", scriptURI, false);
146                     }
147                     else {
148                         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
149                             public Object JavaDoc run() {
150                                 // Code might have restrictions on accessing
151
// items from elsewhere.
152
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 JavaDoc("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 JavaDoc err) {
187                     this.warn("Unable to evaluate Javascript code", err);
188                 }
189             } finally {
190                 Context.exit();
191             }
192         }
193     }
194 }
195
Popular Tags