KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
19
20 import org.apache.avalon.framework.service.ServiceException;
21 import org.apache.cocoon.ResourceNotFoundException;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.caching.CacheableProcessingComponent;
24 import org.apache.cocoon.components.sax.XMLDeserializer;
25
26 import org.apache.excalibur.source.SourceValidity;
27 import org.apache.excalibur.source.impl.validity.NOPValidity;
28 import org.apache.excalibur.store.Store;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * The generation half of FragmentExtractor.
34  *
35  * FragmentExtractor is a transformer-generator pair which is designed to allow
36  * sitemap managers to extract certain nodes from a SAX stream and move them
37  * into a separate pipeline. The main use for this is to extract inline SVG
38  * images and serve them up through a separate pipeline, usually serializing
39  * them to PNG or JPEG format first.
40  *
41  * This is by no means complete yet, but it should prove useful, particularly
42  * for offline generation.
43  *
44  * @author <a HREF="mailto:paul@luminas.co.uk">Paul Russell</a>
45  * @version CVS $Id: FragmentExtractorGenerator.java 224618 2005-07-24 13:42:15Z joerg $
46  */

47 public class FragmentExtractorGenerator extends ServiceableGenerator
48                                         implements CacheableProcessingComponent {
49
50     /**
51      * Generate the unique key.
52      * This key must be unique inside the space of this component.
53      *
54      * @return The generated key hashes the src
55      */

56     public Serializable JavaDoc getKey() {
57         return this.source;
58     }
59
60     /**
61      * Generate the validity object.
62      *
63      * @return The generated validity object or <code>null</code> if the
64      * component is currently not cacheable.
65      */

66     public SourceValidity getValidity() {
67         return NOPValidity.SHARED_INSTANCE;
68     }
69
70     public void generate() throws SAXException JavaDoc, ProcessingException {
71         // Obtain the fragmentID (which is simply the filename portion of the source)
72
if (getLogger().isDebugEnabled()) {
73             getLogger().debug("Retrieving fragment " + source + ".");
74         }
75
76         Store store = null;
77         XMLDeserializer deserializer = null;
78         Object JavaDoc fragment = null;
79         try {
80             store = (Store) this.manager.lookup(Store.TRANSIENT_STORE);
81             fragment = store.get(source);
82             if (fragment == null) {
83                 throw new ResourceNotFoundException("Could not find fragment " + source + " in store");
84             }
85
86             deserializer = (XMLDeserializer) this.manager.lookup(XMLDeserializer.ROLE);
87             deserializer.setConsumer(this.xmlConsumer);
88             deserializer.deserialize(fragment);
89
90         } catch (ServiceException ce) {
91             if (getLogger().isDebugEnabled()) {
92                 getLogger().debug("Could not lookup for component.", ce);
93             }
94             throw new SAXException JavaDoc("Could not lookup for component.", ce);
95         } finally {
96             this.manager.release(store);
97             this.manager.release(deserializer);
98         }
99     }
100 }
101
Popular Tags