Allow POIFSLister to switch between the two different POIFS implementations when listing

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1053274 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-12-28 07:16:12 +00:00
parent 8d19052b6a
commit bfcc46192c
1 changed files with 66 additions and 51 deletions

View File

@ -17,12 +17,15 @@
package org.apache.poi.poifs.dev; package org.apache.poi.poifs.dev;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentNode; import org.apache.poi.poifs.filesystem.DocumentNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/** /**
@ -31,59 +34,71 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* Much simpler than {@link POIFSViewer} * Much simpler than {@link POIFSViewer}
*/ */
public class POIFSLister { public class POIFSLister {
/** /**
* Display the entries of multiple POIFS files * Display the entries of multiple POIFS files
* *
* @param args the names of the files to be displayed * @param args the names of the files to be displayed
*/ */
public static void main(final String args[]) throws IOException { public static void main(final String args[]) throws IOException {
if (args.length == 0) { if (args.length == 0) {
System.err.println("Must specify at least one file to view"); System.err.println("Must specify at least one file to view");
System.exit(1); System.exit(1);
} }
boolean withSizes = false; boolean withSizes = false;
for (int j = 0; j < args.length; j++) { boolean newPOIFS = true;
if (args[j].equalsIgnoreCase("-size") || args[j].equalsIgnoreCase("-sizes")) { for (int j = 0; j < args.length; j++) {
withSizes = true; if (args[j].equalsIgnoreCase("-size") || args[j].equalsIgnoreCase("-sizes")) {
} else { withSizes = true;
viewFile(args[j], withSizes); } else if (args[j].equalsIgnoreCase("-old") || args[j].equalsIgnoreCase("-old-poifs")) {
} newPOIFS = false;
} } else {
} if(newPOIFS) {
viewFile(args[j], withSizes);
} else {
viewFileOld(args[j], withSizes);
}
}
}
}
public static void viewFile(final String filename, boolean withSizes) throws IOException { public static void viewFile(final String filename, boolean withSizes) throws IOException {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(filename));
displayDirectory(fs.getRoot(), "", withSizes); displayDirectory(fs.getRoot(), "", withSizes);
} }
public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) { public static void viewFileOld(final String filename, boolean withSizes) throws IOException {
System.out.println(indent + dir.getName() + " -"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
String newIndent = indent + " "; displayDirectory(fs.getRoot(), "", withSizes);
}
boolean hadChildren = false; public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) {
for (Iterator it = dir.getEntries(); it.hasNext();) { System.out.println(indent + dir.getName() + " -");
hadChildren = true; String newIndent = indent + " ";
Object entry = it.next();
if (entry instanceof DirectoryNode) { boolean hadChildren = false;
displayDirectory((DirectoryNode) entry, newIndent, withSizes); for(Iterator<Entry> it = dir.getEntries(); it.hasNext();) {
} else { hadChildren = true;
DocumentNode doc = (DocumentNode) entry; Entry entry = it.next();
String name = doc.getName(); if (entry instanceof DirectoryNode) {
String size = ""; displayDirectory((DirectoryNode) entry, newIndent, withSizes);
if (name.charAt(0) < 10) { } else {
String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1); DocumentNode doc = (DocumentNode) entry;
name = name.substring(1) + " <" + altname + ">"; String name = doc.getName();
} String size = "";
if (withSizes) { if (name.charAt(0) < 10) {
size = " [" + doc.getSize() + " / 0x" + Integer.toHexString(doc.getSize()) String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1);
+ "]"; name = name.substring(1) + " <" + altname + ">";
} }
System.out.println(newIndent + name + size); if (withSizes) {
} size = " [" + doc.getSize() + " / 0x" +
} Integer.toHexString(doc.getSize()) + "]";
if (!hadChildren) { }
System.out.println(newIndent + "(no children)"); System.out.println(newIndent + name + size);
} }
} }
if (!hadChildren) {
System.out.println(newIndent + "(no children)");
}
}
} }