List of usage examples for org.apache.commons.io LineIterator hasNext
public boolean hasNext()
Reader
has more lines. From source file:de.tudarmstadt.lt.seg.sentence.SentenceSplitterTest.java
@Test public void ruleSplitterLineTest() { ISentenceSplitter sentenceSplitter = new RuleSplitter(); ITokenizer tokenizer = new EmptySpaceTokenizer(); StringWriter s = new StringWriter(); PrintWriter w = new PrintWriter(s); LineIterator liter = new LineIterator(new BufferedReader(new StringReader(TEST_TEXT))); for (long lc = 0; liter.hasNext();) { if (++lc % 1000 == 0) System.err.format("Processing line %d %n", lc); Segmenter.split_and_tokenize(new StringReader(liter.next()), String.format("%s:%d", "TEST_TEXT", lc), sentenceSplitter, tokenizer, 2, 0, false, false, "\n", "\n", "\n", w); }/*from www. j av a 2s. c om*/ System.out.println(s.toString()); }
From source file:hudson.plugins.clearcase.base.BaseHistoryAction.java
protected List<HistoryEntry> parseUpdt(FilePath updtFile, String viewPath) throws IOException, InterruptedException { Validate.notNull(updtFile);// w w w. j av a 2 s. c o m List<HistoryEntry> history = new ArrayList<HistoryEntry>(); List<UpdtEntry> updtEntries = new ArrayList<UpdtEntry>(); InputStream is = updtFile.read(); try { LineIterator it = IOUtils.lineIterator(is, "UTF-8"); while (it.hasNext()) { String line = it.nextLine(); UpdtEntry entry = UpdtEntry.getEntryFromLine(line); if (entry.getState() == UpdtEntry.State.NEW || entry.getState() == UpdtEntry.State.UPDATED) { updtEntries.add(entry); } } } finally { IOUtils.closeQuietly(is); } for (UpdtEntry entry : updtEntries) { try { BufferedReader reader = new BufferedReader( cleartool.describe(getLsHistoryFormat(), viewPath, entry.getObjectSelectorNewVersion())); try { parseLsHistory(reader, history); } catch (ParseException e) { // no op } reader.close(); } catch (IOException e) { // skip describe errors } } return history; }
From source file:eu.eexcess.domaindetection.wordnet.WordnetDomainsReader.java
/** * @param wordNetFile//from ww w . j ava2 s . c om * @throws FileNotFoundException */ public void readDefinition(File wordNetFile) throws FileNotFoundException { File file = new File(wordNetFile.getParentFile(), "wn-domains-3.2-tree.csv"); LineIterator iterator = new LineIterator(new FileReader(file)); String[] currentParents = new String[4]; while (iterator.hasNext()) { String line = iterator.nextLine(); String[] tokens = line.split("[,]"); int depth = -1; for (int i = 0; i < tokens.length; i++) { if (!tokens[i].trim().isEmpty()) { depth = i; break; } } String domain = tokens[depth].trim().toLowerCase(Locale.US); if (depth >= 0) { Map<String, Double> parentToWeight = domainToParentDomainToWeight.get(domain); if (parentToWeight == null) { parentToWeight = new LinkedHashMap<String, Double>(); domainToParentDomainToWeight.put(domain, parentToWeight); } for (int i = 0; i < depth; i++) { double weight = 1.0 / ((depth - i + 1) * (depth - i + 1)); String parent = currentParents[i]; parentToWeight.put(parent, weight); } currentParents[depth] = domain; for (int i = depth + 1; i < 4; i++) { currentParents[i] = null; } } else { domainToParentDomainToWeight.put(domain, null); } } iterator.close(); }
From source file:com.textocat.textokit.dictmatcher.TaggedChunkerBuilderResource.java
@Override public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams) throws ResourceInitializationException { if (!super.initialize(aSpecifier, aAdditionalParams)) return false; ////w w w . j a v a 2 s. c o m ChunkerBuilder<String> builder; try { //noinspection unchecked builder = chunkerBuilderClass.newInstance(); } catch (Exception e) { throw new ResourceInitializationException(e); } // try (InputStream in = resourceMeta.getInputStream()) { LineIterator lineIterator = IOUtils.lineIterator(in, "UTF-8"); int lineNum = 0; while (lineIterator.hasNext()) { lineNum++; DictEntry de = parseLine(lineIterator.nextLine(), lineNum); if (de != null) builder.addEntry(de.tokens, de.tag); } } catch (IOException e) { throw new ResourceInitializationException(e); } chunker = builder.build(); return true; }
From source file:com.icantrap.collections.dawg.DawgValidationTest.java
@Test public void containsAllWords() throws IOException { LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null); StopWatch stopWatch = new StopWatch(); stopWatch.start();//w w w. j a va 2 s . c o m while (iter.hasNext()) { String word = iter.next(); assertTrue("Missing word (" + word + ")", dawg.contains(word)); } stopWatch.stop(); System.out.println("Time to query: " + stopWatch.getTime() + " ms."); LineIterator.closeQuietly(iter); }
From source file:fr.treeptik.cloudunit.cli.exception.CustomResponseErrorHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else {//from www .j av a 2 s .c om switch (status) { case 500: InputStreamReader reader = null; reader = new InputStreamReader(response.getEntity().getContent()); LineIterator lineIterator = new LineIterator(reader); StringBuilder jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } JsonResponseError error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); case 401: throw new ClientProtocolException("Status 401 - Bad credentials!"); case 403: throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!"); case 404: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); default: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); } } }
From source file:cn.org.once.cstack.cli.exception.CustomResponseErrorHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else {/*from w w w .j av a 2 s. com*/ switch (status) { case 500: InputStreamReader reader = null; reader = new InputStreamReader(response.getEntity().getContent()); LineIterator lineIterator = new LineIterator(reader); StringBuilder jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } HttpErrorServer error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); case 401: throw new ClientProtocolException("Status 401 - Bad credentials!"); case 403: throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!"); case 404: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); default: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); } } }
From source file:fr.aliacom.obm.common.calendar.MailSendTest.java
private void assertTextCalendarContentTransferEncodingIsCorrect(CalendarEncoding encoding) throws Exception { String icsContent = IOUtils.toString(getClass().getResourceAsStream("meetingWithOneAttendee.ics")); EventMail eventMail = new EventMail(new InternetAddress("sender@test"), ImmutableList.of(newAttendee("attendee1")), SUBJECT, BODY_TEXT, BODY_HTML, icsContent, ICS_METHOD, encoding);/*from w w w. j av a 2 s . c om*/ String content = writeEventMail(eventMail); LineIterator lineIterator = new LineIterator(new StringReader(content)); boolean textCalendarFound = false; while (lineIterator.hasNext()) { if (lineIterator.next().contains("Content-Type: text/calendar")) { textCalendarFound = true; break; } } assertThat(textCalendarFound).isTrue(); assertThat(lineIterator.next()).contains("Content-Transfer-Encoding: " + encoding.getValue()); }
From source file:eu.eexcess.europeana.clickstream.EuropeanaQueryCollector.java
private void processDirectory(File dir, PrintWriter writer, QueryParser queryParser, Set<String> uniqueQueryCollector) throws IOException, FileNotFoundException, UnsupportedEncodingException { for (File file : dir.listFiles()) { if (!file.getName().endsWith(".gz")) { continue; }/*from w ww . j av a 2 s. co m*/ System.err.println("Parsing file: " + file); GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(file)); LineIterator iterator = new LineIterator(new InputStreamReader(inputStream, "UTF-8")); while (iterator.hasNext()) { String line = iterator.nextLine(); if (line.contains(QUERY_PREFIX) && !line.contains("query=DATA_PROVIDER") && !line.contains("qf=DATA_PROVIDER") && !line.contains("bot") && !line.contains("slurp") && !line.contains("spider")) { int start = line.indexOf(QUERY_PREFIX), end = line.indexOf("\"", start), end2 = line.indexOf("&", start); if (end2 < end && end2 > 0) { end = end2; } if (end < 0) { end = line.length(); } try { String query = URLDecoder.decode(line.substring(start + QUERY_PREFIX.length(), end), "UTF-8"); if (!query.contains(":")) { Query parsedQuery = queryParser.parse(query); if (parsedQuery instanceof BooleanQuery) { List<BooleanClause> clauses = ((BooleanQuery) parsedQuery).clauses(); if (clauses != null) { List<String> queryTerms = new ArrayList<String>(); boolean onlyTermQueries = true; for (BooleanClause clause : clauses) { if (!(clause.getQuery() instanceof TermQuery)) { // there is at lease a single non term query onlyTermQueries = false; break; } else { TermQuery termQuery = (TermQuery) clause.getQuery(); if (termQuery.getTerm().field().equals(DEFAULT_FIELD)) { queryTerms.add(termQuery.getTerm().text()); } } } if (onlyTermQueries && queryTerms.size() == 2) { StringBuilder builder = new StringBuilder(); for (String e : new TreeSet<String>(queryTerms)) { if (builder.length() > 0) { builder.append('\t'); } builder.append(e); } // queryTerms.stream().map( (a, b) -> {b.append(a)}); String normalisedQuery = builder.toString(); if (uniqueQueryCollector.add(normalisedQuery)) { StringBuilder b = new StringBuilder(); for (String e : queryTerms) { if (b.length() > 0) { b.append('\t'); } b.append(e); } String queryInNaturalSequence = b.toString(); writer.println(queryInNaturalSequence); System.out.println(queryInNaturalSequence); } } } } } } catch (Exception e) { e.printStackTrace(); } } } iterator.close(); inputStream.close(); } }
From source file:mitm.application.djigzo.backup.DjigzoBackupRunner.java
private String getFirstLine(ByteArrayOutputStream bos) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); LineIterator it = IOUtils.lineIterator(bis, "US-ASCII"); String result = null;/*from www . j a v a 2s .co m*/ if (it.hasNext()) { result = it.nextLine(); } return result; }