KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.component.Recomposable;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.ComponentException;
21
22 import org.apache.cocoon.caching.CacheValidity;
23 import org.apache.cocoon.caching.Cacheable;
24 import org.apache.cocoon.caching.CacheableProcessingComponent;
25 import org.apache.cocoon.components.language.generator.CompiledComponent;
26 import org.apache.cocoon.environment.Request;
27
28 import org.apache.excalibur.source.SourceValidity;
29 import org.apache.excalibur.source.impl.validity.NOPValidity;
30
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.helpers.AttributesImpl JavaDoc;
33
34 import java.io.File JavaDoc;
35 import java.io.Serializable JavaDoc;
36
37 /**
38  * Base implementation of <code>ServerPagesGenerator</code>. This class
39  * declares variables that must be explicitly initialized by code generators.
40  *
41  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
42  * @version CVS $Id: AbstractServerPage.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public abstract class AbstractServerPage
45   extends ServletGenerator
46   implements CompiledComponent, CacheableProcessingComponent, Cacheable, Recomposable {
47     /**
48      * Code generators should produce a constructor
49      * block that initializes the generator's
50      * creation date and file dependency list.
51      * Example:
52      *
53      * {
54      * this.dateCreated = 958058788948L;
55      * this.dependencies = new File[] {
56      * new File("source.xml"),
57      * };
58      * }
59      *
60      */

61
62     /** The creation date */
63     protected long dateCreated = -1L;
64     /** The dependency file list */
65     protected File JavaDoc[] dependencies = null;
66
67     /**
68      * Recompose with the actual <code>ComponentManager</code> that should
69      * be used.
70      */

71     public void recompose(ComponentManager manager) throws ComponentException {
72         this.manager = manager;
73     }
74
75     /**
76      * Determines whether this generator's source files have changed
77      *
78      * @return Whether any of the files this generator depends on has changed
79      * since it was created
80      */

81     public boolean modifiedSince(long date) {
82         if (date == 0 || dateCreated < date) {
83             return true;
84         }
85
86         for (int i = 0; i < dependencies.length; i++) {
87             if (dateCreated < dependencies[i].lastModified()) {
88                 return true;
89             }
90         }
91
92         return false;
93     }
94
95     /**
96      * Determines whether generated content has changed since
97      * last invocation. Users may override this method to take
98      * advantage of SAX event cacheing
99      *
100      * @param request The request whose data must be inspected to assert whether
101      * dynamically generated content has changed
102      * @return Whether content has changes for this request's data
103      */

104     public boolean hasContentChanged(Request request) {
105       return true;
106     }
107
108     /**
109      * Generate the unique key.
110      * This key must be unique inside the space of this component.
111      * This method must be invoked before the generateValidity() method.
112      *
113      * @return The generated key or <code>null</code> if the component
114      * is currently not cacheable.
115      */

116     public Serializable JavaDoc getKey() {
117         return null;
118     }
119
120     /**
121      * Generate the validity object.
122      * Before this method can be invoked the generateKey() method
123      * must be invoked.
124      *
125      * @return The generated validity object, <code>NOPCacheValidity</code>
126      * is the default if hasContentChange() gives false otherwise
127      * <code>null</code> will be returned.
128      */

129     public SourceValidity getValidity() {
130         if (hasContentChanged(request))
131             return null;
132         else
133             return NOPValidity.SHARED_INSTANCE;
134     }
135
136     // FIXME: Add more methods!
137
/* SAX Utility Methods */
138     /**
139      * Add an attribute
140      *
141      * @param attr The attribute list to add to
142      * @param name The attribute name
143      * @param value The attribute value
144      */

145     protected void attribute(AttributesImpl JavaDoc attr, String JavaDoc name, String JavaDoc value) {
146         attr.addAttribute("", name, name, "CDATA", value);
147     }
148
149     /**
150      * Start an element
151      *
152      * @param name The element name
153      * @param attr The element attributes
154      */

155     protected void start(String JavaDoc name, AttributesImpl JavaDoc attr) throws SAXException JavaDoc {
156         this.contentHandler.startElement("", name, name, attr);
157         attr.clear();
158     }
159
160     /**
161      * End an element
162      *
163      * @param name The element name
164      */

165     protected void end(String JavaDoc name) throws SAXException JavaDoc {
166         this.contentHandler.endElement("", name, name);
167     }
168
169     /**
170      * Add character data
171      *
172      * @param data The character data
173      */

174     protected void characters(String JavaDoc data) throws SAXException JavaDoc {
175         this.contentHandler.characters(data.toCharArray(), 0, data.length());
176     }
177
178     /**
179      * Add a comment
180      *
181      * @param data The comment data
182      */

183     protected void comment(String JavaDoc data) throws SAXException JavaDoc {
184         this.lexicalHandler.comment(data.toCharArray(), 0, data.length());
185     }
186
187     /**
188      * Generates the unique key.
189      * This key must be unique inside the space of this component.
190      * Users may override this method to take
191      * advantage of SAX event cacheing
192      *
193      * @return A long representing the cache key (defaults to not cachable)
194      */

195     public long generateKey() {
196         return 0;
197     }
198
199     /**
200      * Generate the validity object.
201      *
202      * @return The generated validity object, <code>NOPCacheValidity</code>
203      * is the default if hasContentChange() gives false otherwise
204      * <code>null</code> will be returned.
205      */

206     public CacheValidity generateValidity() {
207         if (hasContentChanged(request))
208             return null;
209         else
210             return NOPCacheValidity.CACHE_VALIDITY;
211     }
212
213 }
214
215 /**
216  * This is here to avaid references to the deprecated package.
217  * It is required to support the deprecated caching algorithm
218  */

219 final class NOPCacheValidity
220 implements CacheValidity {
221
222     public static final CacheValidity CACHE_VALIDITY = new NOPCacheValidity();
223
224     public boolean isValid(CacheValidity validity) {
225         return validity instanceof NOPCacheValidity;
226     }
227
228     public String JavaDoc toString() {
229         return "NOP Validity";
230     }
231 }
Popular Tags