From: <chr...@us...> - 2006-04-17 23:55:45
|
Revision: 94 Author: christianhujer Date: 2006-04-17 16:55:39 -0700 (Mon, 17 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=94&view=rev Log Message: ----------- Added Grep example. Added Paths: ----------- trunk/src/doc/guide/io/src/GrepJAPI.java Added: trunk/src/doc/guide/io/src/GrepJAPI.java =================================================================== --- trunk/src/doc/guide/io/src/GrepJAPI.java (rev 0) +++ trunk/src/doc/guide/io/src/GrepJAPI.java 2006-04-17 23:55:39 UTC (rev 94) @@ -0,0 +1,43 @@ +import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.Command; +import net.sf.japi.io.ARGV; + +/** Implementation of Grep using JAPI. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + */ +public class GrepJAPI implements Command { + + /** Main program. + * @param args command line arguments + */ + public static void main(final String... args) { + final GrepJAPI grepJAPI = new GrepJAPI(); + ArgParser.parse(grepJAPI, args); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + public void run(final List<String> args) { + final String regex = args.remove(0); + final Pattern pattern = Pattern.compile(regex); + final Matcher matcher = pattern.matcher(""); + for (final String line : new ARGV(args.toArray(new String[args.size()]))) { + if (matcher.reset(line).find()) { + System.out.println(line); + } + } + } + + @StopOption @Option({"h", "help"}) + public void help() { + System.out.println("help"); + } + + @StopOption @Option({"V", "version"}) + public void version() { + System.out.println("The version."); + } +} // class GrepJAPI Property changes on: trunk/src/doc/guide/io/src/GrepJAPI.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <chr...@us...> - 2006-04-20 23:04:29
|
Revision: 103 Author: christianhujer Date: 2006-04-20 16:04:22 -0700 (Thu, 20 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=103&view=rev Log Message: ----------- Added missing imports. Modified Paths: -------------- trunk/src/doc/guide/io/src/GrepJAPI.java Modified: trunk/src/doc/guide/io/src/GrepJAPI.java =================================================================== --- trunk/src/doc/guide/io/src/GrepJAPI.java 2006-04-20 23:01:16 UTC (rev 102) +++ trunk/src/doc/guide/io/src/GrepJAPI.java 2006-04-20 23:04:22 UTC (rev 103) @@ -3,6 +3,8 @@ import java.util.regex.Matcher; import net.sf.japi.io.args.ArgParser; import net.sf.japi.io.args.Command; +import net.sf.japi.io.args.StopOption; +import net.sf.japi.io.args.Option; import net.sf.japi.io.ARGV; /** Implementation of Grep using JAPI. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <chr...@us...> - 2006-05-27 22:20:48
|
Revision: 121 Author: christianhujer Date: 2006-05-27 15:20:42 -0700 (Sat, 27 May 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=121&view=rev Log Message: ----------- Improved Grep example. Modified Paths: -------------- trunk/src/doc/guide/io/src/GrepJAPI.java Modified: trunk/src/doc/guide/io/src/GrepJAPI.java =================================================================== --- trunk/src/doc/guide/io/src/GrepJAPI.java 2006-05-27 22:20:16 UTC (rev 120) +++ trunk/src/doc/guide/io/src/GrepJAPI.java 2006-05-27 22:20:42 UTC (rev 121) @@ -12,34 +12,51 @@ */ public class GrepJAPI implements Command { + /** Whether to display the total line number. */ + private boolean totalLine; + /** Main program. * @param args command line arguments */ public static void main(final String... args) { - final GrepJAPI grepJAPI = new GrepJAPI(); - ArgParser.parse(grepJAPI, args); + ArgParser.parse(new GrepJAPI(), args); } /** {@inheritDoc} */ @SuppressWarnings({"InstanceMethodNamingConvention"}) public void run(final List<String> args) { + int totalLines = 0; final String regex = args.remove(0); final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(""); for (final String line : new ARGV(args.toArray(new String[args.size()]))) { + totalLines++; if (matcher.reset(line).find()) { - System.out.println(line); + if (totalLine) { + System.out.format("%d:%s%n", totalLines, line); + } else { + System.out.format("%s%n", line); + } } } } + /** Print help and quit. */ @StopOption @Option({"h", "help"}) public void help() { - System.out.println("help"); + System.out.println("Usage: java GrepJAPI regex [file...]"); } + /** Print version and quit. */ @StopOption @Option({"V", "version"}) public void version() { - System.out.println("The version."); + System.out.println("GrepJAPI V 0.1"); } + + /** Set displaying the total line number. */ + @Option({"l", "total-line"}) + public void totalLine() { + totalLine = true; + } + } // class GrepJAPI This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |