List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java
/** Loads a dictionary into a <CODE>Set</CODE> where the <PRE>key</PRE> is entry and its <PRE>value</PRE> is a * <CODE>List</CODE> (each entry can have multiple values) * @param set The set/*from w w w . ja v a 2s .c o m*/ * @param name A human-readable name * @param is The stream * @throws RuntimeException If a problem occurs when reading the dictionary */ private void loadDictionary(Map set, String name, InputStream is) throws RuntimeException { //TODO : should be static HashSet lemmas = new HashSet(); int forms = 0; String lemmaID = ""; System.out.print("Loading dictionary : " + name + " "); try { LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1")); String line = null; while ((line = IN.readLine()) != null) { if ((IN.getLineNumber() % 1000) == 1) System.out.print("."); // new lemma if (line.startsWith(";; ")) { lemmaID = line.substring(3); // lemmaID's must be unique if (lemmas.contains(lemmaID)) throw new RuntimeException("Lemma " + lemmaID + "in " + name + " (line " + IN.getLineNumber() + ") isn't unique"); lemmas.add(lemmaID); } // comment else if (line.startsWith(";")) { } else { String split[] = line.split("\t", -1); //-1 to avoid triming of trail values //a little error-checking won't hurt : if (split.length != 4) { throw new RuntimeException("Entry in " + name + " (line " + IN.getLineNumber() + ") doesn't have 4 fields (3 tabs)"); } String entry = split[0]; // get the entry for use as key String vocalization = split[1]; String morphology = split[2]; String glossPOS = split[3]; String gloss; String POS; Pattern p; Matcher m; // two ways to get the POS info: // (1) explicitly, by extracting it from the gloss field: p = Pattern.compile(".*" + "<pos>(.+?)</pos>" + ".*"); m = p.matcher(glossPOS); if (m.matches()) { POS = m.group(1); //extract POS from glossPOS gloss = glossPOS; //we clean up the gloss later (see below) } // (2) by deduction: use the morphology (and sometimes the voc and gloss) to deduce the appropriate POS else { // we need the gloss to guess proper names gloss = glossPOS; // null prefix or suffix if (morphology.matches("^(Pref-0|Suff-0)$")) { POS = ""; } else if (morphology.matches("^F" + ".*")) { POS = vocalization + "/FUNC_WORD"; } else if (morphology.matches("^IV" + ".*")) { POS = vocalization + "/VERB_IMPERFECT"; } else if (morphology.matches("^PV" + ".*")) { POS = vocalization + "/VERB_PERFECT"; } else if (morphology.matches("^CV" + ".*")) { POS = vocalization + "/VERB_IMPERATIVE"; } else if (morphology.matches("^N" + ".*")) { // educated guess (99% correct) if (gloss.matches("^[A-Z]" + ".*")) { POS = vocalization + "/NOUN_PROP"; } // (was NOUN_ADJ: some of these are really ADJ's and need to be tagged manually) else if (vocalization.matches(".*" + "iy~$")) { POS = vocalization + "/NOUN"; } else POS = vocalization + "/NOUN"; } else { throw new RuntimeException( "No POS can be deduced in " + name + " (line " + IN.getLineNumber() + ")"); } } // clean up the gloss: remove POS info and extra space, and convert upper-ASCII to lower (it doesn't convert well to UTF-8) gloss = gloss.replaceFirst("<pos>.+?</pos>", ""); gloss = gloss.trim(); //TODO : we definitely need a translate() method in the java packages ! gloss = gloss.replaceAll(";", "/"); //TODO : is it necessary ? gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "C"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "N"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "c"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "n"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "AE"); gloss = gloss.replaceAll("", "Sh"); gloss = gloss.replaceAll("", "Zh"); gloss = gloss.replaceAll("", "ss"); gloss = gloss.replaceAll("", "ae"); gloss = gloss.replaceAll("", "sh"); gloss = gloss.replaceAll("", "zh"); // note that although we read 4 fields from the dict we now save 5 fields in the hash table // because the info in last field, glossPOS, was split into two: gloss and POS DictionaryEntry de = new DictionaryEntry(entry, lemmaID, vocalization, morphology, gloss, POS); if (set.containsKey(entry)) { ((Collection) set.get(entry)).add(de); } else set.put(entry, de); forms++; } } IN.close(); System.out.println(); if (!"".equals(lemmaID)) System.out.print(lemmas.size() + " lemmas and "); System.out.println(set.size() + " entries totalizing " + forms + " forms"); } catch (IOException e) { throw new RuntimeException("Can not open : " + name); } }
From source file:gtu._work.etc.GoogleContactUI.java
void googleTableMouseClicked(MouseEvent evt) { try {//from www . ja v a 2 s . c o m JPopupMenuUtil popupUtil = JPopupMenuUtil.newInstance(googleTable).applyEvent(evt); //CHANGE ENCODE popupUtil.addJMenuItem("set encode", new ActionListener() { public void actionPerformed(ActionEvent e) { try { String code = StringUtils.defaultString(JOptionPaneUtil.newInstance().iconPlainMessage() .showInputDialog("input file encode", "ENCODE"), "UTF8"); encode = Charset.forName(code).displayName(); } catch (Exception ex) { JCommonUtil.handleException(ex); } System.err.println("encode : " + encode); } }); //SIMPLE LOAD GOOGLE CSV FILE popupUtil.addJMenuItem("open Google CSV file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile("csv", ".csv") .showOpenDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } try { if (file.getName().endsWith(".csv")) { DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); LineNumberReader reader = new LineNumberReader( new InputStreamReader(new FileInputStream(file), GOOGLE_CVS_ENCODE)); for (String line = null; (line = reader.readLine()) != null;) { if (reader.getLineNumber() == 1) { continue; } model.addRow(line.split(",")); } reader.close(); googleTable.setModel(model); JTableUtil.newInstance(googleTable).hiddenAllEmptyColumn(); } } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //SAVE CSV FILE FOR GOOGLE popupUtil.addJMenuItem("save to Google CVS file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile(".csv", ".csv") .showSaveDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } file = FileUtil.getIndicateFileExtension(file, ".csv"); try { BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(file), GOOGLE_CVS_ENCODE)); StringBuilder sb = new StringBuilder(); for (Object title : googleColumns) { sb.append(title + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); for (int row = 0; row < model.getRowCount(); row++) { sb = new StringBuilder(); for (int col = 0; col < model.getColumnCount(); col++) { String colVal = StringUtils.defaultString((String) model.getValueAt(row, col), ""); if (colVal.equalsIgnoreCase("null")) { colVal = ""; } sb.append(colVal + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); } writer.flush(); writer.close(); } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //PASTE CLIPBOARD popupUtil.addJMenuItem("paste clipboard", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_multiRowData(true); } }); popupUtil.addJMenuItem("paste clipboard to selected cell", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_singleValueToSelectedCell(); } }); JMenuItem addEmptyRowItem = JTableUtil.newInstance(googleTable).jMenuItem_addRow(false, "add row count?"); addEmptyRowItem.setText("add row"); JMenuItem removeColumnItem = JTableUtil.newInstance(googleTable).jMenuItem_removeColumn(null); removeColumnItem.setText("remove column"); JMenuItem removeRowItem = JTableUtil.newInstance(googleTable).jMenuItem_removeRow(null); removeRowItem.setText("remove row"); JMenuItem removeAllRowItem = JTableUtil.newInstance(googleTable) .jMenuItem_removeAllRow("remove all row?"); removeAllRowItem.setText("remove all row"); JMenuItem clearSelectedCellItem = JTableUtil.newInstance(googleTable) .jMenuItem_clearSelectedCell("are you sure clear selected area?"); clearSelectedCellItem.setText("clear selected area"); popupUtil.addJMenuItem(addEmptyRowItem, removeColumnItem, removeRowItem, removeAllRowItem, clearSelectedCellItem); popupUtil.show(); } catch (Exception ex) { JCommonUtil.handleException(ex); } }
From source file:core.Inject.java
private String[] loadPayloadsFromfile(String pathTo_payloads) { String[] _payloads = null;//w ww .j a va2s . co m try { FileInputStream fstream = new FileInputStream(pathTo_payloads); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; int i = 0; LineNumberReader lnr = new LineNumberReader(new FileReader(pathTo_payloads)); lnr.skip(Long.MAX_VALUE); _payloads = new String[lnr.getLineNumber()]; while ((strLine = br.readLine()) != null) { _payloads[i] = strLine; i++; } in.close(); } catch (Exception e) { Debug.printError("ERROR: unable to load the attack payloads"); HaltHandler.quit_nok(); } return _payloads; }
From source file:org.kuali.test.runner.execution.FileOperationExecution.java
private List<File> findFilesContainingText(List<File> inputFiles, String txt) { List<File> retval = new ArrayList<File>(); LineNumberReader lnr = null;/*from w w w . j a v a2s . c om*/ for (File f : inputFiles) { try { lnr = new LineNumberReader(new FileReader(f)); String line = null; while ((line = lnr.readLine()) != null) { if (line.contains(txt)) { retval.add(f); break; } } } catch (Exception ex) { } finally { try { if (lnr != null) { lnr.close(); } } catch (Exception ex) { } ; } } return retval; }
From source file:org.apache.sling.maven.slingstart.run.LauncherCallable.java
public static void stop(final Log LOG, final ProcessDescription cfg) throws Exception { boolean isNew = false; if (cfg.getProcess() != null || isNew) { LOG.info("Stopping Launchpad " + cfg.getId()); boolean destroy = true; final int twoMinutes = 2 * 60 * 1000; final File controlPortFile = getControlPortFile(cfg.getDirectory()); LOG.debug("Control port file " + controlPortFile + " exists: " + controlPortFile.exists()); if (controlPortFile.exists()) { // reading control port int controlPort = -1; String secretKey = null; LineNumberReader lnr = null; String serverName = null; try { lnr = new LineNumberReader(new FileReader(controlPortFile)); final String portLine = lnr.readLine(); final int pos = portLine.indexOf(':'); controlPort = Integer.parseInt(portLine.substring(pos + 1)); if (pos > 0) { serverName = portLine.substring(0, pos); }// w w w . j a v a 2 s . c om secretKey = lnr.readLine(); } catch (final NumberFormatException ignore) { // we ignore this LOG.debug("Error reading control port file " + controlPortFile, ignore); } catch (final IOException ignore) { // we ignore this LOG.debug("Error reading control port file " + controlPortFile, ignore); } finally { IOUtils.closeQuietly(lnr); } if (controlPort != -1) { final List<String> hosts = new ArrayList<String>(); if (serverName != null) { hosts.add(serverName); } hosts.add("localhost"); hosts.add("127.0.0.1"); LOG.debug("Found control port " + controlPort); int index = 0; while (destroy && index < hosts.size()) { final String hostName = hosts.get(index); Socket clientSocket = null; DataOutputStream out = null; BufferedReader in = null; try { LOG.debug("Trying to connect to " + hostName + ":" + controlPort); clientSocket = new Socket(); // set a socket timeout clientSocket.connect(new InetSocketAddress(hostName, controlPort), twoMinutes); // without that, read() call on the InputStream associated with this Socket is infinite clientSocket.setSoTimeout(twoMinutes); LOG.debug(hostName + ":" + controlPort + " connection estabilished, sending the 'stop' command..."); out = new DataOutputStream(clientSocket.getOutputStream()); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); if (secretKey != null) { out.writeBytes(secretKey); out.write(' '); } out.writeBytes("stop\n"); in.readLine(); destroy = false; LOG.debug("'stop' command sent to " + hostName + ":" + controlPort); } catch (final Throwable ignore) { // catch Throwable because InetSocketAddress and Socket#connect throws unchecked exceptions // we ignore this for now LOG.debug("Error sending 'stop' command to " + hostName + ":" + controlPort + " due to: " + ignore.getMessage()); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); IOUtils.closeQuietly(clientSocket); } index++; } } } if (cfg.getProcess() != null) { final Process process = cfg.getProcess(); if (!destroy) { // as shutdown might block forever, we use a timeout final long now = System.currentTimeMillis(); final long end = now + twoMinutes; LOG.debug("Waiting for process to stop..."); while (isAlive(process) && (System.currentTimeMillis() < end)) { try { Thread.sleep(2500); } catch (InterruptedException e) { // ignore } } if (isAlive(process)) { LOG.debug("Process timeout out after 2 minutes"); destroy = true; } else { LOG.debug("Process stopped"); } } if (destroy) { LOG.debug("Destroying process..."); process.destroy(); LOG.debug("Process destroyed"); } cfg.setProcess(null); } } else { LOG.warn("Launchpad already stopped"); } }
From source file:org.apache.nutch.protocol.http.api.RobotRulesParser.java
/** command-line main for testing */ public static void main(String[] argv) { if (argv.length < 3) { System.out.println("Usage:"); System.out.println(" java <robots-file> <url-file> <agent-name>+"); System.out.println(""); System.out.println("The <robots-file> will be parsed as a robots.txt file,"); System.out.println("using the given <agent-name> to select rules. URLs "); System.out.println("will be read (one per line) from <url-file>, and tested"); System.out.println("against the rules."); System.exit(-1);//from ww w . j av a 2 s . co m } try { FileInputStream robotsIn = new FileInputStream(argv[0]); LineNumberReader testsIn = new LineNumberReader(new FileReader(argv[1])); String[] robotNames = new String[argv.length - 2]; for (int i = 0; i < argv.length - 2; i++) robotNames[i] = argv[i + 2]; ArrayList bufs = new ArrayList(); byte[] buf = new byte[BUFSIZE]; int totBytes = 0; int rsize = robotsIn.read(buf); while (rsize >= 0) { totBytes += rsize; if (rsize != BUFSIZE) { byte[] tmp = new byte[rsize]; System.arraycopy(buf, 0, tmp, 0, rsize); bufs.add(tmp); } else { bufs.add(buf); buf = new byte[BUFSIZE]; } rsize = robotsIn.read(buf); } byte[] robotsBytes = new byte[totBytes]; int pos = 0; for (int i = 0; i < bufs.size(); i++) { byte[] currBuf = (byte[]) bufs.get(i); int currBufLen = currBuf.length; System.arraycopy(currBuf, 0, robotsBytes, pos, currBufLen); pos += currBufLen; } RobotRulesParser parser = new RobotRulesParser(robotNames); RobotRuleSet rules = parser.parseRules(robotsBytes); System.out.println("Rules:"); System.out.println(rules); System.out.println(); String testPath = testsIn.readLine().trim(); while (testPath != null) { System.out.println((rules.isAllowed(testPath) ? "allowed" : "not allowed") + ":\t" + testPath); testPath = testsIn.readLine(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils.java
protected List<String> getLinesList(String fileLocation, int rows, String encoding) throws IOException { List<String> lines = new ArrayList<String>(); try {// w ww .ja v a 2s . c o m File file = new File(fileLocation); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, encoding); LineNumberReader reader = new LineNumberReader(isr); String line; int lineNumber = 0; while ((line = reader.readLine()) != null && lineNumber < rows) { lines.add(line); lineNumber++; } reader.close(); } catch (Exception e) { log.equals(e); } return lines; }
From source file:stroom.streamstore.server.fs.ManualCheckStreamPerformance.java
public long seekLargeFileTest() throws IOException { final byte[] sb = "some data that may compress quite well TEST\n".getBytes(StreamUtil.DEFAULT_CHARSET); try (OutputStream os = getOutputStream()) { for (int i = 0; i < testSize; i++) { os.write(sb);//w w w .jav a 2 s . c o m } os.close(); onCloseOutput(os); } final long startTime = System.currentTimeMillis(); try (InputStream is = getInputStream()) { StreamUtil.skip(is, (testSize / 2) * sb.length); try (LineNumberReader reader = new LineNumberReader( new InputStreamReader(is, StreamUtil.DEFAULT_CHARSET))) { final String line1 = reader.readLine(); final String line2 = reader.readLine(); if (line1 == null || line2 == null || !line1.contains("TEST") || !line2.contains("TEST")) { throw new RuntimeException("Something has gone wrong"); } } final long timeTaken = System.currentTimeMillis() - startTime; return timeTaken; } }
From source file:net.sf.keystore_explorer.crypto.signing.MidletSigner.java
/** * Read the attributes of the supplied JAD file as properties. * * @param jadFile/*from w w w . ja va2 s . c o m*/ * JAD file * @return JAD file's attributes as properties * @throws IOException * If an I/O problem occurred or supplied file is not a JAD file */ public static Properties readJadFile(File jadFile) throws IOException { LineNumberReader lnr = null; try { Properties jadProperties = new Properties(); lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(jadFile))); String line = null; while ((line = lnr.readLine()) != null) { int index = line.indexOf(": "); if (index == -1) { throw new IOException(res.getString("NoReadJadCorrupt.exception.message")); } String name = line.substring(0, index); String value = line.substring(index + 2); jadProperties.setProperty(name, value); } return jadProperties; } finally { IOUtils.closeQuietly(lnr); } }
From source file:stroom.util.StreamGrepTool.java
private void processFile(final StreamStore streamStore, final long streamId, final String match) { try {// www . ja v a 2s . c o m final StreamSource streamSource = streamStore.openStreamSource(streamId); if (streamSource != null) { final InputStream inputStream = streamSource.getInputStream(); // Build up 2 buffers so we can output the content either side // of // the matching line final ArrayDeque<String> preBuffer = new ArrayDeque<>(); final ArrayDeque<String> postBuffer = new ArrayDeque<>(); final LineNumberReader lineNumberReader = new LineNumberReader( new InputStreamReader(inputStream, StreamUtil.DEFAULT_CHARSET)); String aline = null; while ((aline = lineNumberReader.readLine()) != null) { String lines[] = new String[] { aline }; if (addLineBreak != null) { lines = aline.split(addLineBreak); } for (final String line : lines) { if (match == null) { System.out.println(lineNumberReader.getLineNumber() + ":" + line); } else { postBuffer.add(lineNumberReader.getLineNumber() + ":" + line); if (postBuffer.size() > 5) { final String searchLine = postBuffer.pop(); checkMatch(match, preBuffer, postBuffer, searchLine); preBuffer.add(searchLine); if (preBuffer.size() > 5) { preBuffer.pop(); } } } } } // Look at the end while (postBuffer.size() > 0) { final String searchLine = postBuffer.pop(); checkMatch(match, preBuffer, postBuffer, searchLine); preBuffer.add(searchLine); if (preBuffer.size() > 5) { preBuffer.pop(); } } inputStream.close(); streamStore.closeStreamSource(streamSource); } } catch (final Exception ex) { ex.printStackTrace(); } }