Attachment 'FotoTransformer.java'

Download

   1 /** Creado en 02-may-2003 */
   2 package fcc.ima.cocoon;
   3 
   4 import org.apache.avalon.framework.parameters.Parameters;
   5 import org.apache.cocoon.ProcessingException;
   6 import org.apache.cocoon.components.sax.XMLTeePipe;
   7 import org.apache.cocoon.environment.SourceResolver;
   8 import org.apache.cocoon.transformation.AbstractTransformer;
   9 import org.apache.cocoon.xml.XMLConsumer;
  10 import org.apache.excalibur.source.Source;
  11 import org.xml.sax.SAXException;
  12 
  13 import java.io.FileOutputStream;
  14 import java.io.IOException;
  15 import java.io.OutputStream;
  16 import java.util.Map;
  17 import java.util.Properties;
  18 
  19 import javax.xml.transform.OutputKeys;
  20 import javax.xml.transform.TransformerFactory;
  21 import javax.xml.transform.sax.SAXTransformerFactory;
  22 import javax.xml.transform.sax.TransformerHandler;
  23 import javax.xml.transform.stream.StreamResult;
  24 
  25 /**For debugging purposes.  It saves to the file specified through the <code>logfile</code> parameter, all the
  26  * SAX events it receives.  The file is saved in XML format.
  27  * @author dperezcar */
  28 public class FotoTransformer extends AbstractTransformer {
  29 	protected TransformerHandler handler;
  30 	protected SAXTransformerFactory factory;
  31 	protected Properties format = new Properties();
  32 	protected OutputStream logFile;
  33 
  34 	public FotoTransformer() {
  35 		factory = (SAXTransformerFactory)TransformerFactory.newInstance();
  36 		format.put(OutputKeys.METHOD, "xml");
  37 		format.put(OutputKeys.ENCODING, "ISO-8859-1");
  38 	}
  39 	/** @see org.apache.cocoon.xml.XMLProducer#setConsumer(org.apache.cocoon.xml.XMLConsumer) */
  40 	public void setConsumer(XMLConsumer consumer) {
  41 		handler = null;
  42 		if (logFile != null) {
  43 			try {
  44 				handler = factory.newTransformerHandler();
  45 				handler.getTransformer().setOutputProperties(format);
  46 				handler.setResult(new StreamResult(logFile));
  47 			} catch (Throwable e) {
  48 				// Errors here are non-fatal
  49 				getLogger().error("", e);
  50 				handler = null;
  51 			}
  52 		}
  53 		super.setConsumer(handler == null ? consumer : 
  54 			new XMLTeePipe(consumer, new MiXMLPipe(handler, handler)));
  55 	}
  56 	public void setup(SourceResolver resolver, Map objectModel,
  57 					  String src, Parameters parameters)
  58 	throws ProcessingException, SAXException, IOException {
  59 		final String  logFileName = src;
  60 		final boolean  active = parameters.getParameterAsBoolean("active", true);
  61 
  62 		// Check for null, use System.out if logfile is not specified.
  63 		logFile = null;
  64 		try {
  65 			if ( null != logFileName && active) {
  66 				Source source = null;
  67 				try {
  68 					source = resolver.resolveURI(logFileName);
  69 					final String systemId = source.getURI();
  70 					if ( systemId.startsWith("file:") ) {
  71 						logFile = new FileOutputStream(systemId.substring(5));
  72 					} else {
  73 						throw new ProcessingException("The src parameter must point to a file: " + logFileName);
  74 					}
  75 				} finally {
  76 					if (source != null) {
  77 						resolver.release(source);
  78 					} 
  79 				}
  80 			}
  81 		} catch (Exception e) {
  82 			getLogger().error("", e);
  83 			logFile = null;
  84 		}
  85 	}
  86 	/**@see org.apache.avalon.excalibur.pool.Recyclable#recycle()*/
  87 	public void recycle() {
  88 		super.recycle();
  89 		closeFile();
  90 		handler = null;
  91 	}
  92 	/** Receive notification of the end of a document. */
  93 	public void endDocument() throws SAXException {
  94 		super.endDocument();
  95 		closeFile();
  96 		handler = null;
  97 	}
  98 	/**Attempt to close the log file when the class is GC'd */
  99 	public void destroy() {
 100 		closeFile();
 101 	}
 102 	/**Closes the file*/
 103 	private void closeFile() {
 104 		if (logFile != null) {
 105 			try {
 106 				logFile.close();
 107 			} catch (Exception e) {
 108 				getLogger().debug("LogTransformer.destroy()", e);
 109 			}
 110 			logFile = null;
 111 		}
 112 	}
 113 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.