1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/Atribut.java Mon Dec 11 00:28:30 2023 +0100
1.3 @@ -0,0 +1,94 @@
1.4 +/**
1.5 + * Rozšířené atributy – program na správu rozšířených atributů souborů
1.6 + * Copyright © 2012 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.19 + */
1.20 +package cz.frantovo.rozsireneatributy;
1.21 +
1.22 +import java.nio.ByteBuffer;
1.23 +import java.nio.charset.Charset;
1.24 +
1.25 +public class Atribut {
1.26 +
1.27 + private String klíč;
1.28 + private String hodnota;
1.29 +
1.30 + public Atribut(String klíč, String hodnota) {
1.31 + this.klíč = klíč;
1.32 + this.hodnota = hodnota;
1.33 + }
1.34 +
1.35 + public Atribut(String klíč, ByteBuffer hodnota) {
1.36 + this.klíč = klíč;
1.37 + setHodnota(hodnota);
1.38 + }
1.39 +
1.40 + public Atribut() {
1.41 + }
1.42 +
1.43 + public String getKlíč() {
1.44 + return klíč;
1.45 + }
1.46 +
1.47 + public void setKlíč(String klíč) {
1.48 + this.klíč = klíč;
1.49 + }
1.50 +
1.51 + /**
1.52 + * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
1.53 + * TODO: další kontroly?
1.54 + * @return jestli je platný
1.55 + */
1.56 + public boolean isPlatnýKlíč() {
1.57 + return klíč != null && klíč.length() > 0;
1.58 + }
1.59 +
1.60 + /**
1.61 + * nulová hodnota → smazání atributu
1.62 + * (ale může být prázdný řetězec)
1.63 + * @return jestli je platná
1.64 + */
1.65 + public boolean isPlatnáHodnota() {
1.66 + return hodnota != null;
1.67 + }
1.68 +
1.69 + public String getHodnota() {
1.70 + return hodnota;
1.71 + }
1.72 +
1.73 + public final ByteBuffer getHodnotaBajty() {
1.74 + return zakóduj(getHodnota());
1.75 + }
1.76 +
1.77 + public void setHodnota(String hodnota) {
1.78 + this.hodnota = hodnota;
1.79 + }
1.80 +
1.81 + public final void setHodnota(ByteBuffer hodnota) {
1.82 + setHodnota(dekóduj(hodnota));
1.83 + }
1.84 +
1.85 + private static String dekóduj(ByteBuffer bajty) {
1.86 + bajty.flip();
1.87 + return Charset.defaultCharset().decode(bajty).toString();
1.88 + }
1.89 +
1.90 + private static ByteBuffer zakóduj(String text) {
1.91 + if (text == null) {
1.92 + return null;
1.93 + } else {
1.94 + return Charset.defaultCharset().encode(text);
1.95 + }
1.96 + }
1.97 +}