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/03/07 17:09:47 UTC
cvs commit: xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/castor TestBean.java TestBeanAction.java
nicolaken 02/03/07 08:09:47
Modified: tools build-s.xml
Added: src/scratchpad/lib castor-0.9.3.9-xml.jar LICENSE.castor
src/scratchpad/builds castor.xml
src/scratchpad/src/org/apache/cocoon/transformation
CastorTransformer.java
src/scratchpad/webapp/samples/castor/doc
castortransformer.xml
src/scratchpad/webapp/samples/castor sitemap.xmap test.xml
src/scratchpad/webapp/samples/castor/castor-mappings
test-mapping.xml test-mapping_de.xml
src/scratchpad/src/org/apache/cocoon/samples/castor
TestBean.java TestBeanAction.java
Log:
Castor Transformer kindly submitted by Thorsten Mauch (http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6658); put in scratchpad with minor modification and refactoring of sample.
Revision Changes Path
1.5 +2 -2 xml-cocoon2/tools/build-s.xml
Index: build-s.xml
===================================================================
RCS file: /home/cvs/xml-cocoon2/tools/build-s.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- build-s.xml 5 Mar 2002 08:32:26 -0000 1.4
+++ build-s.xml 7 Mar 2002 16:09:46 -0000 1.5
@@ -17,7 +17,7 @@
<echo message=" You have been warned. "/>
<echo message=" "/>
<echo message=" "/>
- <echo message=" -"/>
+ <echo message=" castor ---- examples for Castor Transformer"/>
<echo message=" -"/>
<echo message=" -"/>
<echo message=" -"/>
@@ -31,7 +31,7 @@
<property name="input.selection" value=""/>
<user-input name="input.selection">Please select a target </user-input>
- <ant antfile="./src/scratchpad/targets/${input.selection}.xml"/>
+ <ant antfile="./src/scratchpad/builds/${input.selection}.xml"/>
</target>
1.1 xml-cocoon2/src/scratchpad/lib/castor-0.9.3.9-xml.jar
<<Binary file>>
1.1 xml-cocoon2/src/scratchpad/lib/LICENSE.castor
Index: LICENSE.castor
===================================================================
Copyright 2000 (C) Intalio Inc. All Rights Reserved.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright statements
and notices. Redistributions must also contain a copy of this
document.
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 name "ExoLab" must not be used to endorse or promote products
derived from this Software without prior written permission of
Intalio Inc. For written permission, please contact info@exolab.org.
4. Products derived from this Software may not be called "Castor"
nor may "Castor" appear in their names without prior written
permission of Intalio Inc. Exolab, Castor and Intalio are
trademarks of Intalio Inc.
5. Due credit should be given to the ExoLab Project
(http://www.exolab.org/).
THIS SOFTWARE IS PROVIDED BY INTALIO AND CONTRIBUTORS ``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 INTALIO OR ITS
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 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.
1.1 xml-cocoon2/src/scratchpad/builds/castor.xml
Index: castor.xml
===================================================================
<project default="scratchpad" basedir="../" name="Scratchpad build">
<!-- =================================================================== -->
<!-- Interactive scratchpad builds -->
<!-- =================================================================== -->
<target name="scratchpad" description="Interactive Scratchpad Build">
<echo>
--------------------------------------------------------------
APACHE COCOON SCRATCHPAD - CASTOR TRANSFORMER
--------------------------------------------------------------
Submitted by Thorsten Mauch (mauch@imkenberg.de)"/>
Initial review and commit by NKB"/>
--------------------------------------------------------------
This is a SCRATCHPAD COMPONENT, NOT guaranteed to work.
You have been warned.
To use this example, do the following:
1. build Cocoon with
build.(bat|sh) installscratchpadwar
2. start servlet container
3. point your browser to http://[server:port]/cocoon/samples/castor/
to see a sample
</echo>
</target>
</project>
<!-- End of file -->
1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/CastorTransformer.java
Index: CastorTransformer.java
===================================================================
package org.apache.cocoon.transformation;
import org.apache.cocoon.environment.SourceResolver;
import java.util.Map;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.transformation.*;
import org.apache.cocoon.xml.*;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.Session;
import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.xml.sax.HandlerBase;
import org.xml.sax.AttributeList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.AttributeListImpl;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import java.util.HashMap;
import java.io.File;
import java.io.IOException;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.cocoon.environment.Context;
/**
* Description: Marshals a object from the Sitemap, Session, Request or
* the Conext into a series of SAX events
*
* Configuation: The Castortransformer need to be configured with a
* default mapping. This mapping is used as long as no other mapping
* is spezfied as the element
*
*<pre>
* <map:transformer name="CastorTransformer" src="org.apache.cocoon.transformation.CastorTransformer">
* <mapping>castor/xmapping.xml</mapping>
* </map:transformer>
*</pre>
*
* A sample for the use:
* <pre>
* <root xmlns:castor="http://castor.exolab.org/cocoontransfomer">
* <castor:InsertBean name="invoice"/>
* <castor:InsertBean name="product" scope="sitemap" mapping="castor/specicalmapping.xml"/>
* </root>
* </pre>
* The CastorTransfomer support only one Element <code>castor:InsertBean</code>. This
* element is replaced with the marshalled object. The Object given through the
* attrbute <code>name</code> will be searched in the <code>sitemap, request,
* session</code> and at least in <code>application</code>
* If the scope is explicitly given, e.g , the object will ge located only here
* The Attribut <code>mapping</code> specifys the mapping to be used. If not given
* the default mapping is used
* <pre/>
* Author <a href="mailto:mauch@imkenberg.de">Thorsten Mauch</a>
*
*/
public class CastorTransformer extends AbstractTransformer implements Configurable {
private static String CASTOR_URI="http://castor.exolab.org/cocoontransfomer";
private boolean in_castor_element = false;
final static String CMD_INSERT_BEAN="InsertBean";
final static String ATTRIB_NAME= "name";
final static String ATTRIB_SCOPE= "scope";
final static String VALUE_SITEMAP ="sitemap";
final static String VALUE_SESSION ="session";
final static String VALUE_REQUEST ="request";
final static String VALUE_CONTEXT ="context";
final static String MAPPING_CONFIG ="mapping";
private final static String FILE_PREFIX="file:";
private HandlerBase CastorEventAdapter;
private Map objectModel;
// stores all used mappings in the cache
private static HashMap mappingCache;
private String defaultmapping="castor/mapping.xml";
private SourceResolver resolver;
public CastorTransformer() {
/**
* Inner class eventhandler, forward the Castor SAX events
* to Cocoon 2 Events
*/
CastorEventAdapter = new HandlerBase(){
public void startElement(String name, AttributeList attributes) throws SAXException
{
AttributesImpl a= new AttributesImpl();
for(int i=0;i <attributes.getLength(); i++){
a.addAttribute("",attributes.getName(i),attributes.getName(i),
"",attributes.getValue(i));
}
CastorTransformer.super.contentHandler.startElement("",name,name,a);
}
public void characters(char[] chars, int offset, int length) throws SAXException
{
CastorTransformer.super.contentHandler.characters(chars, offset, length);
}
public void endElement(String name) throws SAXException
{
CastorTransformer.super.contentHandler.endElement("", name,name);
}
};
}
public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params) throws org.apache.cocoon.ProcessingException, org.xml.sax.SAXException, java.io.IOException {
this.objectModel=objectModel;
this.resolver=resolver;
}
public void endElement(String uri, String name, String raw) throws org.xml.sax.SAXException {
if(CASTOR_URI.equals(uri)){
in_castor_element= false;
return;
}
super.endElement( uri, name, raw);
}
public void startElement(String uri, String name, String raw, Attributes attr) throws org.xml.sax.SAXException {
if(CASTOR_URI.equals(uri)){
in_castor_element= true;
process(name,attr);
return;
}
super.startElement( uri, name, raw, attr);
}
public void characters(char[] ch, int start, int len) throws org.xml.sax.SAXException {
if(in_castor_element)
return;
super.characters(ch,start, len);
}
private void process(String command,Attributes attr){
if(command.equals(CMD_INSERT_BEAN)) {
String sourcemap = attr.getValue(ATTRIB_SCOPE);
String name = attr.getValue(ATTRIB_NAME);
String mapping = attr.getValue("mapping");
Object toInsert;
Request request =(Request)objectModel.get(Constants.REQUEST_OBJECT);
if(name == null){
getLogger().error("attribut to insert not set");
}
/*
searcl all maps for the given bean
*/
else{
if( sourcemap == null || VALUE_SITEMAP.equals(sourcemap)){
//System.out.println("Searching bean " + name+ " in "+VALUE_SITEMAP);
toInsert=objectModel.get(name);
if(toInsert != null){
insertBean(toInsert,mapping);
return;
}
}
if( sourcemap == null || VALUE_REQUEST.equals(sourcemap)){
//System.out.println("Searching bean " + name+ " in "+ VALUE_REQUEST);
toInsert=request.getAttribute(name);
if(toInsert != null){
insertBean(toInsert,mapping);
return;
}
}
if(sourcemap == null || VALUE_SESSION.equals(sourcemap)){
//System.out.println("Searching bean " + name+ " in "+VALUE_SESSION);
Session session =request.getSession(false);
if(session != null){
toInsert=session.getAttribute(name);
if(toInsert != null){
insertBean(toInsert,mapping);
return;
}
}
}
if(sourcemap == null || VALUE_CONTEXT.equals(sourcemap)){
Context context = (Context)objectModel.get(Constants.CONTEXT_OBJECT );
if(context != null){
toInsert=context.getAttribute(name);
if(toInsert != null){
insertBean(toInsert,mapping);
return;
}
}
}
}
getLogger().debug("Bean " +name + " could not be found");
return;
} // end CMD_INSERT_BEAN
getLogger().error("Unknown command: " +command);
}
private void insertBean(Object bean,String mappingpath){
if(bean == null){
getLogger().debug ("no bean found");
return;
}
try{
Mapping mapping;
if(mappingpath != null){
mapping=mappingLoader(mappingpath);
}
else{
mapping=mappingLoader(defaultmapping);
}
Marshaller marshaller= new Marshaller(CastorEventAdapter);
marshaller.setMapping(mapping);
marshaller.marshal(bean);
}
catch(Exception e){
e.printStackTrace();
}
}
private Mapping mappingLoader(String path) throws MappingException,IOException{
if(mappingCache == null){
mappingCache= new HashMap();
}
// cache already exsit
else{
// and contain the mapping already
if(mappingCache.containsKey(path)){
return (Mapping)mappingCache.get(path);
}
}
// mapping not found in cache or the cache is new
Mapping mapping = new Mapping();
mapping.loadMapping(getFile(resolver,path));
mappingCache.put(path,mapping);
return mapping;
}
public String getFile(SourceResolver sr, String FileName) {
try{
String path = sr.resolve(FileName).getSystemId();
if(path.startsWith(FILE_PREFIX)){
path = path.substring(FILE_PREFIX.length());
}
return path;
}
catch(Exception e){
getLogger().error("could not read mapping file",e);
return null;
}
}
public void configure(Configuration conf) throws org.apache.avalon.framework.configuration.ConfigurationException {
try{
defaultmapping=conf.getChild(MAPPING_CONFIG).getValue();
}
catch(Exception e){
e.printStackTrace();
getLogger().error("can't load default mapping",e);
}
}
}
1.1 xml-cocoon2/src/scratchpad/webapp/samples/castor/doc/castortransformer.xml
Index: castortransformer.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.0//EN" "../../dtd/document-v10.dtd">
<document>
<header>
<title>Castor Transformer</title>
<version>0.9</version>
<type>Technical document</type>
<authors>
<person name="Thorsten Mauch" email="mauch@imkenberg.de"/>
</authors>
<abstract>This document describes the castor transformer of Cocoon.</abstract>
</header>
<body>
<s1 title="Castor Transformer">
<p>With this transformer, a Bean that is stored in the session,Sitemap, Session, Request or
the Conext is marehalled to XML and inserted in the SAX stream at a given position.</p>
<ul>
<li>Name : CastorTransfomer</li>
<li>Class: org.apache.cocoon.transformation.CastorTransformer</li>
<li>Cacheable: no.</li>
</ul>
<p>
Configuation: The Castortransformer need to be configured with a default mapping.
This mapping is used as long as no other mapping is spezfied as the element
</p>
<source>
<![CDATA[
<map:transformer name="CastorTransformer" src="org.apache.cocoon.transformation.CastorTransformer">
<mapping>castor/mapping.xml</mapping>
</map:transformer>
]]>
</source>
<p>
The CastorTransfomer support only one Element <code>castor:InsertBean</code>. This
element is replaced with the marshalled object. The Object given through the
attrbute <code>name</code> will be searched in the <code>sitemap, request,
session</code> and at least in <code>application</code>
If the scope is explicitly given, e.g , the object will ge located only here
The Attribut <code>mapping</code> specifys the mapping to be used. If not given
the default mapping is used
</p>
<p>
<source>
<![CDATA[
<zoo xmlns:castor="http://castor.exolab.org/cocoontransfomer">
<castor:InsertBean name="Mouse"/>
<castor:InsertBean name="Hamster" mapping="castor/mapping_de.xml"/>
<castor:InsertBean name="Elefant" scope="session"/>
<castor:InsertBean name="Elefant" scope="request"/>
</zoo>
]]>
</source>
</p>
<p>
Documentaion about the castor mapping is provided at the Castor Website
<link href="http://castor.exolab.org/xml-mapping.html">Castor XML Mapping </link>.
</p>
</s1>
</body>
</document>
1.1 xml-cocoon2/src/scratchpad/webapp/samples/castor/sitemap.xmap
Index: sitemap.xmap
===================================================================
<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<!-- =========================== Components ================================ -->
<map:components>
<map:generators default="file"/>
<map:transformers default="xslt">
<map:transformer name="castor" src="org.apache.cocoon.transformation.CastorTransformer">
<mapping>castor-mappings/test-mapping.xml</mapping>
</map:transformer>
</map:transformers>
<map:readers default="resource"/>
<map:serializers default="xml"/>
<map:selectors default="browser"/>
<map:matchers default="wildcard">
<map:matcher name="wildcard" src="org.apache.cocoon.matching.WildcardURIMatcherFactory"/>
</map:matchers>
<map:actions>
<map:action name="TestBeanAction" src="org.apache.cocoon.samples.castor.TestBeanAction"/>
</map:actions>
</map:components>
<!-- =========================== Pipelines ================================= -->
<map:pipelines>
<map:pipeline>
<map:match pattern="*">
<map:act type="TestBeanAction"/>
<map:generate src="test.xml"/>
<map:transform type="castor"/>
<map:serialize/>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>
<!-- end of file -->
1.1 xml-cocoon2/src/scratchpad/webapp/samples/castor/test.xml
Index: test.xml
===================================================================
<?xml version="1.0"?>
<zoo xmlns:castor="http://castor.exolab.org/cocoontransfomer">
<castor:InsertBean name="Mouse"/>
<castor:InsertBean name="Lion"/>
<castor:InsertBean name="Hamster" mapping="castor-mappings/test-mapping_de.xml"/>
<castor:InsertBean name="Wale"/>
<castor:InsertBean name="Elefant" scope="session"/>
<castor:InsertBean name="Elefant" scope="request"/>
</zoo>
1.1 xml-cocoon2/src/scratchpad/webapp/samples/castor/castor-mappings/test-mapping.xml
Index: test-mapping.xml
===================================================================
<mapping>
<class name="org.apache.cocoon.samples.castor.TestBean">
<map-to xml="animal"/>
<field name="name" type="java.lang.String">
<bind-xml name="name" node="element"/>
</field>
<field name="scope" type="java.lang.String">
<bind-xml name="scope" node="element"/>
</field>
</class>
</mapping>
1.1 xml-cocoon2/src/scratchpad/webapp/samples/castor/castor-mappings/test-mapping_de.xml
Index: test-mapping_de.xml
===================================================================
<mapping>
<class name="org.apache.cocoon.samples.castor.TestBean">
<map-to xml="Tier"/>
<field name="name" type="java.lang.String">
<bind-xml name="Name" node="element"/>
</field>
<field name="scope" type="java.lang.String">
<bind-xml name="Herkunft" node="element"/>
</field>
</class>
</mapping>
1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/castor/TestBean.java
Index: TestBean.java
===================================================================
package org.apache.cocoon.samples.castor;
public class TestBean {
private String name;
private String scope;
public TestBean(){
}
public TestBean(String newName,String newScope){
name= newName;
scope=newScope;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public void setScope(String newScope) {
scope = newScope;
}
public String getScope() {
return scope;
}
}
1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/castor/TestBeanAction.java
Index: TestBeanAction.java
===================================================================
package org.apache.cocoon.samples.castor;
import java.util.Map;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.acting.*;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.Context;
public class TestBeanAction extends AbstractAction {
public TestBeanAction() {
}
public Map act(Redirector redirector, SourceResolver resolver, Map objectmodel, String src, Parameters param) throws java.lang.Exception {
Request request =(Request)objectmodel.get(Constants.REQUEST_OBJECT);
Context context = (Context)objectmodel.get(Constants.CONTEXT_OBJECT);
if(context != null)
request.setAttribute("Wale",new TestBean("Wale in the big sea","context"));
Session session =request.getSession(true);
session.setAttribute("Mouse",new TestBean("Liveing in the session","session"));
objectmodel.put("Lion", new TestBean("Lion:walking on the sitemap","sitemap") );
request.setAttribute("Hamster",new TestBean("Hamster:Wer hat nach mir gefragt","request") );
session.setAttribute("Elefant",new TestBean("Elefant:from Africa","session"));
request.setAttribute("Elefant",new TestBean("Elefant:from India","request"));
return objectmodel;
}
}
----------------------------------------------------------------------
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