Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

OleUtil.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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
  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. public byte[] getBytes() throws IOException {
  414. if(_bytes == null) {
  415. throw new IOException("blob is closed");
  416. }
  417. return _bytes;
  418. }
  419. @Override
  420. public byte[] getBytes(long pos, int len) throws SQLException {
  421. return ByteUtil.copyOf(_bytes, fromJdbcOffset(pos), len);
  422. }
  423. @Override
  424. public long position(byte[] pattern, long start) throws SQLException {
  425. int pos = ByteUtil.findRange(PageChannel.wrap(_bytes),
  426. fromJdbcOffset(start), pattern);
  427. return((pos >= 0) ? toJdbcOffset(pos) : pos);
  428. }
  429. @Override
  430. public long position(Blob pattern, long start) throws SQLException {
  431. return position(pattern.getBytes(1L, (int)pattern.length()), start);
  432. }
  433. @Override
  434. public OutputStream setBinaryStream(long position) throws SQLException {
  435. throw new SQLFeatureNotSupportedException();
  436. }
  437. @Override
  438. public void truncate(long len) throws SQLException {
  439. throw new SQLFeatureNotSupportedException();
  440. }
  441. @Override
  442. public int setBytes(long pos, byte[] bytes) throws SQLException {
  443. throw new SQLFeatureNotSupportedException();
  444. }
  445. @Override
  446. public int setBytes(long pos, byte[] bytes, int offset, int lesn)
  447. throws SQLException {
  448. throw new SQLFeatureNotSupportedException();
  449. }
  450. @Override
  451. public void free() {
  452. close();
  453. }
  454. @Override
  455. public void close() {
  456. _bytes = null;
  457. ByteUtil.closeQuietly(_content);
  458. _content = null;
  459. }
  460. private static int toJdbcOffset(int off) {
  461. return off + 1;
  462. }
  463. private static int fromJdbcOffset(long off) {
  464. return (int)off - 1;
  465. }
  466. @Override
  467. public String toString() {
  468. ToStringBuilder sb = CustomToStringStyle.builder(this);
  469. if(_content != null) {
  470. sb.append("content", _content);
  471. } else {
  472. sb.append("bytes", _bytes);
  473. sb.append("content", "(uninitialized)");
  474. }
  475. return sb.toString();
  476. }
  477. }
  478. static abstract class ContentImpl implements Content, Closeable
  479. {
  480. protected final OleBlobImpl _blob;
  481. protected ContentImpl(OleBlobImpl blob) {
  482. _blob = blob;
  483. }
  484. @Override
  485. public OleBlobImpl getBlob() {
  486. return _blob;
  487. }
  488. protected byte[] getBytes() throws IOException {
  489. return getBlob().getBytes();
  490. }
  491. @Override
  492. public void close() {
  493. // base does nothing
  494. }
  495. protected ToStringBuilder toString(ToStringBuilder sb) {
  496. sb.append("type", getType());
  497. return sb;
  498. }
  499. }
  500. static abstract class EmbeddedContentImpl extends ContentImpl
  501. implements EmbeddedContent
  502. {
  503. private final int _position;
  504. private final int _length;
  505. protected EmbeddedContentImpl(OleBlobImpl blob, int position, int length)
  506. {
  507. super(blob);
  508. _position = position;
  509. _length = length;
  510. }
  511. @Override
  512. public long length() {
  513. return _length;
  514. }
  515. @Override
  516. public InputStream getStream() throws IOException {
  517. return new ByteArrayInputStream(getBytes(), _position, _length);
  518. }
  519. @Override
  520. public void writeTo(OutputStream out) throws IOException {
  521. out.write(getBytes(), _position, _length);
  522. }
  523. @Override
  524. protected ToStringBuilder toString(ToStringBuilder sb) {
  525. super.toString(sb);
  526. if(_position >= 0) {
  527. sb.append("content", ByteBuffer.wrap(_blob._bytes, _position, _length));
  528. }
  529. return sb;
  530. }
  531. }
  532. static abstract class EmbeddedPackageContentImpl
  533. extends EmbeddedContentImpl
  534. implements PackageContent
  535. {
  536. private final String _prettyName;
  537. private final String _className;
  538. private final String _typeName;
  539. protected EmbeddedPackageContentImpl(
  540. OleBlobImpl blob, String prettyName, String className,
  541. String typeName, int position, int length)
  542. {
  543. super(blob, position, length);
  544. _prettyName = prettyName;
  545. _className = className;
  546. _typeName = typeName;
  547. }
  548. @Override
  549. public String getPrettyName() {
  550. return _prettyName;
  551. }
  552. @Override
  553. public String getClassName() {
  554. return _className;
  555. }
  556. @Override
  557. public String getTypeName() {
  558. return _typeName;
  559. }
  560. @Override
  561. protected ToStringBuilder toString(ToStringBuilder sb) {
  562. sb.append("prettyName", _prettyName)
  563. .append("className", _className)
  564. .append("typeName", _typeName);
  565. super.toString(sb);
  566. return sb;
  567. }
  568. }
  569. private static final class LinkContentImpl
  570. extends EmbeddedPackageContentImpl
  571. implements LinkContent
  572. {
  573. private final String _fileName;
  574. private final String _linkPath;
  575. private final String _filePath;
  576. private LinkContentImpl(OleBlobImpl blob, String prettyName,
  577. String className, String typeName,
  578. String fileName, String linkPath,
  579. String filePath)
  580. {
  581. super(blob, prettyName, className, typeName, -1, -1);
  582. _fileName = fileName;
  583. _linkPath = linkPath;
  584. _filePath = filePath;
  585. }
  586. @Override
  587. public ContentType getType() {
  588. return ContentType.LINK;
  589. }
  590. @Override
  591. public String getFileName() {
  592. return _fileName;
  593. }
  594. @Override
  595. public String getLinkPath() {
  596. return _linkPath;
  597. }
  598. @Override
  599. public String getFilePath() {
  600. return _filePath;
  601. }
  602. @Override
  603. public InputStream getLinkStream() throws IOException {
  604. return new FileInputStream(getLinkPath());
  605. }
  606. @Override
  607. public String toString() {
  608. return toString(CustomToStringStyle.builder(this))
  609. .append("fileName", _fileName)
  610. .append("linkPath", _linkPath)
  611. .append("filePath", _filePath)
  612. .toString();
  613. }
  614. }
  615. private static final class SimplePackageContentImpl
  616. extends EmbeddedPackageContentImpl
  617. implements SimplePackageContent
  618. {
  619. private final String _fileName;
  620. private final String _filePath;
  621. private final String _localFilePath;
  622. private SimplePackageContentImpl(OleBlobImpl blob, String prettyName,
  623. String className, String typeName,
  624. int position, int length,
  625. String fileName, String filePath,
  626. String localFilePath)
  627. {
  628. super(blob, prettyName, className, typeName, position, length);
  629. _fileName = fileName;
  630. _filePath = filePath;
  631. _localFilePath = localFilePath;
  632. }
  633. @Override
  634. public ContentType getType() {
  635. return ContentType.SIMPLE_PACKAGE;
  636. }
  637. @Override
  638. public String getFileName() {
  639. return _fileName;
  640. }
  641. @Override
  642. public String getFilePath() {
  643. return _filePath;
  644. }
  645. @Override
  646. public String getLocalFilePath() {
  647. return _localFilePath;
  648. }
  649. @Override
  650. public String toString() {
  651. return toString(CustomToStringStyle.builder(this))
  652. .append("fileName", _fileName)
  653. .append("filePath", _filePath)
  654. .append("localFilePath", _localFilePath)
  655. .toString();
  656. }
  657. }
  658. private static final class OtherContentImpl
  659. extends EmbeddedPackageContentImpl
  660. implements OtherContent
  661. {
  662. private OtherContentImpl(
  663. OleBlobImpl blob, String prettyName, String className,
  664. String typeName, int position, int length)
  665. {
  666. super(blob, prettyName, className, typeName, position, length);
  667. }
  668. @Override
  669. public ContentType getType() {
  670. return ContentType.OTHER;
  671. }
  672. @Override
  673. public String toString() {
  674. return toString(CustomToStringStyle.builder(this))
  675. .toString();
  676. }
  677. }
  678. private static final class UnknownContentImpl extends ContentImpl
  679. {
  680. private UnknownContentImpl(OleBlobImpl blob) {
  681. super(blob);
  682. }
  683. @Override
  684. public ContentType getType() {
  685. return ContentType.UNKNOWN;
  686. }
  687. @Override
  688. public String toString() {
  689. return toString(CustomToStringStyle.builder(this))
  690. .append("content", _blob._bytes)
  691. .toString();
  692. }
  693. }
  694. }