[Japi-cvs] SF.net SVN: japi: [60] trunk/src/app/net/sf/japi/io
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-04-15 00:30:19
|
Revision: 60 Author: christianhujer Date: 2006-04-14 17:20:22 -0700 (Fri, 14 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=60&view=rev Log Message: ----------- Added pre-version of arg parser. Added Paths: ----------- trunk/src/app/net/sf/japi/io/args/ trunk/src/app/net/sf/japi/io/args/ArgParser.java trunk/src/app/net/sf/japi/io/args/Command.java trunk/src/app/net/sf/japi/io/args/Mandatory.java trunk/src/app/net/sf/japi/io/args/Option.java trunk/src/app/net/sf/japi/io/args/StopOption.java Added: trunk/src/app/net/sf/japi/io/args/ArgParser.java =================================================================== --- trunk/src/app/net/sf/japi/io/args/ArgParser.java (rev 0) +++ trunk/src/app/net/sf/japi/io/args/ArgParser.java 2006-04-15 00:20:22 UTC (rev 60) @@ -0,0 +1,95 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io.args; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class ArgParser { + + /** Parses arguments into an arguments container. + * @param argsContainer object to hold parsed arguments + * @param args Arguments to parse + */ + public static void parse(final Command argsContainer, final String... args) { + final List<String> argList = new ArrayList<String>(Arrays.asList(args)); + final Class<?> argsContainerClass = argsContainer.getClass(); + final Method[] argsContainerMethods = argsContainerClass.getMethods(); + final Map<String, Method> argumentMethods = new HashMap<String, Method>(); + for (final Method method : argsContainerMethods) { + final Option option = method.getAnnotation(Option.class); + if (option != null) { + for (final String optionString : option.value()) { + argumentMethods.put(optionString.length() > 1 ? "-" + optionString : optionString, method); + } + } + } + for (final Iterator<String> it = argList.iterator(); it.hasNext(); ) { + final String arg = it.next(); + if (arg.length() > 1 && arg.charAt(0) == '-') { + it.remove(); + if ("--".equals(arg)) { + break; + } + if (arg.charAt(1) != '-') { + if (invokeMethod(argumentMethods.get(arg.substring(1)), argsContainer)) { + return; + } + } else { + for (final String realArg : arg.substring(1).split("")) { + if (invokeMethod(argumentMethods.get(arg.substring(1)), argsContainer)) { + return; + } + } + } + } + // empty arguments are intentionally not removed. + } + argsContainer.run(argList); + } + + /** Invoke an argument method. + * @param method + * @param argsContainer + * @return true if the method indicated to stop parsing the arguments + */ + private static boolean invokeMethod(final Method method, final Command argsContainer) { + if (method == null) { + throw new RuntimeException("Unknown argument Exception"); + } + try { + method.invoke(argsContainer); + } catch (final IllegalAccessException e) { + System.err.println(e); + } catch (final InvocationTargetException e) { + System.err.println(e.getCause()); + } + return method.getAnnotation(StopOption.class) != null; + } + +} // class ArgParser Property changes on: trunk/src/app/net/sf/japi/io/args/ArgParser.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: trunk/src/app/net/sf/japi/io/args/Command.java =================================================================== --- trunk/src/app/net/sf/japi/io/args/Command.java (rev 0) +++ trunk/src/app/net/sf/japi/io/args/Command.java 2006-04-15 00:20:22 UTC (rev 60) @@ -0,0 +1,38 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io.args; + +import java.util.List; + +/** Shell commands can implement this interface and make use of ArgParser. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + */ +interface Command { + + /** Run the command. + * This method is invoked by {@link ArgParser} once it is finnished with parsing the arguments. + * @param args the argument strings that were not parsed away by {@link ArgParser}. + */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + void run(List<String> args); + +} // interface Command Property changes on: trunk/src/app/net/sf/japi/io/args/Command.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: trunk/src/app/net/sf/japi/io/args/Mandatory.java =================================================================== --- trunk/src/app/net/sf/japi/io/args/Mandatory.java (rev 0) +++ trunk/src/app/net/sf/japi/io/args/Mandatory.java 2006-04-15 00:20:22 UTC (rev 60) @@ -0,0 +1,36 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io.args; + +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** Annotation for command argument methods to declare that the associated argument is mandatory. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + * @see Option + */ +@Retention(RUNTIME) +@Target(METHOD) +@interface Mandatory { +} // @interface Mandatory Property changes on: trunk/src/app/net/sf/japi/io/args/Mandatory.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: trunk/src/app/net/sf/japi/io/args/Option.java =================================================================== --- trunk/src/app/net/sf/japi/io/args/Option.java (rev 0) +++ trunk/src/app/net/sf/japi/io/args/Option.java 2006-04-15 00:20:22 UTC (rev 60) @@ -0,0 +1,41 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io.args; + +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** Annotation to mark a method as command argument method. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + */ +@Retention(RUNTIME) +@Target(METHOD) +@interface Option { + + /** The argument names. + * Usually this is two Strings, a single letter and a descriptive String. + * @note the supplied string values should not contain neither spaces nor non-ascii characters. + */ + String[] value(); +} // @interface Option Property changes on: trunk/src/app/net/sf/japi/io/args/Option.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: trunk/src/app/net/sf/japi/io/args/StopOption.java =================================================================== --- trunk/src/app/net/sf/japi/io/args/StopOption.java (rev 0) +++ trunk/src/app/net/sf/japi/io/args/StopOption.java 2006-04-15 00:20:22 UTC (rev 60) @@ -0,0 +1,38 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io.args; + +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** Annotation to declare an option as stop option. + * A stop option is an option that stops argument parsing and exits normal program flow. + * Typical stop options are "-V" / "--version" and "-h" / "--help". + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + * @see Option + */ +@Retention(RUNTIME) +@Target(METHOD) +@interface StopOption { +} // @interface StopOption Property changes on: trunk/src/app/net/sf/japi/io/args/StopOption.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. |