List of usage examples for java.util StringTokenizer StringTokenizer
public StringTokenizer(String str)
From source file:com.stratuscom.harvester.Utils.java
public static String[] splitOnWhitespace(String input) { List<String> strings = new ArrayList<String>(); StringTokenizer tok = new StringTokenizer(Strings.WHITESPACE_SEPARATORS); while (tok.hasMoreTokens()) { strings.add(tok.nextToken());//from ww w. j a va 2s . c o m } return (String[]) strings.toArray(new String[0]); }
From source file:Main.java
/** * <p>//from ww w. j av a 2s. c o m * Splits the provided text into a array, based on a given separator. * </p> * <p/> * <p> * The separator is not included in the returned String array. The maximum number of splits to perfom can be * controlled. A <code>null</code> separator will cause parsing to be on whitespace. * </p> * <p/> * <p> * This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of * tokens (as <code>StringTokenizer</code> does). * </p> * * @param str The string to parse. * @param separator Characters used as the delimiters. If <code>null</code>, splits on whitespace. * @param max The maximum number of elements to parse. The rest of the string to parse will be contained in the last * array element. A zero or negative value implies no limit. * @return an array of parsed Strings */ public static String[] split(String str, String separator, int max) { StringTokenizer tok = null; if (separator == null) { // Null separator means we're using StringTokenizer's default // delimiter, which comprises all whitespace characters. tok = new StringTokenizer(str); } else { tok = new StringTokenizer(str, separator); } int listSize = tok.countTokens(); if ((max > 0) && (listSize > max)) { listSize = max; } String[] list = new String[listSize]; int i = 0; int lastTokenBegin = 0; int lastTokenEnd = 0; while (tok.hasMoreTokens()) { if ((max > 0) && (i == listSize - 1)) { // In the situation where we hit the max yet have // tokens left over in our input, the last list // element gets all remaining text. String endToken = tok.nextToken(); lastTokenBegin = str.indexOf(endToken, lastTokenEnd); list[i] = str.substring(lastTokenBegin); break; } else { list[i] = tok.nextToken(); lastTokenBegin = str.indexOf(list[i], lastTokenEnd); lastTokenEnd = lastTokenBegin + list[i].length(); } i++; } return list; }
From source file:ch.admin.suis.msghandler.common.MessageType.java
/** * Creates a list of message types from the provided string value. If the value * is null or empty, an empty list is returned. * * @param typesValue the types in the form <code>type1 type2</code>, i.e. the types are * numerical values separated by whitespaces * @return List of message types from a message *//*from w ww .ja v a 2s . c o m*/ public static List<MessageType> from(String typesValue) { // read the message types final ArrayList<MessageType> messageTypes = new ArrayList<>(); if (null != typesValue) { final StringTokenizer types = new StringTokenizer(typesValue); while (types.hasMoreTokens()) { String type = types.nextToken(); messageTypes.add(new MessageType(Integer.decode(type))); } } return messageTypes; }
From source file:edu.stanford.muse.util.SloppyDates.java
private static Triple<Integer, Integer, Integer> parseDate(String s) { // separate into month and date // "jan10", "10jan", "jan 10" "10 jan" should all work s = s.toLowerCase();/*from w w w.j a v a 2 s.c o m*/ s = s.trim(); StringBuilder sb = new StringBuilder(); // detect when string changes from alpha to num or vice versa and ensure a whitespace there boolean prevCharDigit = false, prevCharLetter = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isWhitespace(c)) { sb.append(c); prevCharDigit = prevCharLetter = false; continue; } // treat apostrophe like a space if (c == '\'') { sb.append(' '); prevCharDigit = prevCharLetter = false; continue; } if (Character.isLetter(c)) { if (prevCharDigit) sb.append(' '); sb.append(c); prevCharLetter = true; prevCharDigit = false; } else if (Character.isDigit(c)) { if (prevCharLetter) sb.append(' '); sb.append(c); prevCharDigit = true; prevCharLetter = false; } else throw new RuntimeException(); } String newS = sb.toString(); log.info("string " + s + " parsed to " + newS); StringTokenizer st = new StringTokenizer(newS); int nTokens = st.countTokens(); if (nTokens == 0 || nTokens > 3) return new Triple<Integer, Integer, Integer>(-1, -1, -1); int mm = -1, dd = -1, yy = -1; while (st.hasMoreTokens()) { String token = st.nextToken(); boolean isNumber = true; int num = -1; try { num = Integer.parseInt(token); } catch (NumberFormatException nfe) { isNumber = false; } if (isNumber && num < 0) return new Triple<Integer, Integer, Integer>(-1, -1, -1); if (isNumber) { if (dd == -1 && num > 0 && num <= 31) dd = num; else if (yy == -1) { yy = num; if (yy < 100) { yy = (yy > 12) ? (1900 + yy) : (2000 + yy); } if (yy < 1900 || yy > 2015) return new Triple<Integer, Integer, Integer>(-1, -1, -1); } else return new Triple<Integer, Integer, Integer>(-1, -1, -1); } else { int x = SloppyDates.uniquePrefixIdx(token, monthNames); if (x >= 0 && mm == -1) mm = x; else return new Triple<Integer, Integer, Integer>(-1, -1, -1); } } return new Triple<Integer, Integer, Integer>(dd, mm, yy); }
From source file:org.kuali.mobility.feedback.service.FeedbackServiceImpl.java
private void sendEmail(Feedback f) { try {//w w w .j av a 2s . com String sendEmailException = configParamService.findValueByName("Feedback.SendEmail.On"); if ("true".equals(sendEmailException)) { String emailAddress = configParamService.findValueByName("Feedback.SendEmail.EmailAddress"); StringTokenizer stringTokenizer = new StringTokenizer(emailAddress); while (stringTokenizer.hasMoreTokens()) { String email = stringTokenizer.nextToken(); emailService.sendEmail(f.toString(), "MIU Feedback", email, configParamService.findValueByName("Core.EmailAddress")); } } } catch (Exception e) { LOG.error("Error sending feedback email " + f.getFeedbackId(), e); } }
From source file:com.sshtools.common.util.X11Util.java
public static X11Cookie getCookie(XDisplay xdisplay) { if (System.getProperty("os.name").startsWith("Windows")) return getRNDCookie(); log.debug("Getting X11 cookie using xauth"); try {//w w w .jav a 2 s .c o m String host = xdisplay.getHost(); String display = host + ":" + xdisplay.getDisplay(); String cmd = "xauth list " + display + " 2>/dev/null"; if (host == null || host.equals("localhost") || host.equals("unix")) { cmd = "xauth list :" + xdisplay.getDisplay() + " 2>/dev/null"; } Process process = null; InputStream in = null; OutputStream out = null; try { log.debug("Executing " + cmd); process = Runtime.getRuntime().exec(cmd); BufferedReader reader = new BufferedReader(new InputStreamReader(in = process.getInputStream())); out = process.getOutputStream(); String line = null; String cookie = null; while ((line = reader.readLine()) != null) { log.debug(line); StringTokenizer t = new StringTokenizer(line); try { String fhost = t.nextToken(); String type = t.nextToken(); String value = t.nextToken(); if (cookie == null) { cookie = value; log.debug("Using cookie " + cookie); } return expand(type, cookie); } catch (Exception e) { log.error("Unexpected response from xauth.", e); log.warn("Trying random data."); return getRNDCookie(); } } } finally { IOUtil.closeStream(in); IOUtil.closeStream(out); } return getRNDCookie(); } catch (Exception e) { log.warn("Had problem finding xauth data (" + e + ") trying random data."); return getRNDCookie(); } }
From source file:fr.upmc.qvcs.scanner.service.resolver.name.BasicNameResolver.java
@Override public String resolve(String name) { String oldName = name;/*from w w w . j a v a 2 s. c o m*/ //5.1 case name = name.replace("5.1", ""); for (String s : SPLIT_CHAR) name = name.replace(s, " "); StringTokenizer tokenizer = new StringTokenizer(name); StringBuilder builder = new StringBuilder(); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken().trim(); if (isForbiddenToken(token)) continue; if (StringUtils.isAllUpperCase(token)) continue; builder.append(token.toLowerCase()).append(" "); } LOGGER.info("The name " + oldName + " had been resolved has " + name); return builder.toString(); }
From source file:keel.Algorithms.MIL.G3PMI.Main.java
/** * <p>//from w w w. j ava 2 s. co m * Configure the execution of the algorithm. * * @param jobFilename Name of the KEEL file with properties of the execution * </p> */ private static void configureJob(String jobFilename) { Properties props = new Properties(); try { InputStream paramsFile = new FileInputStream(jobFilename); props.load(paramsFile); paramsFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(0); } // Files training and test String trainFile; String testFile; StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData")); tokenizer.nextToken(); trainFile = tokenizer.nextToken(); trainFile = trainFile.substring(1, trainFile.length() - 1); testFile = tokenizer.nextToken(); testFile = testFile.substring(1, testFile.length() - 1); tokenizer = new StringTokenizer(props.getProperty("outputData")); String reportTrainFile = tokenizer.nextToken(); reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1); String reportTestFile = tokenizer.nextToken(); reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1); //System.out.println("SALIDA: " + reportTestFile); //String reportRulesFile = tokenizer.nextToken(); //reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length()-1); // Algorithm auxiliar configuration XMLConfiguration algConf = new XMLConfiguration(); algConf.setRootElementName("experiment"); algConf.addProperty("process.algorithm[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMIAlgorithm"); algConf.addProperty("process.algorithm.rand-gen-factory[@type]", "org.ayrna.jclec.util.random.RanecuFactory"); algConf.addProperty("process.algorithm.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed"))); algConf.addProperty("process.algorithm.population-size", Integer.parseInt(props.getProperty("population-size"))); algConf.addProperty("process.algorithm.max-of-generations", Integer.parseInt(props.getProperty("max-generations"))); algConf.addProperty("process.algorithm.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size"))); algConf.addProperty("process.algorithm.species[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMISyntaxTreeSpecies"); algConf.addProperty("process.algorithm.species.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size"))); algConf.addProperty("process.algorithm.species.dataset[@type]", "org.ayrna.jclec.util.dataset.KeelMultiInstanceDataSet"); algConf.addProperty("process.algorithm.species.dataset.file-name", trainFile); algConf.addProperty("process.algorithm.species.rand-gen-factory[@type]", "org.ayrna.jclec.util.random.RanecuFactory"); algConf.addProperty("process.algorithm.species.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed"))); algConf.addProperty("process.algorithm.evaluator[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMIEvaluator"); algConf.addProperty("process.algorithm.evaluator.rand-gen-factory[@type]", "org.ayrna.jclec.util.random.RanecuFactory"); algConf.addProperty("process.algorithm.evaluator.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed"))); algConf.addProperty("process.algorithm.evaluator.dataset[@type]", "org.ayrna.jclec.util.dataset.KeelMultiInstanceDataSet"); algConf.addProperty("process.algorithm.evaluator.dataset.file-name", trainFile); algConf.addProperty("process.algorithm.evaluator.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size"))); algConf.addProperty("process.algorithm.provider[@type]", "org.ayrna.jclec.syntaxtree.SyntaxTreeCreator"); algConf.addProperty("process.algorithm.parents-selector[@type]", "org.ayrna.jclec.selector.RouletteSelector"); algConf.addProperty("process.algorithm.recombinator.decorated[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMICrossover"); algConf.addProperty("process.algorithm.recombinator.recombination-prob", Double.parseDouble(props.getProperty("rec-prob"))); algConf.addProperty("process.algorithm.mutator.decorated[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMIMutator"); algConf.addProperty("process.algorithm.mutator.mutation-prob", Double.parseDouble(props.getProperty("mut-prob"))); algConf.addProperty("process.listeners.listener[@type]", "org.ayrna.jclec.problem.classification.syntaxtree.multiinstance.G3PMIKeel.G3PMIPopulationReport"); algConf.addProperty("process.listeners.listener.report-dir-name", "./"); algConf.addProperty("process.listeners.listener.train-report-file", reportTrainFile); algConf.addProperty("process.listeners.listener.test-report-file", reportTestFile); algConf.addProperty("process.listeners.listener.global-report-name", "resumen"); algConf.addProperty("process.listeners.listener.report-frequency", 50); algConf.addProperty("process.listeners.listener.test-dataset[@type]", "org.ayrna.jclec.util.dataset.KeelMultiInstanceDataSet"); algConf.addProperty("process.listeners.listener.test-dataset.file-name", testFile); try { algConf.save(new File("configure.txt")); } catch (ConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } org.ayrna.jclec.genlab.GenLab.main(new String[] { "configure.txt" }); }
From source file:edu.umass.cs.msocket.proxy.console.commands.GroupMemberList.java
@Override public void parse(String commandText) throws Exception { UniversalGnsClient gnsClient = module.getGnsClient(); try {// ww w. j ava 2 s . c om StringTokenizer st = new StringTokenizer(commandText.trim()); String groupGuid; if (st.countTokens() == 0) { groupGuid = module.getAccountGuid().getGuid(); } else if (st.countTokens() == 1) { groupGuid = st.nextToken(); if (!GnsUtils.isValidGuidString(groupGuid)) { // We probably have an alias, lookup the GUID groupGuid = gnsClient.lookupGuid(groupGuid); } } else { console.printString("Wrong number of arguments for this command.\n"); return; } console.printString("Members in group " + groupGuid); console.printNewline(); JSONArray members = gnsClient.groupGetMembers(groupGuid, module.getAccountGuid()); for (int i = 0; i < members.length(); i++) { console.printString(i + ": " + members.getString(i)); console.printNewline(); } } catch (Exception e) { console.printString("Failed to access GNS ( " + e + ")\n"); } }
From source file:keel.Algorithms.Genetic_Rule_Learning.Bojarczuk_GP.Main.java
/** * <p>//from w ww. ja v a 2 s . c om * Configure the execution of the algorithm. * * @param jobFilename Name of the KEEL file with properties of the execution * </p> */ private static void configureJob(String jobFilename) { Properties props = new Properties(); try { InputStream paramsFile = new FileInputStream(jobFilename); props.load(paramsFile); paramsFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(0); } // Files training and test String trainFile; String testFile; StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData")); tokenizer.nextToken(); trainFile = tokenizer.nextToken(); trainFile = trainFile.substring(1, trainFile.length() - 1); testFile = tokenizer.nextToken(); testFile = testFile.substring(1, testFile.length() - 1); tokenizer = new StringTokenizer(props.getProperty("outputData")); String reportTrainFile = tokenizer.nextToken(); reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1); String reportTestFile = tokenizer.nextToken(); reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1); String reportRulesFile = tokenizer.nextToken(); reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length() - 1); // Algorithm auxiliar configuration XMLConfiguration algConf = new XMLConfiguration(); algConf.setRootElementName("experiment"); algConf.addProperty("process[@algorithm-type]", "net.sourceforge.jclec.problem.classification.freitas.FreitasAlgorithm"); algConf.addProperty("process.rand-gen-factory[@type]", "net.sourceforge.jclec.util.random.RanecuFactory"); algConf.addProperty("process.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed"))); algConf.addProperty("process.population-size", Integer.parseInt(props.getProperty("population-size"))); algConf.addProperty("process.max-of-generations", Integer.parseInt(props.getProperty("max-generations"))); algConf.addProperty("process.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size"))); algConf.addProperty("process.dataset[@type]", "net.sourceforge.jclec.util.dataset.KeelDataSet"); algConf.addProperty("process.dataset.train-data.file-name", trainFile); algConf.addProperty("process.dataset.test-data.file-name", testFile); algConf.addProperty("process.species[@type]", "net.sourceforge.jclec.problem.classification.freitas.FreitasSyntaxTreeSpecies"); algConf.addProperty("process.evaluator[@type]", "net.sourceforge.jclec.problem.classification.freitas.FreitasEvaluator"); algConf.addProperty("process.provider[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeCreator"); algConf.addProperty("process.parents-selector[@type]", "net.sourceforge.jclec.selector.RouletteSelector"); algConf.addProperty("process.recombinator[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeRecombinator"); algConf.addProperty("process.recombinator[@rec-prob]", Double.parseDouble(props.getProperty("rec-prob"))); algConf.addProperty("process.recombinator.base-op[@type]", "net.sourceforge.jclec.problem.classification.freitas.FreitasCrossover"); algConf.addProperty("process.copy-prob", Double.parseDouble(props.getProperty("copy-prob"))); algConf.addProperty("process.listener[@type]", "net.sourceforge.jclec.problem.classification.freitas.KeelFreitasPopulationReport"); algConf.addProperty("process.listener.report-dir-name", "./"); algConf.addProperty("process.listener.train-report-file", reportTrainFile); algConf.addProperty("process.listener.test-report-file", reportTestFile); algConf.addProperty("process.listener.rules-report-file", reportRulesFile); algConf.addProperty("process.listener.global-report-name", "resumen"); algConf.addProperty("process.listener.report-frequency", 50); try { algConf.save(new File("configure.txt")); } catch (ConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } net.sourceforge.jclec.RunExperiment.main(new String[] { "configure.txt" }); }