📄 utils.java
字号:
return (a - b < SMALL) && (b - a < SMALL); } /** * Checks if the given array contains any non-empty options. * * @param options an array of strings * @exception Exception if there are any non-empty options */ public static void checkForRemainingOptions(String [] options) throws Exception { int illegalOptionsFound = 0; StringBuffer text = new StringBuffer(); if (options == null) { return; } for (int i = 0; i < options.length; i++) { if (options[i].length() > 0) { illegalOptionsFound++; text.append(options[i] + ' '); } } if (illegalOptionsFound > 0) { throw new Exception("Illegal options: " + text); } } /** * Checks if the given array contains the flag "-Char". Stops * searching at the first marker "--". If the flag is found, * it is replaced with the empty string. * * @param flag the character indicating the flag. * @param options the array of strings containing all the options. * @return true if the flag was found * @exception Exception if an illegal option was found */ public static boolean getFlag(char flag, String [] options) throws Exception { return getFlag("" + flag, options); } /** * Checks if the given array contains the flag "-String". Stops * searching at the first marker "--". If the flag is found, * it is replaced with the empty string. * * @param flag the String indicating the flag. * @param options the array of strings containing all the options. * @return true if the flag was found * @exception Exception if an illegal option was found */ public static boolean getFlag(String flag, String [] options) throws Exception { int pos = getOptionPos(flag, options); if (pos > -1) options[pos] = ""; return (pos > -1); } /** * Gets an option indicated by a flag "-Char" from the given array * of strings. Stops searching at the first marker "--". Replaces * flag and option with empty strings. * * @param flag the character indicating the option. * @param options the array of strings containing all the options. * @return the indicated option or an empty string * @exception Exception if the option indicated by the flag can't be found */ public static /*@non_null@*/ String getOption(char flag, String [] options) throws Exception { return getOption("" + flag, options); } /** * Gets an option indicated by a flag "-String" from the given array * of strings. Stops searching at the first marker "--". Replaces * flag and option with empty strings. * * @param flag the String indicating the option. * @param options the array of strings containing all the options. * @return the indicated option or an empty string * @exception Exception if the option indicated by the flag can't be found */ public static /*@non_null@*/ String getOption(String flag, String [] options) throws Exception { String newString; int i = getOptionPos(flag, options); if (i > -1) { if (options[i].equals("-" + flag)) { if (i + 1 == options.length) { throw new Exception("No value given for -" + flag + " option."); } options[i] = ""; newString = new String(options[i + 1]); options[i + 1] = ""; return newString; } if (options[i].charAt(1) == '-') { return ""; } } return ""; } /** * Gets the index of an option or flag indicated by a flag "-Char" from * the given array of strings. Stops searching at the first marker "--". * * @param flag the character indicating the option. * @param options the array of strings containing all the options. * @return the position if found, or -1 otherwise */ public static int getOptionPos(char flag, String[] options) { return getOptionPos("" + flag, options); } /** * Gets the index of an option or flag indicated by a flag "-String" from * the given array of strings. Stops searching at the first marker "--". * * @param flag the String indicating the option. * @param options the array of strings containing all the options. * @return the position if found, or -1 otherwise */ public static int getOptionPos(String flag, String[] options) { if (options == null) return -1; for (int i = 0; i < options.length; i++) { if ((options[i].length() > 0) && (options[i].charAt(0) == '-')) { // Check if it is a negative number try { Double.valueOf(options[i]); } catch (NumberFormatException e) { // found? if (options[i].equals("-" + flag)) return i; // did we reach "--"? if (options[i].charAt(1) == '-') return -1; } } } return -1; } /** * Quotes a string if it contains special characters. * * The following rules are applied: * * A character is backquoted version of it is one * of <tt>" ' % \ \n \r \t</tt>. * * A string is enclosed within single quotes if a character has been * backquoted using the previous rule above or contains * <tt>{ }</tt> or is exactly equal to the strings * <tt>, ? space or ""</tt> (empty string). * * A quoted question mark distinguishes it from the missing value which * is represented as an unquoted question mark in arff files. * * @param string the string to be quoted * @return the string (possibly quoted) */ public static /*@pure@*/ String quote(String string) { boolean quote = false; // backquote the following characters if ((string.indexOf('\n') != -1) || (string.indexOf('\r') != -1) || (string.indexOf('\'') != -1) || (string.indexOf('"') != -1) || (string.indexOf('\\') != -1) || (string.indexOf('\t') != -1) || (string.indexOf('%') != -1)) { string = backQuoteChars(string); quote = true; } // Enclose the string in 's if the string contains a recently added // backquote or contains one of the following characters. if((quote == true) || (string.indexOf('{') != -1) || (string.indexOf('}') != -1) || (string.indexOf(',') != -1) || (string.equals("?")) || (string.indexOf(' ') != -1) || (string.equals(""))) { string = ("'".concat(string)).concat("'"); } return string; } /** * Converts carriage returns and new lines in a string into \r and \n. * Backquotes the following characters: ` " \ \t and % * @param string the string * @return the converted string */ public static /*@pure@*/ String backQuoteChars(String string) { int index; StringBuffer newStringBuffer; // replace each of the following characters with the backquoted version char charsFind[] = {'\\', '\'', '\t', '"', '%'}; String charsReplace[] = {"\\\\", "\\'", "\\t", "\\\"", "\\%"}; for(int i = 0; i < charsFind.length; i++) { if (string.indexOf(charsFind[i]) != -1 ) { newStringBuffer = new StringBuffer(); while ((index = string.indexOf(charsFind[i])) != -1) { if (index > 0) { newStringBuffer.append(string.substring(0, index)); } newStringBuffer.append(charsReplace[i]); if ((index + 1) < string.length()) { string = string.substring(index + 1); } else { string = ""; } } newStringBuffer.append(string); string = newStringBuffer.toString(); } } return Utils.convertNewLines(string); } /** * Converts carriage returns and new lines in a string into \r and \n. * @param string the string * @return the converted string */ public static /*@pure@*/ String convertNewLines(String string) { int index; // Replace with \n StringBuffer newStringBuffer = new StringBuffer(); while ((index = string.indexOf('\n')) != -1) { if (index > 0) { newStringBuffer.append(string.substring(0, index)); } newStringBuffer.append('\\'); newStringBuffer.append('n'); if ((index + 1) < string.length()) { string = string.substring(index + 1); } else { string = ""; } } newStringBuffer.append(string); string = newStringBuffer.toString(); // Replace with \r newStringBuffer = new StringBuffer(); while ((index = string.indexOf('\r')) != -1) { if (index > 0) { newStringBuffer.append(string.substring(0, index)); } newStringBuffer.append('\\'); newStringBuffer.append('r'); if ((index + 1) < string.length()){ string = string.substring(index + 1); } else { string = ""; } } newStringBuffer.append(string); return newStringBuffer.toString(); } /** * Returns the secondary set of options (if any) contained in * the supplied options array. The secondary set is defined to * be any options after the first "--". These options are removed from * the original options array. * * @param options the input array of options * @return the array of secondary options */ public static String [] partitionOptions(String [] options) { for (int i = 0; i < options.length; i++) { if (options[i].equals("--")) { options[i++] = ""; String [] result = new String [options.length - i]; for (int j = i; j < options.length; j++) { result[j - i] = options[j]; options[j] = ""; } return result; } } return new String [0]; } /** * The inverse operation of backQuoteChars(). * Converts back-quoted carriage returns and new lines in a string * to the corresponding character ('\r' and '\n'). * Also "un"-back-quotes the following characters: ` " \ \t and % * @param string the string * @return the converted string */ public static String unbackQuoteChars(String string) { int index; StringBuffer newStringBuffer; // replace each of the following characters with the backquoted version String charsFind[] = {"\\\\", "\\'", "\\t", "\\\"", "\\%"}; char charsReplace[] = {'\\', '\'', '\t', '"', '%'}; for(int i = 0; i < charsFind.length; i++) { if (string.indexOf(charsFind[i]) != -1 ) { newStringBuffer = new StringBuffer(); while ((index = string.indexOf(charsFind[i])) != -1) { if (index > 0) { newStringBuffer.append(string.substring(0, index)); } newStringBuffer.append(charsReplace[i]); if ((index + charsFind[i].length()) < string.length()) { string = string.substring(index + charsFind[i].length()); } else { string = ""; } } newStringBuffer.append(string); string = newStringBuffer.toString(); } } return Utils.convertNewLines(string); } /** * Split up a string containing options into an array of strings, * one for each option. * * @param quotedOptionString the string containing the options * @return the array of options * @throws Exception in case of an unterminated string, unknown character or * a parse error */ public static String [] splitOptions(String quotedOptionString) throws Exception{ FastVector optionsVec = new FastVector(); String str = new String(quotedOptionString); int i; while (true){ //trimLeft i = 0; while ((i < str.length()) && (Character.isWhitespace(str.charAt(i)))) i++; str = str.substring(i); //stop when str is empty if (str.length() == 0) break; //if str start with a double quote if (str.charAt(0) == '"'){ //find the first not anti-slached double quote i = 1; while(i < str.length()){ if (str.charAt(i) == str.charAt(0)) break; if (str.charAt(i) == '\\'){ i += 1; if (i >= str.length()) throw new Exception("String should not finish with \\"); } i += 1; } if (i >= str.length()) throw new Exception("Quote parse error."); //add the founded string to the option vector (without quotes) String optStr = str.substring(1,i); optStr = unbackQuoteChars(optStr); optionsVec.addElement(optStr); str = str.substring(i+1); } else { //find first whiteSpace i=0; while((i < str.length()) && (!Character.isWhitespace(str.charAt(i)))) i++; //add the founded string to the option vector String optStr = str.substring(0,i); optionsVec.addElement(optStr); str = str.substring(i); } } //convert optionsVec to an array of String String [] options = new String[optionsVec.size()]; for (i = 0; i < optionsVec.size(); i++) { options[i] = (String)optionsVec.elementAt(i); } return options; } /** * Joins all the options in an option array into a single string, * as might be used on the command line. * * @param optionArray the array of options * @return the string containing all options. */ public static String joinOptions(String [] optionArray) { String optionString = ""; for (int i = 0; i < optionArray.length; i++) { if (optionArray[i].equals("")) { continue; } if (optionArray[i].indexOf(' ') != -1) { optionString += '"' + backQuoteChars(optionArray[i]) + '"'; } else { optionString += optionArray[i]; } optionString += " "; } return optionString.trim(); } /** * Creates a new instance of an object given it's class name and * (optional) arguments to pass to it's setOptions method. If the * object implements OptionHandler and the options parameter is * non-null, the object will have it's options set. Example use:<p> * * <code> <pre> * String classifierName = Utils.getOption('W', options); * Classifier c = (Classifier)Utils.forName(Classifier.class, * classifierName, * options); * setClassifier(c); * </pre></code> * * @param classType the class that the instantiated object should * be assignable to -- an exception is thrown if this is not the case * @param className the fully qualified class name of the object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -