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.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 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class ArgoUMLGenerator { |
50 | 1 | private static final String ARGO_PUBLIC_ID = null; |
51 | |
private static final String ARGO_SYSTEM_ID = "argo.dtd"; |
52 | 1 | private static final String ARGO_NAMESPACE = null; |
53 | |
private static final String ARGOUML_VERSION = "0.12"; |
54 | |
|
55 | |
private DocumentBuilder builder; |
56 | 1 | private List<SequenceDiagram> sequenceDiagrams = |
57 | |
new LinkedList<SequenceDiagram>(); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 1 | public ArgoUMLGenerator() throws ParserConfigurationException { |
67 | 1 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
68 | 1 | this.builder = factory.newDocumentBuilder(); |
69 | 1 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Diagram generate(ActivationList activationList) |
86 | |
throws ParserConfigurationException { |
87 | 1 | MModel model = MModelUtil.createMModel("untitledModel"); |
88 | 1 | for (Activation activation : activationList) { |
89 | 1 | SequenceDiagram sequenceDiagram = |
90 | |
MModelUtil.addSequenceDiagram(model, activation); |
91 | 1 | sequenceDiagrams.add(sequenceDiagram); |
92 | 1 | } |
93 | 1 | Document argo = createArgoDocument(sequenceDiagrams); |
94 | 1 | return new ArgoUMLDiagram(argo, model, sequenceDiagrams); |
95 | |
} |
96 | |
|
97 | |
private Document createArgoDocument(List<SequenceDiagram> diagrams) { |
98 | 1 | DOMImplementation impl = builder.getDOMImplementation(); |
99 | 1 | DocumentType docType = |
100 | |
impl.createDocumentType("argo", ARGO_PUBLIC_ID, ARGO_SYSTEM_ID); |
101 | 1 | Document doc = impl.createDocument(ARGO_NAMESPACE, "argo", docType); |
102 | 1 | Element root = doc.getDocumentElement(); |
103 | 1 | root.appendChild(createDocumentationNode(doc, ARGOUML_VERSION)); |
104 | 1 | root.appendChild(createMemberNode(doc, "Untitled.xmi", "xmi")); |
105 | 2 | for (int i = 0; i < diagrams.size(); i++) { |
106 | 1 | String name = "SequenceDiagram" + (i++) + ".pgml"; |
107 | 1 | root.appendChild(createMemberNode(doc, name, "pgml")); |
108 | |
} |
109 | 1 | return doc; |
110 | |
} |
111 | |
|
112 | |
private Element createDocumentationNode(Document doc, String version) { |
113 | 1 | Element documentationNode = doc.createElement("documentation"); |
114 | 1 | Element versionNode = doc.createElement("version"); |
115 | 1 | documentationNode.appendChild(versionNode); |
116 | 1 | versionNode.appendChild(doc.createTextNode(version)); |
117 | 1 | return documentationNode; |
118 | |
} |
119 | |
|
120 | |
private Element createMemberNode(Document doc, String name, String type) { |
121 | 2 | Element member = doc.createElement("member"); |
122 | 2 | member.setAttribute("type", type); |
123 | 2 | member.setAttribute("name", name); |
124 | 2 | return member; |
125 | |
} |
126 | |
} |