1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import org.expeditee.simple.SString;
|
---|
4 |
|
---|
5 | public class AttributeValuePair {
|
---|
6 | public static final char ANNOTATION_CHAR = '@';
|
---|
7 |
|
---|
8 | public static final char SEPARATOR_CHAR = ':';
|
---|
9 |
|
---|
10 | public static final char VALUE_SEPARATOR_CHAR = ' ';
|
---|
11 |
|
---|
12 | public static final String SEPARATOR_STRING = "" + SEPARATOR_CHAR
|
---|
13 | + VALUE_SEPARATOR_CHAR;
|
---|
14 |
|
---|
15 | private String _notes;
|
---|
16 |
|
---|
17 | private SString _attribute;
|
---|
18 |
|
---|
19 | private SString _value;
|
---|
20 |
|
---|
21 | private String _string;
|
---|
22 |
|
---|
23 | private boolean _bAnnotation;
|
---|
24 |
|
---|
25 | public AttributeValuePair(String text) {
|
---|
26 | this(text, true);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public AttributeValuePair(String text, boolean lastSeparator) {
|
---|
30 | _bAnnotation = false;
|
---|
31 | _attribute = null;
|
---|
32 | _value = null;
|
---|
33 | _string = null;
|
---|
34 |
|
---|
35 | if (text == null)
|
---|
36 | return;
|
---|
37 |
|
---|
38 | int lineSeparator = text.indexOf(Character.LINE_SEPARATOR);
|
---|
39 | if (lineSeparator >= 0) {
|
---|
40 | _notes = text.substring(lineSeparator + 1);
|
---|
41 | text = text.substring(0, lineSeparator);
|
---|
42 | }
|
---|
43 |
|
---|
44 | text = text.trim();
|
---|
45 |
|
---|
46 | if (text.length() == 0)
|
---|
47 | return;
|
---|
48 |
|
---|
49 | int ind = lastSeparator ? text.lastIndexOf(SEPARATOR_CHAR) : text
|
---|
50 | .indexOf(SEPARATOR_CHAR);
|
---|
51 |
|
---|
52 | // If its an annotation there must be no space between the @ and colon
|
---|
53 | // and the first character after the annotation must be a letter
|
---|
54 | if (text.charAt(0) == ANNOTATION_CHAR) {
|
---|
55 | _bAnnotation = true;
|
---|
56 | if (text.length() == 1
|
---|
57 | || !Character.isLetterOrDigit(text.charAt(1)))
|
---|
58 | return;
|
---|
59 | // All chars in the attribute must be letters or digits
|
---|
60 | for (int i = 2; i < ind; i++) {
|
---|
61 | if (!Character.isLetterOrDigit(text.charAt(i)))
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | text = text.substring(1);
|
---|
65 | ind--;
|
---|
66 | if (ind < 1) {
|
---|
67 | setAttribute(text);
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | } else if (ind < 1) {
|
---|
71 | setValue(text.trim());
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | setAttribute(text.substring(0, ind).trim());
|
---|
75 | setValue(text.substring(ind + 1).trim());
|
---|
76 | }
|
---|
77 |
|
---|
78 | public AttributeValuePair(String attribute, String value) {
|
---|
79 | setAttribute(attribute.trim());
|
---|
80 | setValue(value.trim());
|
---|
81 | }
|
---|
82 |
|
---|
83 | public boolean isAnnotation() {
|
---|
84 | return _bAnnotation;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public String getAttribute() {
|
---|
88 | if (_attribute == null)
|
---|
89 | return null;
|
---|
90 | return _attribute.stringValue();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public String getValue() {
|
---|
94 | if (_value == null)
|
---|
95 | return "";
|
---|
96 | return _value.stringValue();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public Double getDoubleValue() throws NumberFormatException {
|
---|
100 | assert (_value != null);
|
---|
101 | // Can be null if the text for the AVP was empty
|
---|
102 | if (_value == null) {
|
---|
103 | return Double.NaN;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return _value.doubleValue();
|
---|
107 | }
|
---|
108 |
|
---|
109 | public Double getDoubleAttribute() throws NumberFormatException {
|
---|
110 | assert (_attribute != null);
|
---|
111 | return _attribute.doubleValue();
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void setAttribute(String newAttribute) {
|
---|
115 | if (_attribute == null)
|
---|
116 | _attribute = new SString(newAttribute.trim());
|
---|
117 | else
|
---|
118 | _attribute.parse(newAttribute);
|
---|
119 | _string = null;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void setValue(String newValue) {
|
---|
123 | newValue = newValue.trim();
|
---|
124 | if (_value == null)
|
---|
125 | _value = new SString(newValue);
|
---|
126 | else
|
---|
127 | _value.parse(newValue);
|
---|
128 | _string = null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public String toString() {
|
---|
133 | if (_string == null) {
|
---|
134 | StringBuffer sb = new StringBuffer();
|
---|
135 | if (_bAnnotation)
|
---|
136 | sb.append(ANNOTATION_CHAR);
|
---|
137 | if (_attribute != null) {
|
---|
138 | sb.append(_attribute);
|
---|
139 | if (_value != null) {
|
---|
140 | sb.append(SEPARATOR_STRING);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | if (_value != null) {
|
---|
144 | sb.append(_value);
|
---|
145 | }
|
---|
146 | if (_notes != null) {
|
---|
147 | sb.append(Character.LINE_SEPARATOR).append(_notes);
|
---|
148 | }
|
---|
149 |
|
---|
150 | _string = sb.toString();
|
---|
151 | }
|
---|
152 | return _string;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public boolean isValid() {
|
---|
156 | return _value != null || _attribute != null;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public boolean hasAttribute() {
|
---|
160 | return _attribute != null;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public void appendValue(String value) {
|
---|
164 | if (_value == null)
|
---|
165 | _value = new SString(value.trim());
|
---|
166 | else
|
---|
167 | _value.parse(_value.stringValue() + VALUE_SEPARATOR_CHAR
|
---|
168 | + value.trim());
|
---|
169 | }
|
---|
170 |
|
---|
171 | public Integer getIntegerValue() throws NumberFormatException {
|
---|
172 | assert (_value != null);
|
---|
173 |
|
---|
174 | return _value.integerValue().intValue();
|
---|
175 | }
|
---|
176 |
|
---|
177 | public boolean getBooleanValue() {
|
---|
178 | assert (_value != null && _value.length() > 0);
|
---|
179 |
|
---|
180 | return _value.booleanValue();
|
---|
181 | }
|
---|
182 |
|
---|
183 | public boolean hasPair() {
|
---|
184 | return _attribute != null && _value != null;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public String getAttributeOrValue() {
|
---|
188 | if (_attribute == null)
|
---|
189 | return getValue();
|
---|
190 | return getAttribute();
|
---|
191 | }
|
---|
192 |
|
---|
193 | public boolean hasAttributeOrValue() {
|
---|
194 | return _attribute != null || _value != null;
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|