KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.extended.ExtendedGrammar;
20 import net.sourceforge.chaperon.process.extended.ExtendedDirectParserProcessor;
21
22 import org.apache.avalon.excalibur.pool.Recyclable;
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.logger.LogEnabled;
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.avalon.framework.parameters.ParameterException;
27 import org.apache.avalon.framework.parameters.Parameterizable;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.Serviceable;
32
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.caching.CacheableProcessingComponent;
35 import org.apache.cocoon.components.source.SourceUtil;
36 import org.apache.cocoon.environment.SourceResolver;
37 import org.apache.cocoon.xml.XMLConsumer;
38
39 import org.apache.excalibur.source.Source;
40 import org.apache.excalibur.source.SourceException;
41 import org.apache.excalibur.source.SourceValidity;
42 import org.apache.excalibur.store.Store;
43
44 import org.exolab.castor.mapping.Mapping;
45 import org.exolab.castor.mapping.MappingException;
46 import org.exolab.castor.xml.UnmarshalHandler;
47 import org.exolab.castor.xml.Unmarshaller;
48
49 import org.xml.sax.InputSource JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51
52 import java.io.IOException JavaDoc;
53 import java.io.Serializable JavaDoc;
54
55 import java.util.Map JavaDoc;
56
57 /**
58  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
59  * @version CVS $Id: ExtendedParserTransformer.java 124685 2005-01-08 22:20:56Z antonio $
60  */

