source: trunk/src/org/expeditee/gui/AttributeUtils.java@ 72

Last change on this file since 72 was 72, checked in by ra33, 16 years ago

Added lots of stuff

File size: 23.3 KB
Line 
1package org.expeditee.gui;
2
3import java.awt.Color;
4import java.awt.Font;
5import java.awt.Point;
6import java.lang.reflect.InvocationTargetException;
7import java.lang.reflect.Method;
8import java.util.HashMap;
9import java.util.LinkedList;
10import java.util.List;
11
12import org.expeditee.io.Conversion;
13import org.expeditee.items.Item;
14import org.expeditee.items.Text;
15
16/**
17 * This class provides the methods to extract and set attributes of Items and
18 * Frames. These methods are called when a user merges a text item with
19 * <code>Attribute: Value</code> pairs.
20 *
21 * @author jdm18
22 *
23 */
24public class AttributeUtils {
25
26 private static final char SEPARATOR_CHAR = ':';
27
28 private static final String SEPARATOR_STRING = SEPARATOR_CHAR + " ";
29
30 private static final int GET_LENGTH = "get".length();
31
32 private static int SET_LENGTH = "set".length();
33
34 // List of method names to ignore when searching for a match
35 private static List<Method> _GetMethods = null;
36
37 private static HashMap<String, Method> _SetMethods = null;
38
39 // List of attributes which are ignored when copying.
40 private static List<String> _Ignore = null;
41
42 // List of method names to show in extraced lists even when they return
43 // null
44 // (Null is often used to indicate the default value is used)
45 private static List<Method> _AllowNull = null;
46
47 private static List<String> _ExtractIgnore = null;
48
49 // private static HashMap<String, String> _Abbreviations = null;
50
51 /**
52 * Initialises the _Ignore and _AllowNull lists.
53 */
54 private static void initLists() {
55
56 Class[] param = {};
57
58 try {
59 _Ignore = new LinkedList<String>();
60 _Ignore.add("date");
61 _Ignore.add("datecreated");
62 _Ignore.add("d");
63 _Ignore.add("link");
64 _Ignore.add("l");
65 _Ignore.add("position");
66 _Ignore.add("pos");
67 _Ignore.add("p");
68 _Ignore.add("x");
69 _Ignore.add("y");
70
71 _ExtractIgnore = new LinkedList<String>();
72 _ExtractIgnore.add("x");
73 _ExtractIgnore.add("y");
74
75 _AllowNull = new LinkedList<Method>();
76 _AllowNull.add(Item.class.getMethod("getColor", param));
77 //_AllowNull.add(Item.class.getMethod("getBackgroundColor", param));
78
79 //_AllowNull.add(Frame.class.getMethod("getBackgroundColor", param));
80 _AllowNull.add(Frame.class.getMethod("getForegroundColor", param));
81
82 _GetMethods = new LinkedList<Method>();
83 _GetMethods.add(Item.class.getMethod("getDateCreated", param));
84
85 _GetMethods.add(Item.class.getMethod("getColor", param));
86 _GetMethods.add(Item.class.getMethod("getBackgroundColor", param));
87 _GetMethods.add(Item.class.getMethod("getAction", param));
88 _GetMethods.add(Item.class.getMethod("getLink", param));
89 _GetMethods.add(Item.class.getMethod("getFillColor", param));
90 _GetMethods.add(Item.class.getMethod("getFillPattern", param));
91 _GetMethods.add(Item.class.getMethod("getThickness", param));
92
93 _GetMethods.add(Item.class.getMethod("getOwner", param));
94 _GetMethods.add(Item.class.getMethod("getLinkMark", param));
95 _GetMethods.add(Item.class.getMethod("getActionMark", param));
96
97 _GetMethods
98 .add(Item.class.getMethod("getActionCursorEnter", param));
99 _GetMethods
100 .add(Item.class.getMethod("getActionCursorLeave", param));
101 _GetMethods.add(Item.class.getMethod("getActionEnterFrame", param));
102 _GetMethods.add(Item.class.getMethod("getActionLeaveFrame", param));
103
104 _GetMethods.add(Item.class.getMethod("getTopShadowColor", param));
105 _GetMethods
106 .add(Item.class.getMethod("getBottomShadowColor", param));
107 _GetMethods.add(Item.class.getMethod("getArrow", param));
108
109 _GetMethods.add(Item.class.getMethod("getLinePattern", param));
110 _GetMethods.add(Item.class.getMethod("getLinkFrameset", param));
111 _GetMethods.add(Item.class.getMethod("getLinkTemplate", param));
112 _GetMethods.add(Item.class.getMethod("getPosition", param));
113 _GetMethods.add(Item.class.getMethod("getX", param));
114 _GetMethods.add(Item.class.getMethod("getY", param));
115
116 _GetMethods.add(Text.class.getMethod("getFamily", param));
117 _GetMethods.add(Text.class.getMethod("getFontStyle", param));
118 _GetMethods.add(Text.class.getMethod("getJustification", param));
119 _GetMethods.add(Text.class.getMethod("getWidth", param));
120 _GetMethods.add(Item.class.getMethod("getSize", param));
121
122 _GetMethods.add(Frame.class.getMethod("getOwner", param));
123 _GetMethods.add(Frame.class.getMethod("getProtection", param));
124 _GetMethods.add(Frame.class.getMethod("getDateCreated", param));
125 _GetMethods.add(Frame.class.getMethod("getLastModifyUser", param));
126 _GetMethods.add(Frame.class.getMethod("getLastModifyDate", param));
127 _GetMethods.add(Frame.class.getMethod("getForegroundColor", param));
128 _GetMethods.add(Frame.class.getMethod("getBackgroundColor", param));
129
130 Class[] pPoint = { Point.class };
131 Class[] pString = { String.class };
132 Class[] pInt = { int.class };
133 Class[] pFloat = { float.class };
134 Class[] pColor = { Color.class };
135 Class[] pBool = { boolean.class };
136 Class[] pArrow = { int.class, double.class };
137 Class[] pList = { List.class };
138 Class[] pIntArray = { int[].class };
139
140 _SetMethods = new HashMap<String, Method>();
141 _SetMethods.put("x", Item.class.getMethod("setX", pInt));
142 _SetMethods.put("y", Item.class.getMethod("setY", pInt));
143 _SetMethods.put("position", Item.class.getMethod("setPosition",
144 pPoint));
145 _SetMethods.put("p", Item.class.getMethod("setPosition", pPoint));
146 _SetMethods.put("pos", Item.class.getMethod("setPosition", pPoint));
147 _SetMethods.put("thickness", Item.class.getMethod("setThickness",
148 pFloat));
149 _SetMethods.put("t", Item.class.getMethod("setThickness", pFloat));
150
151 _SetMethods.put("color", Item.class.getMethod("setColor", pColor));
152 _SetMethods.put("c", Item.class.getMethod("setColor", pColor));
153
154 _SetMethods.put("backgroundcolor", Item.class.getMethod(
155 "setBackgroundColor", pColor));
156 _SetMethods.put("bgc", Item.class.getMethod("setBackgroundColor",
157 pColor));
158
159 _SetMethods.put("action", Item.class.getMethod("setAction", pList));
160 _SetMethods.put("a", Item.class.getMethod("setAction", pList));
161
162 _SetMethods.put("link", Item.class.getMethod("setLink", pString));
163 _SetMethods.put("l", Item.class.getMethod("setLink", pString));
164
165 _SetMethods.put("fillcolor", Item.class.getMethod("setFillColor",
166 pColor));
167 _SetMethods.put("fc", Item.class.getMethod("setFillColor", pColor));
168
169 _SetMethods.put("fillpattern", Item.class.getMethod(
170 "setFillPattern", pString));
171 _SetMethods.put("fp", Item.class.getMethod("setFillPattern",
172 pString));
173
174 _SetMethods.put("owner", Item.class.getMethod("setOwner", pString));
175 _SetMethods.put("linkmark", Item.class.getMethod("setLinkMark",
176 pBool));
177 _SetMethods.put("lm", Item.class.getMethod("setLinkMark",
178 pBool));
179 _SetMethods.put("actionmark", Item.class.getMethod("setActionMark",
180 pBool));
181 _SetMethods.put("am", Item.class.getMethod("setActionMark",
182 pBool));
183
184 _SetMethods.put("actioncursorenter", Item.class.getMethod(
185 "setActionCursorEnter", pList));
186 _SetMethods.put("actioncursorleave", Item.class.getMethod(
187 "setActionCursorLeave", pList));
188 _SetMethods.put("actionenterframe", Item.class.getMethod(
189 "setActionEnterFrame", pList));
190 _SetMethods.put("actionleaveframe", Item.class.getMethod(
191 "setActionLeaveFrame", pList));
192
193 _SetMethods.put("topshadow", Item.class.getMethod(
194 "setTopShadowColor", pColor));
195 _SetMethods.put("bottomshadow", Item.class.getMethod(
196 "setBottomShadowColor", pColor));
197 _SetMethods.put("arrow", Item.class.getMethod("setArrow", pArrow));
198
199 _SetMethods.put("linepattern", Item.class.getMethod(
200 "setLinePattern", pIntArray));
201 _SetMethods.put("lp", Item.class.getMethod("setLinePattern",
202 pIntArray));
203
204 _SetMethods.put("linkframeset", Item.class.getMethod(
205 "setLinkFrameset", pString));
206 _SetMethods.put("lf", Item.class.getMethod(
207 "setLinkFrameset", pString));
208 _SetMethods.put("linktemplate", Item.class.getMethod(
209 "setLinkTemplate", pString));
210 _SetMethods.put("lt", Item.class.getMethod(
211 "setLinkTemplate", pString));
212
213 _SetMethods.put("family", Text.class
214 .getMethod("setFamily", pString));
215 _SetMethods.put("face", Text.class.getMethod("setFontStyle",
216 pString));
217 _SetMethods.put("fontstyle", Text.class.getMethod("setFontStyle",
218 pString));
219 _SetMethods.put("justification", Text.class.getMethod(
220 "setJustification", pInt));
221 _SetMethods.put("width", Text.class.getMethod("setWidth", pInt));
222 _SetMethods.put("size", Item.class.getMethod("setSize", pInt));
223 _SetMethods.put("s", Item.class.getMethod("setSize", pInt));
224
225 _SetMethods.put("foregroundcolor", Frame.class.getMethod(
226 "setForegroundColor", pColor));
227 _SetMethods.put("fgc", Frame.class.getMethod("setForegroundColor",
228 pColor));
229 _SetMethods.put("backgroundcolor0", Frame.class.getMethod(
230 "setBackgroundColor", pColor));
231 _SetMethods.put("bgc0", Frame.class.getMethod("setBackgroundColor",
232 pColor));
233 _SetMethods.put("protection", Frame.class.getMethod("setProtection",
234 pString));
235
236 } catch (SecurityException e) {
237 // TODO Auto-generated catch block
238 e.printStackTrace();
239 } catch (NoSuchMethodException e) {
240 // TODO Auto-generated catch block
241 e.printStackTrace();
242 }
243 }
244
245 /**
246 * Extracts a list of attributes from the given Object. Any method that
247 * starts with <code>get</code>, takes no arguments and is not found in
248 * the Ignore list will be run, All the attributes are then put into a Text
249 * Item of the form <Name>:<Value> If the value returned by the get method
250 * is null, then the attribute will not be included, unless the name of the
251 * method is found in the AllowNull list.
252 *
253 * @param toExtract
254 * The Object from which to extract the attributes
255 * @return A Text Item containing the extracted Attributes.
256 */
257 public static Text ExtractAttributes(Object toExtract) {
258 if (toExtract == null)
259 return null;
260
261 // ensure the lists are populated
262 if (_Ignore == null)
263 initLists();
264
265 // StringBuffer to store all the extracted Attribute:Value pairs
266 StringBuffer attributes = new StringBuffer();
267
268 // iterate through the list of methods
269 for (Method m : _GetMethods) {
270
271 // Make sure the classes of the methods match the item
272 if (m.getDeclaringClass() == toExtract.getClass()
273 || m.getDeclaringClass() == toExtract.getClass()
274 .getSuperclass()) {
275 try {
276 Object o = m.invoke(toExtract, (Object[]) null);
277 if (o != null) {
278 // skip methods that are in the ignore lists
279 if (_ExtractIgnore.contains(m.getName().substring(
280 GET_LENGTH).toLowerCase()))
281 continue;
282
283 if (o instanceof Integer) {
284 if (m.getName().endsWith("Justification")
285 && convertJustificationToString((Integer) o) != null)
286 attributes
287 .append(
288 m.getName().substring(
289 GET_LENGTH))
290 .append(SEPARATOR_STRING)
291 .append(
292 convertJustificationToString((Integer) o))
293 .append("\n");
294 // -1 indicates default value
295 else if (((Integer) o) > -1)
296 attributes.append(
297 m.getName().substring(GET_LENGTH))
298 .append(SEPARATOR_STRING).append(o)
299 .append("\n");
300 } else if (o instanceof Float) {
301 // -1 indicates default value
302 if (((Float) o) > 0.0001)
303 attributes.append(
304 m.getName().substring(GET_LENGTH))
305 .append(SEPARATOR_STRING).append(o)
306 .append("\n");
307 } else if (o instanceof Double) {
308 // -1 indicates default value
309 if (((Double) o) > 0.0001)
310 attributes.append(
311 m.getName().substring(GET_LENGTH))
312 .append(SEPARATOR_STRING).append(o)
313 .append("\n");
314 } else if (o instanceof Color) {
315 // converts the color to the KMS code
316 String s = Conversion.getKMSColorCode((Color) o);
317 if (s != null)
318 attributes.append(
319 m.getName().substring(GET_LENGTH))
320 .append(SEPARATOR_STRING).append(s)
321 .append("\n");
322 } else if (o instanceof Point) {
323 Point p = (Point) o;
324 String s = (int) p.getX() + " " + (int) p.getY();
325 attributes
326 .append(m.getName().substring(GET_LENGTH))
327 .append(SEPARATOR_STRING).append(s).append(
328 "\n");
329 } else if (o instanceof Font) {
330 Font f = (Font) o;
331
332 String s = f.getName() + "-";
333 if (f.isPlain())
334 s += "Plain";
335
336 if (f.isBold())
337 s += "Bold";
338
339 if (f.isItalic())
340 s += "Italic";
341
342 s += "-" + f.getSize();
343
344 attributes
345 .append(m.getName().substring(GET_LENGTH))
346 .append(SEPARATOR_STRING).append(s).append(
347 "\n");
348 } else if (o instanceof Text) {
349 attributes
350 .append(m.getName().substring(GET_LENGTH))
351 .append(SEPARATOR_STRING).append(
352 ((Text) o).getFirstLine()).append(
353 "\n");
354 } else if (o instanceof List) {
355 List list = (List) o;
356 for (Object ob : list)
357 attributes.append(
358 m.getName().substring(GET_LENGTH))
359 .append(SEPARATOR_STRING).append(ob)
360 .append("\n");
361 } else if (o instanceof int[]) {
362 attributes
363 .append(m.getName().substring(GET_LENGTH))
364 .append(SEPARATOR_STRING);
365 int[] values = (int[]) o;
366 for (int i = 0; i < values.length; i++) {
367 attributes.append(values[i]).append(' ');
368 }
369 attributes.deleteCharAt(attributes.length() - 1);
370 attributes.append("\n");
371 } else if (o instanceof Boolean) {
372 // true is the default for boolean values
373 if (!((Boolean) o).booleanValue())
374 attributes.append(
375 m.getName().substring(GET_LENGTH))
376 .append(SEPARATOR_STRING).append(o)
377 .append("\n");
378 } else
379 attributes
380 .append(m.getName().substring(GET_LENGTH))
381 .append(SEPARATOR_STRING).append(o).append(
382 "\n");
383
384 /*
385 * } catch (SecurityException e) { e.printStackTrace(); }
386 * /*catch (NoSuchMethodException e) {
387 * e.printStackTrace(); }
388 */
389
390 // methods that return null are only included if they
391 // are in the AllowNull list
392 } else if (_AllowNull.contains(m))
393 attributes.append(m.getName().substring(GET_LENGTH))
394 .append(SEPARATOR_CHAR + "\n");
395 } catch (IllegalArgumentException e) {
396 // TODO Auto-generated catch block
397 e.printStackTrace();
398 } catch (IllegalAccessException e) {
399 // TODO Auto-generated catch block
400 e.printStackTrace();
401 } catch (InvocationTargetException e) {
402 // TODO Auto-generated catch block
403 e.printStackTrace();
404 }
405 }
406 }
407
408 // if no attributes were extracted
409 if (attributes.length() <= 0)
410 return null;
411
412 while (attributes.charAt(attributes.length() - 1) == '\n')
413 attributes.delete(attributes.length() - 1, attributes.length());
414
415 // create the text Item
416 Frame current = DisplayIO.getCurrentFrame();
417 Text attribs = current.getStatsTextItem(attributes.toString());
418 return attribs;
419 }
420
421 /**
422 * Returns the String name of the justification value given. just should
423 * correspond to one of the Item.JUSTIFICATION constants, if it does not
424 * then null is returned.
425 *
426 * @param just
427 * The justification level as defined by the Item.JUSTIFICATION
428 * constants.
429 * @return The String name (i.e. "Center") of the justification given, or
430 * null if no match is found.
431 */
432 private static String convertJustificationToString(int just) {
433 switch (just) {
434 case Item.JUSTIFICATION_CENTER:
435 return "Center";
436 case Item.JUSTIFICATION_FULL:
437 return "Full";
438 case Item.JUSTIFICATION_LEFT:
439 return "Left";
440 case Item.JUSTIFICATION_RIGHT:
441 return "Right";
442 default:
443 return null;
444 }
445 }
446
447 /**
448 * Attempts to set the attribute in the given attribute: value pair. The
449 * value string should be formatted as follows:
450 * <code> Attribute: Value </code> Multiple values can be used if they are
451 * separated by spaces
452 *
453 * @param toSet
454 * The Item to set the attribute of
455 * @param attribs
456 * The Text item that contains the list of attributes to set
457 * @return True if the attribute(s) were sucessfully set, false otherwise
458 */
459 public static boolean SetAttribute(Object toSet, Text attribs) {
460 // error checking
461 if (toSet == null || attribs == null)
462 return false;
463
464 if (_Ignore == null)
465 initLists();
466
467 // get the list of attribute: value pairs
468 List<String> values = attribs.getText();
469 // if no paris exist, we are done
470 if (values.size() == 0
471 || (values.size() == 1 && values.get(0).length() == 0))
472 return false;
473
474 // loop through all attribute: value pairs
475 for (int i = 0; i < values.size(); i++) {
476 StringBuffer v = new StringBuffer(values.get(i));
477
478 // remove the annotation mark (if applicable)
479 if (v.indexOf("@") == 0)
480 v = v.deleteCharAt(0);
481
482 // check if the next string is another attribute to merge or a
483 // continuation
484 while (i < values.size() - 1) {
485 StringBuffer next = new StringBuffer(values.get(i + 1));
486
487 // if the next String has a colon, then it may be another
488 // attribute
489 if (next.indexOf("" + SEPARATOR_CHAR) >= 0) {
490 // if the attribute is the same as v, then it is a
491 // continuation
492 if (v.indexOf(stripAttribute(next.toString())) == 0) {
493 // strip the attribute from next
494 next = new StringBuffer(stripValue(next.toString()));
495 // if the attribute is not the same, then it may be a
496 // new method
497 } else {
498 // if this is indeed a method, then leave the while loop
499 // if(_SetMethods.containsKey(StripFromColon(next.toString()).toLowerCase())){
500 break;
501 // }
502 }
503 }
504
505 v.append("\n").append(next);
506 i++;
507 }
508
509 if (v.length() > 0
510 && !SetAttribute(toSet, v.toString(), values.size() > 1)) {
511 // if no other attributes have been set
512 if (i == 0)
513 return false;
514 // otherwise, this is a list of attributes, so continue
515 else {
516 String stripped = stripAttribute(v.toString());
517 if (stripped == null) {
518 // This happens when there is an attribute at the start
519 // Then a bunch of plain text
520 return false;
521 } else if (_Ignore.contains(stripped)) {
522 return false;
523 } else if (!(_SetMethods.containsKey(stripped))) {
524 // Display an error message if its not in our list of
525 // attributes to ignore when copying
526 FrameGraphics.WarningMessage("Attribute: '"
527 + stripAttribute(v.toString())
528 + "' does not exist.");
529 } else {
530 String types = "";
531 for (Class c : _SetMethods.get(stripped)
532 .getParameterTypes())
533 types += c.getSimpleName() + " ";
534 FrameGraphics.WarningMessage("Wrong arguments for: '"
535 + stripAttribute(v.toString()) + "' expecting "
536 + types.trim() + " found '"
537 + stripValue(v.toString()) + "'");
538 }
539 }
540 } else if (v.length() == 0)
541 return false;
542 }
543
544 return true;
545 }
546
547 private static boolean SetAttribute(Object toSet, String value,
548 boolean isAttributeList) {
549 // separate attribute and value from string
550 String attribute = stripAttribute(value);
551 value = stripValue(value);
552
553 // if there was no colon
554 if (value == null || attribute == null)
555 return false;
556
557 if (value.length() == 0)
558 value = "";
559
560 attribute = attribute.toLowerCase();
561
562 // Some properties are ignored when multiple attributes are being set on
563 // an item at the same time
564 if (isAttributeList && _Ignore.contains(attribute)) {
565 // System.out.println("Attribute ignored: " + attribute);
566 return true;
567 }
568
569 // separate multiple values if required
570 Method toRun = _SetMethods.get(attribute);
571
572 // if this is not the name of a method, it may be the name of an agent
573 if (toRun == null) {
574 // System.out.println("Attrib not found for: " + attribute);
575 return false;
576 }
577
578 // if there are duplicate methods with the same name
579 List<Method> possibles = new LinkedList<Method>();
580 if (toRun.getDeclaringClass() == toSet.getClass()
581 || toRun.getDeclaringClass() == toSet.getClass()
582 .getSuperclass())
583 possibles.add(toRun);
584 int i = 0;
585 while (_SetMethods.containsKey(attribute + i)) {
586 if (_SetMethods.get(attribute + i).getDeclaringClass() == toSet
587 .getClass()
588 || _SetMethods.get(attribute + i).getDeclaringClass() == toSet
589 .getClass().getSuperclass())
590 possibles.add(_SetMethods.get(attribute + i));
591 i++;
592 }
593
594 for (Method possible : possibles) {
595 Object current = null;
596 Object[] param = {};
597 Class toSetClass = toSet.getClass();
598 Class toSetSuperClass = toSetClass.getSuperclass();
599 // find the corresponding get method for this set method
600 for (Method m : _GetMethods) {
601 if ((m.getDeclaringClass() == toSetClass || m
602 .getDeclaringClass() == toSetSuperClass)
603 && m.getName().substring(GET_LENGTH).equals(
604 possible.getName().substring(SET_LENGTH))) {
605 try {
606 current = m.invoke(toSet, param);
607 } catch (IllegalArgumentException e) {
608 // TODO Auto-generated catch block
609 e.printStackTrace();
610 } catch (IllegalAccessException e) {
611 // TODO Auto-generated catch block
612 e.printStackTrace();
613 } catch (InvocationTargetException e) {
614 // TODO Auto-generated catch block
615 e.printStackTrace();
616 }
617 break;
618 }
619
620 }
621
622 try {
623 Object[] params = Conversion.Convert(possible, value, current);
624
625 try {
626 possible.invoke(toSet, params);
627 return true;
628 } catch (IllegalArgumentException e) {
629 // TODO Auto-generated catch block
630 e.printStackTrace();
631 } catch (IllegalAccessException e) {
632 // TODO Auto-generated catch block
633 e.printStackTrace();
634 } catch (InvocationTargetException e) {
635 FrameGraphics.DisplayMessage(toSet.getClass()
636 .getSimpleName()
637 + " type does not support that attribute.");
638 // e.printStackTrace();
639 }
640 } catch (NumberFormatException e) {
641
642 }
643 }
644
645 return false;
646
647 }
648
649 /**
650 * Returns the part of the given string that is after a colon, or null if
651 * the given String has no colon
652 *
653 * @param toStrip
654 * The String to strip
655 * @return The part of the String that is after a colon if there is one, or
656 * the entire string if there is no colon
657 */
658 public static String stripValue(String toStrip) {
659 int ind = toStrip.lastIndexOf(SEPARATOR_CHAR);
660 if (ind < 0)
661 return toStrip;
662
663 toStrip = toStrip.substring(ind + 1);
664 // This line must be separate from above
665 // It removes spaces from the string returned by the above statement
666 toStrip = toStrip.trim();
667
668 return toStrip;
669 }
670
671 /**
672 * Returns the part of the given string that is before a colon, or null if
673 * the given String has no colon
674 *
675 * @param toStrip
676 * The String to strip
677 * @return The part of the String that is before a colon if there is one, or
678 * null if there is no colon
679 */
680 public static String stripAttribute(String toStrip) {
681 int ind = toStrip.indexOf(SEPARATOR_CHAR);
682 if (ind < 0)
683 return null;
684
685 toStrip = toStrip.substring(0, ind);
686 toStrip = toStrip.trim();
687
688 if (toStrip.length() == 0)
689 return null;
690
691 return toStrip;
692 }
693
694 public static void setSingleValue(Text text, String value) {
695 assert (value != null);
696
697 String oldText = text.getTextNoList();
698 String attribute = stripAttribute(oldText);
699
700 if (attribute == null)
701 attribute = text.getTextNoList().trim();
702
703 text.setText(attribute + SEPARATOR_STRING + value);
704 }
705
706 public static Double getDoubleValue(String attributeValuePair) {
707 String value = stripValue(attributeValuePair);
708
709 if (value == null)
710 return null;
711
712 try {
713 return Double.parseDouble(value);
714 } catch (Exception e) {
715 }
716 return null;
717 }
718}
Note: See TracBrowser for help on using the repository browser.