KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.caching.CacheableProcessingComponent;
21 import org.apache.cocoon.components.source.SourceUtil;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.apache.excalibur.source.Source;
24 import org.apache.excalibur.source.SourceException;
25 import org.apache.excalibur.source.SourceValidity;
26 import org.xml.sax.SAXException JavaDoc;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * @cocoon.sitemap.component.documentation
34  * The <code>FileGenerator</code> is a class that reads XML from a source
35  * and generates SAX Events.
36  * The FileGenerator implements the <code>CacheableProcessingComponent</code> interface.
37  *
38  * @cocoon.sitemap.component.name file
39  * @cocoon.sitemap.component.label content
40  * @cocoon.sitemap.component.logger sitemap.generator.file
41  * @cocoon.sitemap.component.documentation.caching
42  * Uses the last modification date of the xml document for validation
43  *
44  * @cocoon.sitemap.component.pooling.max 32
45  *
46  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
47  * (Apache Software Foundation)
48  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
49  * @version CVS $Id: FileGenerator.java 153376 2005-02-11 08:50:21Z cziegeler $
50  */

51 public class FileGenerator extends ServiceableGenerator
52 implements CacheableProcessingComponent {
53
54     /** The input source */
55     protected Source inputSource;
56
57     /**
58      * Recycle this component.
59      * All instance variables are set to <code>null</code>.
60      */

61     public void recycle() {
62         if (null != this.inputSource) {
63             super.resolver.release(this.inputSource);
64             this.inputSource = null;
65         }
66         super.recycle();
67     }
68
69     /**
70      * Setup the file generator.
71      * Try to get the last modification date of the source for caching.
72      */

73     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
74         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
75
76         super.setup(resolver, objectModel, src, par);
77         try {
78             this.inputSource = super.resolver.resolveURI(src);
79         } catch (SourceException se) {
80             throw SourceUtil.handle("Error during resolving of '" + src + "'.", se);
81         }
82     }
83
84     /**
85      * Generate the unique key.
86      * This key must be unique inside the space of this component.
87      *
88      * @return The generated key hashes the src
89      */

90     public Serializable JavaDoc getKey() {
91         return this.inputSource.getURI();
92     }
93
94     /**
95      * Generate the validity object.
96      *
97      * @return The generated validity object or <code>null</code> if the
98      * component is currently not cacheable.
99      */

100     public SourceValidity getValidity() {
101         return this.inputSource.getValidity();
102     }
103
104     /**
105      * Generate XML data.
106      */

107     public void generate()
108         throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
109
110         try {
111             if (getLogger().isDebugEnabled()) {
112                 getLogger().debug("Source " + super.source +
113                                   " resolved to " + this.inputSource.getURI());
114             }
115             SourceUtil.parse(this.manager, this.inputSource, super.xmlConsumer);
116         } catch (SAXException JavaDoc e) {
117             SourceUtil.handleSAXException(this.inputSource.getURI(), e);
118         }
119     }
120 }
121
Popular Tags