1 package coras.interchange;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
8 import java.util.Collection;
9 import java.util.Iterator;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipOutputStream;
12
13 import javax.xml.bind.JAXBContext;
14 import javax.xml.bind.JAXBException;
15 import javax.xml.bind.Marshaller;
16 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.dom.DOMResult;
20 import javax.xml.transform.dom.DOMSource;
21 import javax.xml.transform.sax.SAXResult;
22 import javax.xml.transform.sax.SAXTransformerFactory;
23 import javax.xml.transform.sax.TransformerHandler;
24 import javax.xml.transform.stream.StreamResult;
25
26 import no.sintef.file.FileSuffixFilter;
27 import no.sintef.xml.XmlHelper;
28
29 import org.w3c.dom.Node;
30 import org.xml.sax.Attributes;
31 import org.xml.sax.ContentHandler;
32 import org.xml.sax.SAXException;
33 import org.xml.sax.helpers.AttributesImpl;
34 import org.xml.sax.helpers.XMLFilterImpl;
35
36 import util.Base64;
37 import coras.client.SwingWorker;
38 import coras.common.CorasRepresentation;
39 import coras.representations.BinaryRepresentation;
40 import coras.representations.RepresentationTypeEnum;
41 import coras.representations.XmlRepresentation;
42 import coras.reuse.Experience;
43 import coras.reuse.ExperiencePackage;
44 import coras.structure.ViewpointEnum;
45 import coras.table.CorasTableModel;
46 import coras.table.jaxb.TableType;
47 import coras.types.IDocument;
48 import coras.types.IFaultTree;
49 import coras.types.ITable;
50 import coras.types.IUMLModel;
51
52 public class ExperiencePackageExporter extends SwingWorker {
53
54 private ExperiencePackage _experiencePackage = null;
55 private File exportFile = null;
56 private String backupDate = null;
57 private Collection selectedExperiences = null;
58
59 public void setExportFile(File exportFile) {
60 this.exportFile = exportFile;
61 }
62
63 public void setPackage(ExperiencePackage experiencePackage) {
64 this._experiencePackage = experiencePackage;
65 }
66
67 public void setSelectedExperiences(Collection selectedExperiences) {
68 this.selectedExperiences = selectedExperiences;
69 }
70
71 public Object construct() {
72 try {
73 backupDate = new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(Calendar.getInstance().getTime());
74 TransformerHandler th = getTransformerHandler();
75 String filename = exportFile.getName();
76 FileOutputStream fos = new FileOutputStream(exportFile);
77 ZipOutputStream zos = new ZipOutputStream(fos);
78 ZipEntry entry = new ZipEntry(FileSuffixFilter.getPrefix(filename) + ".ci");
79 zos.putNextEntry(entry);
80 StreamResult sr = new StreamResult(zos);
81 th.setResult(sr);
82 th.startDocument();
83 exportPackage(_experiencePackage, th);
84 th.endDocument();
85 zos.closeEntry();
86 zos.close();
87 return null;
88 } catch (SAXException e) {
89 return e;
90 } catch (IOException e) {
91 return e;
92 }
93 }
94
95 private void exportPackage(ExperiencePackage experiencePackage, ContentHandler ch) throws SAXException {
96 Collection experiences = selectedExperiences;
97 if (experiences == null) {
98 experiences = experiencePackage.getExperiences(null);
99 }
100 setTaskLength(experiences.size() + 1);
101 int taskNum = 0;
102
103 setJobName("Exporting Experience Package: " + experiencePackage.getName());
104 setCurrentTask("Exporting Experience Package: " + experiencePackage.getName());
105
106 AttributesImpl atts = new AttributesImpl();
107 addAttribute(atts, Constants.BACKUPDATE_ATTR, backupDate);
108 addAttribute(atts, Constants.ID_ATTR, experiencePackage.getId());
109 addAttribute(atts, Constants.NAME_ATTR, experiencePackage.getName());
110 addAttribute(atts, Constants.CATEGORY_ATTR, experiencePackage.getCategory());
111 atts.addAttribute("", Constants.PREFIX, "xmlns:" + Constants.PREFIX, Constants.CDATA_TYPE, Constants.CORAS_V2_NAMESPACE_URI);
112 startElement(ch, Constants.EXPERIENCE_PACKAGE_ELEM, atts);
113
114 startElement(ch, Constants.METADATA_ELEM);
115 outputTextElement(ch, Constants.FULL_DESCRIPTION_ELEM, experiencePackage.getFullDescription());
116 outputTextElement(ch, Constants.SHORT_DESCRIPTION_ELEM, experiencePackage.getShortDescription());
117 outputTextElement(ch, Constants.DOMAIN_ELEM, experiencePackage.getDomain());
118 endElement(ch, Constants.METADATA_ELEM);
119
120 setProgress(++taskNum);
121 for (Iterator i = experiences.iterator(); i.hasNext();) {
122 if (Thread.currentThread().isInterrupted()) {
123 setInterrupted(true);
124 return;
125 }
126 Experience experience = (Experience) i.next();
127 if (!experience.isDeleted()) {
128 exportExperience(experience, ch);
129 }
130 setProgress(++taskNum);
131 }
132 endElement(ch, Constants.EXPERIENCE_PACKAGE_ELEM);
133 }
134
135 private void exportExperience(Experience experience, ContentHandler ch) throws SAXException {
136 setCurrentTask("Exporting " + experience.getType() + ": " + experience.getName());
137
138 AttributesImpl atts = new AttributesImpl();
139 addAttribute(atts, Constants.BACKUPDATE_ATTR, backupDate);
140 addAttribute(atts, Constants.ID_ATTR, experience.getId());
141 addAttribute(atts, Constants.NAME_ATTR, experience.getName());
142 addAttribute(atts, Constants.CATEGORY_ATTR, experience.getCategory());
143 addAttribute(atts, Constants.TYPE_ATTR, experience.getType());
144 addAttribute(atts, Constants.SUBTYPE_ATTR, experience.getSubtype());
145 startElement(ch, Constants.EXPERIENCE_ELEM, atts);
146
147 startElement(ch, Constants.METADATA_ELEM);
148 outputTextElement(ch, Constants.FULL_DESCRIPTION_ELEM, experience.getFullDescription());
149 outputTextElement(ch, Constants.SHORT_DESCRIPTION_ELEM, experience.getShortDescription());
150 outputTextElement(ch, Constants.DOMAIN_ELEM, experience.getDomain());
151 outputTextElement(ch, Constants.CONCERN_ELEM, experience.getConcern());
152 startElement(ch, Constants.VIEWPOINTS_ELEM);
153 ViewpointEnum[] viewpoints = experience.getViewpoints();
154 if (viewpoints != null) {
155 for (int i = 0; i < viewpoints.length; i++) {
156 ViewpointEnum viewpoint = viewpoints[i];
157 outputTextElement(ch, Constants.VIEWPOINT_ELEM, viewpoint);
158 }
159 }
160 endElement(ch, Constants.VIEWPOINTS_ELEM);
161 endElement(ch, Constants.METADATA_ELEM);
162
163 startElement(ch, Constants.REPRESENTATIONS_ELEM);
164 if (experience instanceof IDocument) {
165 IDocument doc = (IDocument) experience;
166 exportRepresentation(ch, doc.getDocumentContent());
167 } else if (experience instanceof IFaultTree) {
168 IFaultTree ft = (IFaultTree) experience;
169 exportRepresentation(ch, ft.getFaultTreeModel());
170 exportRepresentation(ch, ft.getFaultTreeDiagram());
171 } else if (experience instanceof ITable) {
172 ITable table = (ITable) experience;
173 CorasTableModel tableModel = table.getTableModel();
174 try {
175 Marshaller m = JAXBContext.newInstance("coras.table.jaxb").createMarshaller();
176 TableType t = tableModel.getTable();
177 DOMResult r = new DOMResult();
178 m.marshal(t, r);
179 Node n = r.getNode();
180 exportRepresentation(ch, RepresentationTypeEnum.TABLE, t.getType().replaceAll(" ", "_") + ".xml", n);
181 } catch (JAXBException e) {
182
183 e.printStackTrace();
184 }
185 } else if (experience instanceof IUMLModel) {
186 IUMLModel uml = (IUMLModel) experience;
187 exportRepresentation(ch, uml.getUMLModel());
188 exportRepresentation(ch, uml.getUMLDiagram());
189 }
190 endElement(ch, Constants.REPRESENTATIONS_ELEM);
191
192 endElement(ch, Constants.EXPERIENCE_ELEM);
193 }
194
195 private void exportRepresentation(ContentHandler ch, CorasRepresentation rep) throws SAXException {
196 Object content;
197 if (rep instanceof XmlRepresentation) {
198 content = ((XmlRepresentation)rep).getXmlContent();
199 } else if (rep instanceof BinaryRepresentation) {
200 content = ((BinaryRepresentation)rep).getBinaryContent();
201 } else {
202 return;
203 }
204 exportRepresentation(ch, rep.getType(), rep.getFilename(), content);
205 }
206
207 private void exportRepresentation(ContentHandler ch, RepresentationTypeEnum type, String filename, Object content) throws SAXException {
208 AttributesImpl atts = new AttributesImpl();
209 addAttribute(atts, Constants.TYPE_ATTR, type.toString());
210 addAttribute(atts, Constants.NAME_ATTR, filename);
211 addAttribute(atts, Constants.FORMAT_ATTR, content instanceof byte[] ? Constants.BINARY_FORMAT : Constants.XML_FORMAT);
212 startElement(ch, Constants.REPRESENTATION_ELEM, atts);
213
214 startElement(ch, Constants.CONTENT_ELEM);
215 if (content instanceof Node) {
216 try {
217 XMLFilterImpl filter = new XMLFilterImpl() {
218 public void endDocument() throws SAXException {
219
220 }
221 public void startDocument() throws SAXException {
222
223 }
224 };
225 filter.setContentHandler(ch);
226 Node n = (Node) content;
227 Transformer t = XmlHelper.getIdentityTransformation();
228 DOMSource source = new DOMSource(n);
229 SAXResult result = new SAXResult(filter);
230 t.transform(source, result);
231 } catch (TransformerException e) {
232 e.printStackTrace();
233 }
234 } else if (content instanceof byte[]) {
235 byte[] bytes = (byte[]) content;
236 String s = Base64.encodeBytes(bytes);
237 ch.characters(s.toCharArray(), 0, s.length());
238 }
239 endElement(ch, Constants.CONTENT_ELEM);
240
241 endElement(ch, Constants.REPRESENTATION_ELEM);
242 }
243
244 private static final void startElement(ContentHandler ch, String localName) throws SAXException {
245 startElement(ch, localName, null);
246 }
247
248 private static final void startElement(ContentHandler ch, String localName, Attributes atts) throws SAXException {
249 if (atts == null) {
250 atts = Constants.EMPTY_ATTRIBUTES;
251 }
252 ch.startElement(Constants.CORAS_V2_NAMESPACE_URI, localName, Constants.PREFIX + ":" + localName, atts);
253 }
254
255 private static final void endElement(ContentHandler ch, String localName) throws SAXException {
256 ch.endElement(Constants.CORAS_V2_NAMESPACE_URI, localName, Constants.PREFIX + ":" + localName);
257 }
258
259 private static final void addAttribute(AttributesImpl atts, String name, Object value) {
260 if (value != null) {
261 atts.addAttribute("", "", name, Constants.CDATA_TYPE, value.toString());
262 }
263 }
264
265 private static void outputTextElement(ContentHandler ch, String localName, Object value) throws SAXException {
266 startElement(ch, localName);
267 String text = value != null ? value.toString() : null;
268 if (text != null) {
269 char[] chars = new char[text.length()];
270 text.getChars(0, text.length(), chars, 0);
271 ch.characters(chars, 0, chars.length);
272 }
273 endElement(ch, localName);
274 }
275
276 private static TransformerHandler getTransformerHandler() {
277 try {
278 TransformerFactory factory = TransformerFactory.newInstance();
279 if (factory.getFeature(SAXTransformerFactory.FEATURE)) {
280 SAXTransformerFactory saxFactory = (SAXTransformerFactory)factory;
281 return saxFactory.newTransformerHandler();
282 } else {
283
284 return null;
285 }
286 } catch (Exception e) {
287
288 return null;
289 }
290 }
291
292 }
293