View Javadoc

1   /*
2    * Copyright (c) 2003-2008, by Henrik Arro and Contributors
3    *
4    * This file is part of JSeq, a tool to automatically create
5    * sequence diagrams by tracing program execution.
6    *
7    * See <http://jseq.sourceforge.net> for more information.
8    *
9    * JSeq is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of
12   * the License, or (at your option) any later version.
13   *
14   * JSeq is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public License
20   * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
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   * A singleton that holds all <code>Formatter</code>s known to the system.
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       * Returns the only instance of this singleton. By default, it contains
54       * <code>Formatter</code>s for some commonly used formats, e.g., text and
55       * SVG, but more can be added using the <code>register</code> method.
56       *
57       * @return the only instance of this singleton
58       *
59       * @see #register(String, Formatter)
60       */
61      public static FormatterRegistry getInstance() {
62          return instance;
63      }
64  
65      /**
66       * Returns the <code>Formatter</code> associated with the given name.
67       *
68       * @param type
69       *            the name of the <code>Formatter</code> to look up
70       *
71       * @return the <code>Formatter</code> associated with <code>type</code>
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       * Returns a comma-separated list with the names of the formatters that have
84       * been registered.
85       *
86       * @return a comma-separated list with names of all formatters known to the
87       *         system
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      * Registers a new <code>Formatter</code> so that it can be used
102      *
103      * @param type
104      *            the name of the <code>Formatter</code>
105      *
106      * @param formatter
107      *            the <code>Formatter</code> to associate with
108      *            <code>type</code>
109      */
110     public void register(String type, Formatter formatter) {
111         formatters.put(type, formatter);
112     }
113 
114     //
115     // Nested top-level classes
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 }