KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > slop > generation > SlopGenerator


1 /*
2  * Copyright 1999-2002,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
17 package org.apache.cocoon.slop.generation;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.LineNumberReader JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.caching.CacheableProcessingComponent;
28 import org.apache.cocoon.environment.SourceResolver;
29 import org.apache.cocoon.generation.ServiceableGenerator;
30 import org.apache.cocoon.slop.interfaces.SlopParser;
31 import org.apache.cocoon.slop.parsing.SimpleSlopParser;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceException;
34 import org.apache.excalibur.source.SourceValidity;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.helpers.LocatorImpl JavaDoc;
37
38 /**
39  * SlopGenerator: Simple Line-Oriented Parsing of text files.
40  * General code structure lifted from the Chaperon TextGenerator - thanks Stephan!
41  *
42  * @author <a HREF="mailto:bdelacretaz@apache.org">Bertrand Delacretaz</a>
43  * @version CVS $Id: SlopGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45
46 public class SlopGenerator extends ServiceableGenerator
47         implements CacheableProcessingComponent {
48
49     private Source inputSource = null;
50     private String JavaDoc encoding = null;
51     private SlopParser parser = null;
52     private boolean preserveSpace = false;
53     private String JavaDoc validTagnameChars = null;
54
55     /**
56      * Recycle this component.
57      * All instance variables are set to <code>null</code>.
58      */

59     public void recycle() {
60         if (inputSource != null) {
61             super.resolver.release(inputSource);
62         }
63         inputSource = null;
64         encoding = null;
65         preserveSpace = false;
66         parser = null;
67         validTagnameChars = null;
68
69         super.recycle();
70     }
71
72     /**
73      * Set the SourceResolver, objectModel Map, the source and sitemap
74      * Parameters used to process the request.
75      *
76      * @param resolver Source resolver
77      * @param objectmodel Object model
78      * @param src Source
79      * @param parameters Parameters
80      *
81      * @throws java.io.IOException
82      * @throws org.apache.cocoon.ProcessingException
83      * @throws org.xml.sax.SAXException
84      */

85     public void setup(SourceResolver resolver, Map JavaDoc objectmodel, String JavaDoc src, Parameters parameters)
86             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
87         super.setup(resolver, objectmodel, src, parameters);
88         try {
89             encoding = parameters.getParameter("encoding", null);
90             preserveSpace = parameters.getParameterAsBoolean("preserve-space",false);
91             validTagnameChars = parameters.getParameter("valid-tagname-chars",null);
92             inputSource = resolver.resolveURI(src);
93
94             final SimpleSlopParser ssp = new SimpleSlopParser();
95             parser = ssp;
96             ssp.setPreserveWhitespace(preserveSpace);
97             ssp.setValidTagnameChars(validTagnameChars);
98         } catch (SourceException se) {
99             throw new ProcessingException("Error during resolving of '" + src + "'.", se);
100         }
101     }
102
103     /**
104      * Generate the unique key.
105      * This key must be unique inside the space of this component.
106      *
107      * @return The generated key hashes the src
108      */

109     public Serializable JavaDoc getKey() {
110         return inputSource.getURI();
111     }
112
113     /**
114      * Generate the validity object.
115      *
116      * @return The generated validity object or <code>null</code> if the
117      * component is currently not cacheable.
118      */

119     public SourceValidity getValidity() {
120         return this.inputSource.getValidity();
121     }
122
123     /**
124      * Generate XML data.
125      *
126      * @throws java.io.IOException
127      * @throws org.apache.cocoon.ProcessingException
128      * @throws org.xml.sax.SAXException
129      */

130     public void generate()
131             throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
132
133         // access input data, using specified encoding if any
134
InputStreamReader JavaDoc in = null;
135
136         try {
137             if (this.inputSource.getInputStream() == null) {
138                 throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found");
139             }
140
141             if (encoding != null) {
142                 in = new InputStreamReader JavaDoc(this.inputSource.getInputStream(), encoding);
143             } else {
144                 in = new InputStreamReader JavaDoc(this.inputSource.getInputStream());
145             }
146         } catch (SourceException se) {
147             throw new ProcessingException("Error during resolving of '" + this.source + "'.", se);
148         }
149
150         // setup a Locator in case parser detects input errors
151
final LocatorImpl JavaDoc locator = new LocatorImpl JavaDoc();
152
153         locator.setSystemId(this.inputSource.getURI());
154         locator.setLineNumber(1);
155         locator.setColumnNumber(1);
156
157         contentHandler.setDocumentLocator(locator);
158
159         // start parsing, read and process all input lines
160
parser.startDocument(contentHandler);
161
162         LineNumberReader JavaDoc reader = new LineNumberReader JavaDoc(in);
163         String JavaDoc line, newline = null;
164
165         while (true) {
166             if (newline == null) {
167                 line = reader.readLine();
168             } else {
169                 line = newline;
170             }
171
172             if (line == null) {
173                 break;
174             }
175
176             newline = reader.readLine();
177
178             locator.setLineNumber(reader.getLineNumber());
179             locator.setColumnNumber(1);
180             parser.processLine(line);
181
182             if (newline == null) {
183                 break;
184             }
185         }
186
187         // done parsing
188
parser.endDocument();
189     }
190 }
Popular Tags