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 java.io.Serializable;
020:
021: import org.apache.avalon.framework.service.ServiceException;
022: import org.apache.cocoon.ResourceNotFoundException;
023: import org.apache.cocoon.ProcessingException;
024: import org.apache.cocoon.caching.CacheableProcessingComponent;
025: import org.apache.cocoon.components.sax.XMLDeserializer;
026:
027: import org.apache.excalibur.source.SourceValidity;
028: import org.apache.excalibur.source.impl.validity.NOPValidity;
029: import org.apache.excalibur.store.Store;
030:
031: import org.xml.sax.SAXException;
032:
033: /**
034: * The generation half of FragmentExtractor.
035: *
036: * FragmentExtractor is a transformer-generator pair which is designed to allow
037: * sitemap managers to extract certain nodes from a SAX stream and move them
038: * into a separate pipeline. The main use for this is to extract inline SVG
039: * images and serve them up through a separate pipeline, usually serializing
040: * them to PNG or JPEG format first.
041: *
042: * This is by no means complete yet, but it should prove useful, particularly
043: * for offline generation.
044: *
045: * @author <a href="mailto:paul@luminas.co.uk">Paul Russell</a>
046: * @version CVS $Id: FragmentExtractorGenerator.java 433543 2006-08-22 06:22:54Z crossley $
047: */
048: public class FragmentExtractorGenerator extends ServiceableGenerator
049: implements CacheableProcessingComponent {
050:
051: /**
052: * Generate the unique key.
053: * This key must be unique inside the space of this component.
054: *
055: * @return The generated key hashes the src
056: */
057: public Serializable getKey() {
058: return this .source;
059: }
060:
061: /**
062: * Generate the validity object.
063: *
064: * @return The generated validity object or <code>null</code> if the
065: * component is currently not cacheable.
066: */
067: public SourceValidity getValidity() {
068: return NOPValidity.SHARED_INSTANCE;
069: }
070:
071: public void generate() throws SAXException, ProcessingException {
072: // Obtain the fragmentID (which is simply the filename portion of the source)
073: if (getLogger().isDebugEnabled()) {
074: getLogger().debug("Retrieving fragment " + source + ".");
075: }
076:
077: Store store = null;
078: XMLDeserializer deserializer = null;
079: Object fragment = null;
080: try {
081: store = (Store) this .manager.lookup(Store.TRANSIENT_STORE);
082: fragment = store.get(source);
083: if (fragment == null) {
084: throw new ResourceNotFoundException(
085: "Could not find fragment " + source
086: + " in store");
087: }
088:
089: deserializer = (XMLDeserializer) this .manager
090: .lookup(XMLDeserializer.ROLE);
091: deserializer.setConsumer(this .xmlConsumer);
092: deserializer.deserialize(fragment);
093:
094: } catch (ServiceException ce) {
095: if (getLogger().isDebugEnabled()) {
096: getLogger()
097: .debug("Could not lookup for component.", ce);
098: }
099: throw new SAXException("Could not lookup for component.",
100: ce);
101: } finally {
102: this.manager.release(store);
103: this.manager.release(deserializer);
104: }
105: }
106: }
|