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.argouml;
24  
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  import org.w3c.dom.DOMImplementation;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.DocumentType;
35  import org.w3c.dom.Element;
36  
37  import ru.novosoft.uml.model_management.MModel;
38  
39  import th.co.edge.jseq.Activation;
40  import th.co.edge.jseq.ActivationList;
41  import th.co.edge.jseq.Diagram;
42  import th.co.edge.jseq.argouml.util.MModelUtil;
43  
44  /**
45   * An <code>ArgoUMLGenerator</code> is used to generate
46   * <code>ArgoUMLDiagram</code>s from an <code>ActivationList</code>
47   * representing a number of root activations.
48   */
49  public class ArgoUMLGenerator {
50      private static final String ARGO_PUBLIC_ID = null;
51      private static final String ARGO_SYSTEM_ID = "argo.dtd";
52      private static final String ARGO_NAMESPACE = null;
53      private static final String ARGOUML_VERSION = "0.12";
54  
55      private DocumentBuilder builder;
56      private List<SequenceDiagram> sequenceDiagrams =
57              new LinkedList<SequenceDiagram>();
58  
59      /**
60       * Creates a new <code>ArgoUMLGenerator</code>.
61       *
62       * @throws ParserConfigurationException
63       *             if there is some serious error in the XML configuration
64       *             (should normally not occur)
65       */
66      public ArgoUMLGenerator() throws ParserConfigurationException {
67          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
68          this.builder = factory.newDocumentBuilder();
69      }
70  
71      /**
72       * Returns a new <code>ArgoUMLDiagram</code> representing the method calls
73       * in the given <code>AcivationList</code>.
74       *
75       * @param activationList
76       *            the root activations to use to create the sequence diagrams
77       *
78       * @return a new <code>ArgoUMLDiagram</code> representing the root
79       *         activations in <code>activationList</code>
80       *
81       * @throws ParserConfigurationException
82       *             if there is some serious error in the XML configuration
83       *             (should normally not occur)
84       */
85      public Diagram generate(ActivationList activationList)
86              throws ParserConfigurationException {
87          MModel model = MModelUtil.createMModel("untitledModel");
88          for (Activation activation : activationList) {
89              SequenceDiagram sequenceDiagram =
90                      MModelUtil.addSequenceDiagram(model, activation);
91              sequenceDiagrams.add(sequenceDiagram);
92          }
93          Document argo = createArgoDocument(sequenceDiagrams);
94          return new ArgoUMLDiagram(argo, model, sequenceDiagrams);
95      }
96  
97      private Document createArgoDocument(List<SequenceDiagram> diagrams) {
98          DOMImplementation impl = builder.getDOMImplementation();
99          DocumentType docType =
100                 impl.createDocumentType("argo", ARGO_PUBLIC_ID, ARGO_SYSTEM_ID);
101         Document doc = impl.createDocument(ARGO_NAMESPACE, "argo", docType);
102         Element root = doc.getDocumentElement();
103         root.appendChild(createDocumentationNode(doc, ARGOUML_VERSION));
104         root.appendChild(createMemberNode(doc, "Untitled.xmi", "xmi"));
105         for (int i = 0; i < diagrams.size(); i++) {
106             String name = "SequenceDiagram" + (i++) + ".pgml";
107             root.appendChild(createMemberNode(doc, name, "pgml"));
108         }
109         return doc;
110     }
111 
112     private Element createDocumentationNode(Document doc, String version) {
113         Element documentationNode = doc.createElement("documentation");
114         Element versionNode = doc.createElement("version");
115         documentationNode.appendChild(versionNode);
116         versionNode.appendChild(doc.createTextNode(version));
117         return documentationNode;
118     }
119 
120     private Element createMemberNode(Document doc, String name, String type) {
121         Element member = doc.createElement("member");
122         member.setAttribute("type", type);
123         member.setAttribute("name", name);
124         return member;
125     }
126 }