KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > AbstractSAXSource


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;
17
18 import org.apache.avalon.framework.component.ComponentException;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.ComponentSelector;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.environment.Environment;
24 import org.apache.cocoon.environment.Source;
25 import org.apache.cocoon.serialization.Serializer;
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.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34
35 /**
36  * This abstract class provides convenience methods to implement
37  * a SAX based Source. Implement toSAX() and getSystemId() and
38  * optionally override getLastModified() and getContentLength() to
39  * obtain a valid Source implementation.
40  *
41  * @deprecated Use the new Avalon Excalibur Source Resolving
42  * @author <a HREF="mailto:gianugo@apache.org">Gianugo Rabellino</a>
43  * @version CVS $Id: AbstractSAXSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public abstract class AbstractSAXSource
46   implements Source {
47
48     /** The Logger instance */
49     protected Logger log;
50
51     /** The ComponentManager instance */
52     protected ComponentManager manager;
53
54     /**
55      * The constructor.
56      *
57      * @param environment the Cocoon Environment.
58      * @param manager an Avalon Component Manager
59      * @param logger A LogKit logger
60      */

61
62     public AbstractSAXSource(Environment environment,
63                        ComponentManager manager,
64                        Logger logger) {
65       this.log = logger;
66       this.manager = manager;
67
68     }
69
70     /**
71      * Get an InputSource for the given URL. Shamelessly stolen
72      * from SitemapSource.
73      *
74      */

75
76     public InputStream JavaDoc getInputStream()
77       throws ProcessingException, IOException JavaDoc {
78
79         ComponentSelector serializerSelector = null;
80         Serializer serializer = null;
81         try {
82
83             serializerSelector = (ComponentSelector) this.manager.lookup(Serializer.ROLE + "Selector");
84             serializer = (Serializer)serializerSelector.select("xml");
85             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
86             serializer.setOutputStream(os);
87
88             this.toSAX(serializer);
89
90             return new ByteArrayInputStream JavaDoc(os.toByteArray());
91         } catch (ComponentException cme) {
92             throw new ProcessingException("could not lookup pipeline components", cme);
93         } catch (Exception JavaDoc e) {
94             throw new ProcessingException("Exception during processing of " + this.getSystemId(), e);
95         } finally {
96             if (serializer != null) serializerSelector.release(serializer);
97             if (serializerSelector != null) this.manager.release(serializerSelector);
98         }
99     }
100
101     /**
102      * Get an InputSource for the given URL.
103      *
104      */

105
106     public InputSource JavaDoc getInputSource()
107       throws ProcessingException, IOException JavaDoc {
108       InputSource JavaDoc is = new InputSource JavaDoc(this.getInputStream());
109       is.setSystemId(this.getSystemId());
110
111       return is;
112     }
113
114     /**
115      * Implement this method to obtain SAX events.
116      *
117      */

118
119     public abstract void toSAX(ContentHandler JavaDoc handler)
120       throws SAXException JavaDoc;
121
122     /**
123      * Implement this method to set the unique identifier.
124      *
125      */

126
127     public abstract String JavaDoc getSystemId();
128
129     /**
130      * Override this method to set the Content Length
131      *
132      */

133
134     public long getContentLength() {
135       return -1;
136     }
137
138     /**
139      * Override this method to set the Last Modification date
140      *
141      */

142
143     public long getLastModified() {
144       return 0;
145     }
146 }
147
Popular Tags