KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.component.ComponentManager;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.components.source.SourceUtil;
21 import org.apache.cocoon.environment.Environment;
22 import org.apache.cocoon.environment.ModifiableSource;
23 import org.apache.excalibur.source.Source;
24 import org.apache.excalibur.source.SourceException;
25 import org.apache.excalibur.source.SourceResolver;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32
33 /**
34  * This source objects wraps an Avalon Excalibur Source to get
35  * an obsolete Cocoon Source object for the use of the deprecated
36  * {@link org.apache.cocoon.environment.SourceResolver#resolve(String)}
37  * method.
38  *
39  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
40  * @version CVS $Id: AvalonToCocoonSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public final class AvalonToCocoonSource
43     implements ModifiableSource {
44
45     /** The real source */
46     protected Source source;
47
48     /** The source resolver */
49     protected SourceResolver resolver;
50
51     /** The environment */
52     protected Environment environment;
53
54     /** The manager */
55     protected ComponentManager manager;
56     
57     /**
58      * Constructor
59      */

60     public AvalonToCocoonSource(Source source,
61                                 SourceResolver resolver,
62                                 Environment environment,
63                                 ComponentManager manager) {
64         this.source = source;
65         this.resolver = resolver;
66         this.environment = environment;
67         this.manager = manager;
68     }
69
70     /**
71      * Get the last modification date of the source or 0 if it
72      * is not possible to determine the date.
73      */

74     public long getLastModified() {
75         return this.source.getLastModified();
76     }
77
78     /**
79      * Get the content length of the source or -1 if it
80      * is not possible to determine the length.
81      */

82     public long getContentLength() {
83         return this.source.getContentLength();
84     }
85
86     /**
87      * Return an <code>InputStream</code> object to read from the source.
88      */

89     public InputStream JavaDoc getInputStream()
90     throws ProcessingException, IOException JavaDoc {
91         try {
92             return this.source.getInputStream();
93         } catch (SourceException e) {
94             throw SourceUtil.handle(e);
95         }
96     }
97
98     /**
99      * Return an <code>InputSource</code> object to read the XML
100      * content.
101      *
102      * @return an <code>InputSource</code> value
103      * @exception ProcessingException if an error occurs
104      * @exception IOException if an error occurs
105      */

106     public InputSource JavaDoc getInputSource()
107     throws ProcessingException, IOException JavaDoc {
108         try {
109             InputSource JavaDoc newObject = new InputSource JavaDoc(this.source.getInputStream());
110             newObject.setSystemId(this.getSystemId());
111             return newObject;
112         } catch (SourceException se) {
113             throw SourceUtil.handle(se);
114         }
115     }
116
117     /**
118      * Return the unique identifer for this source
119      */

120     public String JavaDoc getSystemId() {
121         return this.source.getURI();
122     }
123
124     public void recycle() {
125         this.resolver.release(this.source);
126         this.source = null;
127         this.environment = null;
128     }
129
130     public void refresh() {
131         this.source.refresh();
132     }
133
134     /**
135      * Stream content to a content handler or to an XMLConsumer.
136      *
137      * @throws SAXException if failed to parse source document.
138      */

139     public void toSAX(ContentHandler JavaDoc handler)
140     throws SAXException JavaDoc {
141         try {
142             SourceUtil.parse(this.manager, this.source, handler);
143         } catch (ProcessingException pe) {
144             throw new SAXException JavaDoc("ProcessingException during streaming.", pe);
145         } catch (IOException JavaDoc ioe) {
146             throw new SAXException JavaDoc("IOException during streaming.", ioe);
147         }
148     }
149
150 }
151
Popular Tags