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.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.StringWriter;
30 import java.io.Writer;
31 import java.util.List;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipOutputStream;
34
35 import org.w3c.dom.Document;
36
37 import ru.novosoft.uml.model_management.MModel;
38 import ru.novosoft.uml.xmi.IncompleteXMIException;
39 import ru.novosoft.uml.xmi.XMIWriter;
40 import th.co.edge.jseq.Diagram;
41 import th.co.edge.jseq.util.XMLUtil;
42
43
44
45
46
47
48
49
50
51
52
53
54 public class ArgoUMLDiagram implements Diagram {
55 private Document argo;
56 private MModel model;
57 private List<SequenceDiagram> diagrams;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public ArgoUMLDiagram(Document argo, MModel model,
73 List<SequenceDiagram> diagrams) {
74 this.argo = argo;
75 this.model = model;
76 this.diagrams = diagrams;
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public void save(File file) throws IOException {
90 Writer writer = null;
91 try {
92 ZipOutputStream zip =
93 new ZipOutputStream(new FileOutputStream(file));
94 writer = new OutputStreamWriter(zip);
95 writeXML(argo, "Untitled.argo", zip, writer);
96 writeModel(model, "Untitled.xmi", zip, writer);
97 int n = 0;
98 for (SequenceDiagram diagram : diagrams) {
99 String name = "SequenceDiagram" + (n++) + ".pgml";
100 writeXML(diagram.getDocument(), name, zip, writer);
101 }
102 } finally {
103 if (writer != null) {
104 try {
105 writer.close();
106 } catch (IOException e) {
107 System.err.println(e);
108 }
109 }
110 }
111 }
112
113 private void writeXML(Document xml, String name, ZipOutputStream zip,
114 Writer writer) throws IOException {
115 zip.putNextEntry(new ZipEntry(name));
116 writer.write(XMLUtil.toString(xml));
117 writer.flush();
118 zip.closeEntry();
119 }
120
121 private void writeModel(MModel mModel, String name, ZipOutputStream zip,
122 Writer writer) throws IOException {
123
124
125
126 StringWriter stringWriter = new StringWriter();
127 XMIWriter xmiWriter = new XMIWriter(mModel, stringWriter);
128 try {
129 xmiWriter.gen();
130 } catch (IncompleteXMIException e) {
131 e.printStackTrace();
132 throw new IOException("Writing XMI failed: " + e.getMessage());
133 }
134 zip.putNextEntry(new ZipEntry(name));
135 writer.write(stringWriter.toString());
136 writer.flush();
137 zip.closeEntry();
138 }
139 }