[919] | 1 | /**
|
---|
| 2 | * AttributeValuePair.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[157] | 19 | package org.expeditee.gui;
|
---|
| 20 |
|
---|
[235] | 21 | import org.expeditee.simple.SString;
|
---|
| 22 |
|
---|
[157] | 23 | public class AttributeValuePair {
|
---|
[235] | 24 | public static final char ANNOTATION_CHAR = '@';
|
---|
| 25 |
|
---|
| 26 | public static final char SEPARATOR_CHAR = ':';
|
---|
| 27 |
|
---|
| 28 | public static final char VALUE_SEPARATOR_CHAR = ' ';
|
---|
[707] | 29 |
|
---|
| 30 | public static final char VALUE_DELIM_CHAR = '\n';
|
---|
[235] | 31 |
|
---|
| 32 | public static final String SEPARATOR_STRING = "" + SEPARATOR_CHAR
|
---|
| 33 | + VALUE_SEPARATOR_CHAR;
|
---|
| 34 |
|
---|
| 35 | private String _notes;
|
---|
| 36 |
|
---|
| 37 | private SString _attribute;
|
---|
| 38 |
|
---|
| 39 | private SString _value;
|
---|
| 40 |
|
---|
| 41 | private String _string;
|
---|
| 42 |
|
---|
| 43 | private boolean _bAnnotation;
|
---|
| 44 |
|
---|
| 45 | public AttributeValuePair(String text) {
|
---|
[247] | 46 | this(text, true);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public AttributeValuePair(String text, boolean lastSeparator) {
|
---|
[235] | 50 | _bAnnotation = false;
|
---|
| 51 | _attribute = null;
|
---|
| 52 | _value = null;
|
---|
| 53 | _string = null;
|
---|
| 54 |
|
---|
| 55 | if (text == null)
|
---|
| 56 | return;
|
---|
| 57 |
|
---|
| 58 | int lineSeparator = text.indexOf(Character.LINE_SEPARATOR);
|
---|
| 59 | if (lineSeparator >= 0) {
|
---|
| 60 | _notes = text.substring(lineSeparator + 1);
|
---|
| 61 | text = text.substring(0, lineSeparator);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | text = text.trim();
|
---|
| 65 |
|
---|
| 66 | if (text.length() == 0)
|
---|
| 67 | return;
|
---|
| 68 |
|
---|
[247] | 69 | int ind = lastSeparator ? text.lastIndexOf(SEPARATOR_CHAR) : text
|
---|
| 70 | .indexOf(SEPARATOR_CHAR);
|
---|
[235] | 71 |
|
---|
| 72 | // If its an annotation there must be no space between the @ and colon
|
---|
| 73 | // and the first character after the annotation must be a letter
|
---|
| 74 | if (text.charAt(0) == ANNOTATION_CHAR) {
|
---|
| 75 | _bAnnotation = true;
|
---|
[376] | 76 | if (text.length() == 1
|
---|
| 77 | || !Character.isLetterOrDigit(text.charAt(1)))
|
---|
[235] | 78 | return;
|
---|
| 79 | // All chars in the attribute must be letters or digits
|
---|
| 80 | for (int i = 2; i < ind; i++) {
|
---|
| 81 | if (!Character.isLetterOrDigit(text.charAt(i)))
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
[286] | 84 | text = text.substring(1);
|
---|
| 85 | ind--;
|
---|
[235] | 86 | if (ind < 1) {
|
---|
[286] | 87 | setAttribute(text);
|
---|
[235] | 88 | return;
|
---|
| 89 | }
|
---|
| 90 | } else if (ind < 1) {
|
---|
| 91 | setValue(text.trim());
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
| 94 | setAttribute(text.substring(0, ind).trim());
|
---|
| 95 | setValue(text.substring(ind + 1).trim());
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[157] | 98 | public AttributeValuePair(String attribute, String value) {
|
---|
[242] | 99 | setAttribute(attribute.trim());
|
---|
[235] | 100 | setValue(value.trim());
|
---|
[157] | 101 | }
|
---|
[235] | 102 |
|
---|
| 103 | public boolean isAnnotation() {
|
---|
| 104 | return _bAnnotation;
|
---|
[157] | 105 | }
|
---|
[235] | 106 |
|
---|
| 107 | public String getAttribute() {
|
---|
| 108 | if (_attribute == null)
|
---|
| 109 | return null;
|
---|
| 110 | return _attribute.stringValue();
|
---|
[157] | 111 | }
|
---|
[235] | 112 |
|
---|
| 113 | public String getValue() {
|
---|
| 114 | if (_value == null)
|
---|
| 115 | return "";
|
---|
| 116 | return _value.stringValue();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[247] | 119 | public Double getDoubleValue() throws NumberFormatException {
|
---|
[157] | 120 | assert (_value != null);
|
---|
[376] | 121 | // Can be null if the text for the AVP was empty
|
---|
| 122 | if (_value == null) {
|
---|
| 123 | return Double.NaN;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[235] | 126 | return _value.doubleValue();
|
---|
[157] | 127 | }
|
---|
[235] | 128 |
|
---|
[247] | 129 | public Double getDoubleAttribute() throws NumberFormatException {
|
---|
[214] | 130 | assert (_attribute != null);
|
---|
[235] | 131 | return _attribute.doubleValue();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public void setAttribute(String newAttribute) {
|
---|
| 135 | if (_attribute == null)
|
---|
[242] | 136 | _attribute = new SString(newAttribute.trim());
|
---|
[235] | 137 | else
|
---|
| 138 | _attribute.parse(newAttribute);
|
---|
| 139 | _string = null;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public void setValue(String newValue) {
|
---|
| 143 | newValue = newValue.trim();
|
---|
| 144 | if (_value == null)
|
---|
[242] | 145 | _value = new SString(newValue);
|
---|
[235] | 146 | else
|
---|
| 147 | _value.parse(newValue);
|
---|
| 148 | _string = null;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | @Override
|
---|
| 152 | public String toString() {
|
---|
| 153 | if (_string == null) {
|
---|
| 154 | StringBuffer sb = new StringBuffer();
|
---|
| 155 | if (_bAnnotation)
|
---|
| 156 | sb.append(ANNOTATION_CHAR);
|
---|
| 157 | if (_attribute != null) {
|
---|
| 158 | sb.append(_attribute);
|
---|
| 159 | if (_value != null) {
|
---|
| 160 | sb.append(SEPARATOR_STRING);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | if (_value != null) {
|
---|
| 164 | sb.append(_value);
|
---|
| 165 | }
|
---|
| 166 | if (_notes != null) {
|
---|
| 167 | sb.append(Character.LINE_SEPARATOR).append(_notes);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | _string = sb.toString();
|
---|
[214] | 171 | }
|
---|
[235] | 172 | return _string;
|
---|
[214] | 173 | }
|
---|
[235] | 174 |
|
---|
| 175 | public boolean isValid() {
|
---|
| 176 | return _value != null || _attribute != null;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | public boolean hasAttribute() {
|
---|
| 180 | return _attribute != null;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | public void appendValue(String value) {
|
---|
| 184 | if (_value == null)
|
---|
[242] | 185 | _value = new SString(value.trim());
|
---|
[235] | 186 | else
|
---|
[707] | 187 | _value.parse(_value.stringValue() + VALUE_DELIM_CHAR
|
---|
[242] | 188 | + value.trim());
|
---|
[235] | 189 | }
|
---|
| 190 |
|
---|
[247] | 191 | public Integer getIntegerValue() throws NumberFormatException {
|
---|
[235] | 192 | assert (_value != null);
|
---|
| 193 |
|
---|
| 194 | return _value.integerValue().intValue();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | public boolean getBooleanValue() {
|
---|
| 198 | assert (_value != null && _value.length() > 0);
|
---|
| 199 |
|
---|
| 200 | return _value.booleanValue();
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | public boolean hasPair() {
|
---|
| 204 | return _attribute != null && _value != null;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[238] | 207 | public String getAttributeOrValue() {
|
---|
[242] | 208 | if (_attribute == null)
|
---|
[238] | 209 | return getValue();
|
---|
| 210 | return getAttribute();
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[376] | 213 | public boolean hasAttributeOrValue() {
|
---|
| 214 | return _attribute != null || _value != null;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[157] | 217 | }
|
---|