src/main/java/cz/frantovo/rozsireneatributy/CSV.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 Dec 2023 23:54:00 +0100
branchv_0
changeset 40 1978eaf429de
parent 39 ec0e970e0830
permissions -rw-r--r--
možnost spustit program přes Maven: mvn install exec:java
franta-hg@35
     1
/**
franta-hg@35
     2
 * Rozšířené atributy – program na správu rozšířených atributů souborů
franta-hg@35
     3
 * Copyright © 2023 František Kučera (frantovo.cz)
franta-hg@35
     4
 *
franta-hg@35
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@35
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@35
     7
 * the Free Software Foundation, either version 3 of the License.
franta-hg@35
     8
 *
franta-hg@35
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@35
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@35
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@35
    12
 * GNU General Public License for more details.
franta-hg@35
    13
 *
franta-hg@35
    14
 * You should have received a copy of the GNU General Public License
franta-hg@35
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@35
    16
 */
franta-hg@35
    17
package cz.frantovo.rozsireneatributy;
franta-hg@35
    18
franta-hg@35
    19
import java.io.IOException;
franta-hg@35
    20
import java.io.Writer;
franta-hg@35
    21
franta-hg@35
    22
/**
franta-hg@35
    23
 * Generátor standardních CSV.
franta-hg@35
    24
 *
franta-hg@40
    25
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@35
    26
 */
franta-hg@35
    27
public class CSV {
franta-hg@35
    28
franta-hg@35
    29
	private final Writer výstup;
franta-hg@35
    30
	private int sloupec = 0;
franta-hg@35
    31
	private int početSloupců = -1;
franta-hg@35
    32
franta-hg@35
    33
	public CSV(Writer výstup) {
franta-hg@35
    34
		this.výstup = výstup;
franta-hg@35
    35
	}
franta-hg@35
    36
franta-hg@35
    37
	public void hodnota(String hodnota) throws IOException {
franta-hg@35
    38
		if (sloupec > 0) výstup.write(',');
franta-hg@35
    39
		if (hodnota != null && !hodnota.isEmpty()) {
franta-hg@35
    40
			výstup.write('"');
franta-hg@35
    41
			for (char ch : hodnota.toCharArray()) {
franta-hg@35
    42
				if (ch == '"') výstup.write('"');
franta-hg@35
    43
				výstup.write(ch);
franta-hg@35
    44
			}
franta-hg@35
    45
			výstup.write('"');
franta-hg@35
    46
		}
franta-hg@35
    47
		sloupec++;
franta-hg@35
    48
		výstup.flush();
franta-hg@35
    49
	}
franta-hg@35
    50
franta-hg@35
    51
	public void konecŘádku() throws IOException {
franta-hg@35
    52
		if (početSloupců < 0) početSloupců = sloupec;
franta-hg@35
    53
		if (sloupec == početSloupců) {
franta-hg@35
    54
			výstup.write("\r\n");
franta-hg@35
    55
			sloupec = 0;
franta-hg@35
    56
		} else {
franta-hg@35
    57
			throw new IOException("Neodpovídá počet sloupců:"
franta-hg@35
    58
				+ " aktuální=" + sloupec
franta-hg@35
    59
				+ " celkový=" + početSloupců
franta-hg@35
    60
			);
franta-hg@35
    61
		}
franta-hg@35
    62
		výstup.flush();
franta-hg@35
    63
	}
franta-hg@35
    64
franta-hg@35
    65
}