KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > SimpleFormInstanceExtractionTransformer


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.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.ServiceSelector;
28 import org.apache.avalon.framework.service.Serviceable;
29
30 import org.apache.avalon.excalibur.pool.Recyclable;
31
32 import org.apache.cocoon.ProcessingException;
33 import org.apache.cocoon.components.modules.output.OutputModule;
34 import org.apache.cocoon.environment.SourceResolver;
35 import org.apache.cocoon.xml.dom.DocumentWrapper;
36
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.Attributes JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * @cocoon.sitemap.component.documentation
43  * This transformer sieves an incoming stream of xml and extracts a
44  * document fragment from it depending on a given tag and stores the
45  * fragment using an OutputModule with a name based an attribute of
46  * another enclosing tag. Default configuration fires on
47  * <form-instance/> and uses @name of enclosing <form/>
48  * tag. Default OutputModule is request-attr. This is usefull in
49  * conjunction with the SimpleFormTransformer when setting the
50  * InputModule for it to a chain of request-param and request-attr so
51  * that the extracted form instance data is used only when no similar
52  * request parameter exists.
53  *
54  * @cocoon.sitemap.component.name simple-form-instance
55  * @cocoon.sitemap.component.logger sitemap.transformer.simple-form-instance
56  *
57  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
58  * @version CVS $Id: SimpleFormInstanceExtractionTransformer.java 124685 2005-01-08 22:20:56Z antonio $
59  */

60 public class SimpleFormInstanceExtractionTransformer extends AbstractExtractionTransformer
61     implements Configurable, Serviceable, Recyclable {
62
63     protected static class ElementData {
64         public String JavaDoc uri = null;
65         public String JavaDoc loc = null;
66         public String JavaDoc raw = null;
67
68         public ElementData() {
69         }
70
71         public ElementData(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) {
72             this.uri = uri;
73             this.loc = loc;
74             this.raw =raw;
75         }
76
77         public boolean equals(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) {
78
79             if (!this.uri.equals(uri))
80                 return false;
81             if (!this.loc.equals(loc))
82                 return false;
83             if (!this.raw.equals(raw))
84                 return false;
85             return true;
86         }
87
88     }
89
90     protected final static String JavaDoc OUTPUT_MODULE_SELECTOR = OutputModule.ROLE+"Selector";
91
92     ElementData startElement = null;
93     ElementData nameElement = null;
94     String JavaDoc qname = "name";
95
96     String JavaDoc instanceName = null;
97     boolean nameAsRoot = true;
98
99     String JavaDoc outputModuleName = "request-attr";
100     Configuration outputConf = null;
101
102     ServiceManager manager = null;
103     Map JavaDoc objectModel = null;
104
105     public void configure(Configuration config) throws ConfigurationException {
106         this.startElement = new ElementData();
107         this.startElement.uri = config.getChild("start").getAttribute("uri", "");
108         this.startElement.loc = config.getChild("start").getAttribute("local-name", "form-instance");
109         this.startElement.raw = config.getChild("start").getAttribute("raw-name", "form-instance");
110
111         this.nameElement = new ElementData();
112         this.nameElement.uri = config.getChild("name").getAttribute("uri", "");
113         this.nameElement.loc = config.getChild("name").getAttribute("local-name", "form");
114         this.nameElement.raw = config.getChild("name").getAttribute("raw-name", "form");
115         this.qname = config.getChild("name").getAttribute("name-attribute", "name");
116
117         this.nameAsRoot = config.getChild("name-as-root").getValueAsBoolean(this.nameAsRoot);
118
119         this.outputConf = config.getChild("output");
120         this.outputModuleName = this.outputConf.getAttribute("name",this.outputModuleName);
121     }
122
123     public void service(ServiceManager manager) throws ServiceException {
124         this.manager = manager;
125     }
126
127     /** Setup the transformer. */
128     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
129             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
130         super.setup(resolver, objectModel, src, parameters);
131         this.objectModel = objectModel;
132     }
133
134     public void recycle() {
135         super.recycle();
136         this.instanceName = null;
137     }
138
139     /**
140      * Receive notification of the beginning of an element.
141      *
142      * @param uri The Namespace URI, or the empty string if the element has no
143      * Namespace URI or if Namespace
144      * processing is not being performed.
145      * @param loc The local name (without prefix), or the empty string if
146      * Namespace processing is not being performed.
147      * @param raw The raw XML 1.0 name (with prefix), or the empty string if
148      * raw names are not available.
149      * @param a The attributes attached to the element. If there are no
150      * attributes, it shall be an empty Attributes object.
151      * @return a <code>boolean</code> value to signal to start extracting
152      */

