Search in sources :

Example 1 with ClassID

use of org.apache.poi.hpsf.ClassID in project poi by apache.

the class HSSFWorkbook method addOlePackage.

/**
     * Adds an OLE package manager object with the given POIFS to the sheet
     *
     * @param poiData an POIFS containing the embedded document, to be added
     * @param label the label of the payload
     * @param fileName the original filename
     * @param command the command to open the payload
     * @return the index of the added ole object
     * @throws IOException if the object can't be embedded
     */
public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command) throws IOException {
    DirectoryNode root = poiData.getRoot();
    Map<String, ClassID> olemap = getOleMap();
    for (Map.Entry<String, ClassID> entry : olemap.entrySet()) {
        if (root.hasEntry(entry.getKey())) {
            root.setStorageClsid(entry.getValue());
            break;
        }
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    poiData.writeFilesystem(bos);
    return addOlePackage(bos.toByteArray(), label, fileName, command);
}
Also used : FilteringDirectoryNode(org.apache.poi.poifs.filesystem.FilteringDirectoryNode) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) ClassID(org.apache.poi.hpsf.ClassID) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ClassID

use of org.apache.poi.hpsf.ClassID 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 3 with ClassID

use of org.apache.poi.hpsf.ClassID in project poi by apache.

the class TestWrite method writeTwoSections.

/**
     * <p>Writes a simple property set with two sections to a POIFS and reads it
     * back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception WritingNotSupportedException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeTwoSections() throws WritingNotSupportedException, IOException {
    final String STREAM_NAME = "PropertySetStream";
    final String SECTION1 = "Section 1";
    final String SECTION2 = "Section 2";
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    ps.clearSections();
    final ClassID formatID = new ClassID();
    formatID.setBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
    final MutableSection s1 = new MutableSection();
    s1.setFormatID(formatID);
    s1.setProperty(2, SECTION1);
    ps.addSection(s1);
    final MutableSection s2 = new MutableSection();
    s2.setFormatID(formatID);
    s2.setProperty(2, SECTION2);
    ps.addSection(s2);
    poiFs.createDocument(ps.toInputStream(), STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final PropertySet[] psa = new PropertySet[1];
    final POIFSReader r = new POIFSReader();
    r.registerListener(new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                psa[0] = PropertySetFactory.create(event.getStream());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }, STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    Section s = (psa[0].getSections().get(0));
    assertEquals(s.getFormatID(), formatID);
    Object p = s.getProperty(2);
    assertEquals(SECTION1, p);
    s = (psa[0].getSections().get(1));
    p = s.getProperty(2);
    assertEquals(SECTION2, p);
}
Also used : MutableSection(org.apache.poi.hpsf.MutableSection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) ClassID(org.apache.poi.hpsf.ClassID) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) NoPropertySetStreamException(org.apache.poi.hpsf.NoPropertySetStreamException) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalPropertySetDataException(org.apache.poi.hpsf.IllegalPropertySetDataException) UnsupportedVariantTypeException(org.apache.poi.hpsf.UnsupportedVariantTypeException) IOException(java.io.IOException) NoFormatIDException(org.apache.poi.hpsf.NoFormatIDException) ReadingNotSupportedException(org.apache.poi.hpsf.ReadingNotSupportedException) HPSFException(org.apache.poi.hpsf.HPSFException) FileInputStream(java.io.FileInputStream) POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 4 with ClassID

use of org.apache.poi.hpsf.ClassID in project poi by apache.

the class TestClassID method testClassID.

/**
     * <p>Tests the {@link PropertySet} methods. The test file has two
     * property set: the first one is a {@link SummaryInformation},
     * the second one is a {@link DocumentSummaryInformation}.</p>
     */
public void testClassID() {
    ClassID clsidTest = new ClassID(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }, 0);
    Assert.assertEquals(clsidTest.toString().toUpperCase(Locale.ROOT), "{04030201-0605-0807-090A-0B0C0D0E0F10}");
}
Also used : ClassID(org.apache.poi.hpsf.ClassID)

Example 5 with ClassID

use of org.apache.poi.hpsf.ClassID in project poi by apache.

the class HSSFWorkbook method getOleMap.

protected static Map<String, ClassID> getOleMap() {
    Map<String, ClassID> olemap = new HashMap<String, ClassID>();
    olemap.put("PowerPoint Document", ClassID.PPT_SHOW);
    for (String str : WORKBOOK_DIR_ENTRY_NAMES) {
        olemap.put(str, ClassID.XLS_WORKBOOK);
    }
    // ... to be continued
    return olemap;
}
Also used : HashMap(java.util.HashMap) ClassID(org.apache.poi.hpsf.ClassID) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString)

Aggregations

ClassID (org.apache.poi.hpsf.ClassID)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Map (java.util.Map)2 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)2 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 Test (org.junit.Test)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)1 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)1 HPSFException (org.apache.poi.hpsf.HPSFException)1