commandgroupadapter.java
来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 508 行 · 第 1/2 页
JAVA
508 行
} catch (Exception e) { out.println(e.getMessage()); return -1; } } /** * Method to do argument parsing. See parsed syntax in class * <tt>CommandGroupAdapter</tt> description. * * @param args * argument list passed to the command * @param usage * usage string * @return Dictionary with parsed arguments * @exception Exception * Thrown if it fails to parse args or usage */ public Dictionary getOpt(String[] args, String usage) throws Exception { Hashtable res = new Hashtable(); res.put("command", args[0]); args[0] = null; parseUsage(usage.trim(), 0, args, res, 0); for (int i = 0; i < args.length; i++) { if (args[i] != null) { throw new Exception("Unknown argument: " + args[i]); } } return res; } // // Internal methods // private final static String DOING_ARGS = "_D_"; private final static String LAST = "_L_"; private int parseUsage(String usage, int pos, String[] args, Hashtable res, int level) throws Exception { int ulen = usage.length(); while (pos < ulen) { switch (usage.charAt(pos)) { case '-': if (res.containsKey(DOING_ARGS)) { throw new Exception("Can not mix flags and args: " + usage.substring(pos)); } boolean found = false; String flag = ""; boolean alt; do { alt = false; int start = pos++; while (pos < ulen && usage.charAt(pos) != ' ' && usage.charAt(pos) != ']') pos++; flag = usage.substring(start, pos); Object val = new Integer(1); int fpos = -1; for (int i = 0; i < args.length; i++) { if (flag.equals(args[i])) { fpos = i; break; } } while (pos < ulen && usage.charAt(pos) == ' ') pos++; if (pos < ulen) { if (usage.charAt(pos) == '#') { while (pos < ulen && usage.charAt(pos) != ' ' && usage.charAt(pos) != ']') pos++; if (fpos >= 0) { if (fpos + 1 < args.length) { val = args[fpos + 1]; args[fpos + 1] = null; } else { throw new Exception("No value for: " + flag); } } while (pos < ulen && usage.charAt(pos) == ' ') pos++; } if (pos < ulen) { if (usage.charAt(pos) == '|') { pos++; while (pos < ulen && usage.charAt(pos) == ' ') pos++; if (usage.charAt(pos) == '-') { alt = true; } else { throw new Exception( "Missing flag in OR-expression: " + usage.substring(pos)); } } } } if (fpos >= 0) { Object old = res.put(args[fpos], val); if (old != null) { if (old instanceof Integer) { res.put(args[fpos], new Integer(((Integer) old) .intValue() + 1)); } else { throw new Exception( "Duplicate flagname with value in usage: " + args[fpos]); } } args[fpos] = null; found = true; } } while (alt); if (!found && level == 0) { throw new Exception("Mandatory flag not set, flags: " + usage); } break; case '<': res.put(DOING_ARGS, ""); int wstart = ++pos; pos = usage.indexOf('>', ++pos); if (pos == -1) { throw new Exception("Unmatched: " + usage.substring(wstart - 1)); } String key = usage.substring(wstart, pos++); int i; for (i = 0; i < args.length; i++) { if (args[i] != null) { if (args[i].startsWith("-")) { // '--' means '-' at begining of args if (args[i].startsWith("--")) { args[i] = args[i].substring(1); } else { throw new Exception("Unknown flag: " + args[i]); } } if (res.put(key, args[i]) != null) { throw new Exception("Duplicate argname in usage: " + key); } args[i] = null; break; } } if (i == args.length && level == 0) { throw new Exception("Mandatory argument not set: " + key); } res.put(LAST, key); break; case '[': pos = parseUsage(usage, pos + 1, args, res, level + 1); break; case ']': if (level == 0) { throw new Exception("Unmatched: " + usage.substring(pos)); } return pos + 1; case '.': if (usage.substring(pos).equals("...")) { String repeat = (String) res.get(LAST); if (repeat != null && res.containsKey(repeat)) { ArrayList v = new ArrayList(); do { v.add(res.remove(repeat)); parseUsage("<" + repeat + ">]", 0, args, res, 1); } while (res.containsKey(repeat)); String[] vres = new String[v.size()]; res.put(repeat, v.toArray(vres)); } pos = ulen; } else { throw new Exception("Unexpected usage end: " + usage.substring(pos)); } break; case ' ': pos++; break; default: throw new Exception("Unexpected character: " + usage.charAt(pos)); } } if (level > 0) { throw new Exception("Missing " + level + " closing ']'"); } res.remove(DOING_ARGS); res.remove(LAST); return pos; }}class DynamicCmd { Method cmd; String usage; String[] help; DynamicCmd(CommandGroup cg, String name) throws Exception { try { Class cls = cg.getClass(); String hname = "HELP_" + name.toUpperCase(); Field[] f = cls.getFields(); int match = -1; for (int i = 0; i < f.length; i++) { String fname = f[i].getName(); if (fname.startsWith(hname)) { if (match != -1) { throw new Exception("Multiple matching commands for: " + hname.substring(5)); } match = i; if (fname.equals(hname)) { break; } name = fname.substring(5); } } if (match == -1) { throw new Exception("No such command: " + name); } help = (String[]) f[match].get(cg); usage = (String) cls.getField("USAGE_" + name.toUpperCase()) .get(cg); cmd = cls.getMethod("cmd" + name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(), new Class[] { java.util.Dictionary.class, java.io.Reader.class, java.io.PrintWriter.class, Session.class }); if (!cmd.getReturnType().getName().equals("int")) { throw new Exception("No such command: " + name); } } catch (ClassNotFoundException e) { throw new Exception("Internal error"); } catch (NoSuchFieldException e) { throw new Exception( "Command implementation incomplete (USAGE string missing): " + name); } catch (NoSuchMethodException e) { throw new Exception( "Command implementation incomplete (cmd method missing): " + name); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?