java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/ExtendedAttribute.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 02 Oct 2016 18:20:38 +0200
changeset 110 d4e5d65ba7cf
parent 106 02739f60b1ec
child 111 e4900596abdb
permissions -rw-r--r--
netbeans
franta-hg@106
     1
/**
franta-hg@106
     2
 * Alt2XML
franta-hg@106
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@106
     4
 *
franta-hg@106
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@106
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@106
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@106
     8
 * (at your option) any later version.
franta-hg@106
     9
 *
franta-hg@106
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@106
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@106
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@106
    13
 * GNU General Public License for more details.
franta-hg@106
    14
 *
franta-hg@106
    15
 * You should have received a copy of the GNU General Public License
franta-hg@106
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@106
    17
 */
franta-hg@106
    18
package cz.frantovo.alt2xml.in.fs;
franta-hg@106
    19
franta-hg@106
    20
import java.nio.ByteBuffer;
franta-hg@106
    21
import java.nio.charset.Charset;
franta-hg@106
    22
franta-hg@106
    23
public class ExtendedAttribute {
franta-hg@106
    24
franta-hg@106
    25
	private String key;
franta-hg@106
    26
	private String value;
franta-hg@106
    27
franta-hg@106
    28
	public ExtendedAttribute(String key, String value) {
franta-hg@106
    29
		this.key = key;
franta-hg@106
    30
		this.value = value;
franta-hg@106
    31
	}
franta-hg@106
    32
franta-hg@106
    33
	public ExtendedAttribute(String key, ByteBuffer value) {
franta-hg@106
    34
		this.key = key;
franta-hg@106
    35
		setValue(value);
franta-hg@106
    36
	}
franta-hg@106
    37
franta-hg@106
    38
	public ExtendedAttribute() {
franta-hg@106
    39
	}
franta-hg@106
    40
franta-hg@106
    41
	public String getKey() {
franta-hg@106
    42
		return key;
franta-hg@106
    43
	}
franta-hg@106
    44
franta-hg@106
    45
	public void setKey(String key) {
franta-hg@106
    46
		this.key = key;
franta-hg@106
    47
	}
franta-hg@106
    48
franta-hg@106
    49
	public String getValue() {
franta-hg@106
    50
		return value;
franta-hg@106
    51
	}
franta-hg@106
    52
franta-hg@106
    53
	public final ByteBuffer getValueBytes() {
franta-hg@106
    54
		return encode(getValue());
franta-hg@106
    55
	}
franta-hg@106
    56
franta-hg@106
    57
	public void setValue(String value) {
franta-hg@106
    58
		this.value = value;
franta-hg@106
    59
	}
franta-hg@106
    60
franta-hg@106
    61
	public final void setValue(ByteBuffer value) {
franta-hg@106
    62
		setValue(decode(value));
franta-hg@106
    63
	}
franta-hg@106
    64
franta-hg@106
    65
	private static String decode(ByteBuffer bytes) {
franta-hg@106
    66
		bytes.flip();
franta-hg@106
    67
		return Charset.defaultCharset().decode(bytes).toString();
franta-hg@106
    68
	}
franta-hg@106
    69
franta-hg@106
    70
	private static ByteBuffer encode(String text) {
franta-hg@106
    71
		if (text == null) {
franta-hg@106
    72
			return null;
franta-hg@106
    73
		} else {
franta-hg@106
    74
			return Charset.defaultCharset().encode(text);
franta-hg@106
    75
		}
franta-hg@106
    76
	}
franta-hg@106
    77
}