List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
From source file:csns.importer.parser.csula.RosterParserImpl.java
/** * This parser handles the format under CSULA Baseline -> CSULA Student * Records -> Class Roster on GET. A sample record is as follows: * "1 123456789 Doe,John M 3.00 ETG CS MS G1". Note that some fields like * middle name and units may not be present, and some people's last name has * space in it.//from w w w.ja v a 2s.c o m */ private List<ImportedUser> parse1(String text) { List<ImportedUser> students = new ArrayList<ImportedUser>(); Scanner scanner = new Scanner(text); scanner.useDelimiter("\\s+|\\r\\n|\\r|\\n"); while (scanner.hasNext()) { String cin = scanner.next(); if (!isCin(cin)) continue; String name = ""; boolean nameFound = false; while (scanner.hasNext()) { String token = scanner.next(); name += token + " "; if (token.matches(".+,.*")) { if (token.endsWith(",") && scanner.hasNext()) name += scanner.next(); nameFound = true; break; } } String grade = null; boolean gradeFound = false; boolean unitsFound = false; while (nameFound && scanner.hasNext()) { String token = scanner.next(); if (isUnits(token)) { unitsFound = true; continue; } if (isGrade(token)) { if (unitsFound) // this must be a grade { grade = token; gradeFound = true; break; } else // this could be a grade or a middle name { grade = token; continue; } } if (isProgram(token)) { if (grade != null) gradeFound = true; break; } name += token + " "; } if (nameFound) { ImportedUser student = new ImportedUser(); student.setCin(cin); student.setName(name); if (gradeFound) student.setGrade(grade); students.add(student); } } scanner.close(); return students; }
From source file:net.ftb.util.DownloadUtils.java
/** * Checks the file for corruption.// w ww. j a v a2 s.co m * @param file - File to check * @param url - base url to grab md5 with old method * @return boolean representing if it is valid * @throws IOException */ public static boolean backupIsValid(File file, String url) throws IOException { Logger.logInfo("Issue with new md5 method, attempting to use backup method."); String content = null; Scanner scanner = null; String resolved = (downloadServers.containsKey(Settings.getSettings().getDownloadServer())) ? "http://" + downloadServers.get(Settings.getSettings().getDownloadServer()) : Locations.masterRepo; resolved += "/md5/FTB2/" + url; HttpURLConnection connection = null; try { connection = (HttpURLConnection) new URL(resolved).openConnection(); connection.setRequestProperty("Cache-Control", "no-transform"); int response = connection.getResponseCode(); if (response == 200) { scanner = new Scanner(connection.getInputStream()); scanner.useDelimiter("\\Z"); content = scanner.next(); } if (response != 200 || (content == null || content.isEmpty())) { for (String server : backupServers.values()) { resolved = "http://" + server + "/md5/FTB2/" + url.replace("/", "%5E"); connection = (HttpURLConnection) new URL(resolved).openConnection(); connection.setRequestProperty("Cache-Control", "no-transform"); response = connection.getResponseCode(); if (response == 200) { scanner = new Scanner(connection.getInputStream()); scanner.useDelimiter("\\Z"); content = scanner.next(); if (content != null && !content.isEmpty()) { break; } } } } } catch (IOException e) { } finally { connection.disconnect(); if (scanner != null) { scanner.close(); } } String result = fileMD5(file); Logger.logInfo("Local: " + result.toUpperCase()); Logger.logInfo("Remote: " + content.toUpperCase()); return content.equalsIgnoreCase(result); }
From source file:com.pushinginertia.commons.net.client.AbstractHttpPostClient.java
/** * Retrieves the response message from the remote host. * * @param con instantiated connection/* ww w . j av a2s.c o m*/ * @param encoding encoding to use in the response * @return response message from the remote host or null if none exists * @throws HttpConnectException if there is a problem retrieving the message */ protected String getResponseMessage(final HttpURLConnection con, final String encoding) throws HttpConnectException { try { final InputStream is = con.getInputStream(); final Scanner s = new Scanner(is, encoding); s.useDelimiter("\\A"); // \A is the beginning of input if (s.hasNext()) return s.next(); return null; } catch (NoSuchElementException e) { // no input return null; } catch (Exception e) { final String msg = "An unexpected error occurred while trying to retrieve the response message from [" + getUrl() + "]: " + e.getMessage(); LOG.error(getClass().getSimpleName(), msg, e); throw new HttpConnectException(msg, e); } }
From source file:it.acubelab.batframework.systemPlugins.TagmeAnnotator.java
@Override public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException { // System.out.println(text.length()+ " "+text.substring(0, // Math.min(text.length(), 15))); // TODO: workaround for a bug in tagme. should be deleted afterwards. String newText = ""; for (int i = 0; i < text.length(); i++) newText += (text.charAt(i) > 127 ? ' ' : text.charAt(i)); text = newText;// w w w. j av a 2 s. c om // avoid crashes for empty documents int j = 0; while (j < text.length() && text.charAt(j) == ' ') j++; if (j == text.length()) return new HashSet<ScoredAnnotation>(); HashSet<ScoredAnnotation> res; String params = null; try { res = new HashSet<ScoredAnnotation>(); params = "key=" + this.key; params += "&lang=en"; if (epsilon >= 0) params += "&epsilon=" + epsilon; if (minComm >= 0) params += "&min_comm=" + minComm; if (minLink >= 0) params += "&min_link=" + minLink; params += "&text=" + URLEncoder.encode(text, "UTF-8"); URL wikiApi = new URL(url); HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection(); slConnection.setRequestProperty("accept", "text/xml"); slConnection.setDoOutput(true); slConnection.setDoInput(true); slConnection.setRequestMethod("POST"); slConnection.setRequestProperty("charset", "utf-8"); slConnection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length)); slConnection.setUseCaches(false); slConnection.setReadTimeout(0); DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream()); wr.writeBytes(params); wr.flush(); wr.close(); Scanner s = new Scanner(slConnection.getInputStream()); Scanner s2 = s.useDelimiter("\\A"); String resultStr = s2.hasNext() ? s2.next() : ""; s.close(); JSONObject obj = (JSONObject) JSONValue.parse(resultStr); lastTime = (Long) obj.get("time"); JSONArray jsAnnotations = (JSONArray) obj.get("annotations"); for (Object js_ann_obj : jsAnnotations) { JSONObject js_ann = (JSONObject) js_ann_obj; System.out.println(js_ann); int start = ((Long) js_ann.get("start")).intValue(); int end = ((Long) js_ann.get("end")).intValue(); int id = ((Long) js_ann.get("id")).intValue(); float rho = Float.parseFloat((String) js_ann.get("rho")); System.out.println(text.substring(start, end) + "->" + id + " (" + rho + ")"); res.add(new ScoredAnnotation(start, end - start, id, (float) rho)); } } catch (Exception e) { e.printStackTrace(); throw new AnnotationException("An error occurred while querying TagMe API. Message: " + e.getMessage() + " Parameters:" + params); } return res; }
From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProviderTest.java
protected boolean checkTestFileExists(String filename) { boolean ok = false; try {// w ww . j a v a 2 s.c o m File f = s3FileProvider.getResource(filename); if (f.exists()) { Scanner fileScanner = new Scanner(f); fileScanner.useDelimiter("\\Z"); String content = fileScanner.next(); int contentLength = content.length(); if (contentLength > 10) { System.out.println("Returned file contents: " + content); ok = TEST_FILE_CONTENTS.equals(content); } fileScanner.close(); } } catch (Exception e) { ok = false; } return ok; }
From source file:org.unitedinternet.cosmo.db.DbInitializer.java
private List<String> readStatements(String resource) { List<String> statements = new ArrayList<>(); Scanner scanner = null; try {/*w ww.j a va2 s .co m*/ scanner = new Scanner(this.getClass().getResourceAsStream(resource), StandardCharsets.UTF_8.name()); scanner.useDelimiter(";"); while (scanner.hasNext()) { String statement = scanner.next().replace("\n", " ").trim(); if (!statement.isEmpty()) { statements.add(statement); } } } finally { if (scanner != null) { scanner.close(); } } return statements; }
From source file:eremeykin.pete.plotter.PlotterTopComponent.java
public void update(File home) { // for first rpt file if (model == null) { clear();//from w w w . ja v a2 s . co m return; } File[] rptFiles = home.listFiles(filter()); // catch if there is no such file if (rptFiles.length == 0) { clear(); return; } File firstRPT = rptFiles[0]; Scanner scanner; try { scanner = new Scanner(firstRPT); scanner.useDelimiter("\\s+|\n"); } catch (FileNotFoundException ex) { clear(); return; } List<Map.Entry<Double, Double>> tmpList = new ArrayList<>(); for (int i = 0; scanner.hasNext(); i++) { String line = scanner.next(); try { double x1 = Double.valueOf(line); line = scanner.next(); double x2 = Double.valueOf(line); // System.out.println("x1=" + x1 + "\nx2=" + x2); tmpList.add(new AbstractMap.SimpleEntry<>(x1, x2)); } catch (NumberFormatException ex) { // only if it is the third or following line if (i > 1) { LOGGER.error("Error while parsing double from file: " + firstRPT.getAbsolutePath()); JOptionPane.showMessageDialog(this, "Error while parsing result file.", "Parsing error", JOptionPane.ERROR_MESSAGE); } } } if (tmpList.isEmpty()) { clear(); return; } fillData(tmpList, dataSeries, toleranceSeries); }
From source file:org.grouter.core.readers.FtpReaderJob.java
/** * Parses comma separated string of paths. * * @param pathIncludingFiles a list with comma separated strings, e.g /tmp/kalle.xml,nisse.xml * @return paths/*from ww w .j a va 2 s . c om*/ */ private List<String> getPathIncludingFile(String pathIncludingFiles) { List<String> paths = new ArrayList<String>(); Scanner scanner = new Scanner(pathIncludingFiles); try { scanner.useDelimiter(","); while (scanner.hasNext()) { String pathInclFile = scanner.next(); logger.debug(pathInclFile); paths.add(pathInclFile); } } catch (Exception e) { logger.warn(e, e); } finally { scanner.close(); } return paths; }
From source file:eu.project.ttc.resources.GeneralLanguageResource.java
public void load(InputStream inputStream) throws ResourceInitializationException { words = Sets.newHashSet();/*from ww w . ja v a2 s.c o m*/ Scanner scanner = null; try { scanner = new Scanner(inputStream, "UTF-8"); scanner.useDelimiter("\n"); int index = 0; while (scanner.hasNext()) { index++; String line = scanner.next(); String[] items = line.split("::"); if (items.length == 3) { String key = items[0].trim(); if (!key.contains(" ")) this.words.add(key); Integer value = Integer.valueOf(items[2].trim()); this.cumulatedFrequency += value.intValue(); String lemma = key; this.frequencies.put(lemma, new Entry(lemma, items[1], new Integer(value.intValue()))); } else { throw new IOException("Wrong general language format at line " + index + ": " + line); } } this.words = ImmutableSet.copyOf(this.words); if (this.frequencies.containsKey(PARAM_NB_CORPUS_WORDS)) this.nbCorpusWords = this.frequencies.get(PARAM_NB_CORPUS_WORDS).iterator().next().getFrequency(); else { LOGGER.warn("No such key for in GeneralLanguage resource {}", PARAM_NB_CORPUS_WORDS); LOGGER.warn("Switch to cumulatedFrequency mode"); this.cumulatedFrequencyMode = true; } } catch (Exception e) { throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(scanner); } }
From source file:eu.project.ttc.resources.PrefixTree.java
@Override public void load(DataResource data) throws ResourceInitializationException { InputStream inputStream = null; try {/* w w w. ja v a 2 s. c o m*/ inputStream = data.getInputStream(); Scanner scanner = null; try { String line; scanner = new Scanner(inputStream, "UTF-8"); scanner.useDelimiter(TermSuiteConstants.LINE_BREAK); while (scanner.hasNext()) { line = scanner.next().split(TermSuiteConstants.DIESE)[0].trim(); if (line.startsWith("#")) continue; else if (line.isEmpty()) continue; else if (line.endsWith("-")) { if (line.length() == 1) continue; else line = line.substring(0, line.length() - 1); } rootNode.indexString(toCharQueue(line)); } } catch (Exception e) { e.printStackTrace(); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(scanner); } } catch (IOException e) { LOGGER.error("Could not load file {}", data.getUrl()); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(inputStream); } }