KickJava   Java API By Example, From Geeks To Geeks.

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


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.transformation;
17
18 import org.apache.avalon.framework.component.ComponentException;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.Composable;
21 import org.apache.avalon.framework.parameters.Parameters;
22
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.caching.CacheValidity;
25 import org.apache.cocoon.caching.Cacheable;
26 import org.apache.cocoon.caching.IncludeCacheValidity;
27 import org.apache.cocoon.components.source.SourceUtil;
28 import org.apache.cocoon.environment.SourceResolver;
29 import org.apache.cocoon.xml.IncludeXMLConsumer;
30 import org.apache.cocoon.xml.XMLUtils;
31
32 import org.apache.excalibur.source.Source;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * <p>This transformer triggers for the element <code>include</code> in the
41  * namespace "http://apache.org/cocoon/include/1.0".
42  * The <code>src</code> attribute contains the url which points to
43  * an xml resource which is include instead of the element.
44  * With the attributes <code>element</code>, <code>ns</code> and
45  * <code>prefix</code> it is possible to specify an element
46  * which surrounds the included content.</p>
47  *
48  * <p>Validity of cached pipelines is calculated not by comparing old and new
49  * IncludeCacheValidity objects (as in AggregatedCacheValidity) but by comparing
50  * timestamps. Validity object of cached pipeline contain two lists: source urls
51  * and timestamps. When it comes to checking validity of cached pipeline we know
52  * that generation/transformation steps before CIncludeTransformer are valid (otherwise
53  * we would have had discarded cached pipeline already) so source url list
54  * of new validity will be the same as of old one. Only timestamps have to be
55  * recalculated and compared.</p>
56  *
57  * @see IncludeTransformer (scratchpad)
58  * @author <a HREF="mailto:maciejka@tiger.com.pl">Maciek Kaminski</a>
59  * @deprecated This transformer violates the avalon/cocoon design principles. Use IncludeTransformer.
60  * @version $Id: CachingCIncludeTransformer.java 164808 2005-04-26 16:07:03Z vgritsenko $
61  */

62 public class CachingCIncludeTransformer extends AbstractTransformer
63                                         implements Composable, Cacheable {
64
65     public static final String JavaDoc CINCLUDE_NAMESPACE_URI = "http://apache.org/cocoon/include/1.0";
66     public static final String JavaDoc CINCLUDE_INCLUDE_ELEMENT = "include";
67     public static final String JavaDoc CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE = "src";
68     public static final String JavaDoc CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE = "element";
69     public static final String JavaDoc CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE = "ns";
70     public static final String JavaDoc CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE = "prefix";
71
72     /** The <code>SourceResolver</code> */
73     protected SourceResolver sourceResolver;
74
75     /** The current <code>ComponentManager</code>. */
76     protected ComponentManager manager = null;
77
78     /** The current <code>IncludeCacheValidity</code>. */
79     protected IncludeCacheValidity currentCacheValidity;
80
81     /** The current <code>IncludeXMLConsumer</code> that ommits start and endDocument events. */
82     protected IncludeXMLConsumer consumer;
83
84     /**
85      * Setup the component.
86      */

87     public void setup(SourceResolver resolver, Map JavaDoc objectModel,
88                       String JavaDoc source, Parameters parameters)
89     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
90         this.sourceResolver = resolver;
91     }
92
93     /**
94      * Composable Interface
95      */

96     public final void compose(final ComponentManager manager) throws ComponentException {
97         this.manager = manager;
98     }
99
100     /**
101      * Recycle the component
102      */

