Search in sources :

Example 6 with HSLFException

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

the class HSLFSlideShow method addEmbed.

/**
	 * Add a embedded object to this presentation
	 *
	 * @return 0-based index of the embedded object
	 */
public int addEmbed(POIFSFileSystem poiData) {
    DirectoryNode root = poiData.getRoot();
    // prepare embedded data
    if (new ClassID().equals(root.getStorageClsid())) {
        // need to set class id
        Map<String, ClassID> olemap = getOleMap();
        ClassID classID = null;
        for (Map.Entry<String, ClassID> entry : olemap.entrySet()) {
            if (root.hasEntry(entry.getKey())) {
                classID = entry.getValue();
                break;
            }
        }
        if (classID == null) {
            throw new IllegalArgumentException("Unsupported embedded document");
        }
        root.setStorageClsid(classID);
    }
    ExEmbed exEmbed = new ExEmbed();
    // remove unneccessary infos, so we don't need to specify the type
    // of the ole object multiple times
    Record[] children = exEmbed.getChildRecords();
    exEmbed.removeChild(children[2]);
    exEmbed.removeChild(children[3]);
    exEmbed.removeChild(children[4]);
    ExEmbedAtom eeEmbed = exEmbed.getExEmbedAtom();
    eeEmbed.setCantLockServerB(true);
    ExOleObjAtom eeAtom = exEmbed.getExOleObjAtom();
    eeAtom.setDrawAspect(ExOleObjAtom.DRAW_ASPECT_VISIBLE);
    eeAtom.setType(ExOleObjAtom.TYPE_EMBEDDED);
    // eeAtom.setSubType(ExOleObjAtom.SUBTYPE_EXCEL);
    // should be ignored?!?, see MS-PPT ExOleObjAtom, but Libre Office sets it ...
    eeAtom.setOptions(1226240);
    ExOleObjStg exOleObjStg = new ExOleObjStg();
    try {
        final String OLESTREAM_NAME = "Ole";
        if (!root.hasEntry(OLESTREAM_NAME)) {
            // the following data was taken from an example libre office document
            // beside this "Ole" record there were several other records, e.g. CompObj,
            // OlePresXXX, but it seems, that they aren't neccessary
            byte[] oleBytes = { 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            poiData.createDocument(new ByteArrayInputStream(oleBytes), OLESTREAM_NAME);
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        poiData.writeFilesystem(bos);
        exOleObjStg.setData(bos.toByteArray());
    } catch (IOException e) {
        throw new HSLFException(e);
    }
    int psrId = addPersistentObject(exOleObjStg);
    exOleObjStg.setPersistId(psrId);
    eeAtom.setObjStgDataRef(psrId);
    int objectId = addToObjListAtom(exEmbed);
    eeAtom.setObjID(objectId);
    return objectId;
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) ClassID(org.apache.poi.hpsf.ClassID) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with HSLFException

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

the class HSLFSlideShow method addPersistentObject.

protected int addPersistentObject(PositionDependentRecord slideRecord) {
    slideRecord.setLastOnDiskOffset(HSLFSlideShowImpl.UNSET_OFFSET);
    _hslfSlideShow.appendRootLevelRecord((Record) slideRecord);
    // 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<RecordTypes, PositionDependentRecord> interestingRecords = new HashMap<RecordTypes, PositionDependentRecord>();
    try {
        _hslfSlideShow.updateAndWriteDependantRecords(null, interestingRecords);
    } catch (IOException e) {
        throw new HSLFException(e);
    }
    PersistPtrHolder ptr = (PersistPtrHolder) interestingRecords.get(RecordTypes.PersistPtrIncrementalBlock);
    UserEditAtom usr = (UserEditAtom) interestingRecords.get(RecordTypes.UserEditAtom);
    // persist ID is UserEditAtom.maxPersistWritten + 1
    int psrId = usr.getMaxPersistWritten() + 1;
    // Last view is now of the slide
    usr.setLastViewType((short) UserEditAtom.LAST_VIEW_SLIDE_VIEW);
    // increment the number of persistent objects
    usr.setMaxPersistWritten(psrId);
    // Add the new slide into the last PersistPtr
    // (Also need to tell it where it is)
    int slideOffset = slideRecord.getLastOnDiskOffset();
    slideRecord.setLastOnDiskOffset(slideOffset);
    ptr.addSlideLookup(psrId, slideOffset);
    logger.log(POILogger.INFO, "New slide/object ended up at " + slideOffset);
    return psrId;
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 8 with HSLFException

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

the class HSLFSlideMaster method setSlideShow.

/**
     * Assign SlideShow for this slide master.
     */
@Internal
@Override
protected void setSlideShow(HSLFSlideShow ss) {
    super.setSlideShow(ss);
    //after the slide show is assigned collect all available style records
    assert (_txmaster == null);
    _txmaster = new TxMasterStyleAtom[9];
    TxMasterStyleAtom txdoc = getSlideShow().getDocumentRecord().getEnvironment().getTxMasterStyleAtom();
    _txmaster[txdoc.getTextType()] = txdoc;
    TxMasterStyleAtom[] txrec = ((MainMaster) getSheetContainer()).getTxMasterStyleAtoms();
    for (int i = 0; i < txrec.length; i++) {
        int txType = txrec[i].getTextType();
        if (txType < _txmaster.length && _txmaster[txType] == null) {
            _txmaster[txType] = txrec[i];
        }
    }
    for (List<HSLFTextParagraph> paras : getTextParagraphs()) {
        for (HSLFTextParagraph htp : paras) {
            int txType = htp.getRunType();
            if (txType >= _txmaster.length || _txmaster[txType] == null) {
                throw new HSLFException("Master styles not initialized");
            }
            int level = htp.getIndentLevel();
            List<TextPropCollection> charStyles = _txmaster[txType].getCharacterStyles();
            List<TextPropCollection> paragraphStyles = _txmaster[txType].getParagraphStyles();
            if (charStyles == null || paragraphStyles == null || charStyles.size() <= level || paragraphStyles.size() <= level) {
                throw new HSLFException("Master styles not initialized");
            }
            htp.setMasterStyleReference(paragraphStyles.get(level));
            for (HSLFTextRun htr : htp.getTextRuns()) {
                htr.setMasterStyleReference(charStyles.get(level));
            }
        }
    }
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) MainMaster(org.apache.poi.hslf.record.MainMaster) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) TxMasterStyleAtom(org.apache.poi.hslf.record.TxMasterStyleAtom) Internal(org.apache.poi.util.Internal)

Example 9 with HSLFException

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

the class HSLFTextParagraph method findTextParagraphs.

/**
     * Scans through the supplied record array, looking for
     * a TextHeaderAtom followed by one of a TextBytesAtom or
     * a TextCharsAtom. Builds up TextRuns from these
     *
     * @param wrapper an EscherTextboxWrapper
     */
protected static List<HSLFTextParagraph> findTextParagraphs(EscherTextboxWrapper wrapper, HSLFSheet sheet) {
    // propagate parents to parent-aware records
    RecordContainer.handleParentAwareRecords(wrapper);
    int shapeId = wrapper.getShapeId();
    List<HSLFTextParagraph> rv = null;
    OutlineTextRefAtom ota = (OutlineTextRefAtom) wrapper.findFirstOfType(OutlineTextRefAtom.typeID);
    if (ota != null) {
        // if we are based on an outline, there are no further records to be parsed from the wrapper
        if (sheet == null) {
            throw new HSLFException("Outline atom reference can't be solved without a sheet record");
        }
        List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
        assert (sheetRuns != null);
        int idx = ota.getTextIndex();
        for (List<HSLFTextParagraph> r : sheetRuns) {
            if (r.isEmpty()) {
                continue;
            }
            int ridx = r.get(0).getIndex();
            if (ridx > idx) {
                break;
            }
            if (ridx == idx) {
                if (rv == null) {
                    rv = r;
                } else {
                    // create a new container
                    // TODO: ... is this case really happening?
                    rv = new ArrayList<HSLFTextParagraph>(rv);
                    rv.addAll(r);
                }
            }
        }
        if (rv == null || rv.isEmpty()) {
            logger.log(POILogger.WARN, "text run not found for OutlineTextRefAtom.TextIndex=" + idx);
        }
    } else {
        if (sheet != null) {
            // check sheet runs first, so we get exactly the same paragraph list
            List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
            assert (sheetRuns != null);
            for (List<HSLFTextParagraph> paras : sheetRuns) {
                if (!paras.isEmpty() && paras.get(0)._headerAtom.getParentRecord() == wrapper) {
                    rv = paras;
                    break;
                }
            }
        }
        if (rv == null) {
            // if we haven't found the wrapper in the sheet runs, create a new paragraph list from its record
            List<List<HSLFTextParagraph>> rvl = findTextParagraphs(wrapper.getChildRecords());
            switch(rvl.size()) {
                // nothing found
                case 0:
                    break;
                // normal case
                case 1:
                    rv = rvl.get(0);
                    break;
                default:
                    throw new HSLFException("TextBox contains more than one list of paragraphs.");
            }
        }
    }
    if (rv != null) {
        StyleTextProp9Atom styleTextProp9Atom = wrapper.getStyleTextProp9Atom();
        for (HSLFTextParagraph htp : rv) {
            htp.setShapeId(shapeId);
            htp.setStyleTextProp9Atom(styleTextProp9Atom);
        }
    }
    return rv;
}
Also used : OutlineTextRefAtom(org.apache.poi.hslf.record.RecordTypes.OutlineTextRefAtom) OutlineTextRefAtom(org.apache.poi.hslf.record.OutlineTextRefAtom) HSLFException(org.apache.poi.hslf.exceptions.HSLFException) List(java.util.List) ArrayList(java.util.ArrayList) StyleTextProp9Atom(org.apache.poi.hslf.record.StyleTextProp9Atom) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)

Example 10 with HSLFException

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

the class HSLFTextParagraph method updateStyles.

/**
     * Update paragraph and character styles - merges them when subsequential styles match
     */
private static void updateStyles(List<HSLFTextParagraph> paragraphs) {
    final String rawText = toInternalString(getRawText(paragraphs));
    TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
    StyleTextPropAtom styleAtom = findStyleAtomPresent(headerAtom, rawText.length());
    // Update the text length for its Paragraph and Character stylings
    // * reset the length, to the new string's length
    // * add on +1 if the last block
    styleAtom.clearStyles();
    TextPropCollection lastPTPC = null, lastRTPC = null, ptpc = null, rtpc = null;
    for (HSLFTextParagraph para : paragraphs) {
        ptpc = para.getParagraphStyle();
        ptpc.updateTextSize(0);
        if (!ptpc.equals(lastPTPC)) {
            lastPTPC = styleAtom.addParagraphTextPropCollection(0);
            lastPTPC.copy(ptpc);
        }
        for (HSLFTextRun tr : para.getTextRuns()) {
            rtpc = tr.getCharacterStyle();
            rtpc.updateTextSize(0);
            if (!rtpc.equals(lastRTPC)) {
                lastRTPC = styleAtom.addCharacterTextPropCollection(0);
                lastRTPC.copy(rtpc);
            }
            int len = tr.getLength();
            ptpc.updateTextSize(ptpc.getCharactersCovered() + len);
            rtpc.updateTextSize(len);
            lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + len);
            lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + len);
        }
    }
    if (lastPTPC == null || lastRTPC == null || ptpc == null || rtpc == null) {
        // NOSONAR
        throw new HSLFException("Not all TextPropCollection could be determined.");
    }
    ptpc.updateTextSize(ptpc.getCharactersCovered() + 1);
    rtpc.updateTextSize(rtpc.getCharactersCovered() + 1);
    lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + 1);
    lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + 1);
    /**
         * If TextSpecInfoAtom is present, we must update the text size in it,
         * otherwise the ppt will be corrupted
         */
    for (Record r : paragraphs.get(0).getRecords()) {
        if (r instanceof TextSpecInfoAtom) {
            ((TextSpecInfoAtom) r).setParentSize(rawText.length() + 1);
            break;
        }
    }
}
Also used : TextSpecInfoAtom(org.apache.poi.hslf.record.TextSpecInfoAtom) HSLFException(org.apache.poi.hslf.exceptions.HSLFException) Record(org.apache.poi.hslf.record.Record) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom) StyleTextPropAtom(org.apache.poi.hslf.record.StyleTextPropAtom) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)

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