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);
}
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;
}
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);
}
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}");
}
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;
}
Aggregations