1 /*
   2  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.jigsaw.cli;
  27 
  28 import java.io.*;
  29 import java.nio.file.*;
  30 import java.util.*;
  31 import org.openjdk.internal.joptsimple.OptionSet;
  32 

  33 /* package */ abstract class Command<C> {
  34 
  35     static class Exception
  36         extends java.lang.Exception
  37     {

  38         private static final long serialVersionUID = 74132770414881L;
  39 
  40         public Exception(String fmt, Object ... args) {
  41             super(String.format(fmt, args));
  42         }
  43 
  44         private static String summarize(String what, IOException x) {
  45             String msg = null;
  46             if (x instanceof FileSystemException) {
  47                 FileSystemException y = (FileSystemException)x;
  48                 // ## There should be a better way to do this!
  49                 msg = (y.getClass().getName()
  50                        .replace("Exception", "")
  51                        .replace("java.nio.file.", "")
  52                        .replaceAll("(\\p{Lower})(\\p{Upper})",
  53                                    "$1 $2"));
  54                 msg = msg.charAt(0) + msg.substring(1).toLowerCase();
  55                 if (what == null)
  56                     what = y.getFile();
  57             } else {
  58                 msg = x.getMessage();
  59             }
  60             if (what != null)
  61                 return String.format("%s: %s", what, msg);
  62             return String.format("I/O error: %s", x.getMessage());
  63         }
  64 
  65         public Exception(IOException x) {
  66             super(summarize(null, x));
  67             initCause(x);
  68         }
  69 
  70         public Exception(String what, IOException x) {
  71             super(summarize(what, x));
  72             initCause(x);
  73         }
  74 
  75         public Exception(java.lang.Exception x) {
  76             super(x);
  77         }

  78     }
  79 
  80     protected boolean verbose;
  81     protected boolean force;
  82     protected boolean dry;
  83     protected boolean quiet;
  84     protected String command;
  85     protected LinkedList<String> args;
  86     protected OptionSet opts;
  87 
  88     final void run(C context, OptionSet opts) throws Command.Exception {
  89         verbose = opts.has("verbose");
  90         force = opts.has("force");
  91         dry = opts.has("dry-run");
  92         quiet = opts.has("quiet");
  93         args = new LinkedList<>(opts.nonOptionArguments());
  94         command = args.remove();
  95         this.opts = opts;
  96         go(context);
  97     }
  98 
  99     protected abstract void go(C context) throws Command.Exception;
 100 
 101     protected boolean hasArg() {
 102         return !args.isEmpty();
 103     }
 104 
 105     protected String takeArg()
 106         throws Command.Exception
 107     {
 108         if (args.isEmpty())
 109             throw new Command.Exception("%s: Insufficient arguments", command);
 110         return args.remove();
 111     }
 112 
 113     protected void finishArgs()
 114         throws Command.Exception
 115     {
 116         if (!args.isEmpty()) {
 117             StringBuilder sb = new StringBuilder();
 118             for (String a : args)
 119                 sb.append(" ").append(a);
 120             throw new Command.Exception("%s: Extraneous arguments:%s",
 121                                         command, sb.toString());
 122         }
 123     }
 124 
 125     protected void noDry()
 126         throws Command.Exception
 127     {
 128         if (dry)
 129             throw new Command.Exception("%s: Option -n (--dry-run)"
 130                                         + " not supported",
 131                                         command);
 132     }
 133 
 134 }
--- EOF ---