[Japi-cvs] SF.net SVN: japi: [410] libs/argparser/trunk/src/doc
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-06-14 20:36:34
|
Revision: 410 http://svn.sourceforge.net/japi/?rev=410&view=rev Author: christianhujer Date: 2007-06-14 13:36:33 -0700 (Thu, 14 Jun 2007) Log Message: ----------- Improved documentation.Improved and unified examples. Modified Paths: -------------- libs/argparser/trunk/src/doc/examples/Cat.java libs/argparser/trunk/src/doc/examples/Head.java libs/argparser/trunk/src/doc/examples/Tail.java libs/argparser/trunk/src/doc/examples/Uniq.java libs/argparser/trunk/src/doc/start.xhtml Modified: libs/argparser/trunk/src/doc/examples/Cat.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Cat.java 2007-06-14 20:05:36 UTC (rev 409) +++ libs/argparser/trunk/src/doc/examples/Cat.java 2007-06-14 20:36:33 UTC (rev 410) @@ -28,8 +28,7 @@ import net.sf.japi.io.args.BasicCommand; import org.jetbrains.annotations.NotNull; -/** - * Java implementation of the UNIX command <q>cat</q> to demonstrate how to use the argparser library. +/** Java implementation of the UNIX command <q>cat</q> to demonstrate how to use the argparser library. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class Cat extends BasicCommand { @@ -58,25 +57,23 @@ return returnCode; } - /** - * Copies data from one input stream to another. + /** Copies data from one input stream to another. * @param in InputStream to read from. * @param out InputStream to write to. * @throws IOException in case of I/O problems. */ - private void copy(final InputStream in, final OutputStream out) throws IOException { + private void copy(@NotNull final InputStream in, @NotNull final OutputStream out) throws IOException { final byte[] buf = new byte[4096]; for (int bytesRead; (bytesRead = in.read(buf)) != -1;) { out.write(buf, 0, bytesRead); } } - /** - * Main method. + /** Main method. * @param args Command line arguments */ public static void main(final String... args) { - ArgParser.simpleParseAndRun(new Head(), args); + ArgParser.simpleParseAndRun(new Cat(), args); } } // class Cat Modified: libs/argparser/trunk/src/doc/examples/Head.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Head.java 2007-06-14 20:05:36 UTC (rev 409) +++ libs/argparser/trunk/src/doc/examples/Head.java 2007-06-14 20:36:33 UTC (rev 410) @@ -19,6 +19,7 @@ package examples; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; @@ -30,8 +31,7 @@ import net.sf.japi.io.args.Option; import org.jetbrains.annotations.NotNull; -/** - * Java implementation of the UNIX command <q>head</q> to demonstrate how to use the argparser library. +/** Java implementation of the UNIX command <q>head</q> to demonstrate how to use the argparser library. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class Head extends BasicCommand { @@ -39,23 +39,20 @@ /** The number of items to print. */ private int numItems = 10; - /** - * The kind of items to print. + /** The kind of items to print. * <code>false</code>: print lines. * <code>true</code>: print bytes. */ private boolean printBytes; - /** - * Quiety / Verbosity. + /** Quiety / Verbosity. * 0 = never print filenames. * 1 = print filenames if more than one file. * 2 = always print filenames. */ private int verbose = 1; - /** - * Sets the number of lines to print. + /** Sets the number of lines to print. * @param lines number of lines to print */ @Option({"n", "lines"}) @@ -64,8 +61,7 @@ printBytes = false; } - /** - * Sets the number of bytes to print. + /** Sets the number of bytes to print. * @param bytes number of bytes to print. */ @Option({"c", "bytes"}) @@ -74,25 +70,19 @@ printBytes = true; } - /** - * Sets the command to be quiet. - */ + /** Sets the command to be quiet. */ @Option({"q", "quiet", "silent"}) public void setQuiet() { verbose = 0; } - /** - * Sets the command to be verbose. - */ + /** Sets the command to be verbose. */ @Option({"v", "verbose"}) public void setVerbose() { verbose = 2; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public int run(@NotNull final List<String> args) throws IOException { int returnCode = 0; if (args.size() == 0) { @@ -106,42 +96,49 @@ System.out.println("==> " + arg + " <=="); } try { - final InputStream in = new FileInputStream(arg); + final InputStream in = new BufferedInputStream(new FileInputStream(arg)); try { copyItems(in); } finally { in.close(); } } catch (final IOException e) { - System.err.println(e); returnCode = 1; + System.err.println(e); } } } return returnCode; } - /** - * Copies the configured number of items from the specified InputStream to System.out. + /** Copies the configured number of items from the specified InputStream to System.out. * @param in InputStream to run on * @throws IOException In case of I/O problems. */ - private void copyItems(final InputStream in) throws IOException { + private void copyItems(@NotNull final InputStream in) throws IOException { if (printBytes) { - for (int i = 0, b; i < numItems && (b = in.read()) != -1; i++) { - System.out.write(b); - } + copyBytes(in); } else { - BufferedReader lin = new BufferedReader(new InputStreamReader(in)); - String line; - for (int i = 0; i < numItems && (line = lin.readLine()) != null; i++) { - System.out.println(line); - } + copyLines(in); } } - /** - * Main method. + private void copyBytes(@NotNull final InputStream in) throws IOException { + final InputStream lin = in instanceof BufferedInputStream ? in : new BufferedInputStream(in); + final byte[] buf = new byte[numItems]; + final int bytesRead = lin.read(buf, 0, numItems); + System.out.write(buf, 0, bytesRead); + } + + private void copyLines(@NotNull final InputStream in) throws IOException { + BufferedReader lin = new BufferedReader(new InputStreamReader(in)); + String line; + for (int i = 0; i < numItems && (line = lin.readLine()) != null; i++) { + System.out.println(line); + } + } + + /** Main method. * @param args Command line arguments */ public static void main(final String... args) { Modified: libs/argparser/trunk/src/doc/examples/Tail.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Tail.java 2007-06-14 20:05:36 UTC (rev 409) +++ libs/argparser/trunk/src/doc/examples/Tail.java 2007-06-14 20:36:33 UTC (rev 410) @@ -31,8 +31,7 @@ import net.sf.japi.io.args.Option; import org.jetbrains.annotations.NotNull; -/** - * Java implementation of the UNIX command <q>tail</q> to demonstrate how to use the argparser library. +/** Java implementation of the UNIX command <q>tail</q> to demonstrate how to use the argparser library. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class Tail extends BasicCommand { @@ -40,23 +39,20 @@ /** The number of items to print. */ private int numItems = 10; - /** - * The kind of items to print. + /** The kind of items to print. * <code>false</code>: print lines. * <code>true</code>: print bytes. */ private boolean printBytes; - /** - * Quiety / Verbosity. + /** Quiety / Verbosity. * 0 = never print filenames. * 1 = print filenames if more than one file. * 2 = always print filenames. */ private int verbose = 1; - /** - * Sets the number of lines to print. + /** Sets the number of lines to print. * @param lines number of lines to print */ @Option({"n", "lines"}) @@ -65,8 +61,7 @@ printBytes = false; } - /** - * Sets the number of bytes to print. + /** Sets the number of bytes to print. * @param bytes number of bytes to print. */ @Option({"c", "bytes"}) @@ -75,50 +70,60 @@ printBytes = true; } - /** - * Sets the command to be quiet. - */ + /** Sets the command to be quiet. */ @Option({"q", "quiet", "silent"}) public void setQuiet() { verbose = 0; } - /** - * Sets the command to be verbose. - */ + /** Sets the command to be verbose. */ @Option({"v", "verbose"}) public void setVerbose() { verbose = 2; } /** {@inheritDoc} */ - @SuppressWarnings({"InstanceMethodNamingConvention"}) - public int run(@NotNull List<String> args) throws Exception { + public int run(@NotNull final List<String> args) throws IOException { int returnCode = 0; - for (final String arg : args) { - try { - final InputStream in = new BufferedInputStream(new FileInputStream(arg)); + if (args.size() == 0) { + if (verbose == 2) { + System.out.println("==> STDIN <=="); + } + copyItems(System.in); + } else { + for (final String arg : args) { + if (verbose == 2 || verbose == 1 && args.size() > 1) { + System.out.println("==> " + arg + " <=="); + } try { - copy(in); - } finally { - in.close(); + final InputStream in = new BufferedInputStream(new FileInputStream(arg)); + try { + copyItems(in); + } finally { + in.close(); + } + } catch (final IOException e) { + returnCode = 1; + System.err.println(e); } - } catch (final IOException e) { - returnCode = 1; - System.err.println(e); } } return returnCode; } - private void copy(final InputStream in) throws IOException { + /** Copies the configured number of items from the specified InputStream to System.out. + * @param in InputStream to run on + * @throws IOException In case of I/O problems. + */ + private void copyItems(@NotNull final InputStream in) throws IOException { if (printBytes) { copyBytes(in); } else { copyLines(in); } } - private void copyBytes(final InputStream in) throws IOException { + + private void copyBytes(@NotNull final InputStream in) throws IOException { final InputStream lin = in instanceof BufferedInputStream ? in : new BufferedInputStream(in); final byte[] buf = new byte[numItems]; int bytesRead; @@ -132,7 +137,7 @@ System.out.write(buf, 0, bytesRead); } - private void copyLines(final InputStream in) throws IOException { + private void copyLines(@NotNull final InputStream in) throws IOException { final String[] buf = new String[numItems]; final BufferedReader lin = new BufferedReader(new InputStreamReader(in)); int num = 0; @@ -147,12 +152,11 @@ } } - /** - * Main method. + /** Main method. * @param args Command line arguments */ public static void main(final String... args) { - ArgParser.simpleParseAndRun(new Head(), args); + ArgParser.simpleParseAndRun(new Tail(), args); } } // class Tail Modified: libs/argparser/trunk/src/doc/examples/Uniq.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Uniq.java 2007-06-14 20:05:36 UTC (rev 409) +++ libs/argparser/trunk/src/doc/examples/Uniq.java 2007-06-14 20:36:33 UTC (rev 410) @@ -21,6 +21,7 @@ import net.sf.japi.io.args.BasicCommand; import net.sf.japi.io.args.Option; +import net.sf.japi.io.args.ArgParser; import org.jetbrains.annotations.NotNull; import java.util.List; import java.io.InputStream; @@ -31,8 +32,7 @@ import java.io.Reader; import java.io.BufferedReader; -/** - * Java implementation of the UNIX command <q>uniq</q> to demonstrate how to use the argparser library. +/** Java implementation of the UNIX command <q>uniq</q> to demonstrate how to use the argparser library. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> */ public class Uniq extends BasicCommand { @@ -46,25 +46,19 @@ /** Whether to ignore case. */ private boolean ignoreCase; - /** - * Sets that lines should prefixed with the count of their occurrence. - */ + /** Sets that lines should prefixed with the count of their occurrence. */ @Option({"c", "count"}) public void setCount() { count = true; } - /** - * Sets that only lines that occurred more than once will be printed. - */ + /** Sets that only lines that occurred more than once will be printed. */ @Option({"d", "repeated"}) public void setRepeated() { repeated = true; } - /** - * Sets that the case should be ignored. - */ + /** Sets that the case should be ignored. */ @Option({"i", "ignore-case"}) public void setIgnoreCase() { ignoreCase = true; @@ -90,8 +84,7 @@ return returnCode; } - /** - * Prints unique lines from the specified InputStream. + /** Prints unique lines from the specified InputStream. * @param in InputStream to print unique lines from. * @throws IOException In case of I/O problems. */ @@ -99,8 +92,7 @@ uniq(new InputStreamReader(in)); } - /** - * Prints unique lines from the specified Reader. + /** Prints unique lines from the specified Reader. * @param in Reader to print unique lines from. * @throws IOException In case of I/O problems. */ @@ -126,4 +118,11 @@ } } + /** Main method. + * @param args Command line arguments + */ + public static void main(final String... args) { + ArgParser.simpleParseAndRun(new Uniq(), args); + } + } // class Uniq Modified: libs/argparser/trunk/src/doc/start.xhtml =================================================================== --- libs/argparser/trunk/src/doc/start.xhtml 2007-06-14 20:05:36 UTC (rev 409) +++ libs/argparser/trunk/src/doc/start.xhtml 2007-06-14 20:36:33 UTC (rev 410) @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> -<html> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /> <meta name="Date" content="$Date: 2006-11-26 16:38:11 +0100 (So, 26 Nov 2006) $" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |