Search in sources :

Example 6 with UserEditAtom

use of org.apache.poi.hslf.record.UserEditAtom in project poi by apache.

the class HSLFSlideShowImpl method updateAndWriteDependantRecords.

/**
     * This is a helper functions, which is needed for adding new position dependent records
     * or finally write the slideshow to a file.
     *
     * @param os                 the stream to write to, if null only the references are updated
     * @param interestingRecords a map of interesting records (PersistPtrHolder and UserEditAtom)
     *                           referenced by their RecordType. Only the very last of each type will be saved to the map.
     *                           May be null, if not needed.
     * @throws IOException
     */
public void updateAndWriteDependantRecords(OutputStream os, Map<RecordTypes, PositionDependentRecord> interestingRecords) throws IOException {
    // For position dependent records, hold where they were and now are
    // As we go along, update, and hand over, to any Position Dependent
    //  records we happen across
    Map<Integer, Integer> oldToNewPositions = new HashMap<Integer, Integer>();
    // First pass - figure out where all the position dependent
    //   records are going to end up, in the new scheme
    // (Annoyingly, some powerpoint files have PersistPtrHolders
    //  that reference slides after the PersistPtrHolder)
    UserEditAtom usr = null;
    PersistPtrHolder ptr = null;
    CountingOS cos = new CountingOS();
    for (Record record : _records) {
        // all top level records are position dependent
        assert (record instanceof PositionDependentRecord);
        PositionDependentRecord pdr = (PositionDependentRecord) record;
        int oldPos = pdr.getLastOnDiskOffset();
        int newPos = cos.size();
        pdr.setLastOnDiskOffset(newPos);
        if (oldPos != UNSET_OFFSET) {
            // new records don't need a mapping, as they aren't in a relation yet
            oldToNewPositions.put(oldPos, newPos);
        }
        // Grab interesting records as they come past
        // this will only save the very last record of each type
        RecordTypes saveme = null;
        int recordType = (int) record.getRecordType();
        if (recordType == RecordTypes.PersistPtrIncrementalBlock.typeID) {
            saveme = RecordTypes.PersistPtrIncrementalBlock;
            ptr = (PersistPtrHolder) pdr;
        } else if (recordType == RecordTypes.UserEditAtom.typeID) {
            saveme = RecordTypes.UserEditAtom;
            usr = (UserEditAtom) pdr;
        }
        if (interestingRecords != null && saveme != null) {
            interestingRecords.put(saveme, pdr);
        }
        // Dummy write out, so the position winds on properly
        record.writeOut(cos);
    }
    cos.close();
    if (usr == null || ptr == null) {
        throw new HSLFException("UserEditAtom or PersistPtr can't be determined.");
    }
    Map<Integer, Integer> persistIds = new HashMap<Integer, Integer>();
    for (Map.Entry<Integer, Integer> entry : ptr.getSlideLocationsLookup().entrySet()) {
        persistIds.put(oldToNewPositions.get(entry.getValue()), entry.getKey());
    }
    HSLFSlideShowEncrypted encData = new HSLFSlideShowEncrypted(getDocumentEncryptionAtom());
    for (Record record : _records) {
        assert (record instanceof PositionDependentRecord);
        // We've already figured out their new location, and
        // told them that
        // Tell them of the positions of the other records though
        PositionDependentRecord pdr = (PositionDependentRecord) record;
        Integer persistId = persistIds.get(pdr.getLastOnDiskOffset());
        if (persistId == null) {
            persistId = 0;
        }
        // For now, we're only handling PositionDependentRecord's that
        // happen at the top level.
        // In future, we'll need the handle them everywhere, but that's
        // a bit trickier
        pdr.updateOtherRecordReferences(oldToNewPositions);
        // Whatever happens, write out that record tree
        if (os != null) {
            record.writeOut(encData.encryptRecord(os, persistId, record));
        }
    }
    encData.close();
    // Update and write out the Current User atom
    int oldLastUserEditAtomPos = (int) currentUser.getCurrentEditOffset();
    Integer newLastUserEditAtomPos = oldToNewPositions.get(oldLastUserEditAtomPos);
    if (newLastUserEditAtomPos == null || usr.getLastOnDiskOffset() != newLastUserEditAtomPos) {
        throw new HSLFException("Couldn't find the new location of the last UserEditAtom that used to be at " + oldLastUserEditAtomPos);
    }
    currentUser.setCurrentEditOffset(usr.getLastOnDiskOffset());
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) HashMap(java.util.HashMap) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) Record(org.apache.poi.hslf.record.Record) PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) PersistRecord(org.apache.poi.hslf.record.PersistRecord) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) RecordTypes(org.apache.poi.hslf.record.RecordTypes)

Example 7 with UserEditAtom

use of org.apache.poi.hslf.record.UserEditAtom in project poi by apache.

the class UserEditAndPersistListing method main.

