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