Printer.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.csv;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.io.Writer;

  26. import org.apache.commons.csv.CSVPrinter;
  27. import org.apache.commons.lang.CharEncoding;
  28. import org.openspcoop2.utils.UtilsException;

  29. /**
  30.  * Printer
  31.  *
  32.  * @author Andrea Poli (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class Printer {
  37.        
  38.     private OutputStream out;
  39.     private Writer writer;
  40.     private CSVPrinter csvPrinter;
  41.    
  42.     public Printer(Format format, File file) throws UtilsException {
  43.         this(format, file, CharEncoding.UTF_8);
  44.     }
  45.     public Printer(Format format, File file, String charset) throws UtilsException {
  46.         try{
  47.             this.out = new FileOutputStream(file);
  48.             this.writer = new OutputStreamWriter(this.out, charset);
  49.             this.csvPrinter = new CSVPrinter(this.writer, format.getCsvFormat());
  50.         }catch(Exception e){
  51.             throw new UtilsException(e.getMessage(),e);
  52.         }
  53.     }
  54.    
  55.     public Printer(Format format, OutputStream out) throws UtilsException {
  56.         this(format,out,CharEncoding.UTF_8);
  57.     }
  58.     public Printer(Format format, OutputStream out, String charset) throws UtilsException {
  59.         try{
  60.             this.writer= new OutputStreamWriter(out, charset);
  61.             this.csvPrinter = new CSVPrinter(this.writer, format.getCsvFormat());
  62.         }catch(Exception e){
  63.             throw new UtilsException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67.     public Printer(Format format, Writer writer) throws UtilsException {
  68.         try{
  69.             this.csvPrinter = new CSVPrinter(writer, format.getCsvFormat());
  70.         }
  71.         catch(Exception e){
  72.             throw new UtilsException(e.getMessage(),e);
  73.         }
  74.     }
  75.     public Printer(Format format, Appendable appendable) throws UtilsException {
  76.         try{
  77.             this.csvPrinter = new CSVPrinter(appendable, format.getCsvFormat());
  78.         }
  79.         catch(Exception e){
  80.             throw new UtilsException(e.getMessage(),e);
  81.         }
  82.     }
  83.    
  84.    
  85.    
  86.     public void close(){
  87.         try{
  88.             if(this.csvPrinter!=null){
  89.                 this.csvPrinter.flush();
  90.                 this.csvPrinter.close();
  91.             }
  92.         }catch(Exception eClose){}
  93.         try{
  94.             if(this.writer!=null){
  95.                 this.writer.flush();
  96.                 this.writer.close();
  97.             }
  98.         }catch(Exception eClose){}
  99.         try{
  100.             if(this.out!=null){
  101.                 this.out.flush();
  102.                 this.out.close();
  103.             }
  104.         }catch(Exception eClose){
  105.             // close
  106.         }
  107.     }
  108.    
  109.    
  110.     public void println() throws UtilsException{
  111.         try{
  112.             this.csvPrinter.println();
  113.         }catch(Exception e){
  114.             throw new UtilsException(e.getMessage(),e);
  115.         }
  116.     }
  117.     public void print(Object line) throws UtilsException{
  118.         try{
  119.             this.csvPrinter.print(line);
  120.         }catch(Exception e){
  121.             throw new UtilsException(e.getMessage(),e);
  122.         }
  123.     }
  124.     public void printRecord(Iterable<?> values) throws UtilsException{
  125.         try{
  126.             this.csvPrinter.printRecord(values);
  127.         }catch(Exception e){
  128.             throw new UtilsException(e.getMessage(),e);
  129.         }
  130.     }
  131.     public void printRecord(Object ... values) throws UtilsException{
  132.         try{
  133.             this.csvPrinter.printRecord(values);
  134.         }catch(Exception e){
  135.             throw new UtilsException(e.getMessage(),e);
  136.         }
  137.     }
  138.     public void printComment(String comment)throws UtilsException{
  139.         try{
  140.             this.csvPrinter.printComment(comment);
  141.         }catch(Exception e){
  142.             throw new UtilsException(e.getMessage(),e);
  143.         }
  144.     }

  145. }