List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:com.theminequest.MineQuest.SQLExecutor.java
/** * Check for initialization. What this does is take the build number * and uses a fast forward mechanism from the previous build to see * if any changes should be merged.<br> * It acts similarly to SVN.//from w w w. j ava2 s . c om */ private void checkInitialization() { if (!datafolder.exists()) { datafolder.mkdir(); } File versionfile = new File(datafolder + File.separator + "version"); Scanner s = null; String lastv = null; try { s = new Scanner(versionfile); if (s.hasNextLine()) { String dbtype = s.nextLine(); if (dbtype.equalsIgnoreCase(databasetype.name())) { if (s.hasNextLine()) lastv = s.nextLine(); } } } catch (FileNotFoundException e) { try { versionfile.createNewFile(); } catch (IOException e1) { throw new RuntimeException(e1); } } if (lastv == null || lastv.compareTo(MineQuest.getVersion()) != 0) { if (lastv == null || lastv.equals("unofficialDev")) { if (lastv == null) MineQuest.log(Level.WARNING, "[SQL] No existing DBVERSION file; initializing DB as new."); else MineQuest.log(Level.WARNING, "[SQL] I don't know what your previous build was; attempting to reinitialize."); lastv = "initial"; } if (lastv != null && !lastv.equals("unofficialDev") && !lastv.equals("initial")) { int last = Integer.parseInt(lastv); MineQuest.log(Level.INFO, "[SQL] Fast forwarding through builds..."); while (last < Integer.parseInt(MineQuest.getVersion())) { try { MineQuest.log("[SQL] Fast forwarding from build " + last + " to " + (last + 1) + "..."); querySQL("update/" + last, ""); MineQuest.log("[SQL] Applied patch for build " + last + " to " + (last + 1) + "!"); } catch (NoSuchElementException e) { //MineQuest.log(Level.WARNING,"[SQL] No update path from build " + last + " to " + (last+1) + " build; Probably normal."); } last++; } } else { try { querySQL("update/" + lastv, ""); } catch (NoSuchElementException e) { MineQuest.log(Level.WARNING, "[SQL] No update path from build " + lastv + " to this build; Probably normal."); } } Writer out; try { out = new OutputStreamWriter(new FileOutputStream(versionfile)); out.write(databasetype.name() + IOUtils.LINE_SEPARATOR + MineQuest.getVersion()); out.close(); } catch (FileNotFoundException e) { MineQuest.log(Level.SEVERE, "[SQL] Failed to commit DBVERSION: " + e.getMessage()); throw new RuntimeException(e); } catch (IOException e) { MineQuest.log(Level.SEVERE, "[SQL] Failed to commit DBVERSION: " + e.getMessage()); throw new RuntimeException(e); } } }
From source file:org.pathwaycommons.pcviz.service.PathwayCommonsGraphService.java
@Cacheable("metadataCache") public String getMetadata(String datatype) { String urlStr = getPathwayCommonsUrl() + "/metadata/" + datatype; try {//w w w. ja va 2 s . c om URL url = new URL(urlStr); URLConnection urlConnection = url.openConnection(); StringBuilder builder = new StringBuilder(); Scanner scanner = new Scanner(urlConnection.getInputStream()); while (scanner.hasNextLine()) { builder.append(scanner.nextLine() + "\n"); } scanner.close(); return builder.toString(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:org.goko.core.gcode.rs274ngcv3.parser.GCodeLexer.java
/** * Create a list of token from an InputStream * @param inStream the input stream/*from ww w .ja v a2 s. co m*/ * @return a list of {@link GCodeToken} * @throws GkException GkException */ public List<List<GCodeToken>> tokenize(InputStream inStream, IValidationTarget validationTarget) throws GkException { Scanner scanner = new Scanner(inStream); List<List<GCodeToken>> lstFileTokens = new ArrayList<List<GCodeToken>>(); String line = null; List<GCodeToken> tokens = null; int lineNumber = 0; while (scanner.hasNextLine()) { line = scanner.nextLine(); tokens = tokenize(line, validationTarget, lineNumber); lineNumber++; lstFileTokens.add(tokens); } scanner.close(); return lstFileTokens; }
From source file:com.joliciel.jochre.search.JochreQueryImpl.java
public JochreQueryImpl(Map<String, String> argMap) { try {// w w w .j av a 2 s. c om for (Entry<String, String> argEntry : argMap.entrySet()) { String argName = argEntry.getKey(); String argValue = argEntry.getValue(); if (argName.equalsIgnoreCase("query")) { this.setQueryString(argValue); } else if (argName.equalsIgnoreCase("queryFile")) { File queryFile = new File(argValue); Scanner scanner = new Scanner( new BufferedReader(new InputStreamReader(new FileInputStream(queryFile), "UTF-8"))); while (scanner.hasNextLine()) { String line = scanner.nextLine(); this.setQueryString(line); break; } scanner.close(); } else if (argName.equalsIgnoreCase("maxDocs")) { int maxDocs = Integer.parseInt(argValue); this.setMaxDocs(maxDocs); } else if (argName.equalsIgnoreCase("decimalPlaces")) { int decimalPlaces = Integer.parseInt(argValue); this.setDecimalPlaces(decimalPlaces); } else if (argName.equalsIgnoreCase("lang")) { this.setLanguage(argValue); } else if (argName.equalsIgnoreCase("filter")) { if (argValue.length() > 0) { String[] idArray = argValue.split(","); this.setDocFilter(idArray); } } else if (argName.equalsIgnoreCase("filterField")) { if (argValue.length() > 0) this.setFilterField(argValue); } else { LOG.trace("JochreQuery unknown option: " + argName); } } } catch (IOException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:org.kuali.mobility.knowledgebase.service.KnowledgeBaseServiceImpl.java
private String inputStreamToString(InputStream in) { StringBuffer sb = new StringBuffer(); try {//www .java2 s. c om Scanner scanner = new Scanner(in); try { while (scanner.hasNextLine()) { sb.append(scanner.nextLine() + "\r\n"); } } finally { scanner.close(); } } catch (Exception e) { // LOG.error("Error retrieving XSL: ", e); } return sb.toString(); }
From source file:io.mindmaps.graql.GraqlShell.java
private void printLicense() { StringBuilder result = new StringBuilder(""); //Get file from resources folder ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream(LICENSE_LOCATION); Scanner scanner = new Scanner(is); while (scanner.hasNextLine()) { String line = scanner.nextLine(); result.append(line).append("\n"); }/*from w ww .ja va 2 s . co m*/ result.append("\n"); scanner.close(); this.print(result.toString()); }
From source file:com.joliciel.talismane.machineLearning.AbstractMachineLearningModel.java
public boolean loadZipEntry(ZipInputStream zis, ZipEntry ze) throws IOException { boolean loaded = true; if (ze.getName().equals("model.bin")) { this.loadModelFromStream(zis); } else if (ze.getName().equals("externalResources.obj")) { ObjectInputStream in = new ObjectInputStream(zis); try {/* w w w . j a v a2 s. com*/ @SuppressWarnings("unchecked") List<ExternalResource<?>> externalResources = (List<ExternalResource<?>>) in.readObject(); this.setExternalResources(externalResources); } catch (ClassNotFoundException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } } else if (ze.getName().endsWith("_descriptors.txt")) { String key = ze.getName().substring(0, ze.getName().length() - "_descriptors.txt".length()); Scanner scanner = new Scanner(zis, "UTF-8"); List<String> descriptorList = new ArrayList<String>(); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); descriptorList.add(descriptor); } this.getDescriptors().put(key, descriptorList); } else if (ze.getName().endsWith("_dependency.obj")) { String key = ze.getName().substring(0, ze.getName().length() - "_dependency.obj".length()); ObjectInputStream in = new ObjectInputStream(zis); try { Object dependency = in.readObject(); this.dependencies.put(key, dependency); } catch (ClassNotFoundException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } } else if (ze.getName().equals("attributes.txt")) { Scanner scanner = new Scanner(zis, "UTF-8"); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.length() > 0) { String[] parts = line.split("\t"); String name = parts[0]; String value = ""; if (parts.length > 1) value = parts[1]; this.addModelAttribute(name, value); } } } else { loaded = this.loadDataFromStream(zis, ze); } return loaded; }
From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java
/** * Cleans the received fileName according to the rules defined in filename_replace_expressions.txt * @param fileName the name to clean/*ww w . j av a 2s . c o m*/ * @return the cleaned name */ private static String getCleanName(String fileName) { File configFile = new File(FilenameUtils.concat(PMS.getConfiguration().getProfileDirectory(), "filename_replace_expressions.txt")); String res = fileName; Scanner scanner = null; try { scanner = new Scanner(new FileInputStream(configFile.getAbsolutePath()), "UTF-8"); String regex; String replaceText; while (scanner.hasNextLine()) { regex = scanner.nextLine(); if (scanner.hasNextLine()) { replaceText = scanner.nextLine(); } else { break; } res = res.replaceAll(regex, replaceText); } } catch (FileNotFoundException e) { if (log.isDebugEnabled()) log.debug(String.format("File '%s' not found. Ignore file name cleaning", configFile.getAbsolutePath())); } finally { if (scanner != null) { scanner.close(); } } res = res.trim(); if (!res.equals(fileName)) { if (log.isInfoEnabled()) log.info(String.format("Cleaned up file '%s' to '%s'", fileName, res)); } return res; }
From source file:io.proleap.cobol.preprocessor.sub.document.impl.CobolDocumentParserListenerImpl.java
protected String buildLines(final String text, final String linePrefix) { final StringBuffer sb = new StringBuffer(text.length()); final Scanner scanner = new Scanner(text); boolean firstLine = true; while (scanner.hasNextLine()) { final String line = scanner.nextLine(); if (!firstLine) { sb.append(CobolPreprocessor.NEWLINE); }/* w ww . ja v a 2 s. c o m*/ sb.append(linePrefix + CobolPreprocessor.WS + line.trim()); firstLine = false; } scanner.close(); return sb.toString(); }
From source file:eu.creatingfuture.propeller.blocklyprop.propellent.Propellent.java
public List<String> getPorts() { List<String> ports = new ArrayList<>(); try {/*from w w w . j a va 2 s . co m*/ CommandLine cmdLine = new CommandLine("propellent/Propellent.exe"); cmdLine.addArgument("/id"); cmdLine.addArgument("/gui").addArgument("OFF"); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(new int[] { 451, 301 }); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); try { exitValue = executor.execute(cmdLine); } catch (ExecuteException ee) { // exitValue = ee.getExitValue(); logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue); return ports; } output = outputStream.toString(); // 301 = None found // 451 = Chip found if (exitValue == 301) { return ports; } // System.out.println("output: " + output); Scanner scanner = new Scanner(output); Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?"); Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$"); while (scanner.hasNextLine()) { String portLine = scanner.nextLine(); if (chipFoundPattern.matcher(portLine).matches()) { Matcher portMatch = pattern.matcher(portLine); if (portMatch.find()) { String port = portMatch.group("comport"); ports.add(port); } } } // System.out.println("output: " + output); // System.out.println("exitValue: " + exitValue); return ports; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); return null; } }