KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > CocoonToAvalonSource


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.components.source.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.avalon.excalibur.pool.Recyclable;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.ResourceNotFoundException;
25 import org.apache.cocoon.environment.ModifiableSource;
26 import org.apache.excalibur.source.Source;
27 import org.apache.excalibur.source.SourceException;
28 import org.apache.excalibur.source.SourceNotFoundException;
29 import org.apache.excalibur.source.SourceValidity;
30 import org.apache.excalibur.source.impl.validity.TimeStampValidity;
31 import org.apache.excalibur.xml.sax.XMLizable;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * This source objects wraps an obsolete Cocoon Source object
37  * to avoid recoding existing source objects.
38  *
39  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
40  * @version CVS $Id: CocoonToAvalonSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public final class CocoonToAvalonSource
43     implements Source, XMLizable, Recyclable {
44
45     /** The real source */
46     protected org.apache.cocoon.environment.Source source;
47
48     /** The protocol */
49     protected String JavaDoc protocol;
50
51     /**
52      * Constructor
53      */

54     public CocoonToAvalonSource(
55         String JavaDoc location,
56         org.apache.cocoon.environment.Source source) {
57         this.source = source;
58         int pos = location.indexOf(':');
59         this.protocol = location.substring(0, pos);
60     }
61
62     /**
63      * Return the protocol identifier.
64      */

65     public String JavaDoc getScheme() {
66         return this.protocol;
67     }
68
69     /**
70      * @see org.apache.excalibur.source.Source#exists()
71      */

72     public boolean exists() {
73         try {
74             this.getInputStream();
75             return true;
76         } catch (Exception JavaDoc local) {
77             return false;
78         }
79     }
80
81     /**
82      * Return an <code>InputStream</code> object to read from the source.
83      */

84     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceException {
85         try {
86             return this.source.getInputStream();
87         } catch (ResourceNotFoundException rnfe) {
88             throw new SourceNotFoundException("Source not found.", rnfe);
89         } catch (ProcessingException pe) {
90             throw new SourceException("ProcessingException", pe);
91         }
92     }
93
94     /**
95      * Return the unique identifer for this source
96      */

97     public String JavaDoc getURI() {
98         return this.source.getSystemId();
99     }
100
101     /**
102      * Get the Validity object. This can either wrap the last modification
103      * date or the expires information or...
104      * If it is currently not possible to calculate such an information
105      * <code>null</code> is returned.
106      */

107     public SourceValidity getValidity() {
108         if (this.source.getLastModified() > 0) {
109             return new TimeStampValidity(this.source.getLastModified());
110         }
111         return null;
112     }
113
114     /**
115      * Refresh this object and update the last modified date
116      * and content length.
117      */

118     public void refresh() {
119         if (this.source instanceof ModifiableSource) {
120             ((ModifiableSource) this.source).refresh();
121         }
122     }
123
124     /**
125      * The mime-type of the content described by this object.
126      * If the source is not able to determine the mime-type by itself
127      * this can be null.
128      */

129     public String JavaDoc getMimeType() {
130         return null;
131     }
132
133     /**
134      * Stream content to the content handler
135      */

136     public void toSAX(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
137         this.source.toSAX(contentHandler);
138     }
139
140     /**
141      * Recyclable
142      */

143     public void recycle() {
144         this.source.recycle();
145     }
146
147     /**
148      * Return the content length of the content or -1 if the length is
149      * unknown
150      */

151     public long getContentLength() {
152         return this.source.getContentLength();
153     }
154
155     /**
156      * Get the last modification date of the source or 0 if it
157      * is not possible to determine the date.
158      */

159     public long getLastModified() {
160         return this.source.getLastModified();
161     }
162
163     /**
164      * Get the value of a parameter.
165      * Using this it is possible to get custom information provided by the
166      * source implementation, like an expires date, HTTP headers etc.
167      */

168     public String JavaDoc getParameter(String JavaDoc name) {
169         return null;
170     }
171
172     /**
173      * Get the value of a parameter.
174      * Using this it is possible to get custom information provided by the
175      * source implementation, like an expires date, HTTP headers etc.
176      */

177     public long getParameterAsLong(String JavaDoc name) {
178         return 0;
179     }
180
181     /**
182      * Get parameter names
183      * Using this it is possible to get custom information provided by the
184      * source implementation, like an expires date, HTTP headers etc.
185      */

186     public Iterator JavaDoc getParameterNames() {
187         return java.util.Collections.EMPTY_LIST.iterator();
188     }
189
190 }
191
Popular Tags