1 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 ; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 31 import javax.xml.transform.OutputKeys ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerFactory ; 34 import javax.xml.transform.dom.DOMSource ; 35 import javax.xml.transform.stream.StreamResult ; 36 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.StringWriter ; 40 import java.lang.reflect.Method ; 41 import java.util.Properties ; 42 43 59 public abstract class AbstractStreamSource extends AbstractLogEnabled 60 implements ModifiableSource { 61 62 63 private static Class jtidyClass; 64 65 66 private static Properties xmlProperties; 67 68 69 public static TransformerFactory transformerFactory = TransformerFactory.newInstance(); 70 71 74 static { 75 jtidyClass = null; 76 try { 77 jtidyClass = ClassUtils.loadClass("org.w3c.tidy.Tidy"); 78 } catch (ClassNotFoundException cnfe) { 79 } 81 xmlProperties = new Properties (); 82 xmlProperties.put(OutputKeys.METHOD, "xml"); 83 xmlProperties.put(OutputKeys.OMIT_XML_DECLARATION, "no"); 84 } 85 86 87 protected ComponentManager manager; 88 89 92 protected AbstractStreamSource(ComponentManager manager) { 93 this.manager = manager; 94 } 95 96 103 protected boolean isHTMLContent() { 104 return false; 105 } 106 107 110 public InputSource getInputSource() throws IOException , ProcessingException { 111 112 InputStream stream = this.getInputStream(); 113 if (jtidyClass != null && isHTMLContent()) { 114 try { 115 final Object xhtmlconvert = jtidyClass.newInstance(); 116 Method m = jtidyClass.getMethod("setXmlOut", new Class [] { Class.forName("java.lang.Boolean")}); 117 m.invoke(xhtmlconvert, new Object [] { Boolean.TRUE }); 118 m = jtidyClass.getMethod("setXHTML", new Class [] {Class.forName("java.lang.Boolean")}); 119 m.invoke(xhtmlconvert, new Object [] { Boolean.TRUE }); 120 m = jtidyClass.getMethod("setShowWarnings", new Class [] { Class.forName("java.lang.Boolean")}); 121 m.invoke(xhtmlconvert, new Object [] { Boolean.FALSE }); 122 m = jtidyClass.getMethod("parseDOM", new Class [] { Class.forName("java.io.InputStream"), Class.forName("java.io.OutputStream")}); 123 final Document doc = (Document )m.invoke(xhtmlconvert, new Object [] { stream, null }); 124 final StringWriter writer = new StringWriter (); 125 final Transformer transformer; 126 transformer = transformerFactory.newTransformer(); 127 transformer.setOutputProperties(xmlProperties); 128 transformer.transform(new DOMSource (doc), new StreamResult (writer)); 129 final String xmlstring = writer.toString(); 130 InputSource newObject = new InputSource (new java.io.StringReader (xmlstring)); 131 newObject.setSystemId(this.getSystemId()); 132 return newObject; 133 } catch (Exception ignore) { 134 this.refresh(); 136 stream = this.getInputStream(); 137 } 138 } 139 InputSource newObject = new InputSource (stream); 140 newObject.setSystemId(this.getSystemId()); 141 return newObject; 142 } 143 144 149 public void toSAX(ContentHandler handler) throws SAXException { 150 SAXParser parser = null; 151 try { 152 parser = (SAXParser)this.manager.lookup(SAXParser.ROLE); 153 154 parser.parse( this.getInputSource(), handler); 155 } catch (SAXException e) { 156 throw e; 158 } catch (Exception e){ 159 throw new SAXException ("Exception during processing of " 160 + this.getSystemId(), e); 161 } finally { 162 if (parser != null) this.manager.release( (Component)parser); 163 } 164 } 165 166 170 public long getContentLength() { 171 return -1; 172 } 173 174 178 public long getLastModified() { 179 return 0; 180 } 181 182 186 public boolean exists() { 187 try { 188 InputStream stream = getInputStream(); 189 stream.close(); 190 return true; 191 } catch(Exception e) { 192 return false; 193 } 194 } 195 196 199 public void recycle() { 200 } 201 202 205 public void refresh() { 206 } 207 } 208 | Popular Tags |