[157] | 1 | package org.expeditee.gui;
|
---|
| 2 |
|
---|
[235] | 3 | import org.expeditee.simple.SString;
|
---|
| 4 |
|
---|
[157] | 5 | public class AttributeValuePair {
|
---|
[235] | 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) {
|
---|
[247] | 26 | this(text, true);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public AttributeValuePair(String text, boolean lastSeparator) {
|
---|
[235] | 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 |
|
---|
[247] | 49 | int ind = lastSeparator ? text.lastIndexOf(SEPARATOR_CHAR) : text
|
---|
| 50 | .indexOf(SEPARATOR_CHAR);
|
---|
[235] | 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;
|
---|
[376] | 56 | if (text.length() == 1
|
---|
| 57 | || !Character.isLetterOrDigit(text.charAt(1)))
|
---|
[235] | 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 | }
|
---|
[286] | 64 | text = text.substring(1);
|
---|
| 65 | ind--;
|
---|
[235] | 66 | if (ind < 1) {
|
---|
[286] | 67 | setAttribute(text);
|
---|
[235] | 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 |
|
---|
[157] | 78 | public AttributeValuePair(String attribute, String value) {
|
---|
[242] | 79 | setAttribute(attribute.trim());
|
---|
[235] | 80 | setValue(value.trim());
|
---|
[157] | 81 | }
|
---|
[235] | 82 |
|
---|
| 83 | public boolean isAnnotation() {
|
---|
| 84 | return _bAnnotation;
|
---|
[157] | 85 | }
|
---|
[235] | 86 |
|
---|
| 87 | public String getAttribute() {
|
---|
| 88 | if (_attribute == null)
|
---|
| 89 | return null;
|
---|
| 90 | return _attribute.stringValue();
|
---|
[157] | 91 | }
|
---|
[235] | 92 |
|
---|
| 93 | public String getValue() {
|
---|
| 94 | if (_value == null)
|
---|
| 95 | return "";
|
---|
| 96 | return _value.stringValue();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[247] | 99 | public Double getDoubleValue() throws NumberFormatException {
|
---|
[157] | 100 | assert (_value != null);
|
---|
[376] | 101 | // Can be null if the text for the AVP was empty
|
---|
| 102 | if (_value == null) {
|
---|
| 103 | return Double.NaN;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[235] | 106 | return _value.doubleValue();
|
---|
[157] | 107 | }
|
---|
[235] | 108 |
|
---|
[247] | 109 | public Double getDoubleAttribute() throws NumberFormatException {
|
---|
[214] | 110 | assert (_attribute != null);
|
---|
[235] | 111 | return _attribute.doubleValue();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public void setAttribute(String newAttribute) {
|
---|
| 115 | if (_attribute == null)
|
---|
[242] | 116 | _attribute = new SString(newAttribute.trim());
|
---|
[235] | 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)
|
---|
[242] | 125 | _value = new SString(newValue);
|
---|
[235] | 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();
|
---|
[214] | 151 | }
|
---|
[235] | 152 | return _string;
|
---|
[214] | 153 | }
|
---|
[235] | 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)
|
---|
[242] | 165 | _value = new SString(value.trim());
|
---|
[235] | 166 | else
|
---|
[242] | 167 | _value.parse(_value.stringValue() + VALUE_SEPARATOR_CHAR
|
---|
| 168 | + value.trim());
|
---|
[235] | 169 | }
|
---|
| 170 |
|
---|
[247] | 171 | public Integer getIntegerValue() throws NumberFormatException {
|
---|
[235] | 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 |
|
---|
[238] | 187 | public String getAttributeOrValue() {
|
---|
[242] | 188 | if (_attribute == null)
|
---|
[238] | 189 | return getValue();
|
---|
| 190 | return getAttribute();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[376] | 193 | public boolean hasAttributeOrValue() {
|
---|
| 194 | return _attribute != null || _value != null;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[157] | 197 | }
|
---|