Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.io;
  
 
 import java.io.File;
 import java.net.URI;

Created by IntelliJ IDEA. User: ndw Date: Sep 28, 2008 Time: 3:15:57 PM To change this template use File | Settings | File Templates.
 
 public class PipeLogger {
     private static final QName cx_basename = new QName("cx"."basename");
     private static final QName cx_logstyle = new QName("cx"."logstyle");
     private Log log = null;
     private Serializer serializer = null;
     private PrintStream stream = null;
     private XProcRuntime runtime = null;
     private boolean logging = false;
     private XProcConfiguration config = null;
     private LogOptions logstyle = null;
     private String basename = null;
     private File baseDir = null;
     private int outputCount = 1;
 
 
     public PipeLogger(XProcRuntime xprocLog log) {
          = xproc;
         this. = log;
          = xproc.getConfiguration();
 
          = .getEpisode();
          = .;
 
         String ext = log.getExtensionAttribute();
         if (ext != null) {
              = ext;
         }
 
         ext = log.getExtensionAttribute();
         if ( != . && ext != null) {
             if (ext.equals("off")) {
                  = .;
             } else if (ext.equals("plain")) {
                  = .;
             } else if (ext.equals("wrapped")) {
                  = .;
             } else if (ext.equals("directory")) {
                  = .;
             } else {
                 ..println("Invalid cx:logstyle ignored: " + ext);
             }
         }
 
         if ( == .) {
             // there's nothing to do here...
             return;
         }
 
          = new Serializer();
 
         switch () {
             case :
             case :
                 if (log.getHref() == null) {
                      = .;
                 } else {
                     try {
                         String href = log.getHref().toASCIIString();
                         if (href.startsWith("file:///")) {
                             href = href.substring(7);
                         } else if (href.startsWith("file:/")) {
                             href = href.substring(5);
                         }
                          = new PrintStream(new File(href));
                     } catch (FileNotFoundException fnfe) {
                         ..println("Failed to create log: " + log.getHref());
                          = .;
                     }
                 }
                 break;
            case :
                String dirstr = null;
                if (log.getHref() != null) {
                    if (log.getHref().getScheme().equals("file")) {
                        dirstr = log.getHref().getPath();
                    } else {
                        ..println("Only file: scheme URIs are supported for directory logging.");
                         = .;
                         = .;
                        break;
                    }
                } else {
                    ..println("Directory logging requires a directory.");
                     = .;
                     = .;
                    break;
                }
                 = new File(dirstr);
                if (.isDirectory()) {
                    // ok
                } else if (.exists()) {
                    ..println("Log location is not a directory: " + log.getHref());
                     = .;
                     = .;
                } else {
                    try {
                        .mkdirs();
                    } catch (Exception e) {
                        ..println("Could not create log directory: " + log.getHref());
                         = .;
                         = .;
                    }
                }
                break;
            case :
            default:
                break;
        }
    }
    private String dateTime() {
        GregorianCalendar cal = new GregorianCalendar();
        String rfc822tz = String.format("%1$tz"cal);
        // I assume it's either -0500 or +0100 or something like that...
        return String.format("%1$tFT%1$tT"cal) + rfc822tz.substring(0,3) + ":" + rfc822tz.substring(3);
    }
    public void startLogging() {
        String dt = dateTime();
        if ( == .) {
            return;
        }
        if ( == .) {
            // nop;
        } else {
            if ( == .) {
                .println("<px:document-sequence xmlns:px='http://xmlcalabash.com/ns/document-sequence'");
                .println("                      port='" + .getPort() + "'");
                .println("                      xpl-file='" + .xplFile() + "'");
                .println("                      xpl-line='" + .xplLine() + "'");
                .println("                      dateTime='" + dt + "'>");
            } else {
                .println("<!-- Start of Calabash output " +  + " on " + dt + " -->");
            }
        }
         = true;
    }
    public void stopLogging() {
        if () {
            if ( == .) {
                .print("</px:document-sequence>");
            }
            if ( == .) {
                .print("\n");
                .println("<!-- End of Calabash output log -->");
            }
        }
         = false;
    }
    public void log(XdmNode node) {
        if ( == .) {
            // there's nothing to do here...
            return;
        }
        if (!) {
            startLogging();
        }
        switch () {
            case :
                .print("<px:document>");
                try {
                    S9apiUtils.serialize(node);
                } catch (SaxonApiException sae) {
                    ..println("Logging failed: " + sae);
                }
                .println("</px:document>");
                break;
            case :
                try {
                    S9apiUtils.serialize(node);
                } catch (SaxonApiException sae) {
                    ..println("Logging failed: " + sae);
                }
                break;
            case :
                String filename = String.format("%1$s-%2$04d.xml"++);
                File output = new File(filename);
                try {
                     = new PrintStream(output);
                } catch (FileNotFoundException fnfe) {
                    ..println("Failed to create log: " + .getHref());
                     = .;
                }
                .setOutputStream();
                
                .println("<!-- Start of Calabash output " +  + " on " + dateTime() + " -->");
                try {
                    S9apiUtils.serialize(node);
                } catch (SaxonApiException sae) {
                    ..println("Logging failed: " + sae);
                }
                .close();
                break;
            default:
                break;
        }
    }
New to GrepCode? Check out our FAQ X