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

Last change on this file since 309 was 286, checked in by ra33, 16 years ago
File size: 4.0 KB
Line 
1package org.expeditee.gui;
2
3import org.expeditee.simple.SString;
4
5public 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 || !Character.isLetterOrDigit(text.charAt(1)))
57 return;
58 // All chars in the attribute must be letters or digits
59 for (int i = 2; i < ind; i++) {
60 if (!Character.isLetterOrDigit(text.charAt(i)))
61 return;
62 }
63 text = text.substring(1);
64 ind--;
65 if (ind < 1) {
66 setAttribute(text);
67 return;
68 }
69 } else if (ind < 1) {
70 setValue(text.trim());
71 return;
72 }
73 setAttribute(text.substring(0, ind).trim());
74 setValue(text.substring(ind + 1).trim());
75 }
76
77 public AttributeValuePair(String attribute, String value) {
78 setAttribute(attribute.trim());
79 setValue(value.trim());
80 }
81
82 public boolean isAnnotation() {
83 return _bAnnotation;
84 }
85
86 public String getAttribute() {
87 if (_attribute == null)
88 return null;
89 return _attribute.stringValue();
90 }
91
92 public String getValue() {
93 if (_value == null)
94 return "";
95 return _value.stringValue();
96 }
97
98 public Double getDoubleValue() throws NumberFormatException {
99 assert (_value != null);
100 return _value.doubleValue();
101 }
102
103 public Double getDoubleAttribute() throws NumberFormatException {
104 assert (_attribute != null);
105
106 return _attribute.doubleValue();
107 }
108
109 public void setAttribute(String newAttribute) {
110 if (_attribute == null)
111 _attribute = new SString(newAttribute.trim());
112 else
113 _attribute.parse(newAttribute);
114 _string = null;
115 }
116
117 public void setValue(String newValue) {
118 newValue = newValue.trim();
119 if (_value == null)
120 _value = new SString(newValue);
121 else
122 _value.parse(newValue);
123 _string = null;
124 }
125
126 @Override
127 public String toString() {
128 if (_string == null) {
129 StringBuffer sb = new StringBuffer();
130 if (_bAnnotation)
131 sb.append(ANNOTATION_CHAR);
132 if (_attribute != null) {
133 sb.append(_attribute);
134 if (_value != null) {
135 sb.append(SEPARATOR_STRING);
136 }
137 }
138 if (_value != null) {
139 sb.append(_value);
140 }
141 if (_notes != null) {
142 sb.append(Character.LINE_SEPARATOR).append(_notes);
143 }
144
145 _string = sb.toString();
146 }
147 return _string;
148 }
149
150 public boolean isValid() {
151 return _value != null || _attribute != null;
152 }
153
154 public boolean hasAttribute() {
155 return _attribute != null;
156 }
157
158 public void appendValue(String value) {
159 if (_value == null)
160 _value = new SString(value.trim());
161 else
162 _value.parse(_value.stringValue() + VALUE_SEPARATOR_CHAR
163 + value.trim());
164 }
165
166 public Integer getIntegerValue() throws NumberFormatException {
167 assert (_value != null);
168
169 return _value.integerValue().intValue();
170 }
171
172 public boolean getBooleanValue() {
173 assert (_value != null && _value.length() > 0);
174
175 return _value.booleanValue();
176 }
177
178 public boolean hasPair() {
179 return _attribute != null && _value != null;
180 }
181
182 public String getAttributeOrValue() {
183 if (_attribute == null)
184 return getValue();
185 return getAttribute();
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.