1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.util.Map ; 21 22 import javax.xml.transform.TransformerConfigurationException ; 23 import javax.xml.transform.TransformerFactory ; 24 import javax.xml.transform.TransformerFactoryConfigurationError ; 25 import javax.xml.transform.sax.SAXTransformerFactory ; 26 import javax.xml.transform.sax.TransformerHandler ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import org.apache.avalon.framework.CascadingRuntimeException; 30 import org.apache.avalon.framework.configuration.Configuration; 31 import org.apache.avalon.framework.configuration.ConfigurationException; 32 import org.apache.avalon.framework.parameters.Parameters; 33 import org.apache.cocoon.ProcessingException; 34 import org.apache.cocoon.components.source.SourceUtil; 35 import org.apache.cocoon.environment.SourceResolver; 36 import org.apache.cocoon.util.ClassUtils; 37 import org.apache.excalibur.source.ModifiableSource; 38 import org.apache.excalibur.source.Source; 39 import org.apache.excalibur.source.SourceException; 40 import org.xml.sax.Attributes ; 41 import org.xml.sax.Locator ; 42 import org.xml.sax.SAXException ; 43 44 63 public class TeeTransformer extends AbstractSAXTransformer { 64 65 66 private TransformerHandler serializer; 67 68 69 private SAXTransformerFactory transformerFactory; 70 71 72 private SourceResolver resolver; 73 74 private OutputStream os; 75 76 83 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 84 throws ProcessingException, SAXException , IOException { 85 86 super.setup(resolver, objectModel, src, parameters); 87 88 Source source = null; 89 try { 90 this.resolver = resolver; 91 source = this.resolver.resolveURI(src); 92 String systemId = source.getURI(); 93 if (!(source instanceof ModifiableSource)) { 94 throw new ProcessingException("Source '" + systemId + "' is not writeable."); 95 } 96 this.serializer = this.transformerFactory.newTransformerHandler(); 97 os = ((ModifiableSource) source).getOutputStream(); 98 this.serializer.setResult(new StreamResult (os)); 99 } catch (SourceException e) { 100 throw SourceUtil.handle(e); 101 } catch (TransformerConfigurationException e) { 102 throw new ProcessingException(e); 103 } catch (TransformerFactoryConfigurationError error) { 104 throw new ProcessingException(error.getException()); 105 } finally { 106 if (source != null) { 107 this.resolver.release(source); 108 } 109 } 110 } 111 112 117 public void configure(Configuration configuration) throws ConfigurationException { 118 String tFactoryClass = configuration.getChild("transformer-factory").getValue(null); 119 if (tFactoryClass != null) { 120 try { 121 this.transformerFactory = (SAXTransformerFactory ) ClassUtils 122 .newInstance(tFactoryClass); 123 if (getLogger().isDebugEnabled()) { 124 getLogger().debug("Using transformer factory " + tFactoryClass); 125 } 126 } catch (Exception e) { 127 throw new ConfigurationException ( 128 "Cannot load transformer factory " + tFactoryClass, e); 129 } 130 } else { 131 this.transformerFactory = (SAXTransformerFactory ) TransformerFactory.newInstance(); 132 133 } 134 } 135 136 139 public void setDocumentLocator(Locator locator) { 140 super.contentHandler.setDocumentLocator(locator); 141 this.serializer.setDocumentLocator(locator); 142 } 143 144 147 public void startDocument() throws SAXException { 148 super.contentHandler.startDocument(); 149 this.serializer.startDocument(); 150 } 151 152 155 public void endDocument() throws SAXException { 156 super.contentHandler.endDocument(); 157 this.serializer.endDocument(); 158 if (os != null) { 159 try { 160 os.close(); 161 } catch (IOException e) { 162 throw new CascadingRuntimeException("Error closing output stream.", e); 163 } 164 } 165 } 166 167 170 public void startPrefixMapping(String prefix, String uri) throws SAXException { 171 super.contentHandler.startPrefixMapping(prefix, uri); 172 this.serializer.startPrefixMapping(prefix, uri); 173 } 174 175 178 public void endPrefixMapping(String prefix) throws SAXException { 179 super.contentHandler.endPrefixMapping(prefix); 180 this.serializer.endPrefixMapping(prefix); 181 } 182 183 186 public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { 187 super.contentHandler.startElement(uri, loc, raw, a); 188 this.serializer.startElement(uri, loc, raw, a); 189 } 190 191 194 public void endElement(String uri, String loc, String raw) throws SAXException { 195 super.contentHandler.endElement(uri, loc, raw); 196 this.serializer.endElement(uri, loc, raw); 197 } 198 199 202 public void characters(char ch[], int start, int len) throws SAXException { 203 super.contentHandler.characters(ch, start, len); 204 this.serializer.characters(ch, start, len); 205 } 206 207 210 public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { 211 super.contentHandler.ignorableWhitespace(ch, start, len); 212 this.serializer.ignorableWhitespace(ch, start, len); 213 } 214 215 218 public void processingInstruction(String target, String data) throws SAXException { 219 super.contentHandler.processingInstruction(target, data); 220 this.serializer.processingInstruction(target, data); 221 } 222 223 226 public void skippedEntity(String name) throws SAXException { 227 super.contentHandler.skippedEntity(name); 228 this.serializer.skippedEntity(name); 229 } 230 231 234 public void startDTD(String name, String publicId, String systemId) throws SAXException { 235 super.lexicalHandler.startDTD(name, publicId, systemId); 236 this.serializer.startDTD(name, publicId, systemId); 237 } 238 239 242 public void endDTD() throws SAXException { 243 super.lexicalHandler.endDTD(); 244 this.serializer.endDTD(); 245 } 246 247 250 public void startEntity(String name) throws SAXException { 251 super.lexicalHandler.startEntity(name); 252 this.serializer.startEntity(name); 253 } 254 255 258 public void endEntity(String name) throws SAXException { 259 super.lexicalHandler.endEntity(name); 260 this.serializer.endEntity(name); 261 } 262 263 266 public void startCDATA() throws SAXException { 267 super.lexicalHandler.startCDATA(); 268 this.serializer.startCDATA(); 269 } 270 271 274 public void endCDATA() throws SAXException { 275 super.lexicalHandler.endCDATA(); 276 this.serializer.endCDATA(); 277 } 278 279 282 public void comment(char ch[], int start, int len) throws SAXException { 283 super.lexicalHandler.comment(ch, start, len); 284 this.serializer.comment(ch, start, len); 285 } 286 287 292 public void recycle() { 293 super.recycle(); 294 this.serializer = null; 295 } 296 } 297 | Popular Tags |