|
1 |
| -/* |
2 |
| - * To change this template, choose Tools | Templates |
3 |
| - * and open the template in the editor. |
4 |
| - */ |
5 |
| -package cz.zcu.kiv.eegdatabase.logic.zip; |
6 |
| - |
7 |
| -/** |
8 |
| - * |
9 |
| - * @author Jan Štěbeták |
10 |
| - */ |
11 |
| - |
12 |
| -import java.io.*; |
13 |
| -import java.sql.Blob; |
14 |
| -import java.util.Set; |
15 |
| -import java.util.zip.ZipEntry; |
16 |
| -import java.util.zip.ZipOutputStream; |
17 |
| - |
18 |
| -import cz.zcu.kiv.eegdatabase.data.pojo.DataFile; |
19 |
| -import cz.zcu.kiv.eegdatabase.data.pojo.Experiment; |
20 |
| -import cz.zcu.kiv.eegdatabase.data.pojo.IScenarioType; |
21 |
| -import cz.zcu.kiv.eegdatabase.data.pojo.Scenario; |
22 |
| - |
23 |
| -import java.sql.SQLException; |
24 |
| -import javax.xml.transform.Result; |
25 |
| -import javax.xml.transform.Source; |
26 |
| -import javax.xml.transform.Transformer; |
27 |
| -import javax.xml.transform.TransformerFactory; |
28 |
| -import javax.xml.transform.dom.DOMSource; |
29 |
| -import javax.xml.transform.stream.StreamResult; |
30 |
| - |
31 |
| -import cz.zcu.kiv.eegdatabase.logic.controller.experiment.MetadataCommand; |
32 |
| -import cz.zcu.kiv.eegdatabase.logic.xml.DataTransformer; |
33 |
| - |
34 |
| -import org.apache.commons.logging.Log; |
35 |
| -import org.apache.commons.logging.LogFactory; |
36 |
| -import org.w3c.dom.Document; |
37 |
| - |
38 |
| -public class ZipGenerator implements Generator { |
39 |
| - |
40 |
| - protected DataTransformer transformer; |
41 |
| - protected String metadata; |
42 |
| - protected String dir; |
43 |
| - protected String dataZip; |
44 |
| - private Log log = LogFactory.getLog(getClass()); |
45 |
| - |
46 |
| - public OutputStream generate(Experiment exp, MetadataCommand mc, Set<DataFile> dataFiles) throws Exception, SQLException, IOException { |
47 |
| - ZipOutputStream zos = null; |
48 |
| - ByteArrayOutputStream baos = null; |
49 |
| - try { |
50 |
| - log.debug("creating output stream"); |
51 |
| - baos = new ByteArrayOutputStream(); |
52 |
| - zos = new ZipOutputStream(baos); |
53 |
| - |
54 |
| - log.debug("transforming metadata from database to xml file"); |
55 |
| - OutputStream meta = getTransformer().transform(exp, mc, dataFiles); |
56 |
| - Scenario scen = exp.getScenario(); |
57 |
| - log.debug("getting scenario file"); |
58 |
| - |
59 |
| - IScenarioType scenFile = scen.getScenarioType(); |
60 |
| - |
61 |
| - byte[] xmlMetadata = null; |
62 |
| - if (meta instanceof ByteArrayOutputStream) { |
63 |
| - xmlMetadata = ((ByteArrayOutputStream) meta).toByteArray(); |
64 |
| - } |
65 |
| - byte[] scenario; |
66 |
| - ZipEntry e; |
67 |
| - |
68 |
| - if (mc.isScenFile()) { |
69 |
| - try { |
70 |
| - scenario = toByteArray(scenFile.getScenarioXml()); |
71 |
| - log.debug("saving scenario file (" + scen.getScenarioName() + ") into a zip file"); |
72 |
| - e = new ZipEntry("Scenario/" + scen.getScenarioName()); |
73 |
| - zos.putNextEntry(e); |
74 |
| - zos.write(scenario); |
75 |
| - zos.closeEntry(); |
76 |
| - } catch (Exception ex) { |
77 |
| - ex.printStackTrace(); |
78 |
| - } |
79 |
| - } |
80 |
| - if (xmlMetadata != null) { |
81 |
| - log.debug("saving xml file of metadata to zip file"); |
82 |
| - e = new ZipEntry(getMetadata() + ".xml"); |
83 |
| - zos.putNextEntry(e); |
84 |
| - zos.write(xmlMetadata); |
85 |
| - zos.closeEntry(); |
86 |
| - } |
87 |
| - |
88 |
| - for (DataFile d : dataFiles) { |
89 |
| - e = new ZipEntry(getDataZip() + "/" + d.getFilename()); |
90 |
| - Blob blob = d.getFileContent(); |
91 |
| - if (blob != null) { |
92 |
| - byte[] pole = blob.getBytes(1, (int) blob.length()); |
93 |
| - log.debug("saving data file to zip file"); |
94 |
| - zos.putNextEntry(e); |
95 |
| - zos.write(pole); |
96 |
| - zos.closeEntry(); |
97 |
| - } |
98 |
| - } |
99 |
| - log.debug("returning output stream of zip file"); |
100 |
| - |
101 |
| - } finally { |
102 |
| - zos.flush(); |
103 |
| - zos.close(); |
104 |
| - |
105 |
| - } |
106 |
| - return baos; |
107 |
| - } |
108 |
| - |
109 |
| - private byte[] toByteArray(Object o) throws Exception { |
110 |
| - if (o instanceof Blob) { |
111 |
| - return ((Blob) o).getBytes(1, (int) ((Blob) o).length()); |
112 |
| - } else if (o instanceof Document) { |
113 |
| - Source source = new DOMSource((Document) o); |
114 |
| - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
115 |
| - Result result = new StreamResult(out); |
116 |
| - TransformerFactory factory = TransformerFactory.newInstance(); |
117 |
| - Transformer transformer = factory.newTransformer(); |
118 |
| - transformer.transform(source, result); |
119 |
| - |
120 |
| - return out.toByteArray(); |
121 |
| - } |
122 |
| - |
123 |
| - return null; |
124 |
| - } |
125 |
| - |
126 |
| - /** |
127 |
| - * @return the transformer |
128 |
| - */ |
129 |
| - public DataTransformer getTransformer() { |
130 |
| - return transformer; |
131 |
| - } |
132 |
| - |
133 |
| - /** |
134 |
| - * @param transformer the transformer to set |
135 |
| - */ |
136 |
| - public void setTransformer(DataTransformer transformer) { |
137 |
| - this.transformer = transformer; |
138 |
| - } |
139 |
| - |
140 |
| - /** |
141 |
| - * @return the metadata |
142 |
| - */ |
143 |
| - public String getMetadata() { |
144 |
| - return metadata; |
145 |
| - } |
146 |
| - |
147 |
| - /** |
148 |
| - * @param metadata the metadata to set |
149 |
| - */ |
150 |
| - public void setMetadata(String metadata) { |
151 |
| - this.metadata = metadata; |
152 |
| - } |
153 |
| - |
154 |
| - /** |
155 |
| - * @return the dataZip |
156 |
| - */ |
157 |
| - public String getDataZip() { |
158 |
| - return dataZip; |
159 |
| - } |
160 |
| - |
161 |
| - /** |
162 |
| - * @param dataZip the dataZip to set |
163 |
| - */ |
164 |
| - public void setDataZip(String dataZip) { |
165 |
| - this.dataZip = dataZip; |
166 |
| - } |
167 |
| - |
168 |
| - @Override |
169 |
| - public String toString() { |
170 |
| - return "metadata: " + metadata; |
171 |
| - } |
172 |
| -} |
173 |
| - |
| 1 | +/* |
| 2 | + * To change this template, choose Tools | Templates |
| 3 | + * and open the template in the editor. |
| 4 | + */ |
| 5 | +package cz.zcu.kiv.eegdatabase.logic.zip; |
| 6 | + |
| 7 | +/** |
| 8 | + * |
| 9 | + * @author Jan Štěbeták |
| 10 | + */ |
| 11 | + |
| 12 | +import cz.zcu.kiv.eegdatabase.data.pojo.DataFile; |
| 13 | +import cz.zcu.kiv.eegdatabase.data.pojo.Experiment; |
| 14 | +import cz.zcu.kiv.eegdatabase.data.pojo.IScenarioType; |
| 15 | +import cz.zcu.kiv.eegdatabase.data.pojo.Scenario; |
| 16 | +import cz.zcu.kiv.eegdatabase.logic.controller.experiment.MetadataCommand; |
| 17 | +import cz.zcu.kiv.eegdatabase.logic.xml.DataTransformer; |
| 18 | +import org.apache.commons.logging.Log; |
| 19 | +import org.apache.commons.logging.LogFactory; |
| 20 | +import org.w3c.dom.Document; |
| 21 | + |
| 22 | +import javax.xml.transform.Result; |
| 23 | +import javax.xml.transform.Source; |
| 24 | +import javax.xml.transform.Transformer; |
| 25 | +import javax.xml.transform.TransformerFactory; |
| 26 | +import javax.xml.transform.dom.DOMSource; |
| 27 | +import javax.xml.transform.stream.StreamResult; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.sql.Blob; |
| 32 | +import java.sql.SQLException; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.zip.ZipEntry; |
| 35 | +import java.util.zip.ZipException; |
| 36 | +import java.util.zip.ZipOutputStream; |
| 37 | + |
| 38 | +public class ZipGenerator implements Generator { |
| 39 | + |
| 40 | + protected DataTransformer transformer; |
| 41 | + protected String metadata; |
| 42 | + protected String dir; |
| 43 | + protected String dataZip; |
| 44 | + private Log log = LogFactory.getLog(getClass()); |
| 45 | + private int fileCounter = 0; |
| 46 | + |
| 47 | + public OutputStream generate(Experiment exp, MetadataCommand mc, Set<DataFile> dataFiles) throws Exception, SQLException, IOException { |
| 48 | + ZipOutputStream zos = null; |
| 49 | + ByteArrayOutputStream baos = null; |
| 50 | + try { |
| 51 | + log.debug("creating output stream"); |
| 52 | + baos = new ByteArrayOutputStream(); |
| 53 | + zos = new ZipOutputStream(baos); |
| 54 | + |
| 55 | + log.debug("transforming metadata from database to xml file"); |
| 56 | + OutputStream meta = getTransformer().transform(exp, mc, dataFiles); |
| 57 | + Scenario scen = exp.getScenario(); |
| 58 | + log.debug("getting scenario file"); |
| 59 | + |
| 60 | + IScenarioType scenFile = scen.getScenarioType(); |
| 61 | + |
| 62 | + byte[] xmlMetadata = null; |
| 63 | + if (meta instanceof ByteArrayOutputStream) { |
| 64 | + xmlMetadata = ((ByteArrayOutputStream) meta).toByteArray(); |
| 65 | + } |
| 66 | + byte[] scenario; |
| 67 | + ZipEntry e; |
| 68 | + |
| 69 | + if (mc.isScenFile()) { |
| 70 | + try { |
| 71 | + scenario = toByteArray(scenFile.getScenarioXml()); |
| 72 | + log.debug("saving scenario file (" + scen.getScenarioName() + ") into a zip file"); |
| 73 | + e = new ZipEntry("Scenario/" + scen.getScenarioName()); |
| 74 | + zos.putNextEntry(e); |
| 75 | + zos.write(scenario); |
| 76 | + zos.closeEntry(); |
| 77 | + } catch (Exception ex) { |
| 78 | + ex.printStackTrace(); |
| 79 | + } |
| 80 | + } |
| 81 | + if (xmlMetadata != null) { |
| 82 | + log.debug("saving xml file of metadata to zip file"); |
| 83 | + e = new ZipEntry(getMetadata() + ".xml"); |
| 84 | + zos.putNextEntry(e); |
| 85 | + zos.write(xmlMetadata); |
| 86 | + zos.closeEntry(); |
| 87 | + } |
| 88 | + |
| 89 | + for (DataFile d : dataFiles) { |
| 90 | + e = new ZipEntry(getDataZip() + "/" + d.getFilename()); |
| 91 | + Blob blob = d.getFileContent(); |
| 92 | + if (blob != null) { |
| 93 | + byte[] pole = blob.getBytes(1, (int) blob.length()); |
| 94 | + log.debug("saving data file to zip file"); |
| 95 | + try { |
| 96 | + zos.putNextEntry(e); |
| 97 | + } catch (ZipException ex) { |
| 98 | + String[] partOfName = d.getFilename().split("[.]"); |
| 99 | + String filename; |
| 100 | + if (partOfName.length < 2) { |
| 101 | + filename = partOfName[0] + "" + fileCounter; |
| 102 | + } else { |
| 103 | + filename = partOfName[0] + "" + fileCounter + "." + partOfName[1]; |
| 104 | + } |
| 105 | + e = new ZipEntry(getDataZip() + "/" + filename); |
| 106 | + zos.putNextEntry(e); |
| 107 | + fileCounter++; |
| 108 | + } |
| 109 | + |
| 110 | + zos.write(pole); |
| 111 | + zos.closeEntry(); |
| 112 | + } |
| 113 | + } |
| 114 | + log.debug("returning output stream of zip file"); |
| 115 | + |
| 116 | + } finally { |
| 117 | + zos.flush(); |
| 118 | + zos.close(); |
| 119 | + fileCounter = 0; |
| 120 | + |
| 121 | + } |
| 122 | + return baos; |
| 123 | + } |
| 124 | + |
| 125 | + private byte[] toByteArray(Object o) throws Exception { |
| 126 | + if (o instanceof Blob) { |
| 127 | + return ((Blob) o).getBytes(1, (int) ((Blob) o).length()); |
| 128 | + } else if (o instanceof Document) { |
| 129 | + Source source = new DOMSource((Document) o); |
| 130 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 131 | + Result result = new StreamResult(out); |
| 132 | + TransformerFactory factory = TransformerFactory.newInstance(); |
| 133 | + Transformer transformer = factory.newTransformer(); |
| 134 | + transformer.transform(source, result); |
| 135 | + |
| 136 | + return out.toByteArray(); |
| 137 | + } |
| 138 | + |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @return the transformer |
| 144 | + */ |
| 145 | + public DataTransformer getTransformer() { |
| 146 | + return transformer; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param transformer the transformer to set |
| 151 | + */ |
| 152 | + public void setTransformer(DataTransformer transformer) { |
| 153 | + this.transformer = transformer; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @return the metadata |
| 158 | + */ |
| 159 | + public String getMetadata() { |
| 160 | + return metadata; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param metadata the metadata to set |
| 165 | + */ |
| 166 | + public void setMetadata(String metadata) { |
| 167 | + this.metadata = metadata; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return the dataZip |
| 172 | + */ |
| 173 | + public String getDataZip() { |
| 174 | + return dataZip; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @param dataZip the dataZip to set |
| 179 | + */ |
| 180 | + public void setDataZip(String dataZip) { |
| 181 | + this.dataZip = dataZip; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String toString() { |
| 186 | + return "metadata: " + metadata; |
| 187 | + } |
| 188 | +} |
| 189 | + |
0 commit comments