1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package th.co.edge.jseq.sdedit;
24
25 import java.io.File;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import th.co.edge.jseq.Activation;
33 import th.co.edge.jseq.ActivationList;
34 import th.co.edge.jseq.Diagram;
35
36
37
38
39
40
41
42 public class SdeditTextDiagram implements Diagram {
43 private static final String NEW_LINE = "\n";
44 private ActivationList activationList;
45
46
47
48
49
50
51
52
53
54 public SdeditTextDiagram(ActivationList activationList) {
55 this.activationList = activationList;
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69 @Override
70 public void save(File file) throws IOException {
71 FileWriter writer = null;
72 try {
73 writer = new FileWriter(file);
74 writeDiagram(writer);
75 } finally {
76 if (writer != null) {
77 writer.close();
78 }
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 void writeDiagram(Writer writer) throws IOException {
96 writeActorsAndObjectNames(writer);
97 writer.write(NEW_LINE);
98
99 int index = 1;
100 for (Activation activation : activationList) {
101 writeActivation(activation, index, writer);
102 writer.write(NEW_LINE);
103 index++;
104 }
105 }
106
107 private void writeActorsAndObjectNames(Writer writer) throws IOException {
108 int index = 1;
109 for (Activation activation : activationList) {
110 writer.write("Actor" + index + ":Actor");
111 writer.write(NEW_LINE);
112 writeObjectNames(activation, writer, index, new ArrayList<String>());
113 index++;
114 }
115 }
116
117 private void writeObjectNames(Activation activation, Writer writer,
118 int index, List<String> objectNames) throws IOException {
119 String objectName = getObjectName(activation, index);
120 if (!objectNames.contains(objectName)) {
121 objectNames.add(objectName);
122 writer.write(objectName);
123 writer.write(":");
124 writer.write(getClassName(activation));
125 writer.write("[a]");
126 writer.write(NEW_LINE);
127 }
128 for (Activation nestedActivation : activation.getNestedActivations()) {
129 writeObjectNames(nestedActivation, writer, index, objectNames);
130 }
131 }
132
133 private void writeActivation(Activation activation, int index, Writer writer)
134 throws IOException {
135 writer.write("Actor" + index);
136 writer.write(":");
137 writer.write(getObjectName(activation, index));
138 writer.write(".");
139 writer.write(getMethodName(activation));
140 writer.write(NEW_LINE);
141 for (Activation nestedActivation : activation.getNestedActivations()) {
142 writeActivation(nestedActivation, index, 1, writer);
143 }
144 writer.write(getObjectName(activation, index) + ":stop");
145 }
146
147 private void writeActivation(Activation activation, int index,
148 int nestingLevel, Writer writer) throws IOException {
149 indent(writer, nestingLevel);
150 writer.write(getObjectName(activation.getParent(), index));
151 writer.write(":");
152 writer.write(getObjectName(activation, index));
153 writer.write(".");
154 writer.write(getMethodName(activation));
155 writer.write(NEW_LINE);
156
157 for (Activation nestedActivation : activation.getNestedActivations()) {
158 writeActivation(nestedActivation, index, nestingLevel + 1, writer);
159 }
160 }
161
162 private void indent(Writer writer, int depth) throws IOException {
163 for (int i = 0; i < depth; i++) {
164 writer.write(" ");
165 }
166 }
167
168 private String getObjectName(Activation activation, int index) {
169 return getClassName(activation).toLowerCase() + index;
170 }
171
172 private String getClassName(Activation activation) {
173 String qualifiedClassName = activation.getClassName();
174 int lastDot = qualifiedClassName.lastIndexOf('.');
175 String result = qualifiedClassName.substring(lastDot + 1);
176 if (result.contains("$")) {
177 int lastInnerClassSeperator = result.lastIndexOf('$');
178 result = result.substring(lastInnerClassSeperator + 1);
179 }
180 return result;
181 }
182
183 private String getMethodName(Activation activation) {
184 String methodName = activation.getMethod().name();
185 return methodName;
186 }
187
188
189
190
191
192
193
194
195
196 @Override
197 public String toString() {
198 return activationList.toString();
199 }
200 }