List of usage examples for org.apache.commons.io LineIterator hasNext
public boolean hasNext()
Reader
has more lines. From source file:tpt.dbweb.cat.io.ConllWriter.java
/** * Reads a conll file./* w ww .ja v a 2 s. c om*/ * @param file * @return map of document ids to a table (list of rows; a row is a list of columns) */ public static Map<String, List<List<String>>> readConllDocuments(Path file) { Map<String, List<List<String>>> result = new HashMap<>(); List<List<String>> docList = new ArrayList<>(); String docid = null; LineIterator it; try { it = FileUtils.lineIterator(file.toFile()); while (it.hasNext()) { String line = it.next(); if (line.startsWith("#")) { if (line.startsWith("#begin document")) { docList = new ArrayList<>(); docid = line.substring("#begin document".length() + 1); } else if (line.startsWith("#end document")) { if (docid == null) { log.error("doc id is null, cannot read conll file {}", file); } else { result.put(docid, docList); } docid = null; } continue; } String[] cols = line.split("\\s+"); if (cols.length == 0 || (cols.length == 1 && "".equals(cols[0]))) { docList.add(Arrays.asList()); } else { docList.add(Arrays.asList(cols)); } // extract column for words // align output line by line to these words // TODO: move comment to right place } } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:uniol.apt.compiler.AbstractServiceProcessor.java
/** * Function that writes a resource list into the class output directory. * Existing entries are preserved. This means that this function only ever adds new entries. * @param resourceName Name of the file in which the resource list should be saved. * @param entries Entries that should be added to the list. * @throws IOException In case I/O errors occur. *///from w w w .ja va 2 s . com protected void writeResourceList(String resourceName, Collection<String> entries) throws IOException { entries = new TreeSet<>(entries); // read already listed services try { FileObject fo = this.filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName); try (InputStream is = fo.openInputStream()) { LineIterator lIter = IOUtils.lineIterator(is, "UTF-8"); while (lIter.hasNext()) { String entry = lIter.next(); entries.add(entry); } } } catch (IOException ex) { /* It's ok if the resource can't get found; we only skip reading it */ } // write new list FileObject fo = this.filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceName); Writer writer = fo.openWriter(); for (String entry : entries) { writer.append(entry + "\n"); } writer.close(); }
From source file:uniol.apt.io.parser.AbstractParsers.java
/** * Constructor/*from ww w . ja va 2s . c o m*/ * * @param clazz Class object of the which the parsers should generate */ @SuppressWarnings("unchecked") // I hate type erasure and other java things ... protected AbstractParsers(Class<T> clazz) { this.parsers = new HashMap<>(); String className = clazz.getCanonicalName(); ClassLoader cl = getClass().getClassLoader(); try { Enumeration<URL> parserNames = cl.getResources( "META-INF/uniol/apt/compiler/" + Parser.class.getCanonicalName() + "/" + className); while (parserNames.hasMoreElements()) { try (InputStream is = parserNames.nextElement().openStream()) { LineIterator lIter = IOUtils.lineIterator(is, "UTF-8"); while (lIter.hasNext()) { String parserName = lIter.next(); Class<? extends Parser<T>> parserClass; try { parserClass = (Class<? extends Parser<T>>) cl.loadClass(parserName); } catch (ClassNotFoundException ex) { throw new RuntimeException(String.format("Could not load class %s", parserName), ex); } Parser<T> parser; try { parser = parserClass.newInstance(); } catch (ClassCastException | IllegalAccessException | InstantiationException ex) { throw new RuntimeException(String.format("Could not instantiate %s", parserName), ex); } String format = parser.getFormat(); if (format == null || format.equals("") || !format.equals(format.toLowerCase())) { throw new RuntimeException( String.format("Parser %s reports an invalid format: %s", parserName, format)); } Parser<T> oldParser = this.parsers.get(format); if (oldParser != null && !oldParser.getClass().equals(parserClass)) { throw new RuntimeException( String.format("Different parsers claim, to interpret format %s:" + " %s and %s", format, oldParser.getClass().getCanonicalName(), parserName)); } this.parsers.put(format, parser); } } } } catch (IOException ex) { throw new RuntimeException("Failed to discover parsers", ex); } }
From source file:uniol.apt.io.renderer.AbstractRenderers.java
/** * Constructor// ww w . j a v a2s .co m * * @param clazz Class object of the which the renderers should render */ @SuppressWarnings("unchecked") // I hate type erasure and other java things ... protected AbstractRenderers(Class<T> clazz) { this.renderers = new HashMap<>(); String className = clazz.getCanonicalName(); ClassLoader cl = getClass().getClassLoader(); try { Enumeration<URL> rendererNames = cl.getResources( "META-INF/uniol/apt/compiler/" + Renderer.class.getCanonicalName() + "/" + className); while (rendererNames.hasMoreElements()) { try (InputStream is = rendererNames.nextElement().openStream()) { LineIterator lIter = IOUtils.lineIterator(is, "UTF-8"); while (lIter.hasNext()) { String rendererName = lIter.next(); Class<? extends Renderer<T>> rendererClass; try { rendererClass = (Class<? extends Renderer<T>>) cl.loadClass(rendererName); } catch (ClassNotFoundException ex) { throw new RuntimeException(String.format("Could not load class %s", rendererName), ex); } Renderer<T> renderer; try { renderer = rendererClass.newInstance(); } catch (ClassCastException | IllegalAccessException | InstantiationException ex) { throw new RuntimeException(String.format("Could not instantiate %s", rendererName), ex); } String format = renderer.getFormat(); if (format == null || format.equals("") || !format.equals(format.toLowerCase())) { throw new RuntimeException(String.format("Renderer %s reports an invalid format: %s", rendererName, format)); } Renderer<T> oldRenderer = this.renderers.get(format); if (oldRenderer != null && !oldRenderer.getClass().equals(rendererClass)) { throw new RuntimeException(String.format( "Different renderers claim, to interpret format %s:" + " %s and %s", format, oldRenderer.getClass().getCanonicalName(), rendererName)); } this.renderers.put(format, renderer); } } } } catch (IOException ex) { throw new RuntimeException("Failed to discover renderers", ex); } }
From source file:voldemort.utils.app.VoldemortApp.java
protected List<HostNamePair> getHostNamesPairsFromFile(File file) { List<HostNamePair> hostNamePairs = new ArrayList<HostNamePair>(); // This was recently changed from using Properties to performing the // file parsing manually. This is due to the fact that we want to // maintain the ordering in the file. Line N in the host names file // should correspond to node ID N-1. Previously they were in hashed // order, so certain tools that auto-configure a cluster based on // this same hosts file were creating invalid configurations between // the cluster.xml and server.properties (node.id) files. LineIterator li = null; int lineNumber = 0; try {/*w ww. j av a2 s. c o m*/ li = FileUtils.lineIterator(file); while (li.hasNext()) { String rawLine = String.valueOf(li.next()).trim(); lineNumber++; // Strip comments int hashIndex = rawLine.indexOf("#"); if (hashIndex != -1) rawLine = rawLine.substring(0, hashIndex).trim(); // Whitespace if (rawLine.length() == 0) continue; String[] line = StringUtils.split(rawLine, " \t=:"); if (line.length < 1 || line.length > 2) { System.err.println("Invalid entry (line " + lineNumber + ") in " + file.getAbsolutePath() + ": " + rawLine); System.exit(2); } String externalHostName = line[0]; String internalHostName = line.length > 1 ? line[1] : externalHostName; hostNamePairs.add(new HostNamePair(externalHostName, internalHostName)); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (li != null) li.close(); } return hostNamePairs; }
From source file:yet.another.hackernews.reader.functions.DownloadText.java
@Override protected String doInBackground(String... urlAdress) { /*//from ww w.ja v a 2 s .c om * Core of the class that does the background task. */ id = urlAdress[0].hashCode(); // If we want to use the cache, try and get it first String cacheResults = null; if (useCache) { cacheResults = HardCache.getString(id, context); if (cacheResults != null) { if (!forceUpdate && !Cache.doUpdate(context, id)) { return cacheResults; } } } // Set target url URL url; try { url = new URL(urlAdress[0]); } catch (MalformedURLException e) { // If listener is attached, report error. if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(MALFORMED_URL_ERROR); } }); } return cacheResults; } // Stream to URL adress BufferedReader bufferedReader = null; LineIterator line = null; // Results, size set to 100 chars final StringBuffer textResults = new StringBuffer(0); try { // Open stream bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), charset), bufferSize); // Download text line = IOUtils.lineIterator(bufferedReader); while (line.hasNext()) { if (this.isCancelled()) { break; } textResults.append(line.nextLine()); } } catch (IOException e) { if (textResults.length() > 0) { textResults.delete(0, textResults.length()); } if (cacheResults != null) { textResults.append(cacheResults); } if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(IOEXCEPTION_ERROR); } }); } } finally { // Close reader LineIterator.closeQuietly(line); IOUtils.closeQuietly(bufferedReader); } if (textResults.length() <= 0) { return null; } // If use cache, put it in cache if (useCache && (!textResults.toString().equals(cacheResults))) { HardCache.putString(id, textResults.toString(), context); Cache.saveUpdate(context, id); } return textResults.toString(); }