r3048 - trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport
Author: echatellier Date: 2010-11-29 15:01:59 +0100 (Mon, 29 Nov 2010) New Revision: 3048 Url: http://chorem.org/repositories/revision/lima/3048 Log: #254: Importing default account plan and result plan from jar doesn't work Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2010-11-29 14:01:39 UTC (rev 3047) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2010-11-29 14:01:59 UTC (rev 3048) @@ -22,28 +22,11 @@ * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ -/* *##% Lima Swing - * Copyright (C) 2008 - 2010 CodeLutin - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ##%*/ - package org.chorem.lima.ui.importexport; import static org.nuiton.i18n.I18n._; + import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; @@ -54,14 +37,20 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.io.StringWriter; import java.util.concurrent.ExecutionException; + import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingWorker; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.business.ExportServiceMonitorable; @@ -112,17 +101,18 @@ } /** - * Call the appropriate methode in business service + * Call the appropriate methode in business service. + * * @param importExportMethode */ public void importExport(ImportExportEnum importExportMethode, String file, Boolean verbose){ final ImportExportEnum importExportMethodeF = importExportMethode; encodingEnum = EncodingEnum.UTF8; - if (file.equals("")){ + if (file.equals("")) { file = chooseFile(importExportMethode.getImportMode(), importExportMethode); } //if export cancel - if (!file.equals("")){ + if (!file.equals("")) { final EncodingEnum charset = encodingEnum; final String filePath = file; final Boolean verboseMode = verbose; @@ -201,12 +191,14 @@ } } } catch (InterruptedException eee) { - if (log.isDebugEnabled()){ + if (log.isErrorEnabled()){ log.error("Can't get result message", eee); } } catch (ExecutionException eee) { DialogHelper.showMessageDialog(eee.getCause().getMessage()); - log.error("Error on import", eee); + if (log.isErrorEnabled()){ + log.error("Error on import", eee); + } } } }.execute(); @@ -266,30 +258,44 @@ out.close(); } catch (IOException eee) { - if (log.isDebugEnabled()){ + if (log.isErrorEnabled()){ log.error("Can't write file " + filePath, eee); } } } - - + /** - * Open csv file and get his datas on a string - * + * Open csv file and get his datas on a string. */ - public String extractFile(String filePath, String charset){ - char[] datas = new char[(int) new File(filePath).length()]; + public String extractFile(String filePath, String charset) { + StringWriter sw = new StringWriter(); + BufferedReader in = null; try { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charset)); - in.read(datas); + InputStream is = null; + + // FIXME echatellier 20101129 remove this hack and modify resources reading mecanism + if (filePath != null && filePath.indexOf(".jar!") > 0) { + String resourcesPath = filePath.substring(filePath.indexOf(".jar!") + 5); + is = ImportExport.class.getResourceAsStream(resourcesPath); + } + else { + is = new FileInputStream(filePath); + } + + in = new BufferedReader(new InputStreamReader(is, charset)); + IOUtils.copy(in, sw); in.close(); } catch (IOException eee) { - if (log.isDebugEnabled()){ - log.error("Can't read file "+filePath, eee); + if (log.isErrorEnabled()) { + log.error("Can't read file " + filePath, eee); } } - return new String(datas); + finally { + IOUtils.closeQuietly(in); + } + + return sw.toString(); } public static ImportExport getInstance(Component view) {
participants (1)
-
echatellier@users.chorem.org