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