You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OleUtil.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.ByteArrayInputStream;
  15. import java.io.Closeable;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.nio.ByteBuffer;
  21. import java.nio.charset.Charset;
  22. import java.nio.charset.StandardCharsets;
  23. import java.sql.Blob;
  24. import java.sql.SQLException;
  25. import java.sql.SQLFeatureNotSupportedException;
  26. import java.text.Normalizer;
  27. import java.util.EnumSet;
  28. import java.util.Set;
  29. import java.util.regex.Pattern;
  30. import com.healthmarketscience.jackcess.DataType;
  31. import com.healthmarketscience.jackcess.util.OleBlob;
  32. import static com.healthmarketscience.jackcess.util.OleBlob.*;
  33. import org.apache.commons.lang3.builder.ToStringBuilder;
  34. /**
  35. * Utility code for working with OLE data.
  36. *
  37. * @author James Ahlborn
  38. * @usage _advanced_class_
  39. */
  40. public class OleUtil
  41. {
  42. /**
  43. * Interface used to allow optional inclusion of the poi library for working
  44. * with compound ole data.
  45. */
  46. interface CompoundPackageFactory
  47. {
  48. public ContentImpl createCompoundPackageContent(
  49. OleBlobImpl blob, String prettyName, String className, String typeName,
  50. ByteBuffer blobBb, int dataBlockLen);
  51. }
  52. private static final int PACKAGE_SIGNATURE = 0x1C15;
  53. private static final Charset OLE_CHARSET = StandardCharsets.US_ASCII;
  54. private static final Charset OLE_UTF_CHARSET = StandardCharsets.UTF_16LE;
  55. private static final byte[] COMPOUND_STORAGE_SIGNATURE =
  56. {(byte)0xd0,(byte)0xcf,(byte)0x11,(byte)0xe0,
  57. (byte)0xa1,(byte)0xb1,(byte)0x1a,(byte)0xe1};
  58. private static final String SIMPLE_PACKAGE_TYPE = "Package";
  59. private static final int PACKAGE_OBJECT_TYPE = 0x02;
  60. private static final int OLE_VERSION = 0x0501;
  61. private static final int OLE_FORMAT = 0x02;
  62. private static final int PACKAGE_STREAM_SIGNATURE = 0x02;
  63. private static final int PS_EMBEDDED_FILE = 0x030000;
  64. private static final int PS_LINKED_FILE = 0x010000;
  65. private static final Set<ContentType> WRITEABLE_TYPES = EnumSet.of(
  66. ContentType.LINK, ContentType.SIMPLE_PACKAGE, ContentType.OTHER);
  67. private static final byte[] NO_DATA = new byte[0];
  68. private static final int LINK_HEADER = 0x01;
  69. private static final byte[] PACKAGE_FOOTER = {
  70. 0x01, 0x05, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x01, (byte)0xAD, 0x05, (byte)0xFE
  72. };
  73. // regex pattern which matches all the crazy extra stuff in unicode
  74. private static final Pattern UNICODE_ACCENT_PATTERN =
  75. Pattern.compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
  76. private static final CompoundPackageFactory COMPOUND_FACTORY;
  77. static {
  78. CompoundPackageFactory compoundFactory = null;
  79. try {
  80. compoundFactory = (CompoundPackageFactory)
  81. Class.forName("com.healthmarketscience.jackcess.impl.CompoundOleUtil")
  82. .newInstance();
  83. } catch(Throwable t) {
  84. // must not have poi, will load compound ole data as "other"
  85. }
  86. COMPOUND_FACTORY = compoundFactory;
  87. }
  88. /**
  89. * Parses an access database blob structure and returns an appropriate
  90. * OleBlob instance.
  91. */
  92. public static OleBlob parseBlob(byte[] bytes) {
  93. return new OleBlobImpl(bytes);
  94. }
  95. /**
  96. * Creates a new OlBlob instance using the given information.
  97. */
  98. public static OleBlob createBlob(Builder oleBuilder)
  99. throws IOException
  100. {
  101. try {
  102. if(!WRITEABLE_TYPES.contains(oleBuilder.getType())) {
  103. throw new IllegalArgumentException(
  104. "Cannot currently create ole values of type " +
  105. oleBuilder.getType());
  106. }
  107. long contentLen = oleBuilder.getContentLength();
  108. byte[] contentBytes = oleBuilder.getBytes();
  109. InputStream contentStream = oleBuilder.getStream();
  110. byte[] packageStreamHeader = NO_DATA;
  111. byte[] packageStreamFooter = NO_DATA;
  112. switch(oleBuilder.getType()) {
  113. case LINK:
  114. packageStreamHeader = writePackageStreamHeader(oleBuilder);
  115. // link "content" is file path
  116. contentBytes = getZeroTermStrBytes(oleBuilder.getFilePath());
  117. contentLen = contentBytes.length;
  118. break;
  119. case SIMPLE_PACKAGE:
  120. packageStreamHeader = writePackageStreamHeader(oleBuilder);
  121. packageStreamFooter = writePackageStreamFooter(oleBuilder);
  122. break;
  123. case OTHER:
  124. // nothing more to do
  125. break;
  126. default:
  127. throw new RuntimeException("unexpected type " + oleBuilder.getType());
  128. }
  129. long payloadLen = packageStreamHeader.length + packageStreamFooter.length +
  130. contentLen;
  131. byte[] packageHeader = writePackageHeader(oleBuilder, payloadLen);
  132. long totalOleLen = packageHeader.length + PACKAGE_FOOTER.length +
  133. payloadLen;
  134. if(totalOleLen > DataType.OLE.getMaxSize()) {
  135. throw new IllegalArgumentException("Content size of " + totalOleLen +
  136. " is too large for ole column");
  137. }
  138. byte[] oleBytes = new byte[(int)totalOleLen];
  139. ByteBuffer bb = PageChannel.wrap(oleBytes);
  140. bb.put(packageHeader);
  141. bb.put(packageStreamHeader);
  142. if(contentLen > 0L) {
  143. if(contentBytes != null) {
  144. bb.put(contentBytes);
  145. } else {
  146. byte[] buf = new byte[8192];
  147. int numBytes = 0;
  148. while((numBytes = contentStream.read(buf)) >= 0) {
  149. bb.put(buf, 0, numBytes);
  150. }
  151. }
  152. }
  153. bb.put(packageStreamFooter);
  154. bb.put(PACKAGE_FOOTER);
  155. return parseBlob(oleBytes);
  156. } finally {
  157. ByteUtil.closeQuietly(oleBuilder.getStream());
  158. }
  159. }
  160. private static byte[] writePackageHeader(Builder oleBuilder,
  161. long contentLen) {
  162. byte[] prettyNameBytes = getZeroTermStrBytes(oleBuilder.getPrettyName());
  163. String className = oleBuilder.getClassName();
  164. String typeName = oleBuilder.getTypeName();
  165. if(className == null) {
  166. className = typeName;
  167. } else if(typeName == null) {
  168. typeName = className;
  169. }
  170. byte[] classNameBytes = getZeroTermStrBytes(className);
  171. byte[] typeNameBytes = getZeroTermStrBytes(typeName);
  172. int packageHeaderLen = 20 + prettyNameBytes.length + classNameBytes.length;
  173. int oleHeaderLen = 24 + typeNameBytes.length;
  174. byte[] headerBytes = new byte[packageHeaderLen + oleHeaderLen];
  175. ByteBuffer bb = PageChannel.wrap(headerBytes);
  176. // write outer package header
  177. bb.putShort((short)PACKAGE_SIGNATURE);
  178. bb.putShort((short)packageHeaderLen);
  179. bb.putInt(PACKAGE_OBJECT_TYPE);
  180. bb.putShort((short)prettyNameBytes.length);
  181. bb.putShort((short)classNameBytes.length);
  182. int prettyNameOff = bb.position() + 8;
  183. bb.putShort((short)prettyNameOff);
  184. bb.putShort((short)(prettyNameOff + prettyNameBytes.length));
  185. bb.putInt(-1);
  186. bb.put(prettyNameBytes);
  187. bb.put(classNameBytes);
  188. // put ole header
  189. bb.putInt(OLE_VERSION);
  190. bb.putInt(OLE_FORMAT);
  191. bb.putInt(typeNameBytes.length);
  192. bb.put(typeNameBytes);
  193. bb.putLong(0L);
  194. bb.putInt((int)contentLen);
  195. return headerBytes;
  196. }
  197. private static byte[] writePackageStreamHeader(Builder oleBuilder) {
  198. byte[] fileNameBytes = getZeroTermStrBytes(oleBuilder.getFileName());
  199. byte[] filePathBytes = getZeroTermStrBytes(oleBuilder.getFilePath());
  200. int headerLen = 6 + fileNameBytes.length + filePathBytes.length;
  201. if(oleBuilder.getType() == ContentType.SIMPLE_PACKAGE) {
  202. headerLen += 8 + filePathBytes.length;
  203. } else {
  204. headerLen += 2;
  205. }
  206. byte[] headerBytes = new byte[headerLen];
  207. ByteBuffer bb = PageChannel.wrap(headerBytes);
  208. bb.putShort((short)PACKAGE_STREAM_SIGNATURE);
  209. bb.put(fileNameBytes);
  210. bb.put(filePathBytes);
  211. if(oleBuilder.getType() == ContentType.SIMPLE_PACKAGE) {
  212. bb.putInt(PS_EMBEDDED_FILE);
  213. bb.putInt(filePathBytes.length);
  214. bb.put(filePathBytes, 0, filePathBytes.length);
  215. bb.putInt((int) oleBuilder.getContentLength());
  216. } else {
  217. bb.putInt(PS_LINKED_FILE);
  218. bb.putShort((short)LINK_HEADER);
  219. }
  220. return headerBytes;
  221. }
  222. private static byte[] writePackageStreamFooter(Builder oleBuilder) {
  223. // note, these are _not_ zero terminated
  224. byte[] fileNameBytes = oleBuilder.getFileName().getBytes(OLE_UTF_CHARSET);
  225. byte[] filePathBytes = oleBuilder.getFilePath().getBytes(OLE_UTF_CHARSET);
  226. int footerLen = 12 + (filePathBytes.length * 2) + fileNameBytes.length;
  227. byte[] footerBytes = new byte[footerLen];
  228. ByteBuffer bb = PageChannel.wrap(footerBytes);
  229. bb.putInt(filePathBytes.length/2);
  230. bb.put(filePathBytes);
  231. bb.putInt(fileNameBytes.length/2);
  232. bb.put(fileNameBytes);
  233. bb.putInt(filePathBytes.length/2);
  234. bb.put(filePathBytes);
  235. return footerBytes;
  236. }
  237. /**
  238. * creates the appropriate ContentImpl for the given blob.
  239. */
  240. private static ContentImpl parseContent(OleBlobImpl blob)
  241. throws IOException
  242. {
  243. ByteBuffer bb = PageChannel.wrap(blob.getBytes());
  244. if((bb.remaining() < 2) || (bb.getShort() != PACKAGE_SIGNATURE)) {
  245. return new UnknownContentImpl(blob);
  246. }
  247. // read outer package header
  248. int headerSize = bb.getShort();
  249. /* int objType = */ bb.getInt();
  250. int prettyNameLen = bb.getShort();
  251. int classNameLen = bb.getShort();
  252. int prettyNameOff = bb.getShort();
  253. int classNameOff = bb.getShort();
  254. /* int objSize = */ bb.getInt();
  255. String prettyName = readStr(bb, prettyNameOff, prettyNameLen);
  256. String className = readStr(bb, classNameOff, classNameLen);
  257. bb.position(headerSize);
  258. // read ole header
  259. int oleVer = bb.getInt();
  260. /* int format = */ bb.getInt();
  261. if(oleVer != OLE_VERSION) {
  262. return new UnknownContentImpl(blob);
  263. }
  264. int typeNameLen = bb.getInt();
  265. String typeName = readStr(bb, bb.position(), typeNameLen);
  266. bb.getLong(); // unused
  267. int dataBlockLen = bb.getInt();
  268. int dataBlockPos = bb.position();
  269. if(SIMPLE_PACKAGE_TYPE.equalsIgnoreCase(typeName)) {
  270. return createSimplePackageContent(
  271. blob, prettyName, className, typeName, bb, dataBlockLen);
  272. }
  273. // if COMPOUND_FACTORY is null, the poi library isn't available, so just
  274. // load compound data as "other"
  275. if((COMPOUND_FACTORY != null) &&
  276. (bb.remaining() >= COMPOUND_STORAGE_SIGNATURE.length) &&
  277. ByteUtil.matchesRange(bb, bb.position(), COMPOUND_STORAGE_SIGNATURE)) {
  278. return COMPOUND_FACTORY.createCompoundPackageContent(
  279. blob, prettyName, className, typeName, bb, dataBlockLen);
  280. }
  281. // this is either some other "special" (as yet unhandled) format, or it is
  282. // simply an embedded file (or it is compound data and poi isn't available)
  283. return new OtherContentImpl(blob, prettyName, className,
  284. typeName, dataBlockPos, dataBlockLen);
  285. }
  286. private static ContentImpl createSimplePackageContent(
  287. OleBlobImpl blob, String prettyName, String className, String typeName,
  288. ByteBuffer blobBb, int dataBlockLen) {
  289. int dataBlockPos = blobBb.position();
  290. ByteBuffer bb = PageChannel.narrowBuffer(blobBb, dataBlockPos,
  291. dataBlockPos + dataBlockLen);
  292. int packageSig = bb.getShort();
  293. if(packageSig != PACKAGE_STREAM_SIGNATURE) {
  294. return new OtherContentImpl(blob, prettyName, className,
  295. typeName, dataBlockPos, dataBlockLen);
  296. }
  297. String fileName = readZeroTermStr(bb);
  298. String filePath = readZeroTermStr(bb);
  299. int packageType = bb.getInt();
  300. if(packageType == PS_EMBEDDED_FILE) {
  301. int localFilePathLen = bb.getInt();
  302. String localFilePath = readStr(bb, bb.position(), localFilePathLen);
  303. int dataLen = bb.getInt();
  304. int dataPos = bb.position();
  305. bb.position(dataLen + dataPos);
  306. // remaining strings are in "reverse" order (local file path, file name,
  307. // file path). these string usee a real utf charset, and therefore can
  308. // "fix" problems with ascii based names (so we prefer these strings to
  309. // the original strings we found)
  310. int strNum = 0;
  311. while(true) {
  312. int rem = bb.remaining();
  313. if(rem < 4) {
  314. break;
  315. }
  316. int strLen = bb.getInt();
  317. String remStr = readStr(bb, bb.position(), strLen * 2, OLE_UTF_CHARSET);
  318. switch(strNum) {
  319. case 0:
  320. localFilePath = remStr;
  321. break;
  322. case 1:
  323. fileName = remStr;
  324. break;
  325. case 2:
  326. filePath = remStr;
  327. break;
  328. default:
  329. // ignore
  330. }
  331. ++strNum;
  332. }
  333. return new SimplePackageContentImpl(
  334. blob, prettyName, className, typeName, dataPos, dataLen,
  335. fileName, filePath, localFilePath);
  336. }
  337. if(packageType == PS_LINKED_FILE) {
  338. bb.getShort(); //unknown
  339. String linkStr = readZeroTermStr(bb);
  340. return new LinkContentImpl(blob, prettyName, className, typeName,
  341. fileName, linkStr, filePath);
  342. }
  343. return new OtherContentImpl(blob, prettyName, className,
  344. typeName, dataBlockPos, dataBlockLen);
  345. }
  346. private static String readStr(ByteBuffer bb, int off, int len) {
  347. return readStr(bb, off, len, OLE_CHARSET);
  348. }
  349. private static String readZeroTermStr(ByteBuffer bb) {
  350. int off = bb.position();
  351. while(bb.hasRemaining()) {
  352. byte b = bb.get();
  353. if(b == 0) {
  354. break;
  355. }
  356. }
  357. int len = bb.position() - off;
  358. return readStr(bb, off, len);
  359. }
  360. private static String readStr(ByteBuffer bb, int off, int len,
  361. Charset charset) {
  362. String str = new String(bb.array(), off, len, charset);
  363. bb.position(off + len);
  364. if(str.charAt(str.length() - 1) == '\0') {
  365. str = str.substring(0, str.length() - 1);
  366. }
  367. return str;
  368. }
  369. private static byte[] getZeroTermStrBytes(String str) {
  370. // since we are converting to ascii, try to make "nicer" versions of crazy
  371. // chars (e.g. convert "u with an umlaut" to just "u"). this may not
  372. // ultimately help anything but it is what ms access does.
  373. // decompose complex chars into combos of char and accent
  374. str = Normalizer.normalize(str, Normalizer.Form.NFD);
  375. // strip the accents
  376. str = UNICODE_ACCENT_PATTERN.matcher(str).replaceAll("");
  377. // (re)normalize what is left
  378. str = Normalizer.normalize(str, Normalizer.Form.NFC);
  379. return (str + '\0').getBytes(OLE_CHARSET);
  380. }
  381. static final class OleBlobImpl implements OleBlob, ColumnImpl.InMemoryBlob
  382. {
  383. private byte[] _bytes;
  384. private ContentImpl _content;
  385. private OleBlobImpl(byte[] bytes) {
  386. _bytes = bytes;
  387. }
  388. @Override
  389. public void writeTo(OutputStream out) throws IOException {
  390. out.write(_bytes);
  391. }
  392. @Override
  393. public Content getContent() throws IOException {
  394. if(_content == null) {
  395. _content = parseContent(this);
  396. }
  397. return _content;
  398. }
  399. @Override
  400. public InputStream getBinaryStream() throws SQLException {
  401. return new ByteArrayInputStream(_bytes);
  402. }
  403. @Override
  404. public InputStream getBinaryStream(long pos, long len)
  405. throws SQLException
  406. {
  407. return new ByteArrayInputStream(_bytes, fromJdbcOffset(pos), (int)len);
  408. }
  409. @Override
  410. public long length() throws SQLException {
  411. return _bytes.length;
  412. }
  413. @Override
  414. public byte[] getBytes() throws IOException {
  415. if(_bytes == null) {
  416. throw new IOException("blob is closed");
  417. }
  418. return _bytes;
  419. }
  420. @Override
  421. public byte[] getBytes(long pos, int len) throws SQLException {
  422. return ByteUtil.copyOf(_bytes, fromJdbcOffset(pos), len);
  423. }
  424. @Override
  425. public long position(byte[] pattern, long start) throws SQLException {
  426. int pos = ByteUtil.findRange(PageChannel.wrap(_bytes),
  427. fromJdbcOffset(start), pattern);
  428. return((pos >= 0) ? toJdbcOffset(pos) : pos);
  429. }
  430. @Override
  431. public long position(Blob pattern, long start) throws SQLException {
  432. return position(pattern.getBytes(1L, (int)pattern.length()), start);
  433. }
  434. @Override
  435. public OutputStream setBinaryStream(long position) throws SQLException {
  436. throw new SQLFeatureNotSupportedException();
  437. }
  438. @Override
  439. public void truncate(long len) throws SQLException {
  440. throw new SQLFeatureNotSupportedException();
  441. }
  442. @Override
  443. public int setBytes(long pos, byte[] bytes) throws SQLException {
  444. throw new SQLFeatureNotSupportedException();
  445. }
  446. @Override
  447. public int setBytes(long pos, byte[] bytes, int offset, int lesn)
  448. throws SQLException {
  449. throw new SQLFeatureNotSupportedException();
  450. }
  451. @Override
  452. public void free() {
  453. close();
  454. }
  455. @Override
  456. public void close() {
  457. _bytes = null;
  458. ByteUtil.closeQuietly(_content);
  459. _content = null;
  460. }
  461. private static int toJdbcOffset(int off) {
  462. return off + 1;
  463. }
  464. private static int fromJdbcOffset(long off) {
  465. return (int)off - 1;
  466. }
  467. @Override
  468. public String toString() {
  469. ToStringBuilder sb = CustomToStringStyle.builder(this);
  470. if(_content != null) {
  471. sb.append("content", _content);
  472. } else {
  473. sb.append("bytes", _bytes);
  474. sb.append("content", "(uninitialized)");
  475. }
  476. return sb.toString();
  477. }
  478. }
  479. static abstract class ContentImpl implements Content, Closeable
  480. {
  481. protected final OleBlobImpl _blob;
  482. protected ContentImpl(OleBlobImpl blob) {
  483. _blob = blob;
  484. }
  485. @Override
  486. public OleBlobImpl getBlob() {
  487. return _blob;
  488. }
  489. protected byte[] getBytes() throws IOException {
  490. return getBlob().getBytes();
  491. }
  492. @Override
  493. public void close() {
  494. // base does nothing
  495. }
  496. protected ToStringBuilder toString(ToStringBuilder sb) {
  497. sb.append("type", getType());
  498. return sb;
  499. }
  500. }
  501. static abstract class EmbeddedContentImpl extends ContentImpl
  502. implements EmbeddedContent
  503. {
  504. private final int _position;
  505. private final int _length;
  506. protected EmbeddedContentImpl(OleBlobImpl blob, int position, int length)
  507. {
  508. super(blob);
  509. _position = position;
  510. _length = length;
  511. }
  512. @Override
  513. public long length() {
  514. return _length;
  515. }
  516. @Override
  517. public InputStream getStream() throws IOException {
  518. return new ByteArrayInputStream(getBytes(), _position, _length);
  519. }
  520. @Override
  521. public void writeTo(OutputStream out) throws IOException {
  522. out.write(getBytes(), _position, _length);
  523. }
  524. @Override
  525. protected ToStringBuilder toString(ToStringBuilder sb) {
  526. super.toString(sb);
  527. if(_position >= 0) {
  528. sb.append("content", ByteBuffer.wrap(_blob._bytes, _position, _length));
  529. }
  530. return sb;
  531. }
  532. }
  533. static abstract class EmbeddedPackageContentImpl
  534. extends EmbeddedContentImpl
  535. implements PackageContent
  536. {
  537. private final String _prettyName;
  538. private final String _className;
  539. private final String _typeName;
  540. protected EmbeddedPackageContentImpl(
  541. OleBlobImpl blob, String prettyName, String className,
  542. String typeName, int position, int length)
  543. {
  544. super(blob, position, length);
  545. _prettyName = prettyName;
  546. _className = className;
  547. _typeName = typeName;
  548. }
  549. @Override
  550. public String getPrettyName() {
  551. return _prettyName;
  552. }
  553. @Override
  554. public String getClassName() {
  555. return _className;
  556. }
  557. @Override
  558. public String getTypeName() {
  559. return _typeName;
  560. }
  561. @Override
  562. protected ToStringBuilder toString(ToStringBuilder sb) {
  563. sb.append("prettyName", _prettyName)
  564. .append("className", _className)
  565. .append("typeName", _typeName);
  566. super.toString(sb);
  567. return sb;
  568. }
  569. }
  570. private static final class LinkContentImpl
  571. extends EmbeddedPackageContentImpl
  572. implements LinkContent
  573. {
  574. private final String _fileName;
  575. private final String _linkPath;
  576. private final String _filePath;
  577. private LinkContentImpl(OleBlobImpl blob, String prettyName,
  578. String className, String typeName,
  579. String fileName, String linkPath,
  580. String filePath)
  581. {
  582. super(blob, prettyName, className, typeName, -1, -1);
  583. _fileName = fileName;
  584. _linkPath = linkPath;
  585. _filePath = filePath;
  586. }
  587. @Override
  588. public ContentType getType() {
  589. return ContentType.LINK;
  590. }
  591. @Override
  592. public String getFileName() {
  593. return _fileName;
  594. }
  595. @Override
  596. public String getLinkPath() {
  597. return _linkPath;
  598. }
  599. @Override
  600. public String getFilePath() {
  601. return _filePath;
  602. }
  603. @Override
  604. public InputStream getLinkStream() throws IOException {
  605. return new FileInputStream(getLinkPath());
  606. }
  607. @Override
  608. public String toString() {
  609. return toString(CustomToStringStyle.builder(this))
  610. .append("fileName", _fileName)
  611. .append("linkPath", _linkPath)
  612. .append("filePath", _filePath)
  613. .toString();
  614. }
  615. }
  616. private static final class SimplePackageContentImpl
  617. extends EmbeddedPackageContentImpl
  618. implements SimplePackageContent
  619. {
  620. private final String _fileName;
  621. private final String _filePath;
  622. private final String _localFilePath;
  623. private SimplePackageContentImpl(OleBlobImpl blob, String prettyName,
  624. String className, String typeName,
  625. int position, int length,
  626. String fileName, String filePath,
  627. String localFilePath)
  628. {
  629. super(blob, prettyName, className, typeName, position, length);
  630. _fileName = fileName;
  631. _filePath = filePath;
  632. _localFilePath = localFilePath;
  633. }
  634. @Override
  635. public ContentType getType() {
  636. return ContentType.SIMPLE_PACKAGE;
  637. }
  638. @Override
  639. public String getFileName() {
  640. return _fileName;
  641. }
  642. @Override
  643. public String getFilePath() {
  644. return _filePath;
  645. }
  646. @Override
  647. public String getLocalFilePath() {
  648. return _localFilePath;
  649. }
  650. @Override
  651. public String toString() {
  652. return toString(CustomToStringStyle.builder(this))
  653. .append("fileName", _fileName)
  654. .append("filePath", _filePath)
  655. .append("localFilePath", _localFilePath)
  656. .toString();
  657. }
  658. }
  659. private static final class OtherContentImpl
  660. extends EmbeddedPackageContentImpl
  661. implements OtherContent
  662. {
  663. private OtherContentImpl(
  664. OleBlobImpl blob, String prettyName, String className,
  665. String typeName, int position, int length)
  666. {
  667. super(blob, prettyName, className, typeName, position, length);
  668. }
  669. @Override
  670. public ContentType getType() {
  671. return ContentType.OTHER;
  672. }
  673. @Override
  674. public String toString() {
  675. return toString(CustomToStringStyle.builder(this))
  676. .toString();
  677. }
  678. }
  679. private static final class UnknownContentImpl extends ContentImpl
  680. {
  681. private UnknownContentImpl(OleBlobImpl blob) {
  682. super(blob);
  683. }
  684. @Override
  685. public ContentType getType() {
  686. return ContentType.UNKNOWN;
  687. }
  688. @Override
  689. public String toString() {
  690. return toString(CustomToStringStyle.builder(this))
  691. .append("content", _blob._bytes)
  692. .toString();
  693. }
  694. }
  695. }