Search in sources :

Example 1 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode in project poi by apache.

the class VBAMacroReader method readMacros.

/**
     * Reads VBA Project modules from a VBA Project directory located at
     * <tt>macroDir</tt> into <tt>modules</tt>.
     *
     * @since 3.15-beta2
     */
protected void readMacros(DirectoryNode macroDir, ModuleMap modules) throws IOException {
    for (Entry entry : macroDir) {
        if (!(entry instanceof DocumentNode)) {
            continue;
        }
        String name = entry.getName();
        DocumentNode document = (DocumentNode) entry;
        DocumentInputStream dis = new DocumentInputStream(document);
        try {
            if ("dir".equalsIgnoreCase(name)) {
                // process DIR
                RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
                String streamName = null;
                int recordId = 0;
                try {
                    while (true) {
                        recordId = in.readShort();
                        if (EOF == recordId || VERSION_INDEPENDENT_TERMINATOR == recordId) {
                            break;
                        }
                        int recordLength = in.readInt();
                        switch(recordId) {
                            case PROJECTVERSION:
                                trySkip(in, 6);
                                break;
                            case PROJECTCODEPAGE:
                                int codepage = in.readShort();
                                modules.charset = Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
                                break;
                            case STREAMNAME:
                                streamName = readString(in, recordLength, modules.charset);
                                int reserved = in.readShort();
                                if (reserved != STREAMNAME_RESERVED) {
                                    throw new IOException("Expected x0032 after stream name before Unicode stream name, but found: " + Integer.toHexString(reserved));
                                }
                                int unicodeNameRecordLength = in.readInt();
                                readUnicodeString(in, unicodeNameRecordLength);
                                // do something with this at some point
                                break;
                            case MODULEOFFSET:
                                readModule(in, streamName, modules);
                                break;
                            default:
                                trySkip(in, recordLength);
                                break;
                        }
                    }
                } catch (final IOException e) {
                    throw new IOException("Error occurred while reading macros at section id " + recordId + " (" + HexDump.shortToHex(recordId) + ")", e);
                } finally {
                    in.close();
                }
            } else if (!startsWithIgnoreCase(name, "__SRP") && !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
                // process module, skip __SRP and _VBA_PROJECT since these do not contain macros
                readModule(dis, name, modules);
            }
        } finally {
            dis.close();
        }
    }
}
Also used : RLEDecompressingInputStream(org.apache.poi.util.RLEDecompressingInputStream) Entry(org.apache.poi.poifs.filesystem.Entry) ZipEntry(java.util.zip.ZipEntry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 2 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode in project poi by apache.

the class POIFSDump method dump.

public static void dump(DirectoryEntry root, File parent) throws IOException {
    for (Iterator<Entry> it = root.getEntries(); it.hasNext(); ) {
        Entry entry = it.next();
        if (entry instanceof DocumentNode) {
            DocumentNode node = (DocumentNode) entry;
            DocumentInputStream is = new DocumentInputStream(node);
            byte[] bytes = IOUtils.toByteArray(is);
            is.close();
            OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
            try {
                out.write(bytes);
            } finally {
                out.close();
            }
        } else if (entry instanceof DirectoryEntry) {
            DirectoryEntry dir = (DirectoryEntry) entry;
            File file = new File(parent, entry.getName());
            if (!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(dir, file);
        } else {
            System.err.println("Skipping unsupported POIFS entry: " + entry);
        }
    }
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File)

Example 3 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode in project tika by apache.

the class POIFSContainerDetector method processCompObjFormatType.

/**
     * Is this one of the kinds of formats which uses CompObj to
     * store all of their data, eg Star Draw, Star Impress or
     * (older) Works?
     * If not, it's likely an embedded resource
     */
private static MediaType processCompObjFormatType(DirectoryEntry root) {
    try {
        Entry e = root.getEntry("CompObj");
        if (e != null && e.isDocumentEntry()) {
            DocumentNode dn = (DocumentNode) e;
            DocumentInputStream stream = new DocumentInputStream(dn);
            byte[] bytes = IOUtils.toByteArray(stream);
            /*
                 * This array contains a string with a normal ASCII name of the
                 * application used to create this file. We want to search for that
                 * name.
                 */
            if (arrayContains(bytes, MS_GRAPH_CHART_BYTES)) {
                return MS_GRAPH_CHART;
            } else if (arrayContains(bytes, STAR_DRAW)) {
                return SDA;
            } else if (arrayContains(bytes, STAR_IMPRESS)) {
                return SDD;
            } else if (arrayContains(bytes, WORKS_QUILL96)) {
                return WPS;
            }
        }
    } catch (Exception e) {
    /*
             * "root.getEntry" can throw FileNotFoundException. The code inside
             * "if" can throw IOExceptions. Theoretically. Practically no
             * exceptions will likely ever appear.
             *
             * Swallow all of them. If any occur, we just assume that we can't
             * distinguish between Draw and Impress and return something safe:
             * x-tika-msoffice
             */
    }
    return OLE;
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) IOException(java.io.IOException)

Example 4 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode in project poi by apache.

the class OldExcelExtractor method open.

private void open(DirectoryNode directory) throws IOException {
    DocumentNode book;
    try {
        book = (DocumentNode) directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME);
    } catch (FileNotFoundException e) {
        // some files have "Workbook" instead
        book = (DocumentNode) directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]);
    }
    if (book == null) {
        throw new IOException("No Excel 5/95 Book stream found");
    }
    ris = new RecordInputStream(directory.createDocumentInputStream(book));
    prepare();
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) RecordInputStream(org.apache.poi.hssf.record.RecordInputStream)

Example 5 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode in project poi by apache.

the class POIFSLister method displayDirectory.

public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) {
    System.out.println(indent + dir.getName() + " -");
    String newIndent = indent + "  ";
    boolean hadChildren = false;
    for (Iterator<Entry> it = dir.getEntries(); it.hasNext(); ) {
        hadChildren = true;
        Entry entry = it.next();
        if (entry instanceof DirectoryNode) {
            displayDirectory((DirectoryNode) entry, newIndent, withSizes);
        } else {
            DocumentNode doc = (DocumentNode) entry;
            String name = doc.getName();
            String size = "";
            if (name.charAt(0) < 10) {
                String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1);
                name = name.substring(1) + " <" + altname + ">";
            }
            if (withSizes) {
                size = " [" + doc.getSize() + " / 0x" + Integer.toHexString(doc.getSize()) + "]";
            }
            System.out.println(newIndent + name + size);
        }
    }
    if (!hadChildren) {
        System.out.println(newIndent + "(no children)");
    }
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode)

Aggregations

DocumentNode (org.apache.poi.poifs.filesystem.DocumentNode)9 IOException (java.io.IOException)6 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)5 Entry (org.apache.poi.poifs.filesystem.Entry)4 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)3 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 NPOIFSDocument (org.apache.poi.poifs.filesystem.NPOIFSDocument)2 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ZipEntry (java.util.zip.ZipEntry)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1