1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.excalibur.source.Source; 21 import org.apache.excalibur.source.SourceException; 22 import org.apache.cocoon.components.source.SourceUtil; 23 import org.apache.cocoon.environment.SourceResolver; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.Locator ; 26 import org.xml.sax.SAXException ; 27 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.util.Date ; 31 import java.util.Map ; 32 33 71 public class LogTransformer 72 extends AbstractTransformer { 73 74 private static String lf = System.getProperty("line.separator", "\n"); 75 76 77 private FileWriter logfile; 78 79 82 public void setup(SourceResolver resolver, Map objectModel, 83 String src, Parameters parameters) 84 throws ProcessingException, SAXException , IOException { 85 final boolean append = parameters.getParameterAsBoolean("append", false); 86 final String logfilename = parameters.getParameter("logfile", null); 87 88 this.logfile = null; 90 if ( null != logfilename ) { 91 Source source = null; 92 try { 93 source = resolver.resolveURI( logfilename ); 94 final String systemId = source.getURI(); 95 if ( systemId.startsWith("file:") ) { 96 this.logfile = new FileWriter (systemId.substring(5), append ); 97 } else { 98 throw new ProcessingException("The logfile parameter must point to a file: " + logfilename); 99 } 100 } catch (SourceException se) { 101 throw SourceUtil.handle(se); 102 } finally { 103 resolver.release( source ); 104 } 105 } 106 107 Date date = new Date (); 108 StringBuffer logEntry = new StringBuffer (); 109 logEntry.append ( "---------------------------- [" ); 110 logEntry.append ( date.toString() ); 111 logEntry.append ( "] ----------------------------" ); 112 this.log("setup", logEntry.toString()); 113 } 114 115 118 public void recycle() { 119 super.recycle(); 120 try { 121 if (this.logfile != null) logfile.close(); 122 } catch (Exception e) { 123 this.getLogger().warn("LogTransformer.recycle()", e); 124 } 125 this.logfile = null; 126 } 127 128 131 public void setDocumentLocator(Locator locator) { 132 this.log("setDocumentLocator", locator != null ? "systemid="+locator.getSystemId()+",publicid="+locator.getPublicId() : "(locator is null)"); 133 if (super.contentHandler!=null) { 134 super.contentHandler.setDocumentLocator(locator); 135 } 136 } 137 138 141 public void startDocument() 142 throws SAXException { 143 this.log("startDocument", ""); 144 if (super.contentHandler!=null) { 145 super.contentHandler.startDocument(); 146 } 147 } 148 149 152 public void endDocument() 153 throws SAXException { 154 this.log ("endDocument", ""); 155 if (super.contentHandler!=null) { 156 super.contentHandler.endDocument(); 157 } 158 } 159 160 163 public void startPrefixMapping(String prefix, String uri) 164 throws SAXException { 165 this.log ("startPrefixMapping", "prefix="+prefix+",uri="+uri); 166 if (super.contentHandler!=null) { 167 super.contentHandler.startPrefixMapping(prefix,uri); 168 } 169 } 170 171 174 public void endPrefixMapping(String prefix) 175 throws SAXException { 176 this.log ("endPrefixMapping", "prefix="+prefix); 177 if (super.contentHandler!=null) { 178 super.contentHandler.endPrefixMapping(prefix); 179 } 180 } 181 182 185 public void startElement(String uri, String loc, String raw, Attributes a) 186 throws SAXException { 187 this.log ("startElement", "uri="+uri+",local="+loc+",raw="+raw); 188 for (int i = 0; i < a.getLength(); i++) { 189 this.log (" ", Integer.toString(i+1) 190 +". uri="+a.getURI(i) 191 +",local="+a.getLocalName(i) 192 +",qname="+a.getQName(i) 193 +",type="+a.getType(i) 194 +",value="+a.getValue(i)); 195 } 196 if (super.contentHandler!=null) { 197 super.contentHandler.startElement(uri,loc,raw,a); 198 } 199 } 200 201 202 205 public void endElement(String uri, String loc, String raw) 206 throws SAXException { 207 this.log ("endElement", "uri="+uri+",local="+loc+",raw="+raw); 208 if (super.contentHandler!=null) { 209 super.contentHandler.endElement(uri,loc,raw); 210 } 211 } 212 213 216 public void characters(char ch[], int start, int len) 217 throws SAXException { 218 this.log ("characters", new String (ch,start,len)); 219 if (super.contentHandler!=null) { 220 super.contentHandler.characters(ch,start,len); 221 } 222 } 223 224 227 public void ignorableWhitespace(char ch[], int start, int len) 228 throws SAXException { 229 this.log ("ignorableWhitespace", new String (ch,start,len)); 230 if (super.contentHandler!=null) { 231 super.contentHandler.ignorableWhitespace(ch,start,len); 232 } 233 } 234 235 238 public void processingInstruction(String target, String data) 239 throws SAXException { 240 log ("processingInstruction", "target="+target+",data="+data); 241 if (super.contentHandler!=null) { 242 super.contentHandler.processingInstruction(target,data); 243 } 244 } 245 246 249 public void skippedEntity(String name) 250 throws SAXException { 251 this.log ("skippedEntity", "name="+name); 252 if (super.contentHandler!=null) { 253 super.contentHandler.skippedEntity(name); 254 } 255 } 256 257 260 public void startDTD(String name, String publicId, String systemId) 261 throws SAXException { 262 this.log ("startDTD", "name="+name+",publicId="+publicId+",systemId="+systemId); 263 if (super.lexicalHandler!=null) { 264 super.lexicalHandler.startDTD(name,publicId,systemId); 265 } 266 } 267 268 271 public void endDTD() 272 throws SAXException { 273 this.log ("endDTD", ""); 274 if (super.lexicalHandler!=null) { 275 super.lexicalHandler.endDTD(); 276 } 277 } 278 279 282 public void startEntity(String name) 283 throws SAXException { 284 this.log ("startEntity", "name="+name); 285 if (super.lexicalHandler!=null) { 286 super.lexicalHandler.startEntity(name); 287 } 288 } 289 290 293 public void endEntity(String name) 294 throws SAXException { 295 this.log ("endEntity", "name="+name); 296 if (super.lexicalHandler!=null) { 297 super.lexicalHandler.endEntity(name); 298 } 299 } 300 301 304 public void startCDATA() 305 throws SAXException { 306 this.log ("startCDATA", ""); 307 if (super.lexicalHandler!=null) { 308 super.lexicalHandler.startCDATA(); 309 } 310 } 311 312 315 public void endCDATA() 316 throws SAXException { 317 this.log ("endCDATA", ""); 318 if (super.lexicalHandler!=null) { 319 super.lexicalHandler.endCDATA(); 320 } 321 } 322 323 326 public void comment(char ch[], int start, int len) 327 throws SAXException { 328 this.log ("comment", new String (ch,start,len)); 329 if (super.lexicalHandler!=null) { 330 super.lexicalHandler.comment(ch,start,len); 331 } 332 } 333 334 337 private void log (String location, String description) { 338 final StringBuffer logEntry = new StringBuffer (); 339 logEntry.append ( "[" ); 340 logEntry.append ( location ); 341 logEntry.append ( "] " ); 342 logEntry.append ( description ); 343 logEntry.append ( lf ); 344 final String text = logEntry.toString(); 345 if ( this.getLogger().isInfoEnabled() ) { 346 this.getLogger().info( text ); 347 } 348 try { 349 if ( null != this.logfile ) { 350 this.logfile.write( text, 0, text.length()); 351 this.logfile.flush(); 352 } else { 353 System.out.println( text ); 354 } 355 } 356 catch(IOException ioe) { 357 this.getLogger().debug("LogTransformer.log", ioe); 358 } 359 } 360 361 364 public void destroy() { 365 try { 366 if (this.logfile != null) logfile.close(); 367 } catch (Exception e) {getLogger().debug("LogTransformer.destroy()", e);} 368 } 369 } 370 | Popular Tags |