r2396 - in trunk: nuiton-csv nuiton-csv/src/main/java/org/nuiton/util/csv nuiton-csv/src/main/java/org/nuiton/util/csv/ext nuiton-validator/src/main/java/org/nuiton/validator/bean/simple
Author: tchemit Date: 2012-08-16 14:31:13 +0200 (Thu, 16 Aug 2012) New Revision: 2396 Url: http://nuiton.org/repositories/revision/nuiton-utils/2396 Log: refs #2259: Add a csv ext package (for extended stuff) Added: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/CsvReaders.java trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/RepeatableExport.java trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/package-info.java Modified: trunk/nuiton-csv/pom.xml trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/simple/SimpleBeanValidators.java Modified: trunk/nuiton-csv/pom.xml =================================================================== --- trunk/nuiton-csv/pom.xml 2012-08-11 07:54:22 UTC (rev 2395) +++ trunk/nuiton-csv/pom.xml 2012-08-16 12:31:13 UTC (rev 2396) @@ -65,6 +65,11 @@ </dependency> <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + + <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> </dependency> Added: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/CsvReaders.java =================================================================== --- trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/CsvReaders.java (rev 0) +++ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/CsvReaders.java 2012-08-16 12:31:13 UTC (rev 2396) @@ -0,0 +1,57 @@ +package org.nuiton.util.csv.ext; +/* + * #%L + * Nuiton Utils :: Nuiton Csv + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Charsets; +import com.google.common.io.Files; +import org.nuiton.util.csv.ImportRuntimeException; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; + +/** + * Useful method around csv readers. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6 + */ +public class CsvReaders { + + public static String[] getHeader(File file, char charSeprator) { + + try { + BufferedReader reader = Files.newReader(file, Charsets.UTF_8); + try { + String header = reader.readLine(); + String[] result = header.split(charSeprator + ""); + return result; + } finally { + reader.close(); + } + } catch (IOException e) { + throw new ImportRuntimeException("Could not obtain header of file " + file, e); + } + } +} Property changes on: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/CsvReaders.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/RepeatableExport.java =================================================================== --- trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/RepeatableExport.java (rev 0) +++ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/RepeatableExport.java 2012-08-16 12:31:13 UTC (rev 2396) @@ -0,0 +1,101 @@ +package org.nuiton.util.csv.ext; +/* + * #%L + * Nuiton Utils :: Nuiton Csv + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.util.csv.Export; +import org.nuiton.util.csv.ExportModel; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; + +/** + * Extends the {@link Export} classes to be able to generate only once + * the header. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6 + */ +public class RepeatableExport<E> extends Export<E> { + + public static <E> RepeatableExport<E> newExport(ExportModel<E> model, + Iterable<E> data, + boolean writeOnceHeader) { + return new RepeatableExport<E>(model, data, writeOnceHeader); + } + + public static <E> void exportToWriter(ExportModel<E> model, + Iterable<E> data, + Writer writer, + boolean writeOnceHeader) throws Exception { + Export<E> exporter = newExport(model, data, writeOnceHeader); + exporter.write(writer); + } + + public static <E> void exportToFile(ExportModel<E> model, + Iterable<E> data, + File file, + Charset charset, + boolean writeOnceHeader) throws Exception { + Export<E> exporter = newExport(model, data, writeOnceHeader); + exporter.write(file, charset); + } + + public static <E> String exportToString(ExportModel<E> model, + Iterable<E> data, + Charset charset, + boolean writeOnceHeader) throws Exception { + Export<E> exporter = newExport(model, data, writeOnceHeader); + return exporter.toString(charset); + } + + protected final boolean writeOnceHeader; + + protected boolean headerWritten; + + public boolean isHeaderWritten() { + return headerWritten; + } + + protected RepeatableExport(ExportModel<E> model, + Iterable<E> data, + boolean writeOnceHeader) { + super(model, data); + this.writeOnceHeader = writeOnceHeader; + } + + @Override + protected void writeHeader(Writer writer) throws IOException { + if (!writeOnceHeader || !headerWritten) { + + // no header generated, let's do it! + + super.writeHeader(writer); + + // mark it as written + headerWritten = true; + } + } +} Property changes on: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/RepeatableExport.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/package-info.java =================================================================== --- trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/package-info.java (rev 0) +++ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/package-info.java 2012-08-16 12:31:13 UTC (rev 2396) @@ -0,0 +1,7 @@ +/** + * Offers some extended api. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6 + */ +package org.nuiton.util.csv.ext; \ No newline at end of file Property changes on: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/ext/package-info.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/simple/SimpleBeanValidators.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/simple/SimpleBeanValidators.java 2012-08-11 07:54:22 UTC (rev 2395) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/simple/SimpleBeanValidators.java 2012-08-16 12:31:13 UTC (rev 2396) @@ -1,4 +1,27 @@ package org.nuiton.validator.bean.simple; +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ import org.nuiton.validator.NuitonValidatorScope; Property changes on: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/simple/SimpleBeanValidators.java ___________________________________________________________________ Modified: svn:keywords - Author Date Id Revision + Author Date Id Revision HeadURL
participants (1)
-
tchemit@users.nuiton.org