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