Search in sources :

Example 16 with HSLFException

use of org.apache.poi.hslf.exceptions.HSLFException 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 17 with HSLFException

use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.

the class HSLFTextParagraph method refreshRecords.

/**
     * Writes the textbox records back to the document record 
     */
private static void refreshRecords(List<HSLFTextParagraph> paragraphs) {
    TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
    RecordContainer _txtbox = headerAtom.getParentRecord();
    if (_txtbox instanceof EscherTextboxWrapper) {
        try {
            ((EscherTextboxWrapper) _txtbox).writeOut(null);
        } catch (IOException e) {
            throw new HSLFException("failed dummy write", e);
        }
    }
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) RecordContainer(org.apache.poi.hslf.record.RecordContainer) EscherTextboxWrapper(org.apache.poi.hslf.record.EscherTextboxWrapper) IOException(java.io.IOException) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom)

Example 18 with HSLFException

use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.

the class HSLFTextShape method afterInsert.

/**
     * When a textbox is added to  a sheet we need to tell upper-level
     * <code>PPDrawing</code> about it.
     *
     * @param sh the sheet we are adding to
     */
@Override
protected void afterInsert(HSLFSheet sh) {
    super.afterInsert(sh);
    storeText();
    EscherTextboxWrapper thisTxtbox = getEscherTextboxWrapper();
    if (thisTxtbox != null) {
        getSpContainer().addChildRecord(thisTxtbox.getEscherRecord());
        PPDrawing ppdrawing = sh.getPPDrawing();
        ppdrawing.addTextboxWrapper(thisTxtbox);
        // Ensure the escher layer knows about the added records
        try {
            thisTxtbox.writeOut(null);
        } catch (IOException e) {
            throw new HSLFException(e);
        }
        boolean isInitialAnchor = getAnchor().equals(new Rectangle2D.Double());
        boolean isFilledTxt = !"".equals(getText());
        if (isInitialAnchor && isFilledTxt) {
            resizeToFitText();
        }
    }
    for (HSLFTextParagraph htp : _paragraphs) {
        htp.setShapeId(getShapeId());
    }
    sh.onAddTextShape(this);
}
Also used : PPDrawing(org.apache.poi.hslf.record.PPDrawing) HSLFException(org.apache.poi.hslf.exceptions.HSLFException) EscherTextboxWrapper(org.apache.poi.hslf.record.EscherTextboxWrapper) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException)

Example 19 with HSLFException

use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.

the class PersistPtrHolder method normalizePersistDirectory.

private void normalizePersistDirectory() {
    TreeMap<Integer, Integer> orderedSlideLocations = new TreeMap<Integer, Integer>(_slideLocations);
    @SuppressWarnings("resource") BufAccessBAOS // NOSONAR
    bos = new BufAccessBAOS();
    byte[] intbuf = new byte[4];
    int lastPersistEntry = -1;
    int lastSlideId = -1;
    for (Entry<Integer, Integer> me : orderedSlideLocations.entrySet()) {
        int nextSlideId = me.getKey();
        int offset = me.getValue();
        try {
            if (lastSlideId + 1 == nextSlideId) {
                // use existing PersistDirectoryEntry, need to increase entry count
                assert (lastPersistEntry != -1);
                int infoBlock = LittleEndian.getInt(bos.getBuf(), lastPersistEntry);
                int entryCnt = cntPersistFld.getValue(infoBlock);
                infoBlock = cntPersistFld.setValue(infoBlock, entryCnt + 1);
                LittleEndian.putInt(bos.getBuf(), lastPersistEntry, infoBlock);
            } else {
                // start new PersistDirectoryEntry
                lastPersistEntry = bos.size();
                int infoBlock = persistIdFld.setValue(0, nextSlideId);
                infoBlock = cntPersistFld.setValue(infoBlock, 1);
                LittleEndian.putInt(intbuf, 0, infoBlock);
                bos.write(intbuf);
            }
            // Add to the ptrData offset lookup hash
            LittleEndian.putInt(intbuf, 0, offset);
            bos.write(intbuf);
            lastSlideId = nextSlideId;
        } catch (IOException e) {
            // ByteArrayOutputStream is very unlikely throwing a IO exception (maybe because of OOM ...)
            throw new HSLFException(e);
        }
    }
    // Save the new ptr data
    _ptrData = bos.toByteArray();
    // Update the atom header
    LittleEndian.putInt(_header, 4, bos.size());
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

Example 20 with HSLFException

use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.

the class WMF method getData.

@Override
public byte[] getData() {
    try {
        byte[] rawdata = getRawData();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream is = new ByteArrayInputStream(rawdata);
        Header header = new Header();
        header.read(rawdata, CHECKSUM_SIZE * getUIDInstanceCount());
        long len = is.skip(header.getSize() + (long) CHECKSUM_SIZE * getUIDInstanceCount());
        assert (len == header.getSize() + CHECKSUM_SIZE * getUIDInstanceCount());
        ImageHeaderWMF aldus = new ImageHeaderWMF(header.getBounds());
        aldus.write(out);
        InflaterInputStream inflater = new InflaterInputStream(is);
        byte[] chunk = new byte[4096];
        int count;
        while ((count = inflater.read(chunk)) >= 0) {
            out.write(chunk, 0, count);
        }
        inflater.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new HSLFException(e);
    }
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ImageHeaderWMF(org.apache.poi.sl.image.ImageHeaderWMF) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

HSLFException (org.apache.poi.hslf.exceptions.HSLFException)20 IOException (java.io.IOException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 DrawPaint (org.apache.poi.sl.draw.DrawPaint)4 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)3 Record (org.apache.poi.hslf.record.Record)3 InputStream (java.io.InputStream)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)2 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)2 EscherTextboxWrapper (org.apache.poi.hslf.record.EscherTextboxWrapper)2 TextHeaderAtom (org.apache.poi.hslf.record.TextHeaderAtom)2 Rectangle2D (java.awt.geom.Rectangle2D)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1