author | František Kučera <franta-hg@frantovo.cz> |
Mon, 25 Dec 2023 23:45:13 +0100 | |
branch | v_0 |
changeset 44 | c43c96b0ab1b |
parent 39 | ec0e970e0830 |
permissions | -rw-r--r-- |
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@30 | 22 |
/** |
franta-hg@30 | 23 |
* @author Ing. František Kučera (frantovo.cz) |
franta-hg@30 | 24 |
*/ |
franta-hg@6 | 25 |
public class Atribut { |
franta-hg@6 | 26 |
|
franta-hg@11 | 27 |
private String klíč; |
franta-hg@6 | 28 |
private String hodnota; |
franta-hg@6 | 29 |
|
franta-hg@11 | 30 |
public Atribut(String klíč, String hodnota) { |
franta-hg@11 | 31 |
this.klíč = klíč; |
franta-hg@6 | 32 |
this.hodnota = hodnota; |
franta-hg@6 | 33 |
} |
franta-hg@6 | 34 |
|
franta-hg@11 | 35 |
public Atribut(String klíč, ByteBuffer hodnota) { |
franta-hg@11 | 36 |
this.klíč = klíč; |
franta-hg@9 | 37 |
setHodnota(hodnota); |
franta-hg@9 | 38 |
} |
franta-hg@9 | 39 |
|
franta-hg@10 | 40 |
public Atribut() { |
franta-hg@10 | 41 |
} |
franta-hg@10 | 42 |
|
franta-hg@11 | 43 |
public String getKlíč() { |
franta-hg@11 | 44 |
return klíč; |
franta-hg@6 | 45 |
} |
franta-hg@6 | 46 |
|
franta-hg@11 | 47 |
public void setKlíč(String klíč) { |
franta-hg@11 | 48 |
this.klíč = klíč; |
franta-hg@11 | 49 |
} |
franta-hg@11 | 50 |
|
franta-hg@11 | 51 |
/** |
franta-hg@11 | 52 |
* Název atributu musí být nenulový a mít nějakou délku, aby šel uložit |
franta-hg@11 | 53 |
* TODO: další kontroly? |
franta-hg@11 | 54 |
* @return jestli je platný |
franta-hg@11 | 55 |
*/ |
franta-hg@11 | 56 |
public boolean isPlatnýKlíč() { |
franta-hg@11 | 57 |
return klíč != null && klíč.length() > 0; |
franta-hg@11 | 58 |
} |
franta-hg@11 | 59 |
|
franta-hg@11 | 60 |
/** |
franta-hg@11 | 61 |
* nulová hodnota → smazání atributu |
franta-hg@11 | 62 |
* (ale může být prázdný řetězec) |
franta-hg@11 | 63 |
* @return jestli je platná |
franta-hg@11 | 64 |
*/ |
franta-hg@11 | 65 |
public boolean isPlatnáHodnota() { |
franta-hg@11 | 66 |
return hodnota != null; |
franta-hg@6 | 67 |
} |
franta-hg@6 | 68 |
|
franta-hg@6 | 69 |
public String getHodnota() { |
franta-hg@6 | 70 |
return hodnota; |
franta-hg@6 | 71 |
} |
franta-hg@6 | 72 |
|
franta-hg@9 | 73 |
public final ByteBuffer getHodnotaBajty() { |
franta-hg@9 | 74 |
return zakóduj(getHodnota()); |
franta-hg@9 | 75 |
} |
franta-hg@9 | 76 |
|
franta-hg@6 | 77 |
public void setHodnota(String hodnota) { |
franta-hg@6 | 78 |
this.hodnota = hodnota; |
franta-hg@6 | 79 |
} |
franta-hg@9 | 80 |
|
franta-hg@9 | 81 |
public final void setHodnota(ByteBuffer hodnota) { |
franta-hg@9 | 82 |
setHodnota(dekóduj(hodnota)); |
franta-hg@9 | 83 |
} |
franta-hg@9 | 84 |
|
franta-hg@9 | 85 |
private static String dekóduj(ByteBuffer bajty) { |
franta-hg@9 | 86 |
bajty.flip(); |
franta-hg@9 | 87 |
return Charset.defaultCharset().decode(bajty).toString(); |
franta-hg@9 | 88 |
} |
franta-hg@9 | 89 |
|
franta-hg@9 | 90 |
private static ByteBuffer zakóduj(String text) { |
franta-hg@10 | 91 |
if (text == null) { |
franta-hg@10 | 92 |
return null; |
franta-hg@10 | 93 |
} else { |
franta-hg@10 | 94 |
return Charset.defaultCharset().encode(text); |
franta-hg@10 | 95 |
} |
franta-hg@9 | 96 |
} |
franta-hg@6 | 97 |
} |