//============================================================================== // Test.java //============================================================================== package tribble.search.test; // System imports import java.io.InputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.lang.Exception; import java.lang.String; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; // Local imports import tribble.util.HexDump; import tribble.search.DocumentI; import tribble.search.DocumentSearcherI; import tribble.search.disk.FDirectory; import tribble.search.zip.ZArchive; /******************************************************************************* * Test driver for this package. * * @version $Revision: 1.6 $ $Date: 2001/07/11 23:39:36 $ * @since 2001-05-18 * @author * David R. Tribble * (david@tribble.com). *
* Copyright * ©2001 by David R. Tribble, all rights reserved.
* * @see tribble.search.disk.FDirectory * @see tribble.search.zip.ZArchive */ class Test { // Identification /** Revision information. */ static final String REV = "@(#)tribble/search/test/Test.java $Revision: 1.6 $ $Date: 2001/07/11 23:39:36 $\n"; /** This package name. */ static final String PACKAGE = "tribble.search.test"; /** This class name. */ static final String CLASS = "Test"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constants /** Series number. */ public static final int SERIES = 300; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private constants //---------------------------------- // Program termination codes. private static final int RC_OKAY = 0; private static final int RC_USAGE = 255; //---------------------------------- // Searcher types. private static final int STYPE_NONE = 0; private static final int STYPE_DISK = 1; private static final int STYPE_ZIP = 2; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private variables /** Standard output stream. */ private PrintWriter m_out = new PrintWriter(System.out, true); /** Date formatter. */ private SimpleDateFormat m_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** Maximum content bytes to display. */ private int m_maxBytes = 0; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public static methods /*************************************************************************** * Test driver. * * @param args * Command line arguments. * * @since 1.1, 2001-05-18 */ public static void main(String[] args) { // Execute this test driver (new Test()).run(args); System.exit(RC_OKAY); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-18 */ private Test() { // Do nothing } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private methods /*************************************************************************** * Display a program usage message, then punt. * * @return * Doesn't, but calls System.exit() instead. * * @since 1.1, 2001-05-18 */ private void usage() { // Display a program usage message m_out.println("Test driver for " + PACKAGE); m_out.println(); m_out.println("usage: java " + PACKAGE + "." + CLASS + " [-option] name..."); m_out.println(); m_out.println("Options:"); m_out.println(" -a Display the attributes for each file."); m_out.println(" -c Display the contents of each file."); m_out.println(" -cs Display the first 1K of the file contents."); m_out.println(" -d Search 'name' as a file directory."); m_out.println(" -z Search 'name' as a ZIP file."); // Punt System.exit(RC_USAGE); } /*************************************************************************** * Test driver. * * @param args * Command line arguments. * * @since 1.1, 2001-05-18 */ private void run(String[] args) { int totalCount; int opt_searcherType = STYPE_DISK; boolean opt_dumpContents = false; boolean opt_keyVals = false; // Check command usage if (args.length == 0) usage(); // Search each command line arg as a document directory totalCount = 0; for (int i = 0; i < args.length; i++) { int count; String dirname; DocumentSearcherI searcher; Enumeration iter; // Check the searcher type if (args[i].startsWith("-")) { if (args[i].equals("-c")) opt_dumpContents = true; else if (args[i].equals("-cs")) { opt_dumpContents = true; m_maxBytes = 1024; } else if (args[i].equals("-a")) opt_keyVals = true; else if (args[i].equals("-d")) opt_searcherType = STYPE_DISK; else if (args[i].equals("-z")) opt_searcherType = STYPE_ZIP; else { m_out.println("Unknown searcher type '" + args[i] + "'"); opt_searcherType = STYPE_NONE; } // Skip to the next command line arg continue; } // Open and search a given name count = 0; try { // Get the directory name to search dirname = args[i]; if (i > 0) m_out.println(); m_out.print(dirname); m_out.println(":"); // Set up to search the next document directory searcher = null; switch (opt_searcherType) { case STYPE_DISK: case STYPE_NONE: searcher = new FDirectory(); break; case STYPE_ZIP: searcher = new ZArchive(); break; } // Search the document directory for documents searcher.initialize(null); searcher.open(dirname); iter = searcher.find(null); while (iter != null && iter.hasMoreElements()) { DocumentI doc; // Get the next document doc = (DocumentI) iter.nextElement(); count++; totalCount++; // Display info about the document entry showInfo(doc); // Display the attribute values of the document if (opt_keyVals) { showAttributes(doc); m_out.println(); } // Dump the contents of the document if (opt_dumpContents) { showContents(doc); m_out.println(); } } // Clean up if (searcher != null) searcher.close(); } catch (Exception ex) { // An exception occurred m_out.println(); m_out.println("*** Exception: " + ex); ex.printStackTrace(m_out); } // Display a summary m_out.println("Entries: " + count); } // Display a summary if (args.length > 1) m_out.println("Total: " + totalCount); } /*************************************************************************** * Display information about a document. * * @param doc * A document. * * @since 1.3, 2001-06-15 */ private void showInfo(DocumentI doc) throws Exception { long len; Date time; // Display info about the document m_out.print(doc.isDirectory() ? 'd' : '-'); m_out.print(doc.canRead() ? 'r' : '-'); m_out.print(doc.canWrite() ? 'w' : '-'); m_out.print(' '); len = doc.length(); for (long p = 1000000000; p > 1; p /= 10) { if (len < p) m_out.print(' '); else break; } m_out.print(len); m_out.print(" "); time = doc.lastModified(); if (time != null) m_out.print(m_sdf.format(time)); else m_out.print(" -"); m_out.print(" "); m_out.print(doc.getName()); m_out.println(); } /*************************************************************************** * Display the values of the attributes associated with a document. * * @param doc * A document. * * @since 1.5, 2001-07-11 */ private void showAttributes(DocumentI doc) throws Exception { String[] keys; // Get the keys associated with the document keys = doc.getAttributeNames(); if (keys == null) return; // Display the attribute values for (int i = 0; i < keys.length; i++) { String key; Object val; String sval; // Get the value of an attribute key = keys[i]; if (key == null) continue; val = doc.getAttribute(key); sval = ""; if (val != null) sval = val.toString(); if (key.length() < 20) key += ' '; while (key.length() < 20) key += '.'; m_out.print(' ' + key + ": "); m_out.print(sval); m_out.println(); } } /*************************************************************************** * Read the contents of a document and display it in hexadecimal. * * @param doc * A document. * * @since 1.3, 2001-06-15 */ private void showContents(DocumentI doc) throws Exception { try { InputStream in; byte[] data; int addr; int off; // Retrieve an input stream from the document in = doc.getInputStream(); if (in == null) { m_out.println("Cannot read: " + doc.getName()); return; } // Read and display the contents of the document data = new byte[64*1024]; addr = 0x00000000; off = 0; for (;;) { int nBytes; int bLen; boolean eof; // Read a block of bytes from the document eof = false; nBytes = in.read(data, off, data.length-off); if (nBytes < 0) { nBytes = 0; eof = true; } nBytes += off; // Limit the output if (m_maxBytes > 0 && addr+nBytes > m_maxBytes) { nBytes = m_maxBytes-addr; eof = true; } // Check for the end of the stream if (nBytes <= 0) break; // Display (most of) the data as hexadecimal if (eof) bLen = nBytes; else bLen = (nBytes/16)*16; if (bLen > 0) { HexDump.hexDumpAt(m_out, data, 0, bLen, addr); addr += bLen; } // Save the undisplayed bytes off = 0; if (nBytes > bLen) { off = nBytes-bLen; System.arraycopy(data, bLen, data, 0, off); } } m_out.flush(); } catch (Exception ex) { // Failure m_out.println(ex); return; } } } // End Test.java