List of usage examples for java.io BufferedReader ready
public boolean ready() throws IOException
From source file:net.sourceforge.doddle_owl.ui.InputDocumentSelectionPanel.java
private void setStopWordSet() { stopWordSet.clear();//from w w w .ja v a 2 s . c o m BufferedReader reader = null; try { File file = new File(STOP_WORD_LIST_FILE); if (!file.exists()) { return; } FileInputStream fis = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(fis, "UTF-8")); while (reader.ready()) { String line = reader.readLine(); stopWordSet.add(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException ioe2) { ioe2.printStackTrace(); } } }
From source file:org.gbif.occurrence.OccurrenceParser.java
/** * Parses a single response gzipFile and returns a List of the contained RawXmlOccurrences. *//*from w w w.ja v a 2 s.c o m*/ public List<RawXmlOccurrence> parseResponseFileToRawXml(File gzipFile) { if (LOG.isDebugEnabled()) LOG.debug(">> parseResponseFileToRawXml [{}]", gzipFile.getAbsolutePath()); ParsedSearchResponse responseBody = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { responseBody = new ParsedSearchResponse(); FileInputStream fis = new FileInputStream(gzipFile); GZIPInputStream inputStream = new GZIPInputStream(fis); // charsets are a nightmare and users can't be trusted, so strategy // is try these encodings in order until one of them (hopefully) works // (note the last two could be repeats of the first two): // - utf-8 // - latin1 (iso-8859-1) // - the declared encoding from the parsing itself // - a guess at detecting the charset from the raw gzipFile bytes List<String> charsets = new ArrayList<String>(); charsets.add("UTF-8"); charsets.add("ISO-8859-1"); // read parsing declaration inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); boolean gotEncoding = false; String encoding = ""; int lineCount = 0; while (bufferedReader.ready() && !gotEncoding && lineCount < 5) { String line = bufferedReader.readLine(); lineCount++; if (line != null && line.contains("encoding=")) { encoding = line.split("encoding=")[1]; // drop trailing ?> encoding = encoding.substring(0, encoding.length() - 2); // drop quotes encoding = encoding.replaceAll("\"", "").replaceAll("'", "").trim(); LOG.debug("Found encoding [{}] in parsing declaration", encoding); try { Charset.forName(encoding); charsets.add(encoding); } catch (Exception e) { LOG.debug( "Could not find supported charset matching detected encoding of [{}] - trying other guesses instead", encoding); } gotEncoding = true; } } // attempt detection from bytes Charset charset = CharsetDetection.detectEncoding(gzipFile); charsets.add(charset.name()); String goodCharset = null; boolean encodingError = false; for (String charsetName : charsets) { LOG.debug("Trying charset [{}]", charsetName); try { // reset streams fis = new FileInputStream(gzipFile); inputStream = new GZIPInputStream(fis); BufferedReader inputReader = new BufferedReader( new XmlSanitizingReader(new InputStreamReader(inputStream, charsetName))); InputSource inputSource = new InputSource(inputReader); Digester digester = new Digester(); digester.setNamespaceAware(true); digester.setValidating(false); digester.push(responseBody); NodeCreateRule rawAbcd = new NodeCreateRule(); digester.addRule(ExtractionSimpleXPaths.ABCD_RECORD_XPATH, rawAbcd); digester.addSetNext(ExtractionSimpleXPaths.ABCD_RECORD_XPATH, "addRecordAsXml"); NodeCreateRule rawAbcd1Header = new NodeCreateRule(); digester.addRule(ExtractionSimpleXPaths.ABCD_HEADER_XPATH, rawAbcd1Header); digester.addSetNext(ExtractionSimpleXPaths.ABCD_HEADER_XPATH, "setAbcd1Header"); NodeCreateRule rawDwc1_0 = new NodeCreateRule(); digester.addRule(ExtractionSimpleXPaths.DWC_1_0_RECORD_XPATH, rawDwc1_0); digester.addSetNext(ExtractionSimpleXPaths.DWC_1_0_RECORD_XPATH, "addRecordAsXml"); NodeCreateRule rawDwc1_4 = new NodeCreateRule(); digester.addRule(ExtractionSimpleXPaths.DWC_1_4_RECORD_XPATH, rawDwc1_4); digester.addSetNext(ExtractionSimpleXPaths.DWC_1_4_RECORD_XPATH, "addRecordAsXml"); // TODO: dwc_manis appears to work without a NodeCreateRule here - why? NodeCreateRule rawDwc2009 = new NodeCreateRule(); digester.addRule(ExtractionSimpleXPaths.DWC_2009_RECORD_XPATH, rawDwc2009); digester.addSetNext(ExtractionSimpleXPaths.DWC_2009_RECORD_XPATH, "addRecordAsXml"); digester.parse(inputSource); LOG.debug("Success with charset [{}] - skipping any others", charsetName); goodCharset = charsetName; break; } catch (SAXException e) { String msg = "SAX exception when parsing parsing from response gzipFile [" + gzipFile.getAbsolutePath() + "] using encoding [" + charsetName + "] - trying another charset"; LOG.debug(msg, e); } catch (IOException e) { if (e instanceof MalformedByteSequenceException) { LOG.debug("Malformed utf-8 byte when parsing with encoding [{}] - trying another charset", charsetName); encodingError = true; } } } if (goodCharset == null) { if (encodingError) { LOG.warn( "Could not parse gzipFile - none of the encoding attempts worked (failed with malformed utf8) - skipping gzipFile [{}]", gzipFile.getAbsolutePath()); } else { LOG.warn("Could not parse gzipFile (malformed parsing) - skipping gzipFile [{}]", gzipFile.getAbsolutePath()); } } } catch (FileNotFoundException e) { LOG.warn("Could not find response gzipFile [{}] - skipping gzipFile", gzipFile.getAbsolutePath(), e); } catch (IOException e) { LOG.warn("Could not read response gzipFile [{}] - skipping gzipFile", gzipFile.getAbsolutePath(), e); } catch (TransformerException e) { LOG.warn("Could not create parsing transformer for [{}] - skipping gzipFile", gzipFile.getAbsolutePath(), e); } catch (ParserConfigurationException e) { LOG.warn("Failed to pull raw parsing from response gzipFile [{}] - skipping gzipFile", gzipFile.getAbsolutePath(), e); } finally { try { if (bufferedReader != null) bufferedReader.close(); if (inputStreamReader != null) inputStreamReader.close(); } catch (IOException e) { LOG.debug("Failed to close input files", e); } } if (LOG.isDebugEnabled()) LOG.debug("<< parseResponseFileToRawXml [{}]", gzipFile.getAbsolutePath()); return (responseBody == null) ? null : responseBody.getRecords(); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient//ww w . java2 s . c om @Ignore public void testCreatePackageFromDRLAsJson(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM); connection.setRequestProperty("Accept", MediaType.APPLICATION_JSON); connection.setDoOutput(true); //Send request BufferedReader br = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("simple_rules2.drl"))); DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); while (br.ready()) dos.writeBytes(br.readLine()); dos.flush(); dos.close(); assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_JSON, connection.getContentType()); //logger.log(LogLevel, IOUtils.toString(connection.getInputStream())); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient/*from w w w.ja v a2 s. co m*/ @Ignore public void testCreatePackageFromDRLAsJaxB(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM); connection.setRequestProperty("Accept", MediaType.APPLICATION_XML); connection.setDoOutput(true); //Send request BufferedReader br = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("simple_rules3.drl"))); DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); while (br.ready()) dos.writeBytes(br.readLine()); dos.flush(); dos.close(); assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_XML, connection.getContentType()); //logger.log(LogLevel, IOUtils.toString(connection.getInputStream())); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient//from w w w . j av a 2 s . c o m @Ignore public void testCreatePackageFromDRLAsEntry(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.setDoOutput(true); //Send request BufferedReader br = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("simple_rules.drl"))); DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); while (br.ready()) dos.writeBytes(br.readLine()); dos.flush(); dos.close(); /* Retry with a -1 from the connection */ if (connection.getResponseCode() == -1) { url = new URL(baseURL, "rest/packages"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.setDoOutput(true); //Send request br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("simple_rules.drl"))); dos = new DataOutputStream(connection.getOutputStream()); while (br.ready()) dos.writeBytes(br.readLine()); dos.flush(); dos.close(); } assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType()); //logger.log(LogLevel, IOUtils.toString(connection.getInputStream())); }
From source file:edu.uci.ics.jung.io.PajekNetReader.java
@SuppressWarnings("unchecked") private String readArcsOrEdges(String curLine, BufferedReader br, Graph<V, E> g, List<V> id, Factory<E> edge_factory) throws IOException { String nextLine = curLine;/* ww w . j a va 2s . co m*/ // in case we're not there yet (i.e., format tag isn't arcs or edges) if (!c_pred.evaluate(curLine)) nextLine = skip(br, c_pred); boolean reading_arcs = false; boolean reading_edges = false; EdgeType directedness = null; if (a_pred.evaluate(nextLine)) { if (g instanceof UndirectedGraph) { throw new IllegalArgumentException( "Supplied undirected-only graph cannot be populated with directed edges"); } else { reading_arcs = true; directedness = EdgeType.DIRECTED; } } if (e_pred.evaluate(nextLine)) { if (g instanceof DirectedGraph) throw new IllegalArgumentException( "Supplied directed-only graph cannot be populated with undirected edges"); else reading_edges = true; directedness = EdgeType.UNDIRECTED; } if (!(reading_arcs || reading_edges)) return nextLine; boolean is_list = l_pred.evaluate(nextLine); while (br.ready()) { nextLine = br.readLine(); if (nextLine == null || t_pred.evaluate(nextLine)) break; if (curLine == "") // skip blank lines continue; StringTokenizer st = new StringTokenizer(nextLine.trim()); int vid1 = Integer.parseInt(st.nextToken()) - 1; V v1; if (id != null) v1 = id.get(vid1); else v1 = (V) new Integer(vid1); if (is_list) // one source, multiple destinations { do { createAddEdge(st, v1, directedness, g, id, edge_factory); } while (st.hasMoreTokens()); } else // one source, one destination, at most one weight { E e = createAddEdge(st, v1, directedness, g, id, edge_factory); // get the edge weight if we care if (edge_weights != null && st.hasMoreTokens()) edge_weights.set(e, new Float(st.nextToken())); } } return nextLine; }
From source file:edu.uci.ics.jung.io.CustomPajekNetReader.java
@SuppressWarnings("unchecked") private String readArcsOrEdges(String curLine, BufferedReader br, Graph<V, E> g, List<V> id, Factory<E> edge_factory) throws IOException { String nextLine = curLine;//from w ww .j av a 2 s. c om // in case we're not there yet (i.e., format tag isn't arcs or edges) if (!c_pred.evaluate(curLine)) { nextLine = skip(br, c_pred); } boolean reading_arcs = false; boolean reading_edges = false; EdgeType directedness = null; if (a_pred.evaluate(nextLine)) { if (g instanceof UndirectedGraph) { throw new IllegalArgumentException( "Supplied undirected-only graph cannot be populated with directed edges"); } else { reading_arcs = true; directedness = EdgeType.DIRECTED; } } if (e_pred.evaluate(nextLine)) { if (g instanceof DirectedGraph) { throw new IllegalArgumentException( "Supplied directed-only graph cannot be populated with undirected edges"); } else { reading_edges = true; } directedness = EdgeType.UNDIRECTED; } if (!(reading_arcs || reading_edges)) { return nextLine; } boolean is_list = l_pred.evaluate(nextLine); while (br.ready()) { nextLine = br.readLine(); if (nextLine == null || t_pred.evaluate(nextLine)) { break; } if (curLine == "") // skip blank lines { continue; } StringTokenizer st = new StringTokenizer(nextLine.trim()); int vid1 = Integer.parseInt(st.nextToken()) - 1; V v1; if (id != null) { v1 = id.get(vid1); } else { v1 = (V) new Integer(vid1); } if (is_list) // one source, multiple destinations { do { createAddEdge(st, v1, directedness, g, id, edge_factory); } while (st.hasMoreTokens()); } else // one source, one destination, at most one weight { E e = createAddEdge(st, v1, directedness, g, id, edge_factory); // get the edge weight if we care if (edge_weights != null && st.hasMoreTokens()) { edge_weights.set(e, new Float(st.nextToken())); } } } return nextLine; }
From source file:com.rsmart.kuali.kfs.cr.batch.CheckReconciliationImportStep.java
/** * Parse File//from w w w. java 2s . com * * @param checkFile Check File Path * @throws Exception */ private void parseTextFile(String checkFile, List<CheckReconError> records) throws Exception { LOG.info("Parsing File : " + checkFile); File file = new File(checkFile); BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; int totalLinesProcessed = 0; Timestamp ts = new Timestamp(new java.util.Date().getTime()); Collection<Bank> banks = businessObjectService.findAll(Bank.class); while ((line = br.readLine()) != null) { if (header && totalLinesProcessed == 0) { headerLine = line; headerMap = parseHeaderLine(); totalLinesProcessed++; } else if (footer && !br.ready()) { footerLine = line; footerMap = parseFooterLine(); totalLinesProcessed++; } else { CheckReconciliation cr = null; if (CRConstants.DELIMITED.equals(fileType)) { cr = parseDelimitedLine(line); } else if (CRConstants.FIXED.equals(fileType)) { cr = parseFixedLine(line); } // Find existing check record CheckReconciliation existingRecord = getCheckReconciliation(cr.getCheckNumber(), cr.getBankAccountNumber()); if (existingRecord == null) { // Update update ts cr.setLastUpdate(ts); // Set Status to Exception cr.setStatus(CRConstants.EXCP); // Set GL Trans False cr.setGlTransIndicator(Boolean.FALSE); String notFoundSrc = getParameterService().getParameterValueAsString( CheckReconciliationImportStep.class, CRConstants.SRC_NOT_FOUND); String notFoundBankCd = getParameterService().getParameterValueAsString( CheckReconciliationImportStep.class, CRConstants.BNK_CD_NOT_FOUND); cr.setSourceCode(notFoundSrc); cr.setBankCode(notFoundBankCd); businessObjectService.save(cr); records.add(getCheckReconError(cr, "The bank record does not exist in reconciliation table. " + cr.getCheckNumber())); LOG.error("Check Record Not Found"); } else { if (CRConstants.PDP_SRC.equals(existingRecord.getSourceCode())) { //update pdp records if checkstatus id cleared String checkStatus = updateCheckStatus(cr, banks, records); //update CheckRecon record if checkstatus is cleared if (checkStatus.equals(CRConstants.CLEARED)) { // ==== CU Customization: Only update when previous check status is Issued. ==== if (CRConstants.ISSUED.equals(existingRecord.getStatus())) { cr.setStatus(checkStatus); existingRecord.setStatus(checkStatus); existingRecord.setStatusChangeDate(cr.getStatusChangeDate()); existingRecord.setLastUpdate(ts); businessObjectService.save(existingRecord); LOG.info("Updated Check Recon Record : " + existingRecord.getId()); } else { LOG.warn("Could not update PDP-sourced Check Recon Record status to " + CRConstants.CLEARED + " because the existing status is " + existingRecord.getStatus() + " for this PDP-sourced record : " + existingRecord.getId()); } } else if (checkStatus.equals(CRConstants.STOP)) { if (!existingRecord.getStatus().equalsIgnoreCase(CRConstants.STOP)) { records.add(getCheckReconError(cr, "Bank file status shows STOP and Check Recon table status is not STOP")); LOG.error( "Bank file status is STOP and Check Recon table status is not STOP for check " + cr.getCheckNumber()); } } else if (checkStatus.equals(CRConstants.VOIDED)) { if (!existingRecord.getStatus().equalsIgnoreCase(CRConstants.VOIDED)) { records.add(getCheckReconError(cr, "Bank file status is VOIDED and Check Recon table status is not VOIDED")); LOG.error( "Bank file status is VOIDED and Check Recon table status is not VOID for check " + cr.getCheckNumber()); } } } else { String checkStatus = getCheckStatus(cr); if (checkStatus.equals(CRConstants.CLEARED)) { // ==== CU Customization: Only update when previous check status is Issued. ==== if (CRConstants.ISSUED.equals(existingRecord.getStatus())) { cr.setStatus(checkStatus); existingRecord.setStatus(checkStatus); existingRecord.setStatusChangeDate(cr.getStatusChangeDate()); existingRecord.setLastUpdate(ts); businessObjectService.save(existingRecord); LOG.info("Updated Check Recon Record : " + existingRecord.getId()); } else { LOG.warn("Could not update Check Recon Record status to " + CRConstants.CLEARED + " because the current status is " + existingRecord.getStatus() + " for this record : " + existingRecord.getId()); } } else if (checkStatus.equals(CRConstants.STOP)) { if (!existingRecord.getStatus().equalsIgnoreCase(CRConstants.STOP)) { records.add(getCheckReconError(cr, "Bank file status is STOP and Check Recon table status is not STOP")); LOG.error( "Bank file status is STOP and Check Recon table status is not STOP for check " + cr.getCheckNumber()); } } else if (checkStatus.equals(CRConstants.VOIDED)) { if (!existingRecord.getStatus().equalsIgnoreCase(CRConstants.VOIDED)) { records.add(getCheckReconError(cr, "Bank file status is VOID and Check Recon table status is not VOID")); LOG.error( "Bank file status is VOIDED and Check Recon table status is not VOID for check " + cr.getCheckNumber()); } } } } totalLinesProcessed++; } } LOG.info("Processed Records : " + totalLinesProcessed); br.close(); }
From source file:org.geoserver.ows.Dispatcher.java
Object parseRequestXML(Object requestBean, BufferedReader input, Request request) throws Exception { //check for an empty input stream //if (input.available() == 0) { if (!input.ready()) { return null; }/*from w ww .j a v a2 s .c o m*/ //create stream parser XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); //parse root element XmlPullParser parser = factory.newPullParser(); //parser.setInput(input, "UTF-8"); parser.setInput(input); parser.nextTag(); String namespace = (parser.getNamespace() != null) ? parser.getNamespace() : ""; String element = parser.getName(); String version = null; String service = null; for (int i = 0; i < parser.getAttributeCount(); i++) { if ("version".equals(parser.getAttributeName(i))) { version = parser.getAttributeValue(i); } if ("service".equals(parser.getAttributeName(i))) { service = parser.getAttributeValue(i); } } parser.setInput(null); //reset input stream input.reset(); XmlRequestReader xmlReader = findXmlReader(namespace, element, service, version); if (xmlReader == null) { //no xml reader, just return object passed in return requestBean; } if (xmlReader instanceof HttpServletRequestAware) { ((HttpServletRequestAware) xmlReader).setHttpRequest(request.getHttpRequest()); } //return xmlReader.read(input); return xmlReader.read(requestBean, input, request.getKvp()); }
From source file:org.apache.webdav.cmd.Client.java
void ereport(String path, String srcFilename) { try {/*ww w. j a v a 2 s.c om*/ String sQuery = ""; if (srcFilename == null) { sQuery = "<D:expand-property xmlns:D='DAV:'><D:property name='version-history'><D:property name='version-set'><D:property name='successor-set'><D:property name='comment'/></D:property></D:property></D:property></D:expand-property>"; } else { File file = new File(dir.getCanonicalPath(), srcFilename); if (!file.exists()) { out.println("report src file not found"); return; } InputStream isQuery = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(isQuery)); while (reader.ready()) { sQuery += reader.readLine(); } reader.close(); sQuery.replace('\t', ' '); out.println(sQuery); } path = checkUri(path); out.println("expand-property Report of '" + path + "':"); Enumeration propertyValues = webdavResource.reportMethod(uriToHttpURL(path), sQuery, 1); if (propertyValues.hasMoreElements()) { while (propertyValues.hasMoreElements()) { out.println(displayXML(propertyValues.nextElement().toString(), 0)); } } else { out.println("failed."); out.println(webdavResource.getStatusMessage()); } } catch (Exception ex) { handleException(ex); } }