View Javadoc

1   /*
2    * $Id: CSVPrinter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 service stats in CSV format
19   */
20  public class CSVPrinter extends AbstractTablePrinter
21  {
22      private String delim = ",";
23      private boolean printHeaders = true;
24  
25      public CSVPrinter(Writer out)
26      {
27          super(out);
28      }
29  
30      public CSVPrinter(OutputStream out)
31      {
32          super(out);
33      }
34  
35      public void print(Collection stats)
36      {
37          try
38          {
39              String[][] table = getTable(stats);
40              int i = (printHeaders ? 0 : 1);
41              for (; i < table.length; i++)
42              {
43                  for (int j = 0; j < table[0].length; j++)
44                  {
45                      print(table[i][j]);
46                      if (j + 1 != table[i].length)
47                      {
48                          print(delim);
49                      }
50                  }
51                  println();
52              }
53          }
54          catch (Throwable e)
55          {
56              // TODO MULE-863: Unlikely to be sufficient
57              // (and nothing explicitly thrown above)
58              e.printStackTrace();
59          }
60      }
61  
62      public boolean isPrintHeaders()
63      {
64          return printHeaders;
65      }
66  
67      public void setPrintHeaders(boolean printHeaders)
68      {
69          this.printHeaders = printHeaders;
70      }
71  }