forked from binfalse/CombineArchive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample.java
309 lines (264 loc) · 9.8 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* CombineArchive - a JAVA library to read/write/create/.. CombineArchives
* Copyright (c) 2014, Martin Scharm <combinearchive-code@binfalse.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package de.unirostock.sems.cbarchive;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.transform.TransformerException;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import de.unirostock.sems.cbarchive.meta.DefaultMetaDataObject;
import de.unirostock.sems.cbarchive.meta.MetaDataObject;
import de.unirostock.sems.cbarchive.meta.OmexMetaDataObject;
import de.unirostock.sems.cbarchive.meta.omex.OmexDescription;
import de.unirostock.sems.cbarchive.meta.omex.VCard;
/**
* execute these commands before running the example:
*
* <pre>
* mkdir -p /tmp/base/path/subdir
* touch /tmp/base/path/{file.sbml,subdir/file.cellml}
* </pre>
*
*
* the directory tree in /tmp/base should then look like:
*
* <pre>
* /tmp/base
* /tmp/base/path
* /tmp/base/path/subdir
* /tmp/base/path/subdir/file.cellml
* /tmp/base/path/file.sbml
* </pre>
*
* This example will create the following files/directories:
* (you may want to delete them afterwards)
*
* <pre>
* /tmp/testArchive.zip
* /tmp/myDestination
* /tmp/myExtractedEntry
* </pre>
*
*
* @author Martin Scharm
*
*/
public class Example
{
/** should we print to sys out?. */
public static boolean PRINT = true;
/** The out stream. */
private static PrintStream ps = System.out;
/**
* Creates an example archive in <code>/tmp/testArchive.zip</code>.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws TransformerException
* the transformer exception
* @throws CombineArchiveException
* @throws ParseException
* @throws JDOMException
* @throws URISyntaxException
*/
public static void createExample ()
throws IOException,
TransformerException,
JDOMException,
ParseException,
CombineArchiveException, URISyntaxException
{
ps.println ("--- creating archive. ---");
// let's create some 'creators' -> meta data.
List<VCard> creators = new ArrayList<VCard> ();
creators.add (new VCard ("Scharm", "Martin",
"martin.scharm@uni-rostock.de", "University of Rostock"));
creators.add (new VCard ("Waltemath", "Dagmar",
"dagmar.waltemath@uni-rostock.de", "University of Rostock"));
// create a new empty archive. since /tmp/testArchive.zip does not exist
// it will be created. (if it exists we'd try to read it)
CombineArchive ca = new CombineArchive (new File ("/tmp/testArchive.zip"));
// add an entry to the archive
ArchiveEntry SBMLFile = ca.addEntry (
// this command will add /tmp/base/path/file.sbml to the root of our archive
// (because base path in that case is /tmp/base/path/). thus we'll see
// /file.sbml in our archive.
new File ("/tmp/base/path"),
new File ("/tmp/base/path/file.sbml"),
// format is http://identifiers.org/combine.specifications/sbml - here i
// see https://sems.uni-rostock.de/trac/combine-ext/wiki/CombineFormatizer
// to find a convenient way to generate these URIs
new URI ("http://identifiers.org/combine.specifications/sbml"));
// we'll also add some OMEX description. creators as defined above
SBMLFile.addDescription (new OmexMetaDataObject (new OmexDescription (
creators, new Date ())));
// add another entry to the archive
ArchiveEntry CellMLFile = ca.addEntry (
// this time we add /tmp/base/path/subdir/file.cellml
new File ("/tmp/base/path/subdir/file.cellml"),
// and we'd like to see it in /subdir/file.cellml
"/subdir/file.cellml",
// format is http://identifiers.org/combine.specifications/cellml.1.0 -
// see https://sems.uni-rostock.de/trac/combine-ext/wiki/CombineFormatizer
// to find a convenient way to generate these URIs
new URI ("http://identifiers.org/combine.specifications/cellml.1.0"),
// in addition: set this entry as main entry in this archive
true);
// same description, but feel free to define different authors.
CellMLFile.addDescription (new OmexMetaDataObject (new OmexDescription (
creators, new Date ())));
// just for fun: add some other meta data:
Element metaParent = new Element ("stuff");
Element metaElement = new Element ("myMetaElement");
metaElement.setAttribute ("someAttribute", "someValue");
metaElement.addContent ("some content");
metaParent.addContent (metaElement);
// but this time we describe the fragment 'someFragment' inside the model
CellMLFile.addDescription ("someFragment", new DefaultMetaDataObject (
metaParent));
// last but not least, lets add some description to the archive itself
ca.addDescription (new OmexMetaDataObject (new OmexDescription (creators,
new Date ())));
// finalise the archive (write manifest and meta data) and close it
ca.pack ();
ca.close ();
}
/**
* Reads the example in <code>/tmp/testArchive.zip</code>.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws JDOMException
* the jDOM exception
* @throws ParseException
* the parse exception
* @throws CombineArchiveException
*/
public static void readExample ()
throws IOException,
JDOMException,
ParseException,
CombineArchiveException
{
ps.println ("--- reading archive. ---");
File archiveFile = new File ("/tmp/testArchive.zip");
File destination = new File ("/tmp/myDestination");
File tmpEntryExtract = new File ("/tmp/myExtractedEntry");
// read the archive stored in `archiveFile`
CombineArchive ca = new CombineArchive (archiveFile);
// read description of the archive itself
ps.println ("found " + ca.getDescriptions ().size ()
+ " meta data entries describing the archive.");
// iterate over all entries in the archive
for (ArchiveEntry entry : ca.getEntries ())
{
// display some information about the archive
ps.println (">>> file name in archive: " + entry.getFileName ()
+ " -- apparently of format: " + entry.getFormat ());
// extract the file to `tmpEntryExtract`
ps.println ("file can be read from: "
+ entry.extractFile (tmpEntryExtract).getAbsolutePath ());
// if you just want to read it, you do not need to extract it
// instead call for an InputStream:
InputStream myReader = Files.newInputStream (entry.getPath (),
StandardOpenOption.READ);
// but here we do not use it...
myReader.close ();
// read the descriptions
for (MetaDataObject description : entry.getDescriptions ())
{
ps.println ("+ found some meta data about "
+ description.getAbout ());
if (description instanceof OmexMetaDataObject)
{
OmexDescription desc = ((OmexMetaDataObject) description)
.getOmexDescription ();
// when was it created?
ps.println ("file was created: " + desc.getCreated ());
// who's created the archive?
VCard firstCreator = desc.getCreators ().get (0);
ps.println ("file's first author: "
+ firstCreator.getGivenName () + " "
+ firstCreator.getFamilyName ());
}
else
{
ps.println ("found some meta data of type '"
+ description.getClass ().getName ()
+ "' that we do not respect in this small example.");
}
// No matter what type of description that is, you can always
// retrieve the XML subtree rooting the meta data using
Element meta = description.getXmlDescription ();
// the descriptions are encoded in
meta.getChildren ();
}
}
// ok, last but not least we can extract the whole archive to our disk:
ca.extractTo (destination);
// and now that we're finished: close the archive
ca.close ();
}
/**
* @param args
* @throws Exception
*/
public static void main (String[] args) throws Exception
{
if (!PRINT)
{
File tmp = File.createTempFile ("CombineArchive", "temporary");
tmp.deleteOnExit ();
ps = new PrintStream (new FileOutputStream (tmp));
}
// create an archive
createExample ();
// read the archive
readExample ();
if (!PRINT)
{
ps.close ();
}
}
}