Example usage for org.apache.poi.poifs.filesystem DirectoryEntry getEntry

List of usage examples for org.apache.poi.poifs.filesystem DirectoryEntry getEntry

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem DirectoryEntry getEntry.

Prototype


public Entry getEntry(final String name) throws FileNotFoundException;

Source Link

Document

get a specified Entry by name

Usage

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

private void parseMsg(DirectoryEntry dir, Message msg) throws IOException {
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);//from  w  w w  .j  a  v  a  2s.  co  m
        int nextRecipientId = propertyStream.readInt();
        int nextAttachmentId = propertyStream.readInt();
        int recipientCount = propertyStream.readInt();
        int attachmentCount = propertyStream.readInt();
        boolean topLevel = dir.getParent() == null;
        if (topLevel) {
            propertyStream.skip(8);
        }

        for (int index = 0; index < recipientCount; index++) {
            DirectoryEntry entry = (DirectoryEntry) dir
                    .getEntry(String.format("__recip_version1.0_#%08X", index));
            parseRecipient(entry, msg);
        }
        for (int index = 0; index < attachmentCount; index++) {
            DirectoryEntry entry = (DirectoryEntry) dir
                    .getEntry(String.format("__attach_version1.0_#%08X", index));
            parseAttachment(entry, msg);
        }
        while (propertyStream.available() > 0) {
            msg.setProperty(new Property(propertyStream, dir));
        }
    }
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

/**
 * Parses a recipient directory entry which holds informations about one of possibly multiple recipients.
 * The parsed information is put into the {@link Message} object.
 *
 * @param dir The current node in the .msg file.
 * @param msg The resulting {@link Message} object.
 * @throws IOException Thrown if the .msg file could not
 *  be parsed.//  w ww.  j  a  v a2 s .c  om
 */
protected void parseRecipient(DirectoryEntry dir, Message msg) throws IOException {
    RecipientEntry recipient = new RecipientEntry();
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);
        while (propertyStream.available() > 0) {
            recipient.setProperty(new Property(propertyStream, dir));
        }
    }

    msg.addRecipient(recipient);
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

private void parseEmbeddedMessage(DirectoryEntry dir, Message msg) throws IOException {
    DirectoryEntry entry = (DirectoryEntry) dir.getEntry("__substg1.0_3701000D");
    Message attachmentMsg = new Message();
    MsgAttachment msgAttachment = new MsgAttachment();
    msgAttachment.setMessage(attachmentMsg);

    parseMsg(entry, attachmentMsg);//  ww w .j  av  a  2 s  .  c o  m

    msg.addAttachment(msgAttachment);
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

private void ParseFileAttachment(DirectoryEntry dir, Message msg) throws IOException {
    FileAttachment fileAttachment = new FileAttachment();
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);/*from   ww  w  . j  a v  a  2s.co m*/
        while (propertyStream.available() > 0) {
            Property property = new Property(propertyStream, dir);
            fileAttachment.setProperty(property.getPid(), property.getValue());
        }
    }

    msg.addAttachment(fileAttachment);
}

From source file:com.healthmarketscience.jackcess.impl.CompoundOleUtil.java

License:Apache License

/**
 * Gets a DocumentEntry from compound storage based on a fully qualified,
 * encoded entry name.//from   w  ww.  j av  a  2s.  c  om
 *
 * @param entryName fully qualified, encoded entry name
 * @param dir root directory of the compound storage
 *
 * @return the relevant DocumentEntry
 * @throws FileNotFoundException if the entry does not exist
 * @throws IOException if some other io error occurs
 */
public static DocumentEntry getDocumentEntry(String entryName, DirectoryEntry dir) throws IOException {
    // split entry name into individual components and decode them
    List<String> entryNames = new ArrayList<String>();
    for (String str : entryName.split(ENTRY_SEPARATOR)) {
        if (str.length() == 0) {
            continue;
        }
        entryNames.add(decodeEntryName(str));
    }

    DocumentEntry entry = null;
    Iterator<String> iter = entryNames.iterator();
    while (iter.hasNext()) {
        org.apache.poi.poifs.filesystem.Entry tmpEntry = dir.getEntry(iter.next());
        if (tmpEntry instanceof DirectoryEntry) {
            dir = (DirectoryEntry) tmpEntry;
        } else if (!iter.hasNext() && (tmpEntry instanceof DocumentEntry)) {
            entry = (DocumentEntry) tmpEntry;
        } else {
            break;
        }
    }

    if (entry == null) {
        throw new FileNotFoundException("Could not find document " + entryName);
    }

    return entry;
}

From source file:com.oneis.graphics.ThumbnailFinder.java

License:Mozilla Public License

/**
 * Try and get a thumbnail from an old Microsoft Office document
 */// w ww . j a  va  2  s.com
