import java.util.Arrays;
import jp.sourceforge.dvibrowser.dvi2epub.cmd.AnnotatedCommand;
-import jp.sourceforge.dvibrowser.dvi2epub.cmd.BooleanValueOption;
import jp.sourceforge.dvibrowser.dvi2epub.cmd.Command;
import jp.sourceforge.dvibrowser.dvi2epub.cmd.CommandException;
import jp.sourceforge.dvibrowser.dvi2epub.cmd.CommandUtils;
-import jp.sourceforge.dvibrowser.dvi2epub.cmd.IntValueOption;
-import jp.sourceforge.dvibrowser.dvi2epub.cmd.StringValueOption;
+import jp.sourceforge.dvibrowser.dvi2epub.opt.BooleanValueOption;
+import jp.sourceforge.dvibrowser.dvi2epub.opt.IntValueOption;
+import jp.sourceforge.dvibrowser.dvi2epub.opt.StringValueOption;
public class Dvi2EpubCmd extends AnnotatedCommand {
import java.io.PrintWriter;
+import jp.sourceforge.dvibrowser.dvi2epub.opt.BooleanValueOption;
+import jp.sourceforge.dvibrowser.dvi2epub.opt.DefaultOptionMapper;
+
public abstract class AnnotatedCommand extends AbstractCommand {
private CommandLineParser parser;
private boolean wantHelp;
+++ /dev/null
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
-
-public class AnnotatedCommandLine implements CommandLine {
- private final String[] args;
- private final AnnotatedCommand command;
-
- public AnnotatedCommandLine(AnnotatedCommand command, String[] args) {
- this.command = command;
- this.args = args;
- }
-
- @Override
- public boolean hasOption(String name) {
- return false;
- }
-
- public String[] getArgs() {
- return args;
- }
-
- public AnnotatedCommand getCommand() {
- return command;
- }
-}
}
@Override
- public CommandLine parse(String[] args) throws CommandException {
- CommandLine commandLine = new AnnotatedCommandLine(command, args);
+ public void parse(String[] args) throws CommandException {
ParserState state = new ParserState(args);
while (!state.wantStop()) {
String arg1 = state.shift();
}
command.setArgs(state.getList().toArray(new String [0]));
-
- return commandLine;
}
private void parseShortOption(final ParserState state, final String arg1) throws CommandException {
+++ /dev/null
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
-
-public interface CommandLine {
- boolean hasOption(String name);
-}
import java.io.PrintWriter;
public interface CommandLineParser {
- CommandLine parse(String [] args) throws CommandException;
+ void parse(String [] args) throws CommandException;
void printHelp(PrintWriter pw) throws CommandException;
}
+++ /dev/null
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Member;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
-import jp.sourceforge.dvibrowser.dvi2epub.reflect.MemberWalkerAdapter;
-import jp.sourceforge.dvibrowser.dvi2epub.reflect.MemberWalkerException;
-
-final class CopyOfOptionAdapter extends MemberWalkerAdapter {
- private final String name;
- private final String arguments;
- private boolean done;
- private final ParserState state;
- private List<Class<?>> classes;
- private String targetMethod;
-
- CopyOfOptionAdapter(ParserState state, String name, String arguments) {
- this.name = name;
- this.arguments = arguments;
- this.state = state;
- targetMethod = "buildArgs";
- classes = new ArrayList<Class<?>>();
- for (Method mm : this.getClass().getDeclaredMethods()) {
- if (mm.getName().equals(targetMethod)) {
- Class<?>[] clss = mm.getParameterTypes();
- if (clss.length > 0) {
- Class<?> cc = clss[0];
- if (cc.isAnnotation()) {
- classes.add(cc);
- }
- }
- }
- }
- }
-
- public boolean wantMember(Object o, Member member) {
- return !done;
- }
-
- public void setDone(boolean value) {
- done = value;
- }
-
- public boolean isDone() {
- return done;
- }
-
- public void processMethod(Object o, Method method)
- throws MemberWalkerException {
- Annotation[] annotations = method.getDeclaredAnnotations();
- try {
- for (Annotation a : annotations) {
- for (Class<?> cls : classes) {
- if (a.annotationType().equals(cls)) {
- // System.out.println("cls=" + cls);
- Method methodS = cls.getDeclaredMethod("shortName",
- new Class<?>[] {});
- Method methodL = cls.getDeclaredMethod("longName",
- new Class<?>[] {});
- String shortName = (String) methodS.invoke(a);
- String longName = (String) methodL.invoke(a);
- if (nameHits(name, shortName, longName)) {
- // System.out.println("shortName=" + shortName +
- // " longName=" + longName);
- Method m = this.getClass().getDeclaredMethod(
- "buildArgs", new Class<?>[] { cls });
- Object[] args = (Object[]) m.invoke(this,
- new Object[] { a });
- method.invoke(o, args);
- setDone(true);
- }
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- state.stopWithError(ex);
- return;
- }
- }
-
- private boolean nameHits(String name, String shortName, String longName) {
- if (shortName != null && shortName.equals(name)) {
- return true;
- } else if (longName != null && longName.equals(name)) {
- return true;
- }
- return false;
- }
-
- protected Object[] buildArgs(IntValueOption p) {
- String a = (arguments == null) ? state.shift() : arguments;
- int value = (a == null) ? p.value() : Integer.parseInt(a);
- return new Object[] { value };
- }
-
- protected Object[] buildArgs(StringValueOption p) {
- String value = CommandUtils.unescapeNull((CommandUtils
- .unescapeNull(arguments) == null) ? p.value() : arguments);
- return new Object[] { value };
- }
-
- protected Object[] buildArgs(BooleanValueOption p) {
- boolean value = CommandUtils.parseBoolean(arguments, p.value());
- return new Object[] { value };
- }
-}
\ No newline at end of file
+++ /dev/null
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Member;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
-import jp.sourceforge.dvibrowser.dvi2epub.reflect.MemberWalkerAdapter;
-import jp.sourceforge.dvibrowser.dvi2epub.reflect.MemberWalkerException;
-
-final class FormatAdapter extends MemberWalkerAdapter {
- private final String name;
- private final String arguments;
- private boolean done;
- private final ParserState state;
- private List<Class<?>> classes;
- private String targetMethod;
-
- FormatAdapter(ParserState state, String name, String arguments) {
- this.name = name;
- this.arguments = arguments;
- this.state = state;
- targetMethod = "buildArgs";
- classes = new ArrayList<Class<?>>();
- for (Method mm : this.getClass().getDeclaredMethods()) {
- if (mm.getName().equals(targetMethod)) {
- Class<?>[] clss = mm.getParameterTypes();
- if (clss.length > 0) {
- Class<?> cc = clss[0];
- if (cc.isAnnotation()) {
- classes.add(cc);
- }
- }
- }
- }
- }
-
- public boolean wantMember(Object o, Member member) {
- return !done;
- }
-
- public void setDone(boolean value) {
- done = value;
- }
-
- public boolean isDone() {
- return done;
- }
-
- public void processMethod(Object o, Method method)
- throws MemberWalkerException {
- Annotation[] annotations = method.getDeclaredAnnotations();
- try {
- for (Annotation a : annotations) {
- for (Class<?> cls : classes) {
- if (a.annotationType().equals(cls)) {
- // System.out.println("cls=" + cls);
- Method methodS = cls.getDeclaredMethod("shortName",
- new Class<?>[] {});
- Method methodL = cls.getDeclaredMethod("longName",
- new Class<?>[] {});
- String shortName = (String) methodS.invoke(a);
- String longName = (String) methodL.invoke(a);
- if (nameHits(name, shortName, longName)) {
- // System.out.println("shortName=" + shortName +
- // " longName=" + longName);
- Method m = this.getClass().getDeclaredMethod(
- "buildArgs", new Class<?>[] { cls });
- Object[] args = (Object[]) m.invoke(this,
- new Object[] { a });
- method.invoke(o, args);
- setDone(true);
- }
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- state.stopWithError(ex);
- return;
- }
- }
-
- private boolean nameHits(String name, String shortName, String longName) {
- if (shortName != null && shortName.equals(name)) {
- return true;
- } else if (longName != null && longName.equals(name)) {
- return true;
- }
- return false;
- }
-
- protected Object[] buildArgs(IntValueOption p) {
- String a = (arguments == null) ? state.shift() : arguments;
- int value = (a == null) ? p.value() : Integer.parseInt(a);
- return new Object[] { value };
- }
-
- protected Object[] buildArgs(StringValueOption p) {
- String value = CommandUtils.unescapeNull((CommandUtils
- .unescapeNull(arguments) == null) ? p.value() : arguments);
- return new Object[] { value };
- }
-
- protected Object[] buildArgs(BooleanValueOption p) {
- boolean value = CommandUtils.parseBoolean(arguments, p.value());
- return new Object[] { value };
- }
-}
\ No newline at end of file
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
+package jp.sourceforge.dvibrowser.dvi2epub.opt;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
+package jp.sourceforge.dvibrowser.dvi2epub.opt;
+
+import jp.sourceforge.dvibrowser.dvi2epub.cmd.CommandUtils;
+import jp.sourceforge.dvibrowser.dvi2epub.cmd.OptionMapper;
+import jp.sourceforge.dvibrowser.dvi2epub.cmd.ParserState;
public class DefaultOptionMapper
implements OptionMapper
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
+package jp.sourceforge.dvibrowser.dvi2epub.opt;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
* limitations under the License.
*/
-package jp.sourceforge.dvibrowser.dvi2epub.cmd;
+package jp.sourceforge.dvibrowser.dvi2epub.opt;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;