java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/Atribut.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 11 Dec 2023 00:28:30 +0100
branchv_0
changeset 28 c2ffda907125
parent 27 java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java@dc5786f3482b
child 30 d511e4bf7d8f
permissions -rw-r--r--
oprava názvu balíčku
franta-hg@19
     1
/**
franta-hg@19
     2
 * Rozšířené atributy – program na správu rozšířených atributů souborů
franta-hg@19
     3
 * Copyright © 2012 František Kučera (frantovo.cz)
franta-hg@19
     4
 * 
franta-hg@19
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@19
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@27
     7
 * the Free Software Foundation, either version 3 of the License.
franta-hg@19
     8
 * 
franta-hg@19
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@19
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@19
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@19
    12
 * GNU General Public License for more details.
franta-hg@19
    13
 * 
franta-hg@19
    14
 * You should have received a copy of the GNU General Public License
franta-hg@19
    15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@19
    16
 */
franta-hg@28
    17
package cz.frantovo.rozsireneatributy;
franta-hg@6
    18
franta-hg@9
    19
import java.nio.ByteBuffer;
franta-hg@9
    20
import java.nio.charset.Charset;
franta-hg@9
    21
franta-hg@6
    22
public class Atribut {
franta-hg@6
    23
franta-hg@11
    24
	private String klíč;
franta-hg@6
    25
	private String hodnota;
franta-hg@6
    26
franta-hg@11
    27
	public Atribut(String klíč, String hodnota) {
franta-hg@11
    28
		this.klíč = klíč;
franta-hg@6
    29
		this.hodnota = hodnota;
franta-hg@6
    30
	}
franta-hg@6
    31
franta-hg@11
    32
	public Atribut(String klíč, ByteBuffer hodnota) {
franta-hg@11
    33
		this.klíč = klíč;
franta-hg@9
    34
		setHodnota(hodnota);
franta-hg@9
    35
	}
franta-hg@9
    36
franta-hg@10
    37
	public Atribut() {
franta-hg@10
    38
	}
franta-hg@10
    39
franta-hg@11
    40
	public String getKlíč() {
franta-hg@11
    41
		return klíč;
franta-hg@6
    42
	}
franta-hg@6
    43
franta-hg@11
    44
	public void setKlíč(String klíč) {
franta-hg@11
    45
		this.klíč = klíč;
franta-hg@11
    46
	}
franta-hg@11
    47
franta-hg@11
    48
	/**
franta-hg@11
    49
	 * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
franta-hg@11
    50
	 * TODO: další kontroly?
franta-hg@11
    51
	 * @return jestli je platný
franta-hg@11
    52
	 */
franta-hg@11
    53
	public boolean isPlatnýKlíč() {
franta-hg@11
    54
		return klíč != null && klíč.length() > 0;
franta-hg@11
    55
	}
franta-hg@11
    56
franta-hg@11
    57
	/**
franta-hg@11
    58
	 * nulová hodnota → smazání atributu
franta-hg@11
    59
	 * (ale může být prázdný řetězec)
franta-hg@11
    60
	 * @return jestli je platná
franta-hg@11
    61
	 */
franta-hg@11
    62
	public boolean isPlatnáHodnota() {
franta-hg@11
    63
		return hodnota != null;
franta-hg@6
    64
	}
franta-hg@6
    65
franta-hg@6
    66
	public String getHodnota() {
franta-hg@6
    67
		return hodnota;
franta-hg@6
    68
	}
franta-hg@6
    69
franta-hg@9
    70
	public final ByteBuffer getHodnotaBajty() {
franta-hg@9
    71
		return zakóduj(getHodnota());
franta-hg@9
    72
	}
franta-hg@9
    73
franta-hg@6
    74
	public void setHodnota(String hodnota) {
franta-hg@6
    75
		this.hodnota = hodnota;
franta-hg@6
    76
	}
franta-hg@9
    77
franta-hg@9
    78
	public final void setHodnota(ByteBuffer hodnota) {
franta-hg@9
    79
		setHodnota(dekóduj(hodnota));
franta-hg@9
    80
	}
franta-hg@9
    81
franta-hg@9
    82
	private static String dekóduj(ByteBuffer bajty) {
franta-hg@9
    83
		bajty.flip();
franta-hg@9
    84
		return Charset.defaultCharset().decode(bajty).toString();
franta-hg@9
    85
	}
franta-hg@9
    86
franta-hg@9
    87
	private static ByteBuffer zakóduj(String text) {
franta-hg@10
    88
		if (text == null) {
franta-hg@10
    89
			return null;
franta-hg@10
    90
		} else {
franta-hg@10
    91
			return Charset.defaultCharset().encode(text);
franta-hg@10
    92
		}
franta-hg@9
    93
	}
franta-hg@6
    94
}