103     public void recycle() {
104         super.recycle();
105         this.sourceResolver = null;
106         this.currentCacheValidity = null;
107     }
108
109     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
110     throws SAXException JavaDoc {
111         if (uri != null && name != null
112             && uri.equals(CINCLUDE_NAMESPACE_URI)
113             && name.equals(CINCLUDE_INCLUDE_ELEMENT)) {
114
115             this.processCIncludeElement(attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE),
116                                         attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE),
117                                         attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE),
118                                         attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE));
119
120         } else {
121             super.startElement(uri, name, raw, attr);
122         }
123     }
124
125     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw) throws SAXException JavaDoc {
126         if (uri != null && name != null
127             && uri.equals(CINCLUDE_NAMESPACE_URI)
128             && name.equals(CINCLUDE_INCLUDE_ELEMENT)) {
129             return;
130         }
131         super.endElement(uri, name, raw);
132     }
133
134     public void endDocument()
135     throws SAXException JavaDoc {
136         super.endDocument();
137         if(currentCacheValidity != null) {
138             currentCacheValidity.setIsNew2False();
139         }
140     }
141
142     protected void processCIncludeElement(String JavaDoc src, String JavaDoc element, String JavaDoc ns, String JavaDoc prefix)
143     throws SAXException JavaDoc {
144
145         if (element == null) element="";
146         if (ns == null) ns="";
147         if (prefix == null) prefix="";
148
149         if (this.getLogger().isDebugEnabled()) {
150             getLogger().debug("Processing CInclude element: SRC=" + src
151                           + ", element=" + element
152                           + ", ns=" + ns
153                           + ", prefix=" + prefix);
154         }
155
156         // complete validity information
157
if(currentCacheValidity != null ) {
158             Source temp = null;
159             try {
160                 temp = sourceResolver.resolveURI(src);
161                 currentCacheValidity.add(src, temp.getLastModified());
162                 if (this.getLogger().isDebugEnabled()) {
163                     getLogger().debug("currentCacheValidity: " + currentCacheValidity);
164                 }
165             } catch (Exception JavaDoc e) {
166                 throw new SAXException JavaDoc("CachingCIncludeTransformer could not resolve resource", e);
167             } finally {
168                 sourceResolver.release(temp);
169             }
170         }
171
172         if (!"".equals(element)) {
173             if (!ns.equals("")) {
174                 super.startPrefixMapping(prefix, ns);
175             }
176             super.startElement(ns,
177                                element,
178                                (!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element),
179                                XMLUtils.EMPTY_ATTRIBUTES);
180         }
181
182         Source source = null;
183         try {
184             source = this.sourceResolver.resolveURI(src);
185             SourceUtil.parse(this.manager, source, getConsumer());
186         } catch (Exception JavaDoc e) {
187             throw new SAXException JavaDoc("CachingCIncludeTransformer could not read resource", e);
188         } finally {
189             sourceResolver.release(source);
190         }
191
192         if (!"".equals(element)) {
193             super.endElement(ns, element, (!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element));
194             if (!ns.equals("")) {
195                 super.endPrefixMapping(prefix);
196             }
197         }
198     }
199
200     /**
201      * Generate the unique key.
202      * This key must be unique inside the space of this component.
203      * CachingCIncludeTransformer always generates the same key since which documents
204      * are included depends only on former generation/transformation stages.
205      *
206      * @return The generated key hashes the src
207      */

208
209     public long generateKey() {
210             return 1;
211     }
212
213     /**
214      * Generate the validity object.
215      * CachingCIncludeTransformer generates "empty" IncludeCacheValidity
216      * and completes it with validity data during transformation.
217      * See processCIncludeElement method.
218      *
219      * @return The generated validity object or <code>null</code> if the
220      * component is currently not cacheable.
221      */

222
223     public CacheValidity generateValidity() {
224
225         try {
226             currentCacheValidity = new IncludeCacheValidity(sourceResolver);
227             return currentCacheValidity;
228         } catch (RuntimeException JavaDoc e) {
229             getLogger().warn("CachingCIncludeTransformer: could not generateKey", e);
230             return null;
231         }
232     }
233
234     protected IncludeXMLConsumer getConsumer() {
235         if(consumer == null) {
236             consumer = new IncludeXMLConsumer(this);
237         }
238         return consumer;
239     }
240 }
241
Popular Tags