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.cocoon.ProcessingException; 22 import org.apache.cocoon.serialization.Serializer; 23 import org.apache.cocoon.xml.AbstractXMLPipe; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.SAXException ; 26 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 43 public abstract class AbstractStreamWriteableSource 44 extends AbstractStreamSource 45 implements org.apache.cocoon.environment.WriteableSource { 46 47 protected AbstractStreamWriteableSource(ComponentManager manager) { 48 super(manager); 49 } 50 51 56 public boolean canCancel(ContentHandler handler) { 57 if (handler instanceof WritingPipe) { 58 WritingPipe pipe = (WritingPipe)handler; 59 if (pipe.getSource() == this) { 60 return pipe.canCancel(); 61 } 62 } 63 64 throw new IllegalArgumentException ("The handler is not associated to this source"); 66 } 67 68 72 public boolean canCancel(OutputStream stream) { 73 return false; 74 } 75 76 81 public void cancel(ContentHandler handler) throws Exception { 82 if (handler instanceof WritingPipe) { 83 WritingPipe pipe = (WritingPipe)handler; 84 if (pipe.getSource() == this) { 85 pipe.cancel(); 86 return; 87 } 88 } 89 90 throw new IllegalArgumentException ("The handler is not associated to this source"); 92 } 93 94 98 public void cancel(OutputStream stream) throws Exception { 99 throw new UnsupportedOperationException ("Cancel is not implemented on " + 100 this.getClass().getName()); 101 } 102 103 109 public ContentHandler getContentHandler() throws SAXException , ProcessingException { 110 111 Serializer serializer; 112 ComponentSelector selector; 113 114 String serializerName = this.isHTMLContent() ? "html" : "xml"; 115 116 try { 118 selector = 119 (ComponentSelector)this.manager.lookup(Serializer.ROLE + "Selector"); 120 serializer = (Serializer)selector.select(serializerName); 121 } catch(ComponentException ce) { 122 throw new ProcessingException("Cannot get '" + serializerName + "' serializer"); 123 } 124 125 try { 126 return new WritingPipe(getOutputStream(), selector, serializer); 127 } catch(IOException ioe) { 128 selector.release(serializer); 129 throw new ProcessingException("Cannot open stream for " + this.getSystemId(), ioe); 130 } 131 } 132 133 136 private class WritingPipe extends AbstractXMLPipe { 137 138 private OutputStream output; 140 141 private Serializer serializer; 143 private ComponentSelector selector; 144 145 public WritingPipe(OutputStream output, ComponentSelector selector, Serializer serializer) 146 throws IOException { 147 this.output = output; 148 this.selector = selector; 149 this.serializer = serializer; 150 151 this.setConsumer(this.serializer); 153 this.serializer.setOutputStream(this.output); 154 } 155 156 public org.apache.cocoon.environment.WriteableSource getSource() { 157 return AbstractStreamWriteableSource.this; 158 } 159 160 163 public void endDocument() throws SAXException { 164 super.endDocument(); 165 try { 166 close(); 167 } 168 catch(Exception e) { 169 throw new SAXException ("Error while closing output stream", e); 170 } 171 } 172 173 public boolean canCancel() { 174 return this.output != null; 175 } 176 177 180 public void cancel() throws Exception { 181 AbstractStreamWriteableSource.this.cancel(output); 182 close(); 183 } 184 185 private void close() throws IOException { 186 if (this.serializer != null) { 187 this.recycle(); 189 this.selector.release(this.serializer); 191 this.serializer = null; 192 } 193 194 if (this.output != null) { 195 this.output.close(); 196 this.output = null; 197 } 198 } 199 200 protected void finalize() throws Throwable { 202 close(); 203 super.finalize(); 204 } 205 } 206 } 207 | Popular Tags |