franta-hg@19: /** franta-hg@19: * Rozšířené atributy – program na správu rozšířených atributů souborů franta-hg@19: * Copyright © 2012 František Kučera (frantovo.cz) franta-hg@19: * franta-hg@19: * This program is free software: you can redistribute it and/or modify franta-hg@19: * it under the terms of the GNU General Public License as published by franta-hg@19: * the Free Software Foundation, either version 3 of the License, or franta-hg@19: * (at your option) any later version. franta-hg@19: * franta-hg@19: * This program is distributed in the hope that it will be useful, franta-hg@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@19: * GNU General Public License for more details. franta-hg@19: * franta-hg@19: * You should have received a copy of the GNU General Public License franta-hg@19: * along with this program. If not, see . franta-hg@19: */ franta-hg@6: package cz.frantovo.rozsireneAtributy; franta-hg@6: franta-hg@9: import java.nio.ByteBuffer; franta-hg@9: import java.nio.charset.Charset; franta-hg@9: franta-hg@6: public class Atribut { franta-hg@6: franta-hg@11: private String klíč; franta-hg@6: private String hodnota; franta-hg@6: franta-hg@11: public Atribut(String klíč, String hodnota) { franta-hg@11: this.klíč = klíč; franta-hg@6: this.hodnota = hodnota; franta-hg@6: } franta-hg@6: franta-hg@11: public Atribut(String klíč, ByteBuffer hodnota) { franta-hg@11: this.klíč = klíč; franta-hg@9: setHodnota(hodnota); franta-hg@9: } franta-hg@9: franta-hg@10: public Atribut() { franta-hg@10: } franta-hg@10: franta-hg@11: public String getKlíč() { franta-hg@11: return klíč; franta-hg@6: } franta-hg@6: franta-hg@11: public void setKlíč(String klíč) { franta-hg@11: this.klíč = klíč; franta-hg@11: } franta-hg@11: franta-hg@11: /** franta-hg@11: * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit franta-hg@11: * TODO: další kontroly? franta-hg@11: * @return jestli je platný franta-hg@11: */ franta-hg@11: public boolean isPlatnýKlíč() { franta-hg@11: return klíč != null && klíč.length() > 0; franta-hg@11: } franta-hg@11: franta-hg@11: /** franta-hg@11: * nulová hodnota → smazání atributu franta-hg@11: * (ale může být prázdný řetězec) franta-hg@11: * @return jestli je platná franta-hg@11: */ franta-hg@11: public boolean isPlatnáHodnota() { franta-hg@11: return hodnota != null; franta-hg@6: } franta-hg@6: franta-hg@6: public String getHodnota() { franta-hg@6: return hodnota; franta-hg@6: } franta-hg@6: franta-hg@9: public final ByteBuffer getHodnotaBajty() { franta-hg@9: return zakóduj(getHodnota()); franta-hg@9: } franta-hg@9: franta-hg@6: public void setHodnota(String hodnota) { franta-hg@6: this.hodnota = hodnota; franta-hg@6: } franta-hg@9: franta-hg@9: public final void setHodnota(ByteBuffer hodnota) { franta-hg@9: setHodnota(dekóduj(hodnota)); franta-hg@9: } franta-hg@9: franta-hg@9: private static String dekóduj(ByteBuffer bajty) { franta-hg@9: bajty.flip(); franta-hg@9: return Charset.defaultCharset().decode(bajty).toString(); franta-hg@9: } franta-hg@9: franta-hg@9: private static ByteBuffer zakóduj(String text) { franta-hg@10: if (text == null) { franta-hg@10: return null; franta-hg@10: } else { franta-hg@10: return Charset.defaultCharset().encode(text); franta-hg@10: } franta-hg@9: } franta-hg@6: }