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.impl;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Iterator;
022:
023: import org.apache.avalon.excalibur.pool.Recyclable;
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.ResourceNotFoundException;
026: import org.apache.cocoon.environment.ModifiableSource;
027: import org.apache.excalibur.source.Source;
028: import org.apache.excalibur.source.SourceException;
029: import org.apache.excalibur.source.SourceNotFoundException;
030: import org.apache.excalibur.source.SourceValidity;
031: import org.apache.excalibur.source.impl.validity.TimeStampValidity;
032: import org.apache.excalibur.xml.sax.XMLizable;
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * This source objects wraps an obsolete Cocoon Source object
038: * to avoid recoding existing source objects.
039: *
040: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
041: * @version CVS $Id: CocoonToAvalonSource.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public final class CocoonToAvalonSource implements Source, XMLizable,
044: Recyclable {
045:
046: /** The real source */
047: protected org.apache.cocoon.environment.Source source;
048:
049: /** The protocol */
050: protected String protocol;
051:
052: /**
053: * Constructor
054: */
055: public CocoonToAvalonSource(String location,
056: org.apache.cocoon.environment.Source source) {
057: this .source = source;
058: int pos = location.indexOf(':');
059: this .protocol = location.substring(0, pos);
060: }
061:
062: /**
063: * Return the protocol identifier.
064: */
065: public String getScheme() {
066: return this .protocol;
067: }
068:
069: /**
070: * @see org.apache.excalibur.source.Source#exists()
071: */
072: public boolean exists() {
073: try {
074: this .getInputStream();
075: return true;
076: } catch (Exception local) {
077: return false;
078: }
079: }
080:
081: /**
082: * Return an <code>InputStream</code> object to read from the source.
083: */
084: public InputStream getInputStream() throws IOException,
085: SourceException {
086: try {
087: return this .source.getInputStream();
088: } catch (ResourceNotFoundException rnfe) {
089: throw new SourceNotFoundException("Source not found.", rnfe);
090: } catch (ProcessingException pe) {
091: throw new SourceException("ProcessingException", pe);
092: }
093: }
094:
095: /**
096: * Return the unique identifer for this source
097: */
098: public String getURI() {
099: return this .source.getSystemId();
100: }
101:
102: /**
103: * Get the Validity object. This can either wrap the last modification
104: * date or the expires information or...
105: * If it is currently not possible to calculate such an information
106: * <code>null</code> is returned.
107: */
108: public SourceValidity getValidity() {
109: if (this .source.getLastModified() > 0) {
110: return new TimeStampValidity(this .source.getLastModified());
111: }
112: return null;
113: }
114:
115: /**
116: * Refresh this object and update the last modified date
117: * and content length.
118: */
119: public void refresh() {
120: if (this .source instanceof ModifiableSource) {
121: ((ModifiableSource) this .source).refresh();
122: }
123: }
124:
125: /**
126: * The mime-type of the content described by this object.
127: * If the source is not able to determine the mime-type by itself
128: * this can be null.
129: */
130: public String getMimeType() {
131: return null;
132: }
133:
134: /**
135: * Stream content to the content handler
136: */
137: public void toSAX(ContentHandler contentHandler)
138: throws SAXException {
139: this .source.toSAX(contentHandler);
140: }
141:
142: /**
143: * Recyclable
144: */
145: public void recycle() {
146: this .source.recycle();
147: }
148:
149: /**
150: * Return the content length of the content or -1 if the length is
151: * unknown
152: */
153: public long getContentLength() {
154: return this .source.getContentLength();
155: }
156:
157: /**
158: * Get the last modification date of the source or 0 if it
159: * is not possible to determine the date.
160: */
161: public long getLastModified() {
162: return this .source.getLastModified();
163: }
164:
165: /**
166: * Get the value of a parameter.
167: * Using this it is possible to get custom information provided by the
168: * source implementation, like an expires date, HTTP headers etc.
169: */
170: public String getParameter(String name) {
171: return null;
172: }
173:
174: /**
175: * Get the value of a parameter.
176: * Using this it is possible to get custom information provided by the
177: * source implementation, like an expires date, HTTP headers etc.
178: */
179: public long getParameterAsLong(String name) {
180: return 0;
181: }
182:
183: /**
184: * Get parameter names
185: * Using this it is possible to get custom information provided by the
186: * source implementation, like an expires date, HTTP headers etc.
187: */
188: public Iterator getParameterNames() {
189: return java.util.Collections.EMPTY_LIST.iterator();
190: }
191:
192: }
|