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.

HSLFSlideShow.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.HashMap;
  26. import java.util.Hashtable;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.apache.poi.POIDocument;
  30. import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
  31. import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException;
  32. import org.apache.poi.hslf.exceptions.HSLFException;
  33. import org.apache.poi.hslf.record.CurrentUserAtom;
  34. import org.apache.poi.hslf.record.ExOleObjStg;
  35. import org.apache.poi.hslf.record.PersistPtrHolder;
  36. import org.apache.poi.hslf.record.PersistRecord;
  37. import org.apache.poi.hslf.record.PositionDependentRecord;
  38. import org.apache.poi.hslf.record.Record;
  39. import org.apache.poi.hslf.record.RecordTypes;
  40. import org.apache.poi.hslf.record.UserEditAtom;
  41. import org.apache.poi.hslf.usermodel.ObjectData;
  42. import org.apache.poi.hslf.usermodel.PictureData;
  43. import org.apache.poi.poifs.filesystem.DirectoryNode;
  44. import org.apache.poi.poifs.filesystem.DocumentEntry;
  45. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  46. import org.apache.poi.poifs.filesystem.EntryUtils;
  47. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  48. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  49. import org.apache.poi.util.LittleEndian;
  50. import org.apache.poi.util.POILogFactory;
  51. import org.apache.poi.util.POILogger;
  52. /**
  53. * This class contains the main functionality for the Powerpoint file
  54. * "reader". It is only a very basic class for now
  55. *
  56. * @author Nick Burch
  57. */
  58. public final class HSLFSlideShow extends POIDocument {
  59. public static final int UNSET_OFFSET = -1;
  60. // For logging
  61. private POILogger logger = POILogFactory.getLogger(this.getClass());
  62. // Holds metadata on where things are in our document
  63. private CurrentUserAtom currentUser;
  64. // Low level contents of the file
  65. private byte[] _docstream;
  66. // Low level contents
  67. private Record[] _records;
  68. // Raw Pictures contained in the pictures stream
  69. private List<PictureData> _pictures;
  70. // Embedded objects stored in storage records in the document stream, lazily populated.
  71. private ObjectData[] _objects;
  72. /**
  73. * Returns the underlying POIFSFileSystem for the document
  74. * that is open.
  75. */
  76. protected POIFSFileSystem getPOIFSFileSystem() {
  77. return directory.getFileSystem();
  78. }
  79. /**
  80. * Returns the directory in the underlying POIFSFileSystem for the
  81. * document that is open.
  82. */
  83. protected DirectoryNode getPOIFSDirectory() {
  84. return directory;
  85. }
  86. /**
  87. * Constructs a Powerpoint document from fileName. Parses the document
  88. * and places all the important stuff into data structures.
  89. *
  90. * @param fileName The name of the file to read.
  91. * @throws IOException if there is a problem while parsing the document.
  92. */
  93. public HSLFSlideShow(String fileName) throws IOException
  94. {
  95. this(new FileInputStream(fileName));
  96. }
  97. /**
  98. * Constructs a Powerpoint document from an input stream. Parses the
  99. * document and places all the important stuff into data structures.
  100. *
  101. * @param inputStream the source of the data
  102. * @throws IOException if there is a problem while parsing the document.
  103. */
  104. public HSLFSlideShow(InputStream inputStream) throws IOException {
  105. //do Ole stuff
  106. this(new POIFSFileSystem(inputStream));
  107. }
  108. /**
  109. * Constructs a Powerpoint document from a POIFS Filesystem. Parses the
  110. * document and places all the important stuff into data structures.
  111. *
  112. * @param filesystem the POIFS FileSystem to read from
  113. * @throws IOException if there is a problem while parsing the document.
  114. */
  115. public HSLFSlideShow(POIFSFileSystem filesystem) throws IOException
  116. {
  117. this(filesystem.getRoot());
  118. }
  119. /**
  120. * Constructs a Powerpoint document from a POIFS Filesystem. Parses the
  121. * document and places all the important stuff into data structures.
  122. *
  123. * @param filesystem the POIFS FileSystem to read from
  124. * @throws IOException if there is a problem while parsing the document.
  125. */
  126. public HSLFSlideShow(NPOIFSFileSystem filesystem) throws IOException
  127. {
  128. this(filesystem.getRoot());
  129. }
  130. /**
  131. * Constructs a Powerpoint document from a specific point in a
  132. * POIFS Filesystem. Parses the document and places all the
  133. * important stuff into data structures.
  134. *
  135. * @deprecated Use {@link #HSLFSlideShow(DirectoryNode)} instead
  136. * @param dir the POIFS directory to read from
  137. * @param filesystem the POIFS FileSystem to read from
  138. * @throws IOException if there is a problem while parsing the document.
  139. */
  140. @Deprecated
  141. public HSLFSlideShow(DirectoryNode dir, POIFSFileSystem filesystem) throws IOException
  142. {
  143. this(dir);
  144. }
  145. /**
  146. * Constructs a Powerpoint document from a specific point in a
  147. * POIFS Filesystem. Parses the document and places all the
  148. * important stuff into data structures.
  149. *
  150. * @param dir the POIFS directory to read from
  151. * @throws IOException if there is a problem while parsing the document.
  152. */
  153. public HSLFSlideShow(DirectoryNode dir) throws IOException
  154. {
  155. super(dir);
  156. // First up, grab the "Current User" stream
  157. // We need this before we can detect Encrypted Documents
  158. readCurrentUserStream();
  159. // Next up, grab the data that makes up the
  160. // PowerPoint stream
  161. readPowerPointStream();
  162. // Check to see if we have an encrypted document,
  163. // bailing out if we do
  164. boolean encrypted = EncryptedSlideShow.checkIfEncrypted(this);
  165. if(encrypted) {
  166. throw new EncryptedPowerPointFileException("Encrypted PowerPoint files are not supported");
  167. }
  168. // Now, build records based on the PowerPoint stream
  169. buildRecords();
  170. // Look for any other streams
  171. readOtherStreams();
  172. }
  173. /**
  174. * Constructs a new, empty, Powerpoint document.
  175. */
  176. public static final HSLFSlideShow create() {
  177. InputStream is = HSLFSlideShow.class.getResourceAsStream("data/empty.ppt");
  178. if (is == null) {
  179. throw new RuntimeException("Missing resource 'empty.ppt'");
  180. }
  181. try {
  182. return new HSLFSlideShow(is);
  183. } catch (IOException e) {
  184. throw new RuntimeException(e);
  185. }
  186. }
  187. /**
  188. * Extracts the main PowerPoint document stream from the
  189. * POI file, ready to be passed
  190. *
  191. * @throws IOException
  192. */
  193. private void readPowerPointStream() throws IOException
  194. {
  195. // Get the main document stream
  196. DocumentEntry docProps =
  197. (DocumentEntry)directory.getEntry("PowerPoint Document");
  198. // Grab the document stream
  199. _docstream = new byte[docProps.getSize()];
  200. directory.createDocumentInputStream("PowerPoint Document").read(_docstream);
  201. }
  202. /**
  203. * Builds the list of records, based on the contents
  204. * of the PowerPoint stream
  205. */
  206. private void buildRecords()
  207. {
  208. // The format of records in a powerpoint file are:
  209. // <little endian 2 byte "info">
  210. // <little endian 2 byte "type">
  211. // <little endian 4 byte "length">
  212. // If it has a zero length, following it will be another record
  213. // <xx xx yy yy 00 00 00 00> <xx xx yy yy zz zz zz zz>
  214. // If it has a length, depending on its type it may have children or data
  215. // If it has children, these will follow straight away
  216. // <xx xx yy yy zz zz zz zz <xx xx yy yy zz zz zz zz>>
  217. // If it has data, this will come straigh after, and run for the length
  218. // <xx xx yy yy zz zz zz zz dd dd dd dd dd dd dd>
  219. // All lengths given exclude the 8 byte record header
  220. // (Data records are known as Atoms)
  221. // Document should start with:
  222. // 0F 00 E8 03 ## ## ## ##
  223. // (type 1000 = document, info 00 0f is normal, rest is document length)
  224. // 01 00 E9 03 28 00 00 00
  225. // (type 1001 = document atom, info 00 01 normal, 28 bytes long)
  226. // 80 16 00 00 E0 10 00 00 xx xx xx xx xx xx xx xx
  227. // 05 00 00 00 0A 00 00 00 xx xx xx
  228. // (the contents of the document atom, not sure what it means yet)
  229. // (records then follow)
  230. // When parsing a document, look to see if you know about that type
  231. // of the current record. If you know it's a type that has children,
  232. // process the record's data area looking for more records
  233. // If you know about the type and it doesn't have children, either do
  234. // something with the data (eg TextRun) or skip over it
  235. // If you don't know about the type, play safe and skip over it (using
  236. // its length to know where the next record will start)
  237. //
  238. _records = read(_docstream, (int)currentUser.getCurrentEditOffset());
  239. }
  240. private Record[] read(byte[] docstream, int usrOffset){
  241. ArrayList<Integer> lst = new ArrayList<Integer>();
  242. HashMap<Integer,Integer> offset2id = new HashMap<Integer,Integer>();
  243. while (usrOffset != 0){
  244. UserEditAtom usr = (UserEditAtom) Record.buildRecordAtOffset(docstream, usrOffset);
  245. lst.add(usrOffset);
  246. int psrOffset = usr.getPersistPointersOffset();
  247. PersistPtrHolder ptr = (PersistPtrHolder)Record.buildRecordAtOffset(docstream, psrOffset);
  248. lst.add(psrOffset);
  249. Hashtable<Integer,Integer> entries = ptr.getSlideLocationsLookup();
  250. for(Integer id : entries.keySet()) {
  251. Integer offset = entries.get(id);
  252. lst.add(offset);
  253. offset2id.put(offset, id);
  254. }
  255. usrOffset = usr.getLastUserEditAtomOffset();
  256. }
  257. //sort found records by offset.
  258. //(it is not necessary but SlideShow.findMostRecentCoreRecords() expects them sorted)
  259. Integer a[] = lst.toArray(new Integer[lst.size()]);
  260. Arrays.sort(a);
  261. Record[] rec = new Record[lst.size()];
  262. for (int i = 0; i < a.length; i++) {
  263. Integer offset = a[i];
  264. rec[i] = Record.buildRecordAtOffset(docstream, offset.intValue());
  265. if(rec[i] instanceof PersistRecord) {
  266. PersistRecord psr = (PersistRecord)rec[i];
  267. Integer id = offset2id.get(offset);
  268. psr.setPersistId(id.intValue());
  269. }
  270. }
  271. return rec;
  272. }
  273. /**
  274. * Find the "Current User" stream, and load it
  275. */
  276. private void readCurrentUserStream() {
  277. try {
  278. currentUser = new CurrentUserAtom(directory);
  279. } catch(IOException ie) {
  280. logger.log(POILogger.ERROR, "Error finding Current User Atom:\n" + ie);
  281. currentUser = new CurrentUserAtom();
  282. }
  283. }
  284. /**
  285. * Find any other streams from the filesystem, and load them
  286. */
  287. private void readOtherStreams() {
  288. // Currently, there aren't any
  289. }
  290. /**
  291. * Find and read in pictures contained in this presentation.
  292. * This is lazily called as and when we want to touch pictures.
  293. */
  294. private void readPictures() throws IOException {
  295. _pictures = new ArrayList<PictureData>();
  296. byte[] pictstream;
  297. try {
  298. DocumentEntry entry = (DocumentEntry)directory.getEntry("Pictures");
  299. pictstream = new byte[entry.getSize()];
  300. DocumentInputStream is = directory.createDocumentInputStream("Pictures");
  301. is.read(pictstream);
  302. } catch (FileNotFoundException e){
  303. // Silently catch exceptions if the presentation doesn't
  304. // contain pictures - will use a null set instead
  305. return;
  306. }
  307. int pos = 0;
  308. // An empty picture record (length 0) will take up 8 bytes
  309. while (pos <= (pictstream.length-8)) {
  310. int offset = pos;
  311. // Image signature
  312. @SuppressWarnings("unused")
  313. int signature = LittleEndian.getUShort(pictstream, pos);
  314. pos += LittleEndian.SHORT_SIZE;
  315. // Image type + 0xF018
  316. int type = LittleEndian.getUShort(pictstream, pos);
  317. pos += LittleEndian.SHORT_SIZE;
  318. // Image size (excluding the 8 byte header)
  319. int imgsize = LittleEndian.getInt(pictstream, pos);
  320. pos += LittleEndian.INT_SIZE;
  321. // When parsing the BStoreDelay stream, [MS-ODRAW] says that we
  322. // should terminate if the type isn't 0xf007 or 0xf018->0xf117
  323. if (!((type == 0xf007) || (type >= 0xf018 && type <= 0xf117)))
  324. break;
  325. // The image size must be 0 or greater
  326. // (0 is allowed, but odd, since we do wind on by the header each
  327. // time, so we won't get stuck)
  328. if(imgsize < 0) {
  329. throw new CorruptPowerPointFileException("The file contains a picture, at position " + _pictures.size() + ", which has a negatively sized data length, so we can't trust any of the picture data");
  330. }
  331. // If they type (including the bonus 0xF018) is 0, skip it
  332. if(type == 0) {
  333. logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
  334. logger.log(POILogger.ERROR, "" + pos);
  335. } else {
  336. // Build the PictureData object from the data
  337. try {
  338. PictureData pict = PictureData.create(type - 0xF018);
  339. // Copy the data, ready to pass to PictureData
  340. byte[] imgdata = new byte[imgsize];
  341. System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length);
  342. pict.setRawData(imgdata);
  343. pict.setOffset(offset);
  344. _pictures.add(pict);
  345. } catch(IllegalArgumentException e) {
  346. logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
  347. }
  348. }
  349. pos += imgsize;
  350. }
  351. }
  352. /**
  353. * This is a helper functions, which is needed for adding new position dependent records
  354. * or finally write the slideshow to a file.
  355. *
  356. * @param os the stream to write to, if null only the references are updated
  357. * @param interestingRecords a map of interesting records (PersistPtrHolder and UserEditAtom)
  358. * referenced by their RecordType. Only the very last of each type will be saved to the map.
  359. * May be null, if not needed.
  360. * @throws IOException
  361. */
  362. public void updateAndWriteDependantRecords(OutputStream os, Map<RecordTypes.Type,PositionDependentRecord> interestingRecords)
  363. throws IOException {
  364. // For position dependent records, hold where they were and now are
  365. // As we go along, update, and hand over, to any Position Dependent
  366. // records we happen across
  367. Hashtable<Integer,Integer> oldToNewPositions = new Hashtable<Integer,Integer>();
  368. // First pass - figure out where all the position dependent
  369. // records are going to end up, in the new scheme
  370. // (Annoyingly, some powerpoint files have PersistPtrHolders
  371. // that reference slides after the PersistPtrHolder)
  372. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  373. for (Record record : _records) {
  374. if(record instanceof PositionDependentRecord) {
  375. PositionDependentRecord pdr = (PositionDependentRecord)record;
  376. int oldPos = pdr.getLastOnDiskOffset();
  377. int newPos = baos.size();
  378. pdr.setLastOnDiskOffset(newPos);
  379. if (oldPos != UNSET_OFFSET) {
  380. // new records don't need a mapping, as they aren't in a relation yet
  381. oldToNewPositions.put(Integer.valueOf(oldPos),Integer.valueOf(newPos));
  382. }
  383. }
  384. // Dummy write out, so the position winds on properly
  385. record.writeOut(baos);
  386. }
  387. baos = null;
  388. // For now, we're only handling PositionDependentRecord's that
  389. // happen at the top level.
  390. // In future, we'll need the handle them everywhere, but that's
  391. // a bit trickier
  392. UserEditAtom usr = null;
  393. for (Record record : _records) {
  394. if (record instanceof PositionDependentRecord) {
  395. // We've already figured out their new location, and
  396. // told them that
  397. // Tell them of the positions of the other records though
  398. PositionDependentRecord pdr = (PositionDependentRecord)record;
  399. pdr.updateOtherRecordReferences(oldToNewPositions);
  400. // Grab interesting records as they come past
  401. // this will only save the very last record of each type
  402. RecordTypes.Type saveme = null;
  403. int recordType = (int)record.getRecordType();
  404. if (recordType == RecordTypes.PersistPtrIncrementalBlock.typeID) {
  405. saveme = RecordTypes.PersistPtrIncrementalBlock;
  406. } else if (recordType == RecordTypes.UserEditAtom.typeID) {
  407. saveme = RecordTypes.UserEditAtom;
  408. usr = (UserEditAtom)pdr;
  409. }
  410. if (interestingRecords != null && saveme != null) {
  411. interestingRecords.put(saveme,pdr);
  412. }
  413. }
  414. // Whatever happens, write out that record tree
  415. if (os != null) {
  416. record.writeOut(os);
  417. }
  418. }
  419. // Update and write out the Current User atom
  420. int oldLastUserEditAtomPos = (int)currentUser.getCurrentEditOffset();
  421. Integer newLastUserEditAtomPos = oldToNewPositions.get(oldLastUserEditAtomPos);
  422. if(usr == null || newLastUserEditAtomPos == null || usr.getLastOnDiskOffset() != newLastUserEditAtomPos) {
  423. throw new HSLFException("Couldn't find the new location of the last UserEditAtom that used to be at " + oldLastUserEditAtomPos);
  424. }
  425. currentUser.setCurrentEditOffset(usr.getLastOnDiskOffset());
  426. }
  427. /**
  428. * Writes out the slideshow file the is represented by an instance
  429. * of this class.
  430. * It will write out the common OLE2 streams. If you require all
  431. * streams to be written out, pass in preserveNodes
  432. * @param out The OutputStream to write to.
  433. * @throws IOException If there is an unexpected IOException from
  434. * the passed in OutputStream
  435. */
  436. public void write(OutputStream out) throws IOException {
  437. // Write out, but only the common streams
  438. write(out,false);
  439. }
  440. /**
  441. * Writes out the slideshow file the is represented by an instance
  442. * of this class.
  443. * If you require all streams to be written out (eg Marcos, embeded
  444. * documents), then set preserveNodes to true
  445. * @param out The OutputStream to write to.
  446. * @param preserveNodes Should all OLE2 streams be written back out, or only the common ones?
  447. * @throws IOException If there is an unexpected IOException from
  448. * the passed in OutputStream
  449. */
  450. public void write(OutputStream out, boolean preserveNodes) throws IOException {
  451. // Get a new Filesystem to write into
  452. POIFSFileSystem outFS = new POIFSFileSystem();
  453. // The list of entries we've written out
  454. List<String> writtenEntries = new ArrayList<String>(1);
  455. // Write out the Property Streams
  456. writeProperties(outFS, writtenEntries);
  457. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  458. // For position dependent records, hold where they were and now are
  459. // As we go along, update, and hand over, to any Position Dependent
  460. // records we happen across
  461. updateAndWriteDependantRecords(baos, null);
  462. // Update our cached copy of the bytes that make up the PPT stream
  463. _docstream = baos.toByteArray();
  464. // Write the PPT stream into the POIFS layer
  465. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  466. outFS.createDocument(bais,"PowerPoint Document");
  467. writtenEntries.add("PowerPoint Document");
  468. currentUser.writeToFS(outFS);
  469. writtenEntries.add("Current User");
  470. // Write any pictures, into another stream
  471. if(_pictures == null) {
  472. readPictures();
  473. }
  474. if (_pictures.size() > 0) {
  475. ByteArrayOutputStream pict = new ByteArrayOutputStream();
  476. for (PictureData p : _pictures) {
  477. p.write(pict);
  478. }
  479. outFS.createDocument(
  480. new ByteArrayInputStream(pict.toByteArray()), "Pictures"
  481. );
  482. writtenEntries.add("Pictures");
  483. }
  484. // If requested, write out any other streams we spot
  485. if(preserveNodes) {
  486. EntryUtils.copyNodes(directory.getFileSystem(), outFS, writtenEntries);
  487. }
  488. // Send the POIFSFileSystem object out to the underlying stream
  489. outFS.writeFilesystem(out);
  490. }
  491. /* ******************* adding methods follow ********************* */
  492. /**
  493. * Adds a new root level record, at the end, but before the last
  494. * PersistPtrIncrementalBlock.
  495. */
  496. public synchronized int appendRootLevelRecord(Record newRecord) {
  497. int addedAt = -1;
  498. Record[] r = new Record[_records.length+1];
  499. boolean added = false;
  500. for(int i=(_records.length-1); i>=0; i--) {
  501. if(added) {
  502. // Just copy over
  503. r[i] = _records[i];
  504. } else {
  505. r[(i+1)] = _records[i];
  506. if(_records[i] instanceof PersistPtrHolder) {
  507. r[i] = newRecord;
  508. added = true;
  509. addedAt = i;
  510. }
  511. }
  512. }
  513. _records = r;
  514. return addedAt;
  515. }
  516. /**
  517. * Add a new picture to this presentation.
  518. *
  519. * @return offset of this picture in the Pictures stream
  520. */
  521. public int addPicture(PictureData img) {
  522. // Process any existing pictures if we haven't yet
  523. if(_pictures == null) {
  524. try {
  525. readPictures();
  526. } catch(IOException e) {
  527. throw new CorruptPowerPointFileException(e.getMessage());
  528. }
  529. }
  530. // Add the new picture in
  531. int offset = 0;
  532. if(_pictures.size() > 0) {
  533. PictureData prev = _pictures.get(_pictures.size() - 1);
  534. offset = prev.getOffset() + prev.getRawData().length + 8;
  535. }
  536. img.setOffset(offset);
  537. _pictures.add(img);
  538. return offset;
  539. }
  540. /* ******************* fetching methods follow ********************* */
  541. /**
  542. * Returns an array of all the records found in the slideshow
  543. */
  544. public Record[] getRecords() { return _records; }
  545. /**
  546. * Returns an array of the bytes of the file. Only correct after a
  547. * call to open or write - at all other times might be wrong!
  548. */
  549. public byte[] getUnderlyingBytes() { return _docstream; }
  550. /**
  551. * Fetch the Current User Atom of the document
  552. */
  553. public CurrentUserAtom getCurrentUserAtom() { return currentUser; }
  554. /**
  555. * Return array of pictures contained in this presentation
  556. *
  557. * @return array with the read pictures or <code>null</code> if the
  558. * presentation doesn't contain pictures.
  559. */
  560. public PictureData[] getPictures() {
  561. if(_pictures == null) {
  562. try {
  563. readPictures();
  564. } catch(IOException e) {
  565. throw new CorruptPowerPointFileException(e.getMessage());
  566. }
  567. }
  568. return _pictures.toArray(new PictureData[_pictures.size()]);
  569. }
  570. /**
  571. * Gets embedded object data from the slide show.
  572. *
  573. * @return the embedded objects.
  574. */
  575. public ObjectData[] getEmbeddedObjects() {
  576. if (_objects == null) {
  577. List<ObjectData> objects = new ArrayList<ObjectData>();
  578. for (Record r : _records) {
  579. if (r instanceof ExOleObjStg) {
  580. objects.add(new ObjectData((ExOleObjStg)r));
  581. }
  582. }
  583. _objects = objects.toArray(new ObjectData[objects.size()]);
  584. }
  585. return _objects;
  586. }
  587. }