View Javadoc

1   /*
2    * $Id: CSVPrinter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.stats.printers;
12  
13  import java.io.OutputStream;
14  import java.io.Writer;
15  import java.util.Collection;
16  
17  /**
18   * <code>CSVPrinter</code> prints component stats in CSV format
19   * 
20   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
21   * @version $Revision: 7976 $
22   */
23  public class CSVPrinter extends AbstractTablePrinter
24  {
25      private String delim = ",";
26      private boolean printHeaders = true;
27  
28      public CSVPrinter(Writer out)
29      {
30          super(out);
31      }
32  
33      public CSVPrinter(OutputStream out)
34      {
35          super(out);
36      }
37  
38      public void print(Collection stats)
39      {
40          try
41          {
42              String[][] table = getTable(stats);
43              int i = (printHeaders ? 0 : 1);
44              for (; i < table.length; i++)
45              {
46                  for (int j = 0; j < table[0].length; j++)
47                  {
48                      print(table[i][j]);
49                      if (j + 1 != table[i].length)
50                      {
51                          print(delim);
52                      }
53                  }
54                  println();
55              }
56          }
57          catch (Throwable e)
58          {
59              // TODO MULE-863: Unlikely to be sufficient
60              // (and nothing explicitly thrown above)
61              e.printStackTrace();
62          }
63      }
64  
65      public boolean isPrintHeaders()
66      {
67          return printHeaders;
68      }
69  
70      public void setPrintHeaders(boolean printHeaders)
71      {
72          this.printHeaders = printHeaders;
73      }
74  }