source: trunk/src/org/expeditee/gui/AttributeValuePair.java@ 1460

Last change on this file since 1460 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 5.0 KB
Line 
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
19package org.expeditee.gui;
20
21import org.expeditee.simple.SString;
22
23public class AttributeValuePair {
24 public static final char ANNOTATION_CHAR = '@';
25
26 public static final char SEPARATOR_CHAR = ':';
27
28 public static final char VALUE_SEPARATOR_CHAR = ' ';
29
30 public static final char VALUE_DELIM_CHAR = '\n';
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) {
46 this(text, true);
47 }
48
49 public AttributeValuePair(String text, boolean lastSeparator) {
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
69 int ind = lastSeparator ? text.lastIndexOf(SEPARATOR_CHAR) : text
70 .indexOf(SEPARATOR_CHAR);
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;
76 if (text.length() == 1
77 || !Character.isLetterOrDigit(text.charAt(1)))
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 }
84 text = text.substring(1);
85 ind--;
86 if (ind < 1) {
87 setAttribute(text);
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
98 public AttributeValuePair(String attribute, String value) {
99 setAttribute(attribute.trim());
100 setValue(value.trim());
101 }
102
103 public boolean isAnnotation() {
104 return _bAnnotation;
105 }
106
107 public String getAttribute() {
108 if (_attribute == null)
109 return null;
110 return _attribute.stringValue();
111 }
112
113 public String getValue() {
114 if (_value == null)
115 return "";
116 return _value.stringValue();
117 }
118
119 public Double getDoubleValue() throws NumberFormatException {
120 assert (_value != null);
121 // Can be null if the text for the AVP was empty
122 if (_value == null) {
123 return Double.NaN;
124 }
125
126 return _value.doubleValue();
127 }
128
129 public Double getDoubleAttribute() throws NumberFormatException {
130 assert (_attribute != null);
131 return _attribute.doubleValue();
132 }
133
134 public void setAttribute(String newAttribute) {
135 if (_attribute == null)
136 _attribute = new SString(newAttribute.trim());
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)
145 _value = new SString(newValue);
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();
171 }
172 return _string;
173 }
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)
185 _value = new SString(value.trim());
186 else
187 _value.parse(_value.stringValue() + VALUE_DELIM_CHAR
188 + value.trim());
189 }
190
191 public Integer getIntegerValue() throws NumberFormatException {
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
207 public String getAttributeOrValue() {
208 if (_attribute == null)
209 return getValue();
210 return getAttribute();
211 }
212
213 public boolean hasAttributeOrValue() {
214 return _attribute != null || _value != null;
215 }
216
217}
Note: See TracBrowser for help on using the repository browser.