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;
24
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import th.co.edge.jseq.argouml.ArgoUMLGenerator;
32 import th.co.edge.jseq.sdedit.SdeditPngDiagram;
33 import th.co.edge.jseq.sdedit.SdeditTextDiagram;
34 import th.co.edge.jseq.svg.SVGGenerator;
35
36
37
38
39 public class FormatterRegistry {
40 private static FormatterRegistry instance = new FormatterRegistry();
41
42 private Map<String, Formatter> formatters = new HashMap<String, Formatter>();
43
44 private FormatterRegistry() {
45 formatters.put("text", new TextFormatter());
46 formatters.put("png", new SdeditPngFormatter());
47 formatters.put("sdedit", new SdeditTextFormatter());
48 formatters.put("svg", new SVGFormatter());
49 formatters.put("argouml", new ArgoUMLFormatter());
50 }
51
52
53
54
55
56
57
58
59
60
61 public static FormatterRegistry getInstance() {
62 return instance;
63 }
64
65
66
67
68
69
70
71
72
73 public Formatter get(String type) {
74 Formatter formatter = formatters.get(type);
75 if (formatter == null) {
76 throw new IllegalArgumentException("Illegal type: " + type
77 + ". Should be one of " + getFormatterTypes());
78 }
79 return formatter;
80 }
81
82
83
84
85
86
87
88
89 public String getFormatterTypes() {
90 StringBuffer s = new StringBuffer();
91 for (Iterator<String> i = formatters.keySet().iterator(); i.hasNext();) {
92 s.append(i.next());
93 if (i.hasNext()) {
94 s.append(", ");
95 }
96 }
97 return s.toString();
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public void register(String type, Formatter formatter) {
111 formatters.put(type, formatter);
112 }
113
114
115
116
117
118 private static class TextFormatter implements Formatter {
119 public Diagram format(ActivationList activationList) {
120 return new TextDiagram(activationList.toString());
121 }
122 }
123
124 private static class SdeditPngFormatter implements Formatter {
125 public Diagram format(ActivationList activationList)
126 throws FormatException {
127 return new SdeditPngDiagram(activationList);
128 }
129 }
130
131 private static class SdeditTextFormatter implements Formatter {
132 public Diagram format(ActivationList activationlist)
133 throws FormatException {
134 return new SdeditTextDiagram(activationlist);
135 }
136 }
137
138 private static class SVGFormatter implements Formatter {
139 public Diagram format(ActivationList activationList)
140 throws FormatException {
141 Diagram diagram;
142 try {
143 SVGGenerator svgGenerator = new SVGGenerator();
144 diagram = svgGenerator.generate(activationList);
145 } catch (ParserConfigurationException e) {
146 throw new FormatException("Failed to create diagram", e);
147 }
148 return diagram;
149 }
150 }
151
152 private static class ArgoUMLFormatter implements Formatter {
153 public Diagram format(ActivationList activationList)
154 throws FormatException {
155 Diagram diagram;
156 try {
157 ArgoUMLGenerator generator = new ArgoUMLGenerator();
158 diagram = generator.generate(activationList);
159 } catch (ParserConfigurationException e) {
160 throw new FormatException("Failed to create diagram", e);
161 }
162 return diagram;
163 }
164 }
165 }