1 package org.glassbox.dotparser;
2
3 import java.awt.*;
4 import java.util.*;
5
6 public class DOTAttributeUtilities {
7 private DOTAttributeUtilities() {
8
9 }
10
11 /***
12 Convert the String representation of a point "X,Y" into a jawa.awt.Point.
13 @return A new point at position X,Y.
14 @exception IllegalArgumentException If the string could not be parsed.
15 */
16 public static Point point(String point) throws IllegalArgumentException {
17 try {
18 int x = Integer.parseInt(point.substring(0, point.indexOf(",")).trim());
19 int y = Integer.parseInt(point.substring(point.indexOf(",")+1).trim());
20 return new Point(x, y);
21 } catch (Exception xx) {
22 IllegalArgumentException iax = new IllegalArgumentException("Could not parse attribute value.");
23 iax.initCause(xx);
24 throw iax;
25 }
26 }
27
28 /***
29 Convert the String representation of a rectangle "X1,Y1,X2,Y2" into a java.awt.Rectangle.
30 @return A new rectangle with the specified coordinates.
31 @exception IllegalArgumentException If the string could not be parsed.
32 */
33 public static Rectangle rectangle(String points) throws IllegalArgumentException {
34 try {
35 int x1 = Integer.parseInt(points.substring(0, points.indexOf(",")).trim());
36 points = points.substring(points.indexOf(",")+1);
37 int y1 = Integer.parseInt(points.substring(0, points.indexOf(",")).trim());
38 points = points.substring(points.indexOf(",")+1);
39 int x2 = Integer.parseInt(points.substring(0, points.indexOf(",")).trim());
40 int y2 = Integer.parseInt(points.substring(points.indexOf(",")+1).trim());
41 return new Rectangle(x1, y1, x2-x1, y2-y1);
42 } catch (Exception xx) {
43 IllegalArgumentException iax = new IllegalArgumentException("Could not parse attribute value.");
44 iax.initCause(xx);
45 throw iax;
46 }
47 }
48
49 /***
50 Convert the String representation of the positions of an edge into a DOTLine.
51 @return A new DOTLine.
52 @exception IllegalArgumentException If the string could not be parsed.
53 @see DOTLine
54 */
55 public static DOTLine edge(String points) throws IllegalArgumentException {
56 try {
57 Point ps = null;
58 Point pe = null;
59 Collection cp = new LinkedList();
60
61 points = points.trim();
62 String pp1 = points.substring(0, points.indexOf(" ")).trim();
63 points = points.substring(points.indexOf(" ")).trim();
64 String pp2 = points.substring(0, points.indexOf(" ")).trim();
65 points = points.substring(points.indexOf(" ")).trim();
66
67 if (pp1.indexOf("s")==0) {
68 pp1 = pp1.substring(pp1.indexOf(",")+1);
69 ps = point(pp1);
70 if (pp2.indexOf("e")==0) {
71 pp2 = pp2.substring(pp2.indexOf(",")+1);
72 pe = point(pp2);
73 } else {
74 cp.add(point(pp2));
75 }
76 } else if (pp1.indexOf("e")==0) {
77 pp1 = pp1.substring(pp1.indexOf(",")+1);
78 pe = point(pp1);
79 if (pp2.indexOf("s")==0) {
80 pp2 = pp2.substring(pp1.indexOf(",")+1);
81 ps = point(pp2);
82 } else {
83 cp.add(point(pp2));
84 }
85 } else {
86 cp.add(point(pp1));
87 cp.add(point(pp2));
88 }
89
90 while (points.length()>0) {
91 String p;
92 if (points.indexOf(" ")>=0) {
93 p = points.substring(0, points.indexOf(" ")).trim();
94 points = points.substring(points.indexOf(" ")).trim();
95 } else {
96 p = points;
97 points = "";
98 }
99 cp.add(point(p));
100 }
101
102 return new DOTLine(ps, pe, (Point[])cp.toArray(new Point[cp.size()]));
103 } catch (Exception xx) {
104 IllegalArgumentException iax = new IllegalArgumentException("Could not parse attribute value.");
105 iax.initCause(xx);
106 throw iax;
107 }
108 }
109 }