KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > ScriptGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
32
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.io.StringReader JavaDoc;
37
38 /**
39  * The Scriptgenerator executes arbitraty scripts using the BSF framework
40  * and additional interpreter (Rhino, Jython, etc.) as a Cocoon Generator.
41  *
42  * Additional language support can be added during configuration, eg
43  * using:
44  *
45  * <pre>
46  * &lt;add-languages&gt;
47  * &lt;language name="potatoscript" SRC="edu.purdue.cs.bsf.engines.Potatoscript"&gt;
48  * &lt;extension&gt;pos&lt;/extension&gt;
49  * &lt;extension&gt;psc&lt;/extension&gt;
50  * &lt;language&gt;
51  * &lt;language name="kawa-scheme" SRC="org.gnu.kawa.bsf.engines.KawaEngine"&gt;
52  * &lt;extension&gt;scm&lt;/extension&gt;
53  * &lt;language&gt;
54  * &lt;/add-languages&gt;
55  * </pre>
56  *
57  * @author <a HREF="mailto:jafoster@engmail.uwaterloo.ca">Jason Foster</a>
58  * @version CVS $Id: ScriptGenerator.java 124685 2005-01-08 22:20:56Z antonio $
59  */

60 public class ScriptGenerator extends ServiceableGenerator implements Configurable {
61
62     protected static class BSFLanguage {
63         public String JavaDoc name;
64         public String JavaDoc engineSrc;
65         public String JavaDoc[] extensions;
66     }
67
68     protected BSFLanguage[] additionalLanguages;
69
70     /** The source */
71     private Source inputSource;
72
73     public void configure(Configuration conf) throws ConfigurationException {
74         if (conf != null) {
75             //add optional support for additional languages
76
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 JavaDoc[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             // Figure out what file to open and do so
115
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 JavaDoc in = new InputStreamReader JavaDoc(this.inputSource.getInputStream());
121
122             // Set up the BSF manager and register relevant helper "beans"
123
BSFManager mgr = new BSFManager();
124
125             // add BSF support for additional languages
126
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 JavaDoc output = new StringBuffer JavaDoc();
137
138             // make useful objects available to scripts
139
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             // provide both a StringBuffer and ContentHandler to script,
146
// so that it can provide XML either as a String or as SAX events
147
mgr.registerBean("output", output);
148             mgr.registerBean("contentHandler",contentHandler);
149
150             // Execute the script
151
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 script wrote something to output buffer, use it
161
if(output.length() > 0) {
162                 if(getLogger().isDebugEnabled()) {
163                     getLogger().debug("Using String output provided by script (" + output.toString() + ")");
164                 }
165                 InputSource JavaDoc xmlInput = new InputSource JavaDoc(new StringReader JavaDoc(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 JavaDoc 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 JavaDoc 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