private void findFromOldMSOffice() {
    try {
        File poiFilesystem = new File(inFilename);

        // Open the POI filesystem.
        InputStream is = new FileInputStream(poiFilesystem);
        POIFSFileSystem poifs = new POIFSFileSystem(is);
        is.close();

        // Read the summary information.
        DirectoryEntry dir = poifs.getRoot();
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        DocumentInputStream dis = new DocumentInputStream(siEntry);
        PropertySet ps = new PropertySet(dis);
        dis.close();
        SummaryInformation si = new SummaryInformation(ps);
        if (si != null) {
            byte[] thumbnailData = si.getThumbnail();
            if (thumbnailData != null) {
                Thumbnail thumbnail = new Thumbnail(thumbnailData);
                byte[] wmf = thumbnail.getThumbnailAsWMF();
                // Got something!
                thumbnailDimensions = tryWMFFormat(new ByteArrayInputStream(wmf), outFilename, outFormat,
                        maxDimension);
            }
        }
    } catch (Exception e) {
        logIgnoredException("ThumbnailFinder Apache POI file reading failed", e);
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.ConfigureXLS.java

License:Open Source License

/**
 * @param poifs//from   w ww .j ava 2 s.c  om
 * @returns the DocumentSummaryInformation for poifs, or null if no DocumentSummaryInformation is found.
 */
protected DocumentSummaryInformation getDocSummary(final POIFSFileSystem poifs) {
    DirectoryEntry dir = poifs.getRoot();
    DocumentSummaryInformation result = null;
    try {
        DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
        DocumentInputStream dis = new DocumentInputStream(dsiEntry);
        PropertySet ps = new PropertySet(dis);
        dis.close();
        result = new DocumentSummaryInformation(ps);
    } catch (FileNotFoundException ex) {
        // There is no document summary information. 
        result = null;
    }
    /*
     * just returning null if anything weird happens. If there is a problem with the xls file,
     * something else will probably blow up later. 
    */
    catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        log.debug(ex);
        result = null;
    } catch (NoPropertySetStreamException ex) {
        //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        log.debug(ex);
        result = null;
    } catch (MarkUnsupportedException ex) {
        //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        log.debug(ex);
        result = null;
    } catch (UnexpectedPropertySetTypeException ex) {
        //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        log.debug(ex);
        result = null;
    } catch (IllegalPropertySetDataException ex) {
        //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        log.debug(ex);
        result = null;
    }
    return result;
}

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static String getSummaryData(String filename, String key) {
    String value = null;/*from  ww  w.ja  v a  2 s .c o m*/
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(new File(filename));
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(stream);
        } catch (Exception e) {
            stream.close();
            return getPOISummaryData(filename, key);
        }
        DirectoryEntry dir = poifs.getRoot();
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        if (siEntry != null) {
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);

            if (key.compareTo(SUMMARY_DATA_AUTHOR) == 0) {
                value = si.getAuthor();
            } else if (key.compareTo(SUMMARY_DATA_KEYWORDS) == 0) {
                value = si.getKeywords();
            } else if (key.compareTo(SUMMARY_DATA_TITLE) == 0) {
                value = si.getTitle();
            } else if (key.compareTo(SUMMARY_DATA_SUBJECT) == 0) {
                value = si.getSubject();
            }
        }
    } catch (Exception ex) {
        ex.getStackTrace();
    } finally {
        try {
            stream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return value;
}

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static String getAuthor(File file) {
    String author = null;/*from ww  w.  j av  a 2 s . c  om*/
    try {
        FileInputStream stream = new FileInputStream(file);
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(stream);
        } catch (Exception e) {
            stream.close();
            return getCreator(file);
        }
        DirectoryEntry dir = null;
        try {
            dir = poifs.getRoot();
        } catch (Exception ex) {
            System.out.println("DirectoryEntry is NULL???");
            return null;
        }
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        if (siEntry != null) {
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);
            author = si.getAuthor();
        }
        stream.close();
    } catch (Exception ex) {
        ex.getStackTrace();
    }
    return author;
}

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static void setAuthor(String filename, String author) {
    try {/*from w  ww  . ja v a  2s  . c  o  m*/
        FileInputStream stream = new FileInputStream(new File(filename));
        POIFSFileSystem poifs = new POIFSFileSystem(stream);
        DirectoryEntry dir = poifs.getRoot();

        System.out.println("SummaryInformation.DEFAULT_STREAM_NAME: " + SummaryInformation.DEFAULT_STREAM_NAME);

        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        DocumentInputStream dis = new DocumentInputStream(siEntry);
        PropertySet ps = new PropertySet(dis);
        SummaryInformation si = new SummaryInformation(ps);

        System.out.println("SummaryInformation setAuthor: " + author);

        si.setAuthor(author);

        OutputStream outStream = null;
        outStream = new FileOutputStream(new File(filename));
        byte[] buffer = new byte[1024];
        int length;

        while ((length = stream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }
        outStream.close();
        stream.close();

    } catch (Exception ex) {
        ex.getStackTrace();
    }
}