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