KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001,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
17 package org.apache.cocoon.components.source;
18
19 import org.apache.avalon.framework.component.Component;
20 import org.apache.avalon.framework.component.ComponentManager;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.environment.ModifiableSource;
24 import org.apache.cocoon.util.ClassUtils;
25 import org.apache.excalibur.xml.sax.SAXParser;
26 import org.w3c.dom.Document JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import javax.xml.transform.OutputKeys JavaDoc;
32 import javax.xml.transform.Transformer JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.dom.DOMSource JavaDoc;
35 import javax.xml.transform.stream.StreamResult JavaDoc;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.StringWriter JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 /**
44  * This abstract class provides convenience methods to implement
45  * a stream based Source. Implement getInputStream(), getSystemId() and
46  * optionally override refresh(), recycle(), getLastModified() and
47  * getContentLength() to obtain a valid Source implementation.
48  * <p>
49  * This base implementation provides services to parse HTML sources
50  * (HTML is not valid XML) using JTidy, if present. The source is
51  * considered to contain HTML if <code>isHTMLContent()</code> returns
52  * true.
53  *
54  * @deprecated Use the new Avalon Excalibur Source Resolving
55  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
56  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
57  * @version CVS $Id: AbstractStreamSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
58  */

59 public abstract class AbstractStreamSource extends AbstractLogEnabled
60     implements ModifiableSource {
61
62     /** Is JTidy available? */
63     private static Class JavaDoc jtidyClass;
64
65     /** Properties used for converting HTML to XML */
66     private static Properties JavaDoc xmlProperties;
67
68     /** The TrAX factory for serializing xml */
69     public static TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
70
71     /**
72      * Test if JTidy is available
73      */

74     static {
75         jtidyClass = null;
76         try {
77             jtidyClass = ClassUtils.loadClass("org.w3c.tidy.Tidy");
78         } catch (ClassNotFoundException JavaDoc cnfe) {
79             // ignore
80
}
81         xmlProperties = new Properties JavaDoc();
82         xmlProperties.put(OutputKeys.METHOD, "xml");
83         xmlProperties.put(OutputKeys.OMIT_XML_DECLARATION, "no");
84     }
85
86     /** The ComponentManager needed for streaming */
87     protected ComponentManager manager;
88
89     /**
90      * Construct a new object
91      */

92     protected AbstractStreamSource(ComponentManager manager) {
93         this.manager = manager;
94     }
95
96     /**
97      * Does this source contain HTML ? If true, JTidy will be used (if available) to
98      * parse the input as XML.
99      * <p>
100      * The default here is to return false. Concrete subclasses should override
101      * this if needed.
102      */

103     protected boolean isHTMLContent() {
104         return false;
105     }
106
107     /**
108      * Return a new <code>InputSource</code> object
109      */

110     public InputSource JavaDoc getInputSource() throws IOException JavaDoc, ProcessingException {
111
112         InputStream JavaDoc stream = this.getInputStream();
113         if (jtidyClass != null && isHTMLContent()) {
114             try {
115                 final Object JavaDoc xhtmlconvert = jtidyClass.newInstance();
116                 Method JavaDoc m = jtidyClass.getMethod("setXmlOut", new Class JavaDoc[] { Class.forName("java.lang.Boolean")});
117                 m.invoke(xhtmlconvert, new Object JavaDoc[] { Boolean.TRUE });
118                 m = jtidyClass.getMethod("setXHTML", new Class JavaDoc[] {Class.forName("java.lang.Boolean")});
119                 m.invoke(xhtmlconvert, new Object JavaDoc[] { Boolean.TRUE });
120                 m = jtidyClass.getMethod("setShowWarnings", new Class JavaDoc[] { Class.forName("java.lang.Boolean")});
121                 m.invoke(xhtmlconvert, new Object JavaDoc[] { Boolean.FALSE });
122                 m = jtidyClass.getMethod("parseDOM", new Class JavaDoc[] { Class.forName("java.io.InputStream"), Class.forName("java.io.OutputStream")});
123                 final Document JavaDoc doc = (Document JavaDoc)m.invoke(xhtmlconvert, new Object JavaDoc[] { stream, null });
124                 final StringWriter JavaDoc writer = new StringWriter JavaDoc();
125                 final Transformer JavaDoc transformer;
126                 transformer = transformerFactory.newTransformer();
127                 transformer.setOutputProperties(xmlProperties);
128                 transformer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(writer));
129                 final String JavaDoc xmlstring = writer.toString();
130                 InputSource JavaDoc newObject = new InputSource JavaDoc(new java.io.StringReader JavaDoc(xmlstring));
131                 newObject.setSystemId(this.getSystemId());
132                 return newObject;
133             } catch (Exception JavaDoc ignore) {
134                 // Let someone else worry about what we got . This is as before.
135
this.refresh();
136                 stream = this.getInputStream();
137             }
138         }
139         InputSource JavaDoc newObject = new InputSource JavaDoc(stream);
140         newObject.setSystemId(this.getSystemId());
141         return newObject;
142     }
143
144     /**
145      * Stream content to a content handler or to an XMLConsumer.
146      *
147      * @throws SAXException if failed to parse source document.
148      */

149     public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
150         SAXParser parser = null;
151         try {
152             parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
153
154             parser.parse( this.getInputSource(), handler);
155         } catch (SAXException JavaDoc e) {
156             // Preserve original exception
157
throw e;
158         } catch (Exception JavaDoc e){
159             throw new SAXException JavaDoc("Exception during processing of "
160                                           + this.getSystemId(), e);
161         } finally {
162             if (parser != null) this.manager.release( (Component)parser);
163         }
164     }
165
166     /**
167      * Override this method to set the Content Length
168      *
169      */

170     public long getContentLength() {
171       return -1;
172     }
173
174     /**
175      * Override this method to set the Last Modification date
176      *
177      */

178     public long getLastModified() {
179       return 0;
180     }
181
182     /**
183      * Returns <code>true</code> if <code>getInputStream()</code> succeeds.
184      * Subclasses can provide a more efficient implementation.
185      */

186     public boolean exists() {
187         try {
188             InputStream JavaDoc stream = getInputStream();
189             stream.close();
190             return true;
191         } catch(Exception JavaDoc e) {
192             return false;
193         }
194     }
195
196     /**
197      * To be overriden in concrete subclasses if needed.
198      */

199     public void recycle() {
200     }
201
202     /**
203      * To be overriden in concrete subclasses if needed.
204      */

205     public void refresh() {
206     }
207 }
208
Popular Tags