|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CSVTools.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 7 |
package org.ejtools.util.export;
|
|
| 8 |
|
|
| 9 |
import java.io.File;
|
|
| 10 |
import java.io.FileWriter;
|
|
| 11 |
import java.io.IOException;
|
|
| 12 |
|
|
| 13 |
import javax.swing.JFileChooser;
|
|
| 14 |
import javax.swing.filechooser.FileFilter;
|
|
| 15 |
|
|
| 16 |
import org.apache.log4j.Logger;
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* Description of the Class
|
|
| 21 |
*
|
|
| 22 |
* @author Laurent Etiemble
|
|
| 23 |
* @version $Revision: 1.1 $
|
|
| 24 |
*/
|
|
| 25 |
public class CSVTools |
|
| 26 |
{
|
|
| 27 |
/** Description of the Field */
|
|
| 28 |
private static FileFilter CSV_FILE_FILTER = |
|
| 29 |
new FileFilter()
|
|
| 30 |
{
|
|
| 31 | 0 |
public boolean accept(File file) |
| 32 |
{
|
|
| 33 | 0 |
return file.getName().endsWith(".csv"); |
| 34 |
} |
|
| 35 |
|
|
| 36 |
|
|
| 37 | 0 |
public String getDescription()
|
| 38 |
{
|
|
| 39 | 0 |
return "Comma Separated Values file (*.csv)"; |
| 40 |
} |
|
| 41 |
}; |
|
| 42 |
/** Description of the Field */
|
|
| 43 |
private static Logger logger = Logger.getLogger(CSVTools.class); |
|
| 44 |
|
|
| 45 |
|
|
| 46 |
/**Constructor for the CSVTools object */
|
|
| 47 | 0 |
private CSVTools()
|
| 48 |
{
|
|
| 49 | 0 |
super();
|
| 50 |
} |
|
| 51 |
|
|
| 52 |
|
|
| 53 |
/**
|
|
| 54 |
* Description of the Method
|
|
| 55 |
*
|
|
| 56 |
* @param output Description of the Parameter
|
|
| 57 |
* @param content Description of the Parameter
|
|
| 58 |
*/
|
|
| 59 | 0 |
public static void exportAsCVS(StringBuffer content, File output) |
| 60 |
{
|
|
| 61 | 0 |
try
|
| 62 |
{
|
|
| 63 | 0 |
FileWriter writer = new FileWriter(output);
|
| 64 | 0 |
writer.write(content.toString()); |
| 65 | 0 |
writer.flush(); |
| 66 | 0 |
writer.close(); |
| 67 |
} |
|
| 68 |
catch (IOException ioe)
|
|
| 69 |
{
|
|
| 70 | 0 |
logger.error("Can't export content as CSV", ioe);
|
| 71 |
} |
|
| 72 |
} |
|
| 73 |
|
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* Description of the Method
|
|
| 77 |
*
|
|
| 78 |
* @return Description of the Return Value
|
|
| 79 |
*/
|
|
| 80 | 0 |
public static File selectCSVFile() |
| 81 |
{
|
|
| 82 |
// Fix for JFileChooser/SecurityManager bug (#4264750)
|
|
| 83 | 0 |
SecurityManager s = System.getSecurityManager(); |
| 84 | 0 |
System.setSecurityManager(null);
|
| 85 |
|
|
| 86 |
// Choose file
|
|
| 87 | 0 |
JFileChooser chooser = new JFileChooser(System.getProperty("user.dir")); |
| 88 | 0 |
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
| 89 | 0 |
chooser.setFileFilter(CSV_FILE_FILTER); |
| 90 | 0 |
int returnVal = chooser.showSaveDialog(null); |
| 91 | 0 |
System.setSecurityManager(s); |
| 92 | 0 |
if (returnVal != JFileChooser.APPROVE_OPTION)
|
| 93 |
{
|
|
| 94 | 0 |
return null; |
| 95 |
} |
|
| 96 | 0 |
return chooser.getSelectedFile();
|
| 97 |
} |
|
| 98 |
} |
|
| 99 |
|
|
||||||||||