public static void main(String[] args) throws IOException {
    if (args.length < 1) {
        System.err.println("Need to give a filename");
        System.exit(1);
    }
    // Create the slideshow object, for normal working with
    HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]);
    fileContents = ss.getUnderlyingBytes();
    System.out.println("");
    // Find any persist ones first
    int pos = 0;
    for (Record r : ss.getRecords()) {
        if (r.getRecordType() == 6001l) {
            // PersistPtrFullBlock
            System.out.println("Found PersistPtrFullBlock at " + pos + " (" + Integer.toHexString(pos) + ")");
        }
        if (r.getRecordType() == 6002l) {
            // PersistPtrIncrementalBlock
            System.out.println("Found PersistPtrIncrementalBlock at " + pos + " (" + Integer.toHexString(pos) + ")");
            PersistPtrHolder pph = (PersistPtrHolder) r;
            // Check the sheet offsets
            Map<Integer, Integer> sheetOffsets = pph.getSlideLocationsLookup();
            for (int id : pph.getKnownSlideIDs()) {
                Integer offset = sheetOffsets.get(id);
                System.out.println("  Knows about sheet " + id);
                System.out.println("    That sheet lives at " + offset);
                Record atPos = findRecordAtPos(offset.intValue());
                System.out.println("    The record at that pos is of type " + atPos.getRecordType());
                System.out.println("    The record at that pos has class " + atPos.getClass().getName());
                if (!(atPos instanceof PositionDependentRecord)) {
                    System.out.println("    ** The record class isn't position aware! **");
                }
            }
        }
        // Increase the position by the on disk size
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        r.writeOut(baos);
        pos += baos.size();
    }
    System.out.println("");
    pos = 0;
    // Now look for UserEditAtoms
    for (Record r : ss.getRecords()) {
        if (r instanceof UserEditAtom) {
            UserEditAtom uea = (UserEditAtom) r;
            System.out.println("Found UserEditAtom at " + pos + " (" + Integer.toHexString(pos) + ")");
            System.out.println("  lastUserEditAtomOffset = " + uea.getLastUserEditAtomOffset());
            System.out.println("  persistPointersOffset  = " + uea.getPersistPointersOffset());
            System.out.println("  docPersistRef          = " + uea.getDocPersistRef());
            System.out.println("  maxPersistWritten      = " + uea.getMaxPersistWritten());
        }
        // Increase the position by the on disk size
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        r.writeOut(baos);
        pos += baos.size();
    }
    System.out.println("");
    // Query the CurrentUserAtom
    CurrentUserAtom cua = ss.getCurrentUserAtom();
    System.out.println("Checking Current User Atom");
    System.out.println("  Thinks the CurrentEditOffset is " + cua.getCurrentEditOffset());
    System.out.println("");
    ss.close();
}
Also used : PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) Record(org.apache.poi.hslf.record.Record) PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) CurrentUserAtom(org.apache.poi.hslf.record.CurrentUserAtom) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)

Example 8 with UserEditAtom

use of org.apache.poi.hslf.record.UserEditAtom in project poi by apache.

the class TestAddingSlides method testAddSlideToExisting2.

/**
     * Test adding a slide to an existing slideshow, with two slides already
     */
@Test
public void testAddSlideToExisting2() throws IOException {
    // grab UserEditAtom
    UserEditAtom usredit = null;
    Record[] _records = ss_two.getSlideShowImpl().getRecords();
    for (Record record : _records) {
        if (record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
            usredit = (UserEditAtom) record;
        }
    }
    assertNotNull(usredit);
    // Has two slides
    assertEquals(2, ss_two.getSlides().size());
    HSLFSlide s1 = ss_two.getSlides().get(0);
    HSLFSlide s2 = ss_two.getSlides().get(1);
    // Check slide 1 is as expected
    assertEquals(256, s1._getSheetNumber());
    // master has notes
    assertEquals(4, s1._getSheetRefId());
    assertEquals(1, s1.getSlideNumber());
    // Check slide 2 is as expected
    assertEquals(257, s2._getSheetNumber());
    // master and 1 have notes
    assertEquals(6, s2._getSheetRefId());
    assertEquals(2, s2.getSlideNumber());
    // Add a third one
    HSLFSlide s3 = ss_two.createSlide();
    assertEquals(3, ss_two.getSlides().size());
    assertEquals(258, s3._getSheetNumber());
    // lots of notes before us
    assertEquals(8, s3._getSheetRefId());
    assertEquals(3, s3.getSlideNumber());
    assertEquals(usredit.getMaxPersistWritten(), s3._getSheetRefId());
    // Write out, and read back in
    HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_two);
    // Check it now has three slides
    assertEquals(3, ss_read.getSlides().size());
    // And check it's as expected
    s1 = ss_read.getSlides().get(0);
    s2 = ss_read.getSlides().get(1);
    s3 = ss_read.getSlides().get(2);
    assertEquals(256, s1._getSheetNumber());
    assertEquals(4, s1._getSheetRefId());
    assertEquals(1, s1.getSlideNumber());
    assertEquals(257, s2._getSheetNumber());
    assertEquals(6, s2._getSheetRefId());
    assertEquals(2, s2.getSlideNumber());
    assertEquals(258, s3._getSheetNumber());
    assertEquals(8, s3._getSheetRefId());
    assertEquals(3, s3.getSlideNumber());
    ss_read.close();
}
Also used : Record(org.apache.poi.hslf.record.Record) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) Test(org.junit.Test)

