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

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

Added @b and @v...
Also changed @f... so that images can be displayed with transparent backgrounds.
Did a bunch of refactoring in the process to remove duplicated code and simplify managing @i, @f and @b.

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