You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ni...@apache.org on 2002/12/19 00:40:53 UTC
cvs commit: xml-cocoon2/src/java/org/apache/cocoon/generation bsf.xmap ScriptGenerator.java
nicolaken 2002/12/18 15:40:53
Added: src/blocks/bsf/conf bsf.xmap
src/blocks/bsf/java/org/apache/cocoon/acting
ScriptAction.java
src/blocks/bsf/java/org/apache/cocoon/generation
ScriptGenerator.java
Removed: src/java/org/apache/cocoon/acting ScriptAction.java
src/java/org/apache/cocoon/generation bsf.xmap
ScriptGenerator.java
Log:
BSF block.
Revision Changes Path
1.1 xml-cocoon2/src/blocks/bsf/conf/bsf.xmap
Index: bsf.xmap
===================================================================
<?xml version="1.0"?>
<xmap xpath="/sitemap/components/generators"
unless="generator[@name='script']">
<map:generator name="script" src="org.apache.cocoon.generation.ScriptGenerator" label="content,data"/>
</xmap>
1.1 xml-cocoon2/src/blocks/bsf/java/org/apache/cocoon/acting/ScriptAction.java
Index: ScriptAction.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.acting;
import com.ibm.bsf.BSFManager;
import com.ibm.bsf.util.IOUtils;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.excalibur.source.Source;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A simple action that executes any script that can be run by the BSF
*
* @author <a href="mailto:jafoster@uwaterloo.ca">Jason Foster</a>
* @version CVS $Id: ScriptAction.java,v 1.1 2002/12/18 23:40:53 nicolaken Exp $
*/
public class ScriptAction
extends ComposerAction
implements ThreadSafe {
public Map act( Redirector redirector,
SourceResolver resolver,
Map objectModel,
String source,
Parameters par )
throws Exception {
Source src = null;
try {
// Figure out what script to open. A missing script name is caught
// by the resolver/SystemId grouping later on and causes an exception
String scriptName = source;
// Locate the appropriate file on the filesytem
src = resolver.resolveURI(scriptName);
String systemID = src.getSystemId();
if (this.getLogger().isDebugEnabled()) {
getLogger().debug("script source [" + scriptName + "]");
getLogger().debug("script resolved to [" + systemID + "]");
}
// TODO: why doesn't this work?
// Reader in = src.getCharacterStream();
Reader in = new InputStreamReader(src.getInputStream());
// Set up the BSF manager and register relevant helper "beans"
BSFManager mgr = new BSFManager();
HashMap actionMap = new HashMap();
// parameters to act(...)
mgr.registerBean("resolver", resolver);
mgr.registerBean("objectModel", objectModel);
mgr.registerBean("parameters", par);
// ScriptAction housekeeping
mgr.registerBean("actionMap", actionMap);
// helpers
// TODO: should we check for a null request object here or let the script handle it?
mgr.registerBean("logger", getLogger());
mgr.registerBean("request", ( ObjectModelHelper.getRequest(objectModel) ) );
mgr.registerBean("scriptaction", this );
mgr.registerBean("manager", this.manager );
if (this.getLogger().isDebugEnabled()) {
getLogger().debug("BSFManager execution begining");
}
// Execute the script
mgr.exec(BSFManager.getLangFromFilename(systemID), systemID, 0, 0,
IOUtils.getStringFromReader(in));
if (this.getLogger().isDebugEnabled()) {
getLogger().debug("BSFManager execution complete");
}
// Figure out what to return
// TODO: decide on a more robust communication method
if ( actionMap.containsKey( "scriptaction-continue" ) )
{
return ( Collections.unmodifiableMap(actionMap) );
}
else
{
return ( null );
}
} catch (Exception e) {
throw new ProcessingException(
"Exception in ScriptAction.act()", e);
} finally {
resolver.release( src );
} // try/catch
} // public Map act(...)
} // public class ScriptAction
1.1 xml-cocoon2/src/blocks/bsf/java/org/apache/cocoon/generation/ScriptGenerator.java
Index: ScriptGenerator.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.generation;
import com.ibm.bsf.BSFException;
import com.ibm.bsf.BSFManager;
import com.ibm.bsf.util.IOUtils;
import org.apache.avalon.excalibur.xml.Parser;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.components.source.SourceUtil;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.xml.sax.InputSource;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.io.StringReader;
/**
* The Scriptgenerator executes arbitraty scripts using the BSF framework
* and additional interpreter (Rhino, Jython, etc.) as a Cocoon Generator.
*
* Additional language support can be added during configuration, eg
* using:
*
* <pre>
* <add-languages>
* <language name="potatoscript" src="edu.purdue.cs.bsf.engines.Potatoscript">
* <extension>pos</extension>
* <extension>psc</extension>
* <language>
* <language name="kawa-scheme" src="org.gnu.kawa.bsf.engines.KawaEngine">
* <extension>scm</extension>
* <language>
* </add-languages>
* </pre>
*
* @author <a href="mailto:jafoster@engmail.uwaterloo.ca">Jason Foster</a>
* @version CVS $Id: ScriptGenerator.java,v 1.1 2002/12/18 23:40:53 nicolaken Exp $
*/
public class ScriptGenerator extends ComposerGenerator implements Configurable {
protected class BSFLanguage
{
public String name;
public String engineSrc;
public String[] extensions;
}
protected BSFLanguage[] additionalLanguages;
/** The source */
private Source inputSource;
public void configure(Configuration conf) throws ConfigurationException
{
if (conf != null)
{
//add optional support for additional languages
Configuration languagesToAdd = conf.getChild("add-languages");
Configuration[] languages = languagesToAdd.getChildren("language");
this.additionalLanguages = new BSFLanguage[languages.length];
for (int i = 0; i < languages.length; ++i)
{
Configuration language = languages[i];
BSFLanguage bsfLanguage = new BSFLanguage();
bsfLanguage.name = language.getAttribute("name");
bsfLanguage.engineSrc = language.getAttribute("src");
getLogger().debug("Configuring ScriptGenerator with additional BSF language " + bsfLanguage.name);
getLogger().debug("Configuring ScriptGenerator with BSF engine " + bsfLanguage.engineSrc);
Configuration[] extensions = language.getChildren("extension");
bsfLanguage.extensions = new String[extensions.length];
for (int j = 0; j < extensions.length; ++j)
{
bsfLanguage.extensions[i] = extensions[i].getValue();
getLogger().debug("Configuring ScriptGenerator with lang extension " + bsfLanguage.extensions[i]);
}
this.additionalLanguages[i] = bsfLanguage;
}
}
}
public void recycle() {
if (this.inputSource != null) {
this.resolver.release(this.inputSource);
this.inputSource = null;
}
super.recycle();
}
public void generate() throws ProcessingException {
Parser parser = null;
try {
// Figure out what file to open and do so
getLogger().debug("processing file [" + super.source + "]");
this.inputSource = this.resolver.resolveURI(super.source);
getLogger().debug("file resolved to [" + this.inputSource.getSystemId() + "]");
// TODO: why doesn't this work?
// Reader in = src.getCharacterStream();
Reader in = new java.io.InputStreamReader(this.inputSource.getInputStream());
// Set up the BSF manager and register relevant helper "beans"
BSFManager mgr = new BSFManager();
// add support for additional languages
if (this.additionalLanguages != null)
{
for (int i = 0; i < this.additionalLanguages.length; ++i)
{
getLogger().debug("adding BSF language " + this.additionalLanguages[i].name + " with engine " + this.additionalLanguages[i].engineSrc);
mgr.registerScriptingEngine(this.additionalLanguages[i].name,
this.additionalLanguages[i].engineSrc,
this.additionalLanguages[i].extensions);
}
}
StringBuffer output = new StringBuffer();
mgr.registerBean("resolver", this.resolver);
mgr.registerBean("source", super.source);
mgr.registerBean("objectModel", this.objectModel);
mgr.registerBean("parameters", this.parameters);
mgr.registerBean("output", output);
mgr.registerBean("logger", getLogger());
getLogger().debug("BSFManager execution begining");
// Execute the script
mgr.exec(BSFManager.getLangFromFilename(this.inputSource.getSystemId()),
this.inputSource.getSystemId(), 0, 0, IOUtils.getStringFromReader(in));
getLogger().debug("BSFManager execution complete");
getLogger().debug("output = [" + output.toString() + "]");
// Extract the XML string from the BSFManager and parse it
InputSource xmlInput =
new InputSource(new StringReader(output.toString()));
parser = (Parser)(this.manager.lookup(Parser.ROLE));
parser.parse(xmlInput, this.xmlConsumer);
} catch (SourceException se) {
throw SourceUtil.handle(se);
} catch (FileNotFoundException e) {
throw new ResourceNotFoundException(
"Could not load script " + this.inputSource.getSystemId(), e);
} catch (BSFException e) {
throw new ProcessingException(
"Exception in ScriptGenerator.generate()", e);
} catch (Exception e) {
throw new ProcessingException(
"Exception in ScriptGenerator.generate()", e);
} finally {
this.manager.release(parser);
}
}
}
----------------------------------------------------------------------
In case of troubles, e-mail: webmaster@xml.apache.org
To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org