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.components.source;
018:
019: import org.apache.avalon.framework.component.ComponentException;
020: import org.apache.avalon.framework.component.ComponentManager;
021: import org.apache.avalon.framework.component.ComponentSelector;
022: import org.apache.avalon.framework.logger.Logger;
023: import org.apache.cocoon.ProcessingException;
024: import org.apache.cocoon.environment.Environment;
025: import org.apache.cocoon.environment.Source;
026: import org.apache.cocoon.serialization.Serializer;
027: import org.xml.sax.ContentHandler;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030:
031: import java.io.ByteArrayInputStream;
032: import java.io.ByteArrayOutputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: /**
037: * This abstract class provides convenience methods to implement
038: * a SAX based Source. Implement toSAX() and getSystemId() and
039: * optionally override getLastModified() and getContentLength() to
040: * obtain a valid Source implementation.
041: *
042: * @deprecated Use the new Avalon Excalibur Source Resolving
043: * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
044: * @version CVS $Id: AbstractSAXSource.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public abstract class AbstractSAXSource implements Source {
047:
048: /** The Logger instance */
049: protected Logger log;
050:
051: /** The ComponentManager instance */
052: protected ComponentManager manager;
053:
054: /**
055: * The constructor.
056: *
057: * @param environment the Cocoon Environment.
058: * @param manager an Avalon Component Manager
059: * @param logger A LogKit logger
060: */
061:
062: public AbstractSAXSource(Environment environment,
063: ComponentManager manager, Logger logger) {
064: this .log = logger;
065: this .manager = manager;
066:
067: }
068:
069: /**
070: * Get an InputSource for the given URL. Shamelessly stolen
071: * from SitemapSource.
072: *
073: */
074:
075: public InputStream getInputStream() throws ProcessingException,
076: IOException {
077:
078: ComponentSelector serializerSelector = null;
079: Serializer serializer = null;
080: try {
081:
082: serializerSelector = (ComponentSelector) this .manager
083: .lookup(Serializer.ROLE + "Selector");
084: serializer = (Serializer) serializerSelector.select("xml");
085: ByteArrayOutputStream os = new ByteArrayOutputStream();
086: serializer.setOutputStream(os);
087:
088: this .toSAX(serializer);
089:
090: return new ByteArrayInputStream(os.toByteArray());
091: } catch (ComponentException cme) {
092: throw new ProcessingException(
093: "could not lookup pipeline components", cme);
094: } catch (Exception e) {
095: throw new ProcessingException(
096: "Exception during processing of "
097: + this .getSystemId(), e);
098: } finally {
099: if (serializer != null)
100: serializerSelector.release(serializer);
101: if (serializerSelector != null)
102: this .manager.release(serializerSelector);
103: }
104: }
105:
106: /**
107: * Get an InputSource for the given URL.
108: *
109: */
110:
111: public InputSource getInputSource() throws ProcessingException,
112: IOException {
113: InputSource is = new InputSource(this .getInputStream());
114: is.setSystemId(this .getSystemId());
115:
116: return is;
117: }
118:
119: /**
120: * Implement this method to obtain SAX events.
121: *
122: */
123:
124: public abstract void toSAX(ContentHandler handler)
125: throws SAXException;
126:
127: /**
128: * Implement this method to set the unique identifier.
129: *
130: */
131:
132: public abstract String getSystemId();
133:
134: /**
135: * Override this method to set the Content Length
136: *
137: */
138:
139: public long getContentLength() {
140: return -1;
141: }
142:
143: /**
144: * Override this method to set the Last Modification date
145: *
146: */
147:
148: public long getLastModified() {
149: return 0;
150: }
151: }
|