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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
Copyright (c) 2013 James Ahlborn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.healthmarketscience.jackcess.util;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import com.healthmarketscience.jackcess.impl.OleUtil;
/**
* Extensions of the Blob interface with additional functionality for working
* with the OLE content from an access database. The ole data type in access
* has a wide range of functionality (including wrappers with nested wrappers
* with nested filesystems!), and jackcess only supports a small portion of
* it. That said, jackcess should support the bulk of the common
* functionality.
* <br>
* The main Blob methods will interact with the <i>entire</i> OLE field data
* which, in most cases, contains additional wrapper information. In order to
* access the ultimate "content" contained within the OLE data, the {@link
* #getContent} method should be used. The type of this content may be a
* variety of formats, so additional sub-interfaces are available to interact
* with it. The most specific sub-interface can be determined by the {@link
* ContentType} of the Content.
* <br>
* Once an OleBlob is no longer useful, <i>it should be closed</i> using
* {@link #free} or {@link #close} methods (after which, the instance will no
* longer be functional).
* <br>
* Note, the OleBlob implementation is read-only (through the interface). In
* order to modify blob contents, create a new OleBlob instance using {@link
* OleBlob.Builder} and write it to the access database.
* <br>
* <b>Example for interpreting an existing OLE field:</b>
* <pre>
* OleBlob oleBlob = null;
* try {
* oleBlob = row.getBlob("MyOleColumn");
* Content content = oleBlob.getContent()
* if(content.getType() == OleBlob.ContentType.SIMPLE_PACKAGE) {
* FileOutputStream out = ...;
* ((SimplePackageContent)content).writeTo(out);
* out.closee();
* }
* } finally {
* if(oleBlob != null) { oleBlob.close(); }
* }
* </pre>
* <br>
* <b>Example for creating new, embedded ole data:</b>
* <pre>
* OleBlob oleBlob = null;
* try {
* oleBlob = new OleBlob.Builder()
* .setSimplePackage(new File("some_data.txt"))
* .toBlob();
* db.addRow(1, oleBlob);
* } finally {
* if(oleBlob != null) { oleBlob.close(); }
* }
* </pre>
* <br>
* <b>Example for creating new, linked ole data:</b>
* <pre>
* OleBlob oleBlob = null;
* try {
* oleBlob = new OleBlob.Builder()
* .setLink(new File("some_data.txt"))
* .toBlob();
* db.addRow(1, oleBlob);
* } finally {
* if(oleBlob != null) { oleBlob.close(); }
* }
* </pre>
*
* @author James Ahlborn
*/
public interface OleBlob extends Blob, Closeable
{
/** Enum describing the types of blob contents which are currently
supported/understood */
public enum ContentType {
/** the blob contents are a link (file path) to some external content.
Content will be an instance of LinkContent */
LINK,
/** the blob contents are a simple wrapper around some embedded content
and related file names/paths. Content will be an instance
SimplePackageContent */
SIMPLE_PACKAGE,
/** the blob contents are a complex embedded data known as compound
storage (aka OLE2). Working with compound storage requires the
optional POI library. Content will be an instance of CompoundContent.
If the POI library is not available on the classpath, then compound
storage data will instead be returned as type {@link #OTHER}. */
COMPOUND_STORAGE,
/** the top-level blob wrapper is understood, but the nested blob contents
are unknown, probably just some embedded content. Content will be an
instance of OtherContent */
OTHER,
/** the top-level blob wrapper is not understood (this may not be a valid
ole instance). Content will simply be an instance of Content (the
data can be accessed from the main blob instance) */
UNKNOWN;
}
/**
* Writes the entire raw blob data to the given stream (this is the access
* db internal format, which includes all wrapper information).
*
* @param out stream to which the blob will be written
*/
public void writeTo(OutputStream out) throws IOException;
/**
* Returns the decoded form of the blob contents, if understandable.
*/
public Content getContent() throws IOException;
public interface Content
{
/**
* Returns the type of this content.
*/
public ContentType getType();
/**
* Returns the blob which owns this content.
*/
public OleBlob getBlob();
}
/**
* Intermediate sub-interface for Content which has a nested package.
*/
public interface PackageContent extends Content
{
public String getPrettyName() throws IOException;
public String getClassName() throws IOException;
public String getTypeName() throws IOException;
}
/**
* Intermediate sub-interface for Content which has embedded content.
*/
public interface EmbeddedContent extends Content
{
public long length();
public InputStream getStream() throws IOException;
public void writeTo(OutputStream out) throws IOException;
}
/**
* Sub-interface for Content which has the {@link ContentType#LINK} type.
* The actual content is external to the access database and can be found at
* {@link #getLinkPath}.
*/
public interface LinkContent extends PackageContent
{
public String getFileName();
public String getLinkPath();
public String getFilePath();
public InputStream getLinkStream() throws IOException;
}
/**
* Sub-interface for Content which has the {@link
* ContentType#SIMPLE_PACKAGE} type. The actual content is embedded within
* the access database (but the original file source path can also be found
* at {@link #getFilePath}).
*/
public interface SimplePackageContent
extends PackageContent, EmbeddedContent
{
public String getFileName();
public String getFilePath();
public String getLocalFilePath();
}
/**
* Sub-interface for Content which has the {@link
* ContentType#COMPOUND_STORAGE} type. Compound storage is a complex
* embedding format also known as OLE2. In some situations (mostly
* non-microsoft office file formats) the actual content is available from
* the {@link #getContentsEntry} method (if {@link #hasContentsEntry}
* returns {@code true}). In other situations (e.g. microsoft office file
* formats), the actual content is most or all of the compound content (but
* retrieving the final file may be a complex operation beyond the scope of
* jackcess). Note that the CompoundContent type will only be available if
* the POI library is in the classpath, otherwise compound content will be
* returned as OtherContent.
*/
public interface CompoundContent extends PackageContent, EmbeddedContent,
Iterable<CompoundContent.Entry>
{
public Entry getEntry(String entryName) throws IOException;
public boolean hasContentsEntry() throws IOException;
public Entry getContentsEntry() throws IOException;
/**
* A document entry in the compound storage.
*/
public interface Entry extends EmbeddedContent
{
public String getName();
/**
* Returns the CompoundContent which owns this entry.
*/
public CompoundContent getParent();
}
}
/**
* Sub-interface for Content which has the {@link ContentType#OTHER} type.
* This may be a simple embedded file or some other, currently not
* understood complex type.
*/
public interface OtherContent extends PackageContent, EmbeddedContent
{
}
/**
* Builder style class for constructing an OleBlob. See {@link OleBlob} for
* example usage.
*/
public class Builder
{
public static final String PACKAGE_PRETTY_NAME = "Packager Shell Object";
public static final String PACKAGE_TYPE_NAME = "Package";
private ContentType _type;
private byte[] _bytes;
private InputStream _stream;
private long _contentLen;
private String _fileName;
private String _filePath;
private String _prettyName;
private String _className;
private String _typeName;
public ContentType getType() {
return _type;
}
public byte[] getBytes() {
return _bytes;
}
public InputStream getStream() {
return _stream;
}
public long getContentLength() {
return _contentLen;
}
public String getFileName() {
return _fileName;
}
public String getFilePath() {
return _filePath;
}
public String getPrettyName() {
return _prettyName;
}
public String getClassName() {
return _className;
}
public String getTypeName() {
return _typeName;
}
public Builder setSimplePackageBytes(byte[] bytes) {
_bytes = bytes;
_contentLen = bytes.length;
setDefaultPackageType();
_type = ContentType.SIMPLE_PACKAGE;
return this;
}
public Builder setSimplePackageStream(InputStream in, long length) {
_stream = in;
_contentLen = length;
setDefaultPackageType();
_type = ContentType.SIMPLE_PACKAGE;
return this;
}
public Builder setSimplePackageFileName(String fileName) {
_fileName = fileName;
setDefaultPackageType();
_type = ContentType.SIMPLE_PACKAGE;
return this;
}
public Builder setSimplePackageFilePath(String filePath) {
_filePath = filePath;
setDefaultPackageType();
_type = ContentType.SIMPLE_PACKAGE;
return this;
}
public Builder setSimplePackage(File f) throws FileNotFoundException {
_fileName = f.getName();
_filePath = f.getAbsolutePath();
return setSimplePackageStream(new FileInputStream(f), f.length());
}
public Builder setLinkFileName(String fileName) {
_fileName = fileName;
setDefaultPackageType();
_type = ContentType.LINK;
return this;
}
public Builder setLinkPath(String link) {
_filePath = link;
setDefaultPackageType();
_type = ContentType.LINK;
return this;
}
public Builder setLink(File f) {
_fileName = f.getName();
_filePath = f.getAbsolutePath();
setDefaultPackageType();
_type = ContentType.LINK;
return this;
}
private void setDefaultPackageType() {
if(_prettyName == null) {
_prettyName = PACKAGE_PRETTY_NAME;
}
if(_className == null) {
_className = PACKAGE_TYPE_NAME;
}
}
public Builder setOtherBytes(byte[] bytes) {
_bytes = bytes;
_contentLen = bytes.length;
_type = ContentType.OTHER;
return this;
}
public Builder setOtherStream(InputStream in, long length) {
_stream = in;
_contentLen = length;
_type = ContentType.OTHER;
return this;
}
public Builder setOther(File f) throws FileNotFoundException {
return setOtherStream(new FileInputStream(f), f.length());
}
public Builder setPackagePrettyName(String prettyName) {
_prettyName = prettyName;
return this;
}
public Builder setPackageClassName(String className) {
_className = className;
return this;
}
public Builder setPackageTypeName(String typeName) {
_typeName = typeName;
return this;
}
public OleBlob toBlob() throws IOException {
return OleUtil.createBlob(this);
}
public static OleBlob fromInternalData(byte[] bytes) throws IOException {
return OleUtil.parseBlob(bytes);
}
}
}
|