cziegeler 02/02/05 03:39:30 Modified: . changes.xml src/java/org/apache/cocoon/transformation LogTransformer.java Log: Minor rewrite of LogTransformer Revision Changes Path 1.101 +5 -1 xml-cocoon2/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/changes.xml,v retrieving revision 1.100 retrieving revision 1.101 diff -u -r1.100 -r1.101 --- changes.xml 5 Feb 2002 11:12:12 -0000 1.100 +++ changes.xml 5 Feb 2002 11:39:29 -0000 1.101 @@ -4,7 +4,7 @@ <!-- History of Cocoon changes - $Id: changes.xml,v 1.100 2002/02/05 11:12:12 cziegeler Exp $ + $Id: changes.xml,v 1.101 2002/02/05 11:39:29 cziegeler Exp $ --> <changes title="History of Changes"> @@ -31,6 +31,10 @@ </devs> <release version="@version@" date="@date@"> + <action dev="CZ" type="update"> + Minor rewrite of the LogTransformer. The logfile is now resolved using + the standard source resolver. The component is now recycled properly. + </action> <action dev="CZ" type="update"> Minor rewrite of the SQLTransformer. Removed several parameters tests and the use of Properties objects - instead the Parameters object 1.5 +80 -76 xml-cocoon2/src/java/org/apache/cocoon/transformation/LogTransformer.java Index: LogTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/LogTransformer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- LogTransformer.java 4 Feb 2002 14:08:34 -0000 1.4 +++ LogTransformer.java 5 Feb 2002 11:39:30 -0000 1.5 @@ -54,9 +54,9 @@ */ package org.apache.cocoon.transformation; -import org.apache.avalon.excalibur.pool.Poolable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.environment.Source; import org.apache.cocoon.environment.SourceResolver; import org.xml.sax.Attributes; import org.xml.sax.Locator; @@ -89,66 +89,73 @@ * <br> * This transformations main purpose is debugging. * + * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> * (PWR Organisation & Entwicklung) - * @version CVS $Id: LogTransformer.java,v 1.4 2002/02/04 14:08:34 cziegeler Exp $ + * @version CVS $Id: LogTransformer.java,v 1.5 2002/02/05 11:39:30 cziegeler Exp $ * */ -public class LogTransformer extends AbstractTransformer implements Poolable { - /** Wether we are forwarding XML data or not. */ - private boolean canReset=true; - - private String lf = System.getProperty("line.separator", "\n"); - - /** true if filename is valid and writeable */ - private boolean isValid = true; - /** filename for log file */ - private String logfilename = null; - /** log file */ - private FileWriter logfile = null; - /** should we append content to the log file */ - private boolean append = false; +public class LogTransformer + extends AbstractTransformer { + + private static String lf = System.getProperty("line.separator", "\n"); - /** BEGIN SitemapComponent methods **/ + /** log file */ + private FileWriter logfile; + /** + * Setup + */ public void setup(SourceResolver resolver, Map objectModel, - String source, Parameters parameters) - throws ProcessingException, SAXException, IOException { - if (logfile == null) { - String appends = parameters.getParameter("append", null); - logfilename = parameters.getParameter("logfile", null); - if ("yes".equals(appends)) { - append = true; - } else { - append = false; - } + String src, Parameters parameters) + throws ProcessingException, SAXException, IOException { + final boolean append = parameters.getParameterAsBoolean("append", false); + final String logfilename = parameters.getParameter("logfile", null); + + // Check for null, use System.out if logfile is not specified. + if ( null != logfilename ) { + Source source = null; try { - // Check for null, use System.out if logfile is not specified. - if(logfilename != null) - logfile = new FileWriter(logfilename, append ); - else - logfile = new FileWriter(java.io.FileDescriptor.out); - } catch (IOException e) { - getLogger().debug("LogTransformer", e); - isValid = false; - throw e; + source = resolver.resolve( logfilename ); + final String systemId = source.getSystemId(); + if ( systemId.startsWith("file:") ) { + this.logfile = new FileWriter(systemId.substring(5), append ); + } else { + throw new ProcessingException("The logfile parameter must point to a file: " + logfilename); + } + } finally { + if (source != null) source.recycle(); } + } else { + this.logfile = new FileWriter(java.io.FileDescriptor.out); } + Date date = new Date(); StringBuffer logEntry = new StringBuffer(); logEntry.append ( "---------------------------- [" ); logEntry.append ( date.toString() ); logEntry.append ( "] ----------------------------" ); - log("setup", logEntry.toString()); + this.log("setup", logEntry.toString()); } - /** END SitemapComponent methods **/ + /** + * Recycle + */ + public void recycle() { + super.recycle(); + try { + if (this.logfile != null) logfile.close(); + } catch (Exception e) { + this.getLogger().debug("LogTransformer.recycle()", e); + } + this.logfile = null; + } /** * Receive an object for locating the origin of SAX document events. */ public void setDocumentLocator(Locator locator) { - log("setDocumentLocator",""); + this.log("setDocumentLocator",""); if (super.contentHandler!=null) { super.contentHandler.setDocumentLocator(locator); } @@ -159,12 +166,10 @@ */ public void startDocument() throws SAXException { - log("startDocument", ""); + this.log("startDocument", ""); if (super.contentHandler!=null) { super.contentHandler.startDocument(); } - - this.canReset=false; } /** @@ -172,12 +177,10 @@ */ public void endDocument() throws SAXException { - log ("endDocument", ""); + this.log ("endDocument", ""); if (super.contentHandler!=null) { super.contentHandler.endDocument(); } - - this.canReset=true; } /** @@ -185,7 +188,7 @@ */ public void startPrefixMapping(String prefix, String uri) throws SAXException { - log ("startPrefixMapping", "prefix="+prefix+",uri="+uri); + this.log ("startPrefixMapping", "prefix="+prefix+",uri="+uri); if (super.contentHandler!=null) { super.contentHandler.startPrefixMapping(prefix,uri); } @@ -196,7 +199,7 @@ */ public void endPrefixMapping(String prefix) throws SAXException { - log ("endPrefixMapping", "prefix="+prefix); + this.log ("endPrefixMapping", "prefix="+prefix); if (super.contentHandler!=null) { super.contentHandler.endPrefixMapping(prefix); } @@ -207,9 +210,9 @@ */ public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { - log ("startElement", "uri="+uri+",local="+loc+",raw="+raw); + this.log ("startElement", "uri="+uri+",local="+loc+",raw="+raw); for (int i = 0; i < a.getLength(); i++) { - log (" ", new Integer(i+1).toString() + this.log (" ", new Integer(i+1).toString() +". uri="+a.getURI(i) +",local="+a.getLocalName(i) +",qname="+a.getQName(i) @@ -227,7 +230,7 @@ */ public void endElement(String uri, String loc, String qname) throws SAXException { - log ("endElement", "uri="+uri+",local="+loc+",qname="+qname); + this.log ("endElement", "uri="+uri+",local="+loc+",qname="+qname); if (super.contentHandler!=null) { super.contentHandler.endElement(uri,loc,qname); } @@ -238,7 +241,7 @@ */ public void characters(char ch[], int start, int len) throws SAXException { - log ("characters", new String(ch,start,len)); + this.log ("characters", new String(ch,start,len)); if (super.contentHandler!=null) { super.contentHandler.characters(ch,start,len); } @@ -249,7 +252,7 @@ */ public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { - log ("ignorableWhitespace", new String(ch,start,len)); + this.log ("ignorableWhitespace", new String(ch,start,len)); if (super.contentHandler!=null) { super.contentHandler.ignorableWhitespace(ch,start,len); } @@ -271,7 +274,7 @@ */ public void skippedEntity(String name) throws SAXException { - log ("skippedEntity", "name="+name); + this.log ("skippedEntity", "name="+name); if (super.contentHandler!=null) { super.contentHandler.skippedEntity(name); } @@ -282,7 +285,7 @@ */ public void startDTD(String name, String publicId, String systemId) throws SAXException { - log ("startDTD", "name="+name+",publicId="+publicId+",systemId="+systemId); + this.log ("startDTD", "name="+name+",publicId="+publicId+",systemId="+systemId); if (super.lexicalHandler!=null) { super.lexicalHandler.startDTD(name,publicId,systemId); } @@ -293,7 +296,7 @@ */ public void endDTD() throws SAXException { - log ("endDTD", ""); + this.log ("endDTD", ""); if (super.lexicalHandler!=null) { super.lexicalHandler.endDTD(); } @@ -304,7 +307,7 @@ */ public void startEntity(String name) throws SAXException { - log ("startEntity", "name="+name); + this.log ("startEntity", "name="+name); if (super.lexicalHandler!=null) { super.lexicalHandler.startEntity(name); } @@ -315,7 +318,7 @@ */ public void endEntity(String name) throws SAXException { - log ("endEntity", "name="+name); + this.log ("endEntity", "name="+name); if (super.lexicalHandler!=null) { super.lexicalHandler.endEntity(name); } @@ -326,7 +329,7 @@ */ public void startCDATA() throws SAXException { - log ("startCDATA", ""); + this.log ("startCDATA", ""); if (super.lexicalHandler!=null) { super.lexicalHandler.startCDATA(); } @@ -337,7 +340,7 @@ */ public void endCDATA() throws SAXException { - log ("endCDATA", ""); + this.log ("endCDATA", ""); if (super.lexicalHandler!=null) { super.lexicalHandler.endCDATA(); } @@ -348,7 +351,7 @@ */ public void comment(char ch[], int start, int len) throws SAXException { - log ("comment", new String(ch,start,len)); + this.log ("comment", new String(ch,start,len)); if (super.lexicalHandler!=null) { super.lexicalHandler.comment(ch,start,len); } @@ -358,21 +361,22 @@ * Report to logfile. */ private void log (String location, String description) { - if (isValid) { - StringBuffer logEntry = new StringBuffer(); - logEntry.append ( "[" ); - logEntry.append ( location ); - logEntry.append ( "] " ); - logEntry.append ( description ); - logEntry.append ( lf ); - getLogger().info(logEntry.toString()); - synchronized (logfile) { - try { - logfile.write( logEntry.toString(), 0, logEntry.length()); - logfile.flush(); - } - catch(IOException ioe) { getLogger().debug("LogTransformer.log", ioe); } - } + StringBuffer logEntry = new StringBuffer(); + logEntry.append ( "[" ); + logEntry.append ( location ); + logEntry.append ( "] " ); + logEntry.append ( description ); + logEntry.append ( lf ); + final String text = logEntry.toString(); + if ( this.getLogger().isInfoEnabled() ) { + getLogger().info( text ); + } + try { + logfile.write( text, 0, text.length()); + logfile.flush(); + } + catch(IOException ioe) { + this.getLogger().debug("LogTransformer.log", ioe); } } @@ -381,7 +385,7 @@ */ public void destroy() { try { - logfile.close(); + if (this.logfile != null) logfile.close(); } catch (Exception e) {getLogger().debug("LogTransformer.destroy()", e);} } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]