Example 9 with UserEditAtom

use of org.apache.poi.hslf.record.UserEditAtom in project poi by apache.

the class TestAddingSlides method testAddSlideToEmpty.

/**
     * Test adding a slide to an empty slideshow
     */
@Test
public void testAddSlideToEmpty() throws IOException {
    // Doesn't have any slides
    assertEquals(0, ss_empty.getSlides().size());
    // Should only have a master SLWT
    assertEquals(1, ss_empty.getDocumentRecord().getSlideListWithTexts().length);
    // grab UserEditAtom
    UserEditAtom usredit = null;
    Record[] _records = ss_empty.getSlideShowImpl().getRecords();
    for (Record record : _records) {
        if (record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
            usredit = (UserEditAtom) record;
        }
    }
    assertNotNull(usredit);
    // Add one
    HSLFSlide slide = ss_empty.createSlide();
    assertEquals(1, ss_empty.getSlides().size());
    assertEquals(256, slide._getSheetNumber());
    assertEquals(3, slide._getSheetRefId());
    assertEquals(1, slide.getSlideNumber());
    assertEquals(usredit.getMaxPersistWritten(), slide._getSheetRefId());
    // Write out, and read back in
    HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_empty);
    // Check it now has a slide
    assertEquals(1, ss_read.getSlides().size());
    // Check it now has two SLWTs
    assertEquals(2, ss_empty.getDocumentRecord().getSlideListWithTexts().length);
    // And check it's as expected
    slide = ss_read.getSlides().get(0);
    assertEquals(256, slide._getSheetNumber());
    assertEquals(3, slide._getSheetRefId());
    assertEquals(1, slide.getSlideNumber());
    ss_read.close();
}
Also used : Record(org.apache.poi.hslf.record.Record) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) Test(org.junit.Test)

Example 10 with UserEditAtom

use of org.apache.poi.hslf.record.UserEditAtom in project poi by apache.

the class TestReWriteSanity method testUserEditAtomsRight.

@Test
public void testUserEditAtomsRight() throws Exception {
    // Write out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ss.write(baos);
    // Build an input stream of it
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    // Create a new one from that
    HSLFSlideShowImpl wss = new HSLFSlideShowImpl(bais);
    // Find the location of the PersistPtrIncrementalBlocks and
    // UserEditAtoms
    Record[] r = wss.getRecords();
    Map<Integer, Record> pp = new HashMap<Integer, Record>();
    Map<Integer, Object> ue = new HashMap<Integer, Object>();
    // Will show 0 if first
    ue.put(Integer.valueOf(0), Integer.valueOf(0));
    int pos = 0;
    int lastUEPos = -1;
    for (final Record rec : r) {
        if (rec instanceof PersistPtrHolder) {
            pp.put(Integer.valueOf(pos), rec);
        }
        if (rec instanceof UserEditAtom) {
            ue.put(Integer.valueOf(pos), rec);
            lastUEPos = pos;
        }
        ByteArrayOutputStream bc = new ByteArrayOutputStream();
        rec.writeOut(bc);
        pos += bc.size();
    }
    // Check that the UserEditAtom's point to right stuff
    for (final Record rec : r) {
        if (rec instanceof UserEditAtom) {
            UserEditAtom uea = (UserEditAtom) rec;
            int luPos = uea.getLastUserEditAtomOffset();
            int ppPos = uea.getPersistPointersOffset();
            assertContains(ue, Integer.valueOf(luPos));
            assertContains(pp, Integer.valueOf(ppPos));
        }
    }
    // Check that the CurrentUserAtom points to the right UserEditAtom
    CurrentUserAtom cua = wss.getCurrentUserAtom();
    int listedUEPos = (int) cua.getCurrentEditOffset();
    assertEquals(lastUEPos, listedUEPos);
    wss.close();
}
Also used : HashMap(java.util.HashMap) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CurrentUserAtom(org.apache.poi.hslf.record.CurrentUserAtom) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Record(org.apache.poi.hslf.record.Record) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) Test(org.junit.Test)

Aggregations

UserEditAtom (org.apache.poi.hslf.record.UserEditAtom)10 PersistPtrHolder (org.apache.poi.hslf.record.PersistPtrHolder)8 Record (org.apache.poi.hslf.record.Record)8 PositionDependentRecord (org.apache.poi.hslf.record.PositionDependentRecord)5 Map (java.util.Map)4 NavigableMap (java.util.NavigableMap)4 TreeMap (java.util.TreeMap)4 HashMap (java.util.HashMap)3 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)3 DocumentEncryptionAtom (org.apache.poi.hslf.record.DocumentEncryptionAtom)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 CorruptPowerPointFileException (org.apache.poi.hslf.exceptions.CorruptPowerPointFileException)2 CurrentUserAtom (org.apache.poi.hslf.record.CurrentUserAtom)2 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 EncryptedPowerPointFileException (org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException)1