bruno 2003/08/04 01:38:01
Added: src/blocks/apples/conf apples-category.xlog
apples-processor.xconf apples-target.xlog
apples.xsamples
src/blocks/apples/java/org/apache/cocoon/components/flow/apples/samples
BindingWoodyApple.java
src/blocks/apples/lib .cvsignore
src/blocks/apples/samples sitemap.xmap welcome.xml
Log:
initial commit of apples block, contributed by Marc Portier
see bugzilla 21900
Revision Changes Path
1.1 cocoon-2.1/src/blocks/apples/conf/apples-category.xlog
Index: apples-category.xlog
===================================================================
<?xml version="1.0"?>
<!-- mind how xpatch oddly requires the file extension to
be equal to the root elm -->
<xlog xpath="/logkit/categories"
remove="/logkit/categories/[EMAIL PROTECTED]'apples']">
<category log-level="DEBUG" name="apples">
<log-target id-ref="apples" />
</category>
</xlog>
1.1 cocoon-2.1/src/blocks/apples/conf/apples-processor.xconf
Index: apples-processor.xconf
===================================================================
<?xml version="1.0"?>
<xconf xpath="/cocoon/flow-interpreters"
remove="/cocoon/flow-interpreters/[EMAIL PROTECTED]'apples']">
<component-instance
class="org.apache.cocoon.components.flow.apples.ApplesProcessor"
name="apples" logger="apples">
<!--
<load-on-startup>resource://org/apache/cocoon/components/flow/javascript/fom/fom_system.js</load-on-startup>
<reload-scripts>true</reload-scripts>
<check-time>4000</check-time>
-->
</component-instance>
</xconf>
1.1 cocoon-2.1/src/blocks/apples/conf/apples-target.xlog
Index: apples-target.xlog
===================================================================
<?xml version="1.0"?>
<xlog xpath="/logkit/targets"
remove="/logkit/targets/[EMAIL PROTECTED]'miles']">
<cocoon id="apples">
<filename>${context-root}/WEB-INF/logs/apples.log</filename>
<format type="cocoon">
%7.7{priority} %{time} [%{category}] (%{uri})
%{thread}/%{class:short}: %{message}\n%{throwable}
</format>
<append>false</append>
</cocoon>
</xlog>
1.1 cocoon-2.1/src/blocks/apples/conf/apples.xsamples
Index: apples.xsamples
===================================================================
<?xml version="1.0"?>
<xsamples xpath="/samples" unless="[EMAIL PROTECTED]'Apples']">
<group name="Apples">
<sample name="Apples" href="apples/">
Apples (an alternative flow implementation) examples.
</sample>
</group>
</xsamples>
1.1
cocoon-2.1/src/blocks/apples/java/org/apache/cocoon/components/flow/apples/samples/BindingWoodyApple.java
Index: BindingWoodyApple.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 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
[EMAIL PROTECTED]
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.
*/
package org.apache.cocoon.components.flow.apples.samples;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.flow.apples.AppleController;
import org.apache.cocoon.components.flow.apples.AppleRequest;
import org.apache.cocoon.components.flow.apples.AppleResponse;
import org.apache.cocoon.woody.FormContext;
import org.apache.cocoon.woody.FormHandler;
import org.apache.cocoon.woody.FormManager;
import org.apache.cocoon.woody.binding.Binding;
import org.apache.cocoon.woody.binding.BindingManager;
import org.apache.cocoon.woody.formmodel.Form;
import org.apache.cocoon.xml.dom.DOMStreamer;
import org.apache.excalibur.source.ModifiableSource;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.xml.dom.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* BindingWoodyApple
*/
public class BindingWoodyApple extends AbstractLogEnabled implements
AppleController, Serviceable {
private static final boolean CONTINUE = false;
private static final boolean FINISHED = true;
private Form form;
private Binding binding;
private Document document;
private ServiceManager serviceManager;
private String formPipeURI;
private String validPipeURI;
private String backendURI;
private Map wrapperContextMap;
private State state;
private interface State {
public void processRequest(AppleRequest req, AppleResponse res)
throws ProcessingException;
}
private final State initializationDelegate = new State() {
public void processRequest(AppleRequest req, AppleResponse res)
throws ProcessingException {
BindingWoodyApple.this.processInitialization(req, res);
}
};
private final State validationDelegate = new State() {
public void processRequest(AppleRequest req, AppleResponse res)
throws ProcessingException {
BindingWoodyApple.this.processValidation(req, res);
}
};
{
state = initializationDelegate;
}
public void process(AppleRequest req, AppleResponse res) throws
ProcessingException {
this.state.processRequest(req, res);
}
private void processInitialization(AppleRequest req, AppleResponse res)
throws ProcessingException {
String formURI = req.getSitemapParameter("form-src");
String bindURI = req.getSitemapParameter("binding-src");
this.backendURI = req.getSitemapParameter("documentURI");
String formHandlerClassName = req.getSitemapParameter("formhandler");
this.formPipeURI = req.getSitemapParameter("form-pipe");
this.validPipeURI = req.getSitemapParameter("valid-pipe");
FormManager formManager = null;
BindingManager binderManager = null;
SourceResolver resolver = null;
Source formSource = null;
Source bindSource = null;
Source documentSource = null;
FormHandler formHandler = null;
try {
formManager = (FormManager)
this.serviceManager.lookup(FormManager.ROLE);
binderManager = (BindingManager)
this.serviceManager.lookup(BindingManager.ROLE);
resolver = (SourceResolver)
this.serviceManager.lookup(SourceResolver.ROLE);
formSource = resolver.resolveURI(formURI);
this.form = formManager.createForm(formSource);
formHandler = (FormHandler)
this.makeInstance(formHandlerClassName);
formHandler.setup(this.form);
this.form.setFormHandler(formHandler);
bindSource = resolver.resolveURI(bindURI);
this.binding = binderManager.createBinding(bindSource);
documentSource = resolver.resolveURI(this.backendURI);
this.document = loadDocumentFromSource(documentSource);
this.binding.loadFormFromModel(this.form, this.document);
this.getLogger().debug("apple initialisation finished .. ");
this.state = validationDelegate;
completeResult(res, this.formPipeURI, CONTINUE);
} catch (Exception e) {
throw new ProcessingException("Failed to initialize
BindingWoodyApple. ", e);
} finally {
if (formManager != null) {
this.serviceManager.release(formManager);
}
if (binderManager != null) {
this.serviceManager.release(binderManager);
}
if (resolver != null) {
if (formSource != null) {
resolver.release(formSource);
}
if (bindSource != null) {
resolver.release(bindSource);
}
if (documentSource != null) {
resolver.release(documentSource);
}
this.serviceManager.release(resolver);
}
}
}
private void processValidation(AppleRequest req, AppleResponse res)
throws ProcessingException {
Source documentTarget = null;
SourceResolver resolver = null;
try {
FormContext formContext = new FormContext(req.getCocoonRequest(),
Locale.US);
if (!this.form.process(formContext)) {
// form is not valid or there was just an event handled
completeResult(res, this.formPipeURI, CONTINUE);
} else {
resolver = (SourceResolver)
this.serviceManager.lookup(SourceResolver.ROLE);
documentTarget =
resolver.resolveURI(makeTargetURI(this.backendURI));
this.binding.saveFormToModel(this.form, this.document);
saveDocumentToSource(documentTarget, this.document);
completeResult(res, this.validPipeURI, FINISHED);
}
getLogger().debug("apple processing done .. ");
} catch (Exception e) {
throw new ProcessingException("Error processing
BindingWoodyApple", e);
} finally {
if (resolver != null) {
if (documentTarget != null) {
resolver.release(documentTarget);
}
this.serviceManager.release(resolver);
}
}
}
private Object makeInstance(String fqcn)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
Class clazz = Class.forName(fqcn);
return clazz.newInstance();
}
private void completeResult(AppleResponse res, String uri, boolean
finished) {
res.setData(getContextMap());
res.setURI(uri);
// TODO think about transferring the fact that the use case has ended.
}
private Map getContextMap() {
if (this.wrapperContextMap == null) {
if (this.form != null) {
this.wrapperContextMap = new HashMap();
this.wrapperContextMap.put("woody-form", this.form);
}
}
return this.wrapperContextMap;
}
/**
* Translate source path into target path so we keep a clean source XML
*
* @param path
* @return
*/
private String makeTargetURI(String path) {
final String sfx = ".xml";
final String newSfx = "-result.xml";
if (path.endsWith(sfx)) {
path = path.substring(0, path.length() - sfx.length());
}
return path + newSfx;
}
/**
* Saves (and serializes) the given Document to the path indicated by the
* specified Source.
*
* @param docTarget must be the ModifieableSource where the doc will be
* serialized to.
* @param doc org.w3c.dom.Document to save
* @throws ProcessingException
*/
private void saveDocumentToSource(Source docTarget, Document doc)
throws ProcessingException {
DOMParser parser = null;
OutputStream os = null;
String uri = docTarget.getURI();
try {
parser =
(DOMParser) this.serviceManager.lookup(DOMParser.ROLE);
getLogger().debug("request to save file " + uri);
TransformerFactory tf = TransformerFactory.newInstance();
if (docTarget instanceof ModifiableSource
&& tf.getFeature(SAXTransformerFactory.FEATURE)) {
ModifiableSource ws = (ModifiableSource) docTarget;
os = ws.getOutputStream();
SAXTransformerFactory stf = (SAXTransformerFactory) tf;
TransformerHandler th = stf.newTransformerHandler();
Transformer t = th.getTransformer();
t.setOutputProperty(OutputKeys.INDENT, "true");
t.setOutputProperty(OutputKeys.METHOD, "xml");
th.setResult(new StreamResult(os));
DOMStreamer streamer = new DOMStreamer(th);
streamer.stream(doc);
} else {
getLogger().error("Cannot not write to source " + uri);
}
} catch (Exception e) {
getLogger().error("Error parsing mock file " + uri, e);
throw new ProcessingException(
"Error parsing mock file " + uri,
e);
} finally {
if (parser != null) {
this.serviceManager.release(parser);
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e1) {
getLogger().warn(
"Failed to flush/close the outputstream. ",
e1);
}
}
}
}
/**
* Loads (and parses) the Document from the specified Source
*
* @param documentSrc
* @return
* @throws ProcessingException
*/
private Document loadDocumentFromSource(Source documentSrc)
throws ProcessingException {
DOMParser parser = null;
try {
parser =
(DOMParser) this.serviceManager.lookup(DOMParser.ROLE);
getLogger().debug(
"request to load file " + documentSrc.getURI());
InputSource input = new InputSource(documentSrc.getURI());
return parser.parseDocument(input);
} catch (Exception e) {
throw new ProcessingException(
"failed to load file to bind to: ",
e);
} finally {
if (parser != null) {
this.serviceManager.release(parser);
}
}
}
public void service(ServiceManager serviceManager) throws
ServiceException {
this.serviceManager = serviceManager;
}
}
1.1 cocoon-2.1/src/blocks/apples/lib/.cvsignore
<<Binary file>>
1.1 cocoon-2.1/src/blocks/apples/samples/sitemap.xmap
Index: sitemap.xmap
===================================================================
<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<map:components>
<map:generators default="file">
<map:generator name="woody"
src="org.apache.cocoon.woody.generation.WoodyGenerator" logger="woody"/>
<map:generator name="jx"
src="org.apache.cocoon.generation.JXTemplateGenerator" label="content,data"
logger="sitemap.generator.jx"/>
</map:generators>
<map:transformers default="xalan">
<map:transformer name="woody"
src="org.apache.cocoon.woody.transformation.WoodyTemplateTransformer"
logger="woody"/>
<map:transformer name="i18n"
src="org.apache.cocoon.transformation.I18nTransformer">
<catalogues default="other">
<catalogue id="other" name="OtherMessages"
location="context://samples/woody/messages"/>
<catalogue id="woody" name="WoodyMessages"
location="context://samples/woody/messages"/>
</catalogues>
<cache-at-startup>true</cache-at-startup>
</map:transformer>
</map:transformers>
<map:selectors default="browser">
<map:selector name="request-method"
src="org.apache.cocoon.selection.RequestMethodSelector"/>
</map:selectors>
<map:actions/>
<map:pipes default="caching">
</map:pipes>
</map:components>
<map:views>
<map:view name="content" from-label="content">
<map:serialize type="xml"/>
</map:view>
<map:view name="pretty-content" from-label="data">
<map:transform src="context://stylesheets/system/xml2html.xslt"/>
<map:serialize type="html"/>
</map:view>
<map:view name="links" from-position="last">
<map:serialize type="links"/>
</map:view>
</map:views>
<map:flow language="apples" />
<map:pipelines>
<map:pipeline>
<map:match pattern="">
<map:generate src="welcome.xml"/>
<map:transform
src="context://samples/common/style/xsl/html/simple-samples2html.xsl">
<map:parameter name="contextPath" value="{request:contextPath}"/>
</map:transform>
<map:serialize/>
</map:match>
<!--
| Binding form sample, stolen from woody
-->
<map:match pattern="*.continue">
<map:call continuation="{1}"/>
</map:match>
<map:match pattern="form2xml.flow">
<map:call
function="org.apache.cocoon.components.flow.apples.samples.BindingWoodyApple">
<map:parameter name="form-src"
value="context://samples/woody/forms/form2_model.xml"/>
<map:parameter name="binding-src"
value="context://samples/woody/forms/form2_bind_xml.xml"/>
<map:parameter name="formhandler"
value="org.apache.cocoon.woody.samples.Form1Handler"/>
<map:parameter name="documentURI"
value="context://samples/woody/forms/form2_data.xml"/>
<map:parameter name="form-pipe"
value="cocoon://samples/woody/form2-display-pipeline"/>
<map:parameter name="valid-pipe"
value="cocoon://samples/woody/form2-success-pipeline"/>
</map:call>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>
1.1 cocoon-2.1/src/blocks/apples/samples/welcome.xml
Index: welcome.xml
===================================================================
<?xml version="1.0" encoding="iso-8859-1"?>
<samples xmlns:xlink="http://www.w3.org/1999/xlink">
<group name="Main examples page.">
<sample name="Back" href="..">to Cocoon examples main page</sample>
<note>
Apples is a new flow implementation for Cocoon that allows you to write
your
deciding flow components (business logic) in pure OO Java.
Apples is currently still alpha software and under active development.
Your
feedback is very much welcome on the Cocoon mailing lists.
</note>
<sample name="Apples Documentation"
href="http://wiki.cocoondev.org/Wiki.jsp?page=Apples">
Currently there is no documentation available (not even on the Cocoon
Wiki).</sample>
</group>
<group name="Apples Samples">
<sample name="Apple doing Woody XML Binding" href="form2xml.flow">An Apple
controlling a form for just editing an XML file.</sample>
</group>
</samples>