KickJava   Java API By Example, From Geeks To Geeks.

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


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.transformation;
18
19 import net.sourceforge.chaperon.build.LexicalAutomatonBuilder;
20 import net.sourceforge.chaperon.model.lexicon.Lexicon;
21 import net.sourceforge.chaperon.model.lexicon.LexiconFactory;
22 import net.sourceforge.chaperon.process.LexicalAutomaton;
23 import net.sourceforge.chaperon.process.LexicalProcessor;
24
25 import org.apache.avalon.excalibur.pool.Recyclable;
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.logger.LogEnabled;
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.avalon.framework.parameters.ParameterException;
30 import org.apache.avalon.framework.parameters.Parameterizable;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.avalon.framework.service.ServiceManager;
34 import org.apache.avalon.framework.service.Serviceable;
35
36 import org.apache.cocoon.ProcessingException;
37 import org.apache.cocoon.caching.CacheableProcessingComponent;
38 import org.apache.cocoon.components.source.SourceUtil;
39 import org.apache.cocoon.environment.SourceResolver;
40 import org.apache.cocoon.xml.XMLConsumer;
41
42 //import org.apache.commons.logging.impl.AvalonLogger;
43

44 import org.apache.excalibur.source.Source;
45 import org.apache.excalibur.source.SourceException;
46 import org.apache.excalibur.source.SourceValidity;
47 import org.apache.excalibur.store.Store;
48
49 import org.xml.sax.SAXException JavaDoc;
50
51 import java.io.IOException JavaDoc;
52 import java.io.Serializable JavaDoc;
53
54 import java.util.Map JavaDoc;
55
56 /**
57  * This transfomer transforms special mark text part of a XML file into lexemes by using a lexicon
58  * file.
59  *
60  * <p>
61  * Input:
62  * </p>
63  * <pre>
64  * &lt;text xmlns="http://chaperon.sourceforge.net/schema/text/1.0"&gt;
65  * Text 123 bla
66  * &lt;/text&gt;
67  * </pre>
68  *
69  * <p>
70  * were transform into the following output:
71  * </p>
72  * <pre>
73  * &lt;lexemes xmlns="http://chaperon.sourceforge.net/schema/lexemes/1.0"&gt;
74  * &lt;lexeme symbol="word" text="Text"/&gt;
75  * &lt;lexeme symbol="number" text="123"/&gt;
76  * &lt;lexeme symbol="word" text="bla"/&gt;
77  * &lt;/lexemes&gt;
78  * </pre>
79  *
80  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
81  * @version CVS $Id: LexicalTransformer.java 124685 2005-01-08 22:20:56Z antonio $
82  */

