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 23KB

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