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 34 /* package */ abstract class Command<C> { 35 36 static class Exception 37 extends java.lang.Exception 38 { 39 40 private static final long serialVersionUID = 74132770414881L; 41 42 public Exception(String fmt, Object ... args) { 43 super(String.format(fmt, args)); 44 } 45 46 private static String summarize(String what, IOException x) { 47 String msg = null; 48 if (x instanceof FileSystemException) { 49 FileSystemException y = (FileSystemException)x; 50 // ## There should be a better way to do this! 51 msg = (y.getClass().getName() 52 .replace("Exception", "") 53 .replace("java.nio.file.", "") 54 .replaceAll("(\\p{Lower})(\\p{Upper})", 55 "$1 $2")); 56 msg = msg.charAt(0) + msg.substring(1).toLowerCase(); 57 if (what == null) 58 what = y.getFile(); 59 } else { 60 msg = x.getMessage(); 61 } 62 if (what != null) 63 return String.format("%s: %s", what, msg); 64 return String.format("I/O error: %s", x.getMessage()); 65 } 66 67 public Exception(IOException x) { 68 super(summarize(null, x)); 69 initCause(x); 70 } 71 72 public Exception(String what, IOException x) { 73 super(summarize(what, x)); 74 initCause(x); 75 } 76 77 public Exception(java.lang.Exception x) { 78 super(x); 79 } 80 81 } 82 83 protected boolean verbose; 84 protected boolean force; 85 protected boolean dry; 86 protected String command; 87 protected LinkedList<String> args; 88 protected OptionSet opts; 89 90 final void run(C context, OptionSet opts) throws Command.Exception { 91 verbose = opts.has("verbose"); 92 force = opts.has("force"); 93 dry = opts.has("dry-run"); 94 args = new LinkedList<String>(opts.nonOptionArguments()); 95 command = args.remove(); 96 this.opts = opts; 97 go(context); 98 } 99 100 protected abstract void go(C context) throws Command.Exception; 101 102 protected boolean hasArg() { 103 return !args.isEmpty(); 104 } 105 106 protected String takeArg() 107 throws Command.Exception 108 { 109 if (args.isEmpty()) 110 throw new Command.Exception("%s: Insufficient arguments", command); 111 return args.remove(); 112 } 113 114 protected void finishArgs() | 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() |