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