1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.activity.Initializable; 19 import org.apache.avalon.framework.configuration.Configurable; 20 import org.apache.avalon.framework.configuration.Configuration; 21 import org.apache.avalon.framework.configuration.ConfigurationException; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.environment.Session; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.mozilla.javascript.Context; 27 import org.mozilla.javascript.ImporterTopLevel; 28 import org.mozilla.javascript.JavaScriptException; 29 import org.mozilla.javascript.Script; 30 import org.mozilla.javascript.Scriptable; 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 35 import java.io.File ; 36 import java.io.FileReader ; 37 import java.io.IOException ; 38 import java.text.DateFormat ; 39 import java.text.ParseException ; 40 import java.util.Collection ; 41 import java.util.Date ; 42 import java.util.Locale ; 43 import java.util.Map ; 44 45 53 public class JSGenerator extends XSPGenerator 54 implements Configurable, Initializable { 55 56 59 private File file; 60 61 private Scriptable global; 62 63 private Script script; 65 private Exception compileError; 66 67 68 public void configure(Configuration configuration) throws ConfigurationException { 69 this.file = new File (configuration.getChild("file").getValue()); 70 71 Configuration[] dependencies = configuration.getChildren("dependency"); 72 this.dependencies = new File [dependencies.length]; 73 for (int i = 0; i < dependencies.length; i ++) { 74 this.dependencies[i] = new File (dependencies[i].getValue()); 75 } 76 } 77 78 84 public boolean modifiedSince(long date) { 85 if (this.file.lastModified() < date) { 86 return true; 87 } 88 89 for (int i = 0; i < dependencies.length; i++) { 90 if (this.file.lastModified() < dependencies[i].lastModified()) { 91 return true; 92 } 93 } 94 95 return false; 96 } 97 98 public void initialize() throws Exception { 99 Context context = Context.enter(); 100 try { 101 global = new ImporterTopLevel(context); 102 global.put("page", global, Context.toObject(this, global)); 103 global.put("logger", global, Context.toObject(getLogger(), global)); 104 global.put("xspAttr", global, Context.toObject(new AttributesImpl (), global)); 105 106 context.setOptimizationLevel(-1); 107 108 if (getLogger().isDebugEnabled()) { 109 getLogger().debug("Compiling script " + file); 110 } 111 script = context.compileReader(global, new FileReader (file), file.toString(), 1, null); 112 } catch (Exception e) { 113 compileError = e; 114 } finally { 115 Context.exit(); 116 } 117 } 118 119 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 120 throws ProcessingException, SAXException , IOException { 121 super.setup(resolver, objectModel, src, par); 122 123 if (compileError != null) { 124 throw new ProcessingException("Failed to compile script", compileError); 125 } 126 127 Context.enter(); 129 try { 130 global.put("objectModel", global, Context.toObject(this.objectModel, global)); 131 global.put("request", global, Context.toObject(this.request, global)); 132 global.put("response", global, Context.toObject(this.response, global)); 133 global.put("context", global, Context.toObject(this.context, global)); 134 global.put("resolver", global, Context.toObject(this.resolver, global)); 135 global.put("parameters", global, Context.toObject(this.parameters, global)); 136 } catch (Exception e) { 137 throw new ProcessingException("setup: Got exception", e); 138 } finally { 139 Context.exit(); 140 } 141 } 142 143 public void generate() throws IOException , ProcessingException { 144 Context context = Context.enter(); 145 try { 146 global.put("contentHandler", global, Context.toObject(this.contentHandler, global)); 147 148 context.setOptimizationLevel(-1); 149 150 if (getLogger().isDebugEnabled()) { 151 getLogger().debug("Executing script " + file); 152 } 153 154 script.exec(context, global); 155 } catch (JavaScriptException e) { 156 throw new ProcessingException("generate: Got Javascript exception", e); 157 } finally { 158 Context.exit(); 159 } 160 } 161 162 public void recycle() { 163 global.delete("contentHandler"); 164 165 global.delete("objectModel"); 166 global.delete("request"); 167 global.delete("response"); 168 global.delete("context"); 169 global.delete("resolver"); 170 global.delete("parameters"); 171 172 super.recycle(); 173 } 174 175 public void dispose() { 176 global.delete("page"); 177 global.delete("logger"); 178 global.delete("xspAttr"); 179 this.global = null; 180 this.script = null; 181 this.compileError = null; 182 183 super.dispose(); 184 } 185 186 188 public void getLocale() throws SAXException { 189 XSPRequestHelper.getLocale(this.objectModel, this.contentHandler); 190 } 191 192 public Locale [] getLocalesAsArray() { 193 return XSPRequestHelper.getLocales(this.objectModel); 194 } 195 196 public void getLocalesAsXML() throws SAXException { 197 XSPRequestHelper.getLocale(this.objectModel, this.contentHandler); 198 } 199 200 public String getParameter(String name, String defaultValue) { 201 return XSPRequestHelper.getParameter(this.objectModel, name, defaultValue); 202 } 203 204 public String getParameter(String name, String defaultValue, 205 String form_encoding, String container_encoding) { 206 return XSPRequestHelper.getParameter(this.objectModel, name, defaultValue, 207 form_encoding, container_encoding); 208 } 209 210 public void getParameterAsXML(String name, String defaultValue, 211 String form_encoding, String container_encoding) 212 throws SAXException { 213 XSPRequestHelper.getParameter(this.objectModel, this.contentHandler, name, defaultValue, 214 form_encoding, container_encoding); 215 } 216 217 public void getParameterValuesAsXML(String name, String form_encoding, 218 String container_encoding) 219 throws SAXException { 220 XSPRequestHelper.getParameterValues(this.objectModel, this.contentHandler, 221 name, form_encoding, container_encoding); 222 } 223 224 public String [] getParameterValues(String name, 225 String form_encoding, 226 String container_encoding) { 227 return XSPRequestHelper.getParameterValues(this.objectModel, 228 name, form_encoding, container_encoding); 229 } 230 231 public String [] getParameterNames() { 232 return XSPRequestHelper.getParameterNames(this.objectModel); 233 } 234 235 public void getParameterNamesAsXML() throws SAXException { 236 XSPRequestHelper.getParameterNames(this.objectModel, this.contentHandler); 237 } 238 239 public void getHeaderNamesAsXML() throws SAXException { 240 XSPRequestHelper.getHeaderNames(this.objectModel, this.contentHandler); 241 } 242 243 public String [] getHeaderNames() { 244 return XSPRequestHelper.getHeaderNames(this.objectModel); 245 } 246 247 public String [] getHeaders(String name) { 248 return XSPRequestHelper.getHeaders(this.objectModel, name); 249 } 250 251 public void getHeadersAsXML(String name) 252 throws SAXException { 253 XSPRequestHelper.getHeaders(this.objectModel, name, this.contentHandler); 254 } 255 256 public Date getDateHeader(String name) { 257 return XSPRequestHelper.getDateHeader(this.objectModel, name); 258 } 259 260 public String getDateHeader(String name, String format) { 261 return XSPRequestHelper.getDateHeader(this.objectModel, name, format); 262 } 263 264 public void getAttributeNames(ContentHandler contentHandler) 265 throws SAXException { 266 XSPRequestHelper.getAttributeNames(this.objectModel, contentHandler); 267 } 268 269 public String [] getAttributeNames() { 270 return XSPRequestHelper.getAttributeNames(this.objectModel); 271 } 272 273 public String getRequestedURL() { 274 return XSPRequestHelper.getRequestedURL(this.objectModel); 275 } 276 277 279 public void responseGetLocale() 280 throws SAXException { 281 XSPResponseHelper.getLocale(this.response, this.contentHandler); 282 } 283 284 public void addDateHeader(String name, long date) { 285 XSPResponseHelper.addDateHeader(this.response, name, date); 286 } 287 288 public void addDateHeader(String name, Date date) { 289 XSPResponseHelper.addDateHeader(this.response, name, date); 290 } 291 292 public void addDateHeader(String name, String date) throws ParseException { 293 XSPResponseHelper.addDateHeader(this.response, name, date); 294 } 295 296 public void addDateHeader(String name, String date, String format) throws ParseException { 297 XSPResponseHelper.addDateHeader(this.response, name, date, format); 298 } 299 300 public void addDateHeader(String name, String date, DateFormat format) throws ParseException { 301 XSPResponseHelper.addDateHeader(this.response, name, date, format); 302 } 303 304 public void setDateHeader(String name, long date) { 305 XSPResponseHelper.setDateHeader(this.response, name, date); 306 } 307 308 public void setDateHeader(String name, Date date) { 309 XSPResponseHelper.setDateHeader(this.response, name, date); 310 } 311 312 public void setDateHeader(String name, String date) throws ParseException { 313 XSPResponseHelper.setDateHeader(this.response, name, date); 314 } 315 316 public void setDateHeader(String name, String date, String format) throws ParseException { 317 XSPResponseHelper.setDateHeader(this.response, name, date, format); 318 } 319 320 public void setDateHeader(String name, String date, DateFormat format) throws ParseException { 321 XSPResponseHelper.setDateHeader(this.response, name, date, format); 322 } 323 324 public Object getSessionAttribute(Session session, String name, Object defaultValue) { 326 return XSPSessionHelper.getSessionAttribute(session, name, defaultValue); 327 } 328 329 public String [] getSessionAttributeNames(Session session) { 330 Collection c = XSPSessionHelper.getSessionAttributeNames(session); 331 return (String [])c.toArray(new String [c.size()]); 332 } 333 } 334 | Popular Tags |