83 public class LexicalTransformer extends LexicalProcessor
84         implements Transformer, LogEnabled, Serviceable, Recyclable, Disposable,
85                    Parameterizable, CacheableProcessingComponent
86 {
87   private String JavaDoc lexicon = null;
88   private Source lexiconSource = null;
89   private Logger logger = null;
90   private ServiceManager manager = null;
91   private SourceResolver resolver = null;
92
93   /**
94    * Provide component with a logger.
95    *
96    * @param logger the logger
97    */

98   public void enableLogging(Logger logger)
99   {
100     this.logger = logger;
101
102     // TODO: check if the loglevel is correct LogKitLogger -> Logger
103
// setLog(new AvalonLogger(logger));
104
}
105
106   /**
107    * Pass the ServiceManager to the object. The Serviceable implementation
108    * should use the specified ServiceManager to acquire the services it needs
109    * for execution.
110    *
111    * @param manager The ServiceManager which this Serviceable uses.
112    */

113   public void service(ServiceManager manager)
114   {
115     this.manager = manager;
116   }
117
118   /**
119    * Provide component with parameters.
120    *
121    * @param parameters the parameters
122    *
123    * @throws ParameterException if parameters are invalid
124    */

125   public void parameterize(Parameters parameters) throws ParameterException
126   {
127     //setRecovery(parameters.getParameterAsBoolean("recovery", false));
128
setLocalizable(parameters.getParameterAsBoolean("localizable", false));
129   }
130
131   /**
132    * Set the <code>XMLConsumer</code> that will receive XML data.
133    *
134    * @param consumer
135    */

136   public void setConsumer(XMLConsumer consumer)
137   {
138     setContentHandler(consumer);
139     setLexicalHandler(consumer);
140   }
141
142   /**
143    * Set the SourceResolver, objectModel Map, the source and sitemap Parameters used to process the
144    * request.
145    *
146    * @param resolver Source resolver
147    * @param objectmodel Object model
148    * @param src Source
149    * @param parameters Parameters
150    *
151    * @throws IOException
152    * @throws ProcessingException
153    * @throws SAXException
154    */

155   public void setup(SourceResolver resolver, Map JavaDoc objectmodel, String JavaDoc src, Parameters parameters)
156     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc
157   {
158     this.resolver = resolver;
159
160     Store store = null;
161
162     try
163     {
164       this.lexicon = src;
165
166       this.lexiconSource = resolver.resolveURI(this.lexicon);
167
168       // Retrieve the parser table from the transient store
169
store = (Store)this.manager.lookup(Store.TRANSIENT_STORE);
170
171       LexicalAutomatonEntry entry = (LexicalAutomatonEntry)store.get(this.lexiconSource.getURI());
172
173       // If the parser table has changed, rebuild the parser table
174
if ((entry==null) || (entry.getValidity()==null) ||
175           (entry.getValidity().isValid(this.lexiconSource.getValidity())<=0))
176       {
177         this.logger.info("(Re)building the automaton from '"+this.lexiconSource.getURI()+"'");
178
179         if (this.lexiconSource.getInputStream()==null)
180           throw new ProcessingException("Source '"+this.lexiconSource.getURI()+"' not found");
181
182         LexiconFactory factory = new LexiconFactory();
183         SourceUtil.toSAX(this.manager, this.lexiconSource, null, factory);
184
185         Lexicon lexicon = factory.getLexicon();
186
187         LexicalAutomatonBuilder builder =
188           new LexicalAutomatonBuilder(lexicon/*, new AvalonLogger(this.logger)*/);
189
190         LexicalAutomaton automaton = builder.getLexicalAutomaton();
191         setLexicalAutomaton(automaton);
192
193         this.logger.info("Store automaton into store for '"+this.lexiconSource.getURI()+"'");
194
195         store.store(this.lexiconSource.getURI(),
196                     new LexicalAutomatonEntry(automaton, this.lexiconSource.getValidity()));
197       }
198       else
199       {
200         this.logger.info("Getting automaton from store for '"+this.lexiconSource.getURI()+"'");
201         setLexicalAutomaton(entry.getLexicalAutomaton());
202       }
203     }
204     catch (SourceException se)
205     {
206       throw new ProcessingException("Error during resolving of '"+src+"'.", se);
207     }
208     catch (ServiceException se)
209     {
210       throw new ProcessingException("Could not lookup for service", se);
211     }
212     finally
213     {
214       if (store!=null)
215         this.manager.release(store);
216     }
217   }
218
219   /**
220    * Generate the unique key. This key must be unique inside the space of this component.
221    *
222    * @return The generated key hashes the src
223    */

224   public Serializable JavaDoc getKey()
225   {
226     return this.lexiconSource.getURI();
227   }
228
229   /**
230    * Generate the validity object.
231    *
232    * @return The generated validity object or <code>null</code> if the component is currently not
233    * cacheable.
234    */

235   public SourceValidity getValidity()
236   {
237     return this.lexiconSource.getValidity();
238   }
239
240   /**
241    * Recycle this component. All instance variables are set to <code>null</code>.
242    */

243   public void recycle()
244   {
245     if ((this.resolver!=null) && (this.lexiconSource!=null))
246     {
247       this.resolver.release(this.lexiconSource);
248       this.lexiconSource = null;
249     }
250   }
251
252   /**
253    * The dispose operation is called at the end of a components lifecycle.
254    */

255   public void dispose()
256   {
257     if ((this.resolver!=null) && (this.lexiconSource!=null))
258     {
259       this.resolver.release(this.lexiconSource);
260       this.lexiconSource = null;
261     }
262
263     this.manager = null;
264   }
265
266   /**
267    * This class represent a entry in a store to cache the lexical automaton.
268    */

269   public static class LexicalAutomatonEntry implements Serializable JavaDoc {
270     private SourceValidity validity = null;
271     private LexicalAutomaton automaton = null;
272
273     /**
274      * Create a new entry.
275      *
276      * @param automaton Lexical automaton.
277      * @param validity Validity of the lexicon file.
278      */

279     public LexicalAutomatonEntry(LexicalAutomaton automaton, SourceValidity validity)
280     {
281       this.automaton = automaton;
282       this.validity = validity;
283     }
284
285     /**
286      * Return the validity of the lexicon file.
287      *
288      * @return Validity of the lexicon file.
289      */

290     public SourceValidity getValidity()
291     {
292       return this.validity;
293     }
294
295     /**
296      * Return the lexical automaton.
297      *
298      * @return Lexical automaton.
299      */

300     public LexicalAutomaton getLexicalAutomaton()
301     {
302       return this.automaton;
303     }
304
305     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
306       throws IOException JavaDoc
307     {
308       out.writeObject(validity);
309       out.writeObject(automaton);
310     }
311
312     private void readObject(java.io.ObjectInputStream JavaDoc in)
313       throws IOException JavaDoc, ClassNotFoundException JavaDoc
314     {
315       validity = (SourceValidity)in.readObject();
316       automaton = (LexicalAutomaton)in.readObject();
317     }
318   }
319 }
320
Popular Tags