001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.generation;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.cocoon.ProcessingException;
021: import org.apache.cocoon.caching.CacheableProcessingComponent;
022: import org.apache.cocoon.components.source.SourceUtil;
023: import org.apache.cocoon.environment.SourceResolver;
024: import org.apache.excalibur.source.Source;
025: import org.apache.excalibur.source.SourceException;
026: import org.apache.excalibur.source.SourceValidity;
027: import org.xml.sax.SAXException;
028:
029: import java.io.IOException;
030: import java.io.Serializable;
031: import java.util.Map;
032:
033: /**
034: * @cocoon.sitemap.component.documentation
035: * The <code>FileGenerator</code> is a class that reads XML from a source
036: * and generates SAX Events.
037: * The FileGenerator implements the <code>CacheableProcessingComponent</code> interface.
038: *
039: * @cocoon.sitemap.component.name file
040: * @cocoon.sitemap.component.label content
041: * @cocoon.sitemap.component.logger sitemap.generator.file
042: * @cocoon.sitemap.component.documentation.caching
043: * Uses the last modification date of the xml document for validation
044: *
045: * @cocoon.sitemap.component.pooling.max 32
046: *
047: * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
048: * (Apache Software Foundation)
049: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
050: * @version CVS $Id: FileGenerator.java 433543 2006-08-22 06:22:54Z crossley $
051: */
052: public class FileGenerator extends ServiceableGenerator implements
053: CacheableProcessingComponent {
054:
055: /** The input source */
056: protected Source inputSource;
057:
058: /**
059: * Recycle this component.
060: * All instance variables are set to <code>null</code>.
061: */
062: public void recycle() {
063: if (null != this .inputSource) {
064: super .resolver.release(this .inputSource);
065: this .inputSource = null;
066: }
067: super .recycle();
068: }
069:
070: /**
071: * Setup the file generator.
072: * Try to get the last modification date of the source for caching.
073: */
074: public void setup(SourceResolver resolver, Map objectModel,
075: String src, Parameters par) throws ProcessingException,
076: SAXException, IOException {
077:
078: super .setup(resolver, objectModel, src, par);
079: try {
080: this .inputSource = super .resolver.resolveURI(src);
081: } catch (SourceException se) {
082: throw SourceUtil.handle("Error during resolving of '" + src
083: + "'.", se);
084: }
085: }
086:
087: /**
088: * Generate the unique key.
089: * This key must be unique inside the space of this component.
090: *
091: * @return The generated key hashes the src
092: */
093: public Serializable getKey() {
094: return this .inputSource.getURI();
095: }
096:
097: /**
098: * Generate the validity object.
099: *
100: * @return The generated validity object or <code>null</code> if the
101: * component is currently not cacheable.
102: */
103: public SourceValidity getValidity() {
104: return this .inputSource.getValidity();
105: }
106:
107: /**
108: * Generate XML data.
109: */
110: public void generate() throws IOException, SAXException,
111: ProcessingException {
112:
113: try {
114: if (getLogger().isDebugEnabled()) {
115: getLogger().debug(
116: "Source " + super .source + " resolved to "
117: + this .inputSource.getURI());
118: }
119: SourceUtil.parse(this .manager, this .inputSource,
120: super .xmlConsumer);
121: } catch (SAXException e) {
122: SourceUtil.handleSAXException(this.inputSource.getURI(), e);
123: }
124: }
125: }
|