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