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 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() 115 throws Command.Exception 116 { 117 if (!args.isEmpty()) { 118 StringBuilder sb = new StringBuilder(); 119 for (String a : args) 120 sb.append(" ").append(a); 121 throw new Command.Exception("%s: Extraneous arguments:%s", 122 command, sb.toString()); 123 } 124 } 125 126 protected void noDry() 127 throws Command.Exception 128 { 129 if (dry) 130 throw new Command.Exception("%s: Option -n (--dry-run)" 131 + " not supported", 132 command); 133 } 134 135 } --- EOF ---