61 public class ExtendedParserTransformer extends ExtendedDirectParserProcessor
62         implements Transformer, LogEnabled, Serviceable, Parameterizable,
63                    Recyclable, Disposable, CacheableProcessingComponent
64 {
65   private String JavaDoc grammar = null;
66   private Source grammarSource = null;
67   private Logger logger = null;
68   private ServiceManager manager = null;
69   private SourceResolver resolver = null;
70
71   /**
72    * Provide component with a logger.
73    *
74    * @param logger the logger
75    */

76   public void enableLogging(Logger logger)
77   {
78     this.logger = logger;
79
80     // TODO: check if the loglevel is correct LogKitLogger -> Logger
81
//setLog(new AvalonLogger(logger));
82
}
83
84   /**
85    * Pass the ServiceManager to the object. The Serviceable implementation
86    * should use the specified ServiceManager to acquire the services it needs
87    * for execution.
88    *
89    * @param manager The ServiceManager which this Serviceable uses.
90    */

91   public void service(ServiceManager manager)
92   {
93     this.manager = manager;
94   }
95
96   /**
97    * Provide component with parameters.
98    *
99    * @param parameters the parameters
100    *
101    * @throws ParameterException if parameters are invalid
102    */

103   public void parameterize(Parameters parameters) throws ParameterException
104   {
105     //setRecovery(parameters.getParameterAsBoolean("recovery", false));
106
}
107
108   /**
109    * Set the <code>XMLConsumer</code> that will receive XML data.
110    *
111    * @param consumer
112    */

113   public void setConsumer(XMLConsumer consumer)
114   {
115     setContentHandler(consumer);
116     setLexicalHandler(consumer);
117   }
118
119   /**
120    * Set the SourceResolver, objectModel Map, the source and sitemap Parameters used to process the
121    * request.
122    *
123    * @param resolver Source resolver
124    * @param objectmodel Object model
125    * @param src Source
126    * @param parameters Parameters
127    *
128    * @throws IOException
129    * @throws ProcessingException
130    * @throws SAXException
131    */

132   public void setup(SourceResolver resolver, Map JavaDoc objectmodel, String JavaDoc src, Parameters parameters)
133     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc
134   {
135     this.resolver = resolver;
136
137     Store store = null;
138
139     try
140     {
141       this.grammar = src;
142
143       this.grammarSource = resolver.resolveURI(this.grammar);
144
145       // Retrieve the parser automaton from the transient store
146
store = (Store)this.manager.lookup(Store.TRANSIENT_STORE);
147
148       GrammarEntry entry = (GrammarEntry)store.get(this.grammarSource.getURI());
149
150       // If the parser automaton has changed, rebuild the parser automaton
151
if ((entry==null) || (entry.getValidity()==null) ||
152           ((entry.getValidity().isValid(this.grammarSource.getValidity()))<=0))
153       {
154         this.logger.info("(Re)building the grammar from '"+this.grammarSource.getURI()+"'");
155
156         if (this.grammarSource.getInputStream()==null)
157           throw new ProcessingException("Source '"+this.grammarSource.getURI()+"' not found");
158
159         Mapping mapping = new Mapping();
160
161         mapping.loadMapping(new InputSource JavaDoc(ExtendedGrammar.class.getResource("mapping.xml")
162                                                                  .openStream()));
163
164         Unmarshaller unmarshaller = new Unmarshaller(ExtendedGrammar.class);
165         unmarshaller.setMapping(mapping);
166
167         UnmarshalHandler unmarshalHandler = unmarshaller.createHandler();
168         SourceUtil.toSAX(this.manager, this.grammarSource, null,
169                          Unmarshaller.getContentHandler(unmarshalHandler));
170
171         ExtendedGrammar grammar = (ExtendedGrammar)unmarshalHandler.getObject();
172
173         if (grammar==null)
174           throw new ProcessingException("Error while reading the grammar from "+src);
175
176         setExtendedGrammar(grammar);
177
178         this.logger.info("Store grammar into store for '"+this.grammarSource.getURI()+"'");
179         store.store(this.grammarSource.getURI(),
180                     new GrammarEntry(grammar, this.grammarSource.getValidity()));
181       }
182       else
183       {
184         this.logger.info("Getting grammar from store for '"+this.grammarSource.getURI()+"'");
185         setExtendedGrammar(entry.getExtendedGrammar());
186       }
187     }
188     catch (MappingException me)
189     {
190       throw new ProcessingException("Error while reading the grammar", me);
191     }
192     catch (SourceException se)
193     {
194       throw new ProcessingException("Error during resolving of '"+src+"'.", se);
195     }
196     catch (ServiceException se)
197     {
198       throw new ProcessingException("Could not lookup for service", se);
199     }
200     finally
201     {
202       if (store!=null)
203         this.manager.release(store);
204     }
205   }
206
207   /**
208    * Generate the unique key. This key must be unique inside the space of this component.
209    *
210    * @return The generated key hashes the src
211    */

212   public Serializable JavaDoc getKey()
213   {
214     return this.grammarSource.getURI();
215   }
216
217   /**
218    * Generate the validity object.
219    *
220    * @return The generated validity object or <code>null</code> if the component is currently not
221    * cacheable.
222    */

223   public SourceValidity getValidity()
224   {
225     return this.grammarSource.getValidity();
226   }
227
228   /**
229    * Recycle this component. All instance variables are set to <code>null</code>.
230    */

231   public void recycle()
232   {
233     if ((this.resolver!=null) && (this.grammarSource!=null))
234     {
235       this.resolver.release(this.grammarSource);
236       this.grammarSource = null;
237     }
238   }
239
240   /**
241    * The dispose operation is called at the end of a components lifecycle.
242    */

243   public void dispose()
244   {
245     if ((this.resolver!=null) && (this.grammarSource!=null))
246     {
247       this.resolver.release(this.grammarSource);
248       this.grammarSource = null;
249     }
250
251     this.manager = null;
252   }
253
254   /**
255    * This class represent a entry in a store to cache the parser automaton.
256    */

257   public static class GrammarEntry implements Serializable JavaDoc {
258     private SourceValidity validity = null;
259     private ExtendedGrammar grammar = null;
260
261     /**
262      * Create a new entry.
263      *
264      * @param grammar Extended grammar
265      * @param validity Validity for the grammar file.
266      */

267     public GrammarEntry(ExtendedGrammar grammar, SourceValidity validity)
268     {
269       this.grammar = grammar;
270       this.validity = validity;
271     }
272
273     /**
274      * Return the validity of the grammar file.
275      *
276      * @return Validity of the grammar file.
277      */

278     public SourceValidity getValidity()
279     {
280       return this.validity;
281     }
282
283     /**
284      * Return the parser automaton.
285      *
286      * @return Parser automaton.
287      */

288     public ExtendedGrammar getExtendedGrammar()
289     {
290       return this.grammar;
291     }
292
293     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
294       throws IOException JavaDoc
295     {
296       out.writeObject(validity);
297       out.writeObject(grammar);
298     }
299
300     private void readObject(java.io.ObjectInputStream JavaDoc in)
301       throws IOException JavaDoc, ClassNotFoundException JavaDoc
302     {
303       validity = (SourceValidity)in.readObject();
304       grammar = (ExtendedGrammar)in.readObject();
305     }
306   }
307 }
308
Popular Tags