153     public boolean startExtracting(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) {
154         if (this.nameElement.equals(uri,loc,raw)) {
155             this.instanceName = a.getValue(this.qname);
156         }
157         boolean res = this.startElement.equals(uri,loc,raw);
158         return res;
159     }
160
161     /**
162      * Receive notification of the beginning of an element.
163      *
164      * @param uri The Namespace URI, or the empty string if the element has no
165      * Namespace URI or if Namespace
166      * processing is not being performed.
167      * @param loc The local name (without prefix), or the empty string if
168      * Namespace processing is not being performed.
169      * @param raw The raw XML 1.0 name (with prefix), or the empty string if
170      * @return a <code>boolean</code> value to signal to stop extracting
171      */

172     public boolean endExtracting(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) {
173         boolean res = this.startElement.equals(uri,loc,raw);
174         return res;
175     }
176
177
178     /**
179      * Start root element and replace it with the instance name.
180      * @see org.apache.cocoon.transformation.AbstractExtractionTransformer#startExtractingDocument(String, String, String, Attributes)
181      */

182     public void startExtractingDocument(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) throws SAXException JavaDoc {
183         if (this.nameAsRoot) {
184             loc = this.instanceName;
185             if (uri != null && !uri.equals("")) {
186                 int pos = raw.indexOf(':');
187                 raw = raw.substring(0, pos+1) + this.instanceName;
188             } else {
189                 raw = loc;
190             }
191         }
192         this.currentBuilder.startElement(uri,loc,raw,a);
193     }
194
195     /**
196      * End root element and replace it with the instance name.
197      * @see org.apache.cocoon.transformation.AbstractExtractionTransformer#endExtractingDocument(String, String, String)
198      */

199     public void endExtractingDocument(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc{
200         if(this.nameAsRoot){
201             loc = this.instanceName;
202             if (uri != null && !uri.equals("")) {
203                 int pos = raw.indexOf(':');
204                 raw = raw.substring(0, pos+1) + this.instanceName;
205             } else {
206                 raw = loc;
207             }
208         }
209         this.currentBuilder.endElement(uri, loc, raw);
210     }
211
212
213     /**
214      * Receive notification of the end of the extracted Document.
215      *
216      * @param doc a <code>Document</code> value
217      */

218     public void handleExtractedDocument(Document JavaDoc doc) {
219         
220         ServiceSelector outputSelector = null;
221         OutputModule output = null;
222
223         try {
224             if (getLogger().isDebugEnabled())
225                 getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" using "+outputConf);
226             outputSelector = (ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR);
227             if (outputSelector.isSelectable(this.outputModuleName)) {
228                 output = (OutputModule) outputSelector.select(this.outputModuleName);
229             }
230             output.setAttribute(outputConf, this.objectModel, this.instanceName, new DocumentWrapper(doc));
231             output.commit(outputConf, this.objectModel);
232             if (getLogger().isDebugEnabled())
233                 getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" using "+outputConf);
234
235         } catch (Exception JavaDoc e) {
236             if (getLogger().isWarnEnabled())
237                 getLogger().warn("Problem writing document data: "+e.getMessage());
238         } finally {
239             if (outputSelector != null) {
240                 if (output != null) {
241                     outputSelector.release(output);
242                     output = null;
243                 }
244                 this.manager.release(outputSelector);
245             }
246         }
247         this.instanceName = null;
248     }
249
250 }
251
Popular Tags