1 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 ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 35 45 public abstract class AbstractSAXSource 46 implements Source { 47 48 49 protected Logger log; 50 51 52 protected ComponentManager manager; 53 54 61 62 public AbstractSAXSource(Environment environment, 63 ComponentManager manager, 64 Logger logger) { 65 this.log = logger; 66 this.manager = manager; 67 68 } 69 70 75 76 public InputStream getInputStream() 77 throws ProcessingException, IOException { 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 os = new ByteArrayOutputStream (); 86 serializer.setOutputStream(os); 87 88 this.toSAX(serializer); 89 90 return new ByteArrayInputStream (os.toByteArray()); 91 } catch (ComponentException cme) { 92 throw new ProcessingException("could not lookup pipeline components", cme); 93 } catch (Exception 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 105 106 public InputSource getInputSource() 107 throws ProcessingException, IOException { 108 InputSource is = new InputSource (this.getInputStream()); 109 is.setSystemId(this.getSystemId()); 110 111 return is; 112 } 113 114 118 119 public abstract void toSAX(ContentHandler handler) 120 throws SAXException ; 121 122 126 127 public abstract String getSystemId(); 128 129 133 134 public long getContentLength() { 135 return -1; 136 } 137 138 142 143 public long getLastModified() { 144 return 0; 145 } 146 } 147 | Popular Tags |