KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.cocoon.generation;
18
19 import org.apache.avalon.framework.parameters.ParameterException;
20 import org.apache.avalon.framework.parameters.Parameterizable;
21 import org.apache.avalon.framework.parameters.Parameters;
22
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.caching.CacheableProcessingComponent;
25 import org.apache.cocoon.environment.SourceResolver;
26 import org.apache.commons.lang.SystemUtils;
27
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceException;
30 import org.apache.excalibur.source.SourceValidity;
31
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.helpers.AttributesImpl JavaDoc;
34 import org.xml.sax.helpers.LocatorImpl JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39 import java.io.LineNumberReader JavaDoc;
40 import java.io.Serializable JavaDoc;
41
42 import java.util.Map JavaDoc;
43
44 /**
45  * Read a plain text file and produce a valid XML file.
46  * <pre>
47  * &lt;text xmlns="http://chaperon.sourceforge.net/schema/text/1.0"&gt;
48  * Text 123 bla
49  * &lt;/text&gt;
50  * </pre>
51  *
52  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
53  * @author <a HREF="mailto:rolf.schumacher@hamburg.de">Rolf Schumacher</a>
54  * @version CVS $Id: TextGenerator.java 124653 2005-01-08 07:41:54Z antonio $
55  */

56 public class TextGenerator extends ServiceableGenerator implements Parameterizable,
57                                                                    CacheableProcessingComponent
58 {
59   /** The URI of the text element */
60   public static final String JavaDoc URI = "http://chaperon.sourceforge.net/schema/text/1.0";
61   private static final char[] initNonXmlChars =
62   {
63     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
64   // 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
65
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
66   };
67
68   /** The input source */
69   private Source inputSource;
70   private String JavaDoc encoding;
71   private char[] nonXmlChars;
72   private boolean localizable = false;
73
74   /**
75    * Recycle this component. All instance variables are set to <code>null</code>.
76    */

77   public void recycle()
78   {
79     if (inputSource!=null)
80       super.resolver.release(inputSource);
81
82     inputSource = null;
83     encoding = null;
84     nonXmlChars = null;
85
86     super.recycle();
87   }
88
89   /**
90    * Provide component with parameters.
91    *
92    * @param parameters the parameters
93    *
94    * @throws ParameterException if parameters are invalid
95    */

96   public void parameterize(Parameters parameters) throws ParameterException
97   {
98     this.localizable = parameters.getParameterAsBoolean("localizable", false);
99   }
100
101   /**
102    * Set the SourceResolver, objectModel Map, the source and sitemap Parameters used to process the
103    * request.
104    *
105    * @param resolver Source resolver
106    * @param objectmodel Object model
107    * @param src Source
108    * @param parameters Parameters
109    *
110    * @throws IOException
111    * @throws ProcessingException
112    * @throws SAXException
113    */

114   public void setup(SourceResolver resolver, Map JavaDoc objectmodel, String JavaDoc src, Parameters parameters)
115     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
116     super.setup(resolver, objectmodel, src, parameters);
117     try {
118       this.encoding = parameters.getParameter("encoding", null);
119       this.inputSource = resolver.resolveURI(src);
120
121       String JavaDoc nXmlCh = parameters.getParameter("nonXmlChars", String.valueOf(initNonXmlChars));
122       if (nXmlCh.length() != initNonXmlChars.length)
123         throw new ProcessingException("Error during resolving of '"+src+"'.",
124                                       new SourceException("length of attribute string 'nonXmlChars' is "+
125                                                           nXmlCh.length()+" where it should be "+
126                                                           initNonXmlChars.length+"!"));
127
128       this.nonXmlChars = nXmlCh.toCharArray();
129     } catch (SourceException se) {
130       throw new ProcessingException("Error during resolving of '"+src+"'.", se);
131     }
132   }
133
134   /**
135    * Generate the unique key. This key must be unique inside the space of this component.
136    *
137    * @return The generated key hashes the src
138    */

139   public Serializable JavaDoc getKey()
140   {
141     return inputSource.getURI()
142         + ";localizable=" + localizable
143         + ";encoding=" + encoding;
144   }
145
146   /**
147    * Generate the validity object.
148    *
149    * @return The generated validity object or <code>null</code> if the component is currently not
150    * cacheable.
151    */

152   public SourceValidity getValidity()
153   {
154     return this.inputSource.getValidity();
155   }
156
157   /**
158    * Generate XML data.
159    *
160    * @throws IOException
161    * @throws ProcessingException
162    * @throws SAXException
163    */

164   public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
165     InputStreamReader JavaDoc in = null;
166
167     try {
168       final InputStream JavaDoc sis = this.inputSource.getInputStream();
169       if (sis == null) {
170         throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found");
171       }
172
173       if (encoding != null) {
174         in = new InputStreamReader JavaDoc(sis, encoding);
175       } else {
176         in = new InputStreamReader JavaDoc(sis);
177       }
178     } catch (SourceException se) {
179       throw new ProcessingException("Error during resolving of '" + this.source + "'.", se);
180     }
181
182     LocatorImpl JavaDoc locator = new LocatorImpl JavaDoc();
183
184     locator.setSystemId(this.inputSource.getURI());
185     locator.setLineNumber(1);
186     locator.setColumnNumber(1);
187
188     contentHandler.setDocumentLocator(locator);
189     contentHandler.startDocument();
190     contentHandler.startPrefixMapping("", URI);
191
192     AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
193     if (localizable) {
194       atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId());
195       atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber()));
196       atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber()));
197     }
198
199     contentHandler.startElement(URI, "text", "text", atts);
200
201     LineNumberReader JavaDoc reader = new LineNumberReader JavaDoc(in);
202     String JavaDoc line;
203     String JavaDoc newline = null;
204
205     while (true) {
206       if (newline==null) {
207         line = convertNonXmlChars(reader.readLine());
208       } else {
209         line = newline;
210       }
211       if (line==null) {
212         break;
213       }
214       newline = convertNonXmlChars(reader.readLine());
215       if (newline != null) {
216           line += SystemUtils.LINE_SEPARATOR;
217       }
218       locator.setLineNumber(reader.getLineNumber());
219       locator.setColumnNumber(1);
220       contentHandler.characters(line.toCharArray(), 0, line.length());
221       if (newline==null) {
222         break;
223       }
224     }
225     reader.close();
226     contentHandler.endElement(URI, "text", "text");
227     contentHandler.endPrefixMapping("");
228     contentHandler.endDocument();
229   }
230
231   private String JavaDoc convertNonXmlChars(String JavaDoc s) {
232     if (s != null) {
233         int nv;
234         char[] sc = s.toCharArray();
235     
236         for (int i = 0; i<sc.length; i++) {
237           nv = sc[i];
238     
239           if ((nv>=0) && (nv<nonXmlChars.length)) {
240             //do not convert white space characters
241
if ((nv!=9) && (nv!=10) && (nv!=13))
242               sc[i] = nonXmlChars[nv];
243           }
244         }
245         return String.valueOf(sc);
246     } else {
247         return null;
248     }
249   }
250 }
251
Popular Tags