ODPSConsoleUtils { public static String[] translateCommandline(String line) throws ODPSConsoleException { if (StringUtils.isNullOrEmpty(line)) { return new String[0]; } final int normal = 0; final int inQuote = 1; final int inDoubleQuote = 2; int state = normal; StringTokenizer tok = new StringTokenizer(line, "\"\' \t\n\r\f", true); ArrayList<String> v = new ArrayList<String>(); StringBuffer current = new StringBuffer(); boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { String nextTok = tok.nextToken(); switch (state) { case inQuote: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; current.append(nextTok); } else { current.append(nextTok); } break; case inDoubleQuote: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; current.append(nextTok); } else { current.append(nextTok); } break; default: if ("\'".equals(nextTok)) { state = inQuote; current.append(nextTok); } else if ("\"".equals(nextTok)) { state = inDoubleQuote; current.append(nextTok); } else if (nextTok.matches("\\s")) { if (lastTokenHasBeenQuoted || current.length() != 0) { v.add(current.toString()); current = new StringBuffer(); } } else { current.append(nextTok); } lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) { v.add(current.toString()); } if (state == inQuote || state == inDoubleQuote) { throw new ODPSConsoleException("unbalanced quotes in " + line); } return v.toArray(new String[]{}); }  static String formatDate(Date date); @SuppressWarnings("resource") static void formaterTable(List<String[]> contentList, int[] columnPercent, int width); static void formaterTableRow(String[] str, int[] columnPercent, int width); static int getConsoleWidth(); static boolean isWindows(); static String getConfigFilePath(); static String getMvnVersion(); static String getUserAgent(); static boolean containsHanBlock(String s); static String shiftOption(List<String> optionList, String option); static TablePart getTablePart(String cmd); static TablePart getTablePartFromPubCommand(String cmd); static String[] parseTableSpec(String str); static String generateLogView(Odps odps, Instance instance, ExecutionContext context); static String[] translateCommandline(String line); synchronized static String runCommand(AbstractCommand command); static void checkThreadInterrupted(); static ODPSConsoleReader getOdpsConsoleReader(); static String safeGetString(LazyLoad resource, String methodName); static String safeGetDateString(LazyLoad resource, String methodName); static Object safeGetObject(LazyLoad resource, String methodName); static int compareVersion(String left, String right); static Map<String, Integer> getDisplayWidth(List<Column> columns,
                                                     List<Column> partitionsColumns,
                                                     List<String> selectColumns); static String makeOutputFrame(Map<String, Integer> displayWidth); static String makeTitle(List<Column> columns, Map<String, Integer> displayWidth); static final SimpleDateFormat DATE_FORMAT; }
@Test public void testTranslateCommandline() throws ODPSConsoleException { String line = "ABC \n \"abc def\" 'd\ne' \t\r\n fge"; String[] st = ODPSConsoleUtils.translateCommandline(line); assertEquals(st[0], "ABC"); assertEquals(st[1], "\"abc def\""); assertEquals(st[2], "'d\ne'"); assertEquals(st[3], "fge"); assertEquals(st.length, 4); }