1 16 package org.apache.cocoon.generation; 17 18 import org.apache.bsf.BSFException; 19 import org.apache.bsf.BSFManager; 20 import org.apache.bsf.util.IOUtils; 21 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.cocoon.ProcessingException; 26 import org.apache.cocoon.ResourceNotFoundException; 27 import org.apache.cocoon.components.source.SourceUtil; 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceException; 30 import org.apache.excalibur.xml.sax.SAXParser; 31 import org.xml.sax.InputSource ; 32 33 import java.io.FileNotFoundException ; 34 import java.io.InputStreamReader ; 35 import java.io.Reader ; 36 import java.io.StringReader ; 37 38 60 public class ScriptGenerator extends ServiceableGenerator implements Configurable { 61 62 protected static class BSFLanguage { 63 public String name; 64 public String engineSrc; 65 public String [] extensions; 66 } 67 68 protected BSFLanguage[] additionalLanguages; 69 70 71 private Source inputSource; 72 73 public void configure(Configuration conf) throws ConfigurationException { 74 if (conf != null) { 75 Configuration languagesToAdd = conf.getChild("add-languages"); 77 78 Configuration[] languages = languagesToAdd.getChildren("language"); 79 this.additionalLanguages = new BSFLanguage[languages.length]; 80 81 for (int i = 0; i < languages.length; i++) { 82 Configuration language = languages[i]; 83 BSFLanguage bsfLanguage = new BSFLanguage(); 84 85 bsfLanguage.name = language.getAttribute("name"); 86 bsfLanguage.engineSrc = language.getAttribute("src"); 87 88 getLogger().debug("Configuring ScriptGenerator with additional BSF language " + bsfLanguage.name); 89 getLogger().debug("Configuring ScriptGenerator with BSF engine " + bsfLanguage.engineSrc); 90 91 Configuration[] extensions = language.getChildren("extension"); 92 bsfLanguage.extensions = new String [extensions.length]; 93 94 for (int j = 0; j < extensions.length; j++) { 95 bsfLanguage.extensions[j] = extensions[j].getValue(); 96 getLogger().debug("Configuring ScriptGenerator with lang extension " + bsfLanguage.extensions[j]); 97 } 98 this.additionalLanguages[i] = bsfLanguage; 99 } 100 } 101 } 102 103 public void recycle() { 104 if (this.inputSource != null) { 105 this.resolver.release(this.inputSource); 106 this.inputSource = null; 107 } 108 super.recycle(); 109 } 110 111 public void generate() throws ProcessingException { 112 SAXParser parser = null; 113 try { 114 getLogger().debug("processing file [" + super.source + "]"); 116 this.inputSource = this.resolver.resolveURI(super.source); 117 118 getLogger().debug("file resolved to [" + this.inputSource.getURI() + "]"); 119 120 Reader in = new InputStreamReader (this.inputSource.getInputStream()); 121 122 BSFManager mgr = new BSFManager(); 124 125 if (this.additionalLanguages != null) { 127 for (int i = 0; i < this.additionalLanguages.length; ++i) { 128 getLogger().debug("adding BSF language " + this.additionalLanguages[i].name + 129 " with engine " + this.additionalLanguages[i].engineSrc); 130 131 BSFManager.registerScriptingEngine(this.additionalLanguages[i].name, 132 this.additionalLanguages[i].engineSrc, 133 this.additionalLanguages[i].extensions); 134 } 135 } 136 StringBuffer output = new StringBuffer (); 137 138 mgr.registerBean("resolver", this.resolver); 140 mgr.registerBean("source", super.source); 141 mgr.registerBean("objectModel", this.objectModel); 142 mgr.registerBean("parameters", this.parameters); 143 mgr.registerBean("logger", getLogger()); 144 145 mgr.registerBean("output", output); 148 mgr.registerBean("contentHandler",contentHandler); 149 150 if(getLogger().isDebugEnabled()) { 152 getLogger().debug("BSFManager execution begining (" + inputSource.getURI() + ")"); 153 } 154 mgr.exec(BSFManager.getLangFromFilename(this.inputSource.getURI()), 155 this.inputSource.getURI(), 0, 0, IOUtils.getStringFromReader(in)); 156 if(getLogger().isDebugEnabled()) { 157 getLogger().debug("BSFManager execution complete"); 158 } 159 160 if(output.length() > 0) { 162 if(getLogger().isDebugEnabled()) { 163 getLogger().debug("Using String output provided by script (" + output.toString() + ")"); 164 } 165 InputSource xmlInput = new InputSource (new StringReader (output.toString())); 166 parser = (SAXParser)(this.manager.lookup(SAXParser.ROLE)); 167 parser.parse(xmlInput, this.xmlConsumer); 168 } else { 169 if(getLogger().isDebugEnabled()) { 170 getLogger().debug("Script provided no String output, content should have been written to contentHandler"); 171 } 172 } 173 174 } catch (SourceException se) { 175 throw SourceUtil.handle(se); 176 } catch (FileNotFoundException e) { 177 throw new ResourceNotFoundException( 178 "Could not load script " + this.inputSource.getURI(), e); 179 } catch (BSFException e) { 180 throw new ProcessingException( 181 "BSFException in ScriptGenerator.generate()", e); 182 } catch (Exception e) { 183 throw new ProcessingException( 184 "Exception in ScriptGenerator.generate()", e); 185 } finally { 186 if(parser!=null) { 187 this.manager.release(parser); 188 } 189 } 190 } 191 } 192 | Popular Tags |