Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

In this page you can find the example usage for java.io LineNumberReader LineNumberReader.

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

From source file:edu.stanford.muse.util.Util.java

public static Collection<String> breakIntoParas(String input) throws IOException {
    List<String> paras = new ArrayList<String>();
    LineNumberReader lnr = new LineNumberReader(new StringReader(input));

    StringBuilder currentPara = new StringBuilder();

    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        line = line.trim();/*from   w w  w. ja  v a 2 s . c  om*/

        if (line.length() == 0) {
            // end para
            if (currentPara.length() > 0)
                paras.add(currentPara.toString());
            currentPara = new StringBuilder();
        } else {
            currentPara.append(line);
            currentPara.append("\n");
        }
    }

    // add any residue
    if (currentPara.length() > 0)
        paras.add(currentPara.toString());
    return paras;
}

From source file:org.opencms.main.CmsShell.java

/**
 * Executes all commands read from the given input stream.<p>
 * /*from   w  w  w. jav  a  2s. c  o m*/
 * @param fileInputStream a file input stream from which the commands are read
 */
private void executeCommands(FileInputStream fileInputStream) {

    try {
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fileInputStream));
        while (!m_exitCalled) {
            printPrompt();
            String line = lnr.readLine();
            if (line == null) {
                // if null the file has been read to the end
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                    // noop
                }
                break;
            }
            if (line.trim().startsWith("#")) {
                System.out.println(line);
                continue;
            }
            StringReader reader = new StringReader(line);
            StreamTokenizer st = new StreamTokenizer(reader);
            st.eolIsSignificant(true);

            // put all tokens into a List
            List<String> parameters = new ArrayList<String>();
            while (st.nextToken() != StreamTokenizer.TT_EOF) {
                if (st.ttype == StreamTokenizer.TT_NUMBER) {
                    parameters.add(Integer.toString(new Double(st.nval).intValue()));
                } else {
                    parameters.add(st.sval);
                }
            }
            reader.close();

            // extract command and arguments
            if (parameters.size() == 0) {
                if (m_echo) {
                    System.out.println();
                }
                continue;
            }
            String command = parameters.get(0);
            parameters = parameters.subList(1, parameters.size());

            // execute the command
            executeCommand(command, parameters);
        }
    } catch (Throwable t) {
        t.printStackTrace(System.err);
    }
}

From source file:org.opencms.applet.upload.FileUploadApplet.java

private List parseDuplicateFiles(String responseBodyAsString) {

    List result = new ArrayList();
    LineNumberReader reader = new LineNumberReader(
            new InputStreamReader(new ByteArrayInputStream(responseBodyAsString.getBytes())));
    try {/*from www  .j a  v a  2  s .c  om*/
        String trim;
        for (String read = reader.readLine(); read != null; read = reader.readLine()) {
            trim = read.trim();
            if (!(trim.equals("") || trim.equals("\n"))) {
                // empty strings could happen if the serverside jsp is edited and has new unwanted linebreaks
                result.add(read);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.sds.acube.ndisc.xadmin.XNDiscAdminUtil.java

/**
 * XNDisc Admin ? ?/*from   www . j a  v a  2 s  .  c  o m*/
 */
private static void readVersionFromFile() {
    XNDiscAdmin_PublishingVersion = "<unknown>";
    XNDiscAdmin_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(
                XNDiscAdminUtil.class.getResourceAsStream("/com/sds/acube/ndisc/xadmin/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNDiscAdmin_PublishingVersion = line
                                .substring("Publishing-Version=".length(), line.length()).trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNDiscAdmin_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNDiscAdmin_PublishingVersion = "<unknown>";
        XNDiscAdmin_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:org.apache.zookeeper.server.quorum.QuorumPeerMainTest.java

/**
 * Verify handling of quorum defaults/*from  w  w  w . j  a  v  a2 s  .c  o m*/
 * * default electionAlg is fast leader election
 */
@Test
public void testQuorumDefaults() throws Exception {
    ClientBase.setupTestEnv();

    // setup the logger to capture all logs
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = getConsoleAppender(os, Level.INFO);
    appender.setImmediateFlush(true);
    Logger zlogger = Logger.getLogger("org.apache.zookeeper");
    zlogger.addAppender(appender);

    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();

        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":"
                + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:"
                + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2;

        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
        q1.start();
        q2.start();

        Assert.assertTrue("waiting for server 1 being up",
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 being up",
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));

        q1.shutdown();
        q2.shutdown();

        Assert.assertTrue("waiting for server 1 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT));

    } finally {
        zlogger.removeAppender(appender);
    }
    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p = Pattern.compile(".*FastLeaderElection.*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("fastleaderelection used", found);
}

From source file:com.ggvaidya.scinames.model.Dataset.java

/**
 * Attempt to load a dataset from a file. We use regular expressions to try to guess the file type,
 * and then delegate the job out. Rather cleverly, we try extracting the names using every extractor
 * this project knows about, and then pick the one that gives us the most number of names.
 * /*w  w w .  j  a va2  s  . c om*/
 * @param proj The project doing the loading, used to get the name extractors.
 * @param f The file to open.
 * @return The dataset loaded from that file.
 * @throws IOException If there was an error loading the file.
 */
public static Dataset loadFromFile(Project proj, File f) throws IOException {
    Dataset ds;

    // Excel file? Handle separately!
    String fileName = f.getName().toLowerCase();
    if (fileName.endsWith(".xlsx") || fileName.endsWith(".xls")) {
        ds = new ExcelImporter(f).asDataset(0);
    } else if (fileName.endsWith(".csv") || fileName.endsWith(".tsv")) {
        CSVFormat csvFormat = CSVFormat.DEFAULT;
        if (fileName.endsWith(".tsv"))
            csvFormat = CSVFormat.TDF.withQuote(null); // We need this to load the AmphibiaWeb files.

        ds = Dataset.fromCSV(csvFormat, f);
    } else {
        // Text-based file? Try using the first line to figure out what's going on.
        String firstLine;
        try (LineNumberReader r = new LineNumberReader(new FileReader(f))) {
            // Load the first line to try to identify the file type.
            firstLine = r.readLine();
        }

        // The most basic type of file is a TaxDiff file, which always
        // begins with:
        if (ChecklistDiff.pTaxDiffFirstLine.matcher(firstLine).matches()) {
            // Note that checklist diffs don't need name extractors!
            return ChecklistDiff.fromTaxDiffFile(f);
        }

        // If all else fails, try loading it as a checklist. Also don't need name extractors!
        return Checklist.fromListInFile(f);
    }

    // If we're here, we need name extractors.

    // Try all name extractors, see which one matches the most names.
    Set<List<NameExtractor>> allAvailableNameExtractors = proj.getNameExtractors();
    allAvailableNameExtractors.add(NameExtractorFactory.getDefaultExtractors());

    LOGGER.info("Starting name extractor comparisons");
    List<NameExtractor> bestExtractor = null;
    long bestExtractorCount = Long.MIN_VALUE;
    for (List<NameExtractor> extractor : allAvailableNameExtractors) {
        long count = ds.rows.stream()
                .flatMap(row -> NameExtractorFactory.extractNamesUsingExtractors(extractor, row).stream())
                .distinct().count();

        if (count > bestExtractorCount) {
            bestExtractorCount = count;
            bestExtractor = extractor;
        }
    }
    LOGGER.info("Finished name extractor comparisons: best extractor at " + bestExtractorCount + " names was "
            + NameExtractorFactory.serializeExtractorsToString(bestExtractor));

    try {
        ds.setNameExtractorsString(NameExtractorFactory.serializeExtractorsToString(bestExtractor));
    } catch (NameExtractorParseException ex) {
        // Forget about it. We'll go with the default.
    }

    return ds;
}

From source file:org.agnitas.web.ImportWizardForm.java

/**
 * Tries to read csv file Reads database column structure reads first line
 * splits line into tokens//w ww .  j av a2  s . co  m
 */
protected ActionErrors parseFirstline(HttpServletRequest req) {
    ApplicationContext aContext = this.getWebApplicationContext();
    DataSource ds = (DataSource) aContext.getBean("dataSource");
    String csvString = new String("");
    String firstline = null;
    ActionErrors errors = new ActionErrors();
    int colNum = 0;

    // try to read csv file:
    try {
        csvString = new String(this.getCsvFile().getFileData(), status.getCharset());
    } catch (Exception e) {
        AgnUtils.logger().error("parseFirstline: " + e);
        errors.add("global", new ActionMessage("error.import.charset"));
        return errors;
    }

    if (csvString.length() == 0) {
        errors.add("global", new ActionMessage("error.import.no_file"));
    }

    // read out DB column structure:
    this.readDBColumns(this.getCompanyID(req), ds);
    this.csvAllColumns = new ArrayList();
    LineNumberReader aReader = new LineNumberReader(new StringReader(csvString));

    try {
        // read first line:
        if ((firstline = aReader.readLine()) != null) {
            aReader.close(); // 

            // split line into tokens:
            CsvTokenizer st = new CsvTokenizer(firstline, status.getSeparator(), status.getDelimiter());
            String curr = "";
            CsvColInfo aCol = null;
            List<String> tempList = new ArrayList<String>();
            while ((curr = st.nextToken()) != null) {

                // curr = (String)st.nextElement();
                curr = curr.trim();
                curr = curr.toLowerCase();
                aCol = new CsvColInfo();
                aCol.setName(curr);
                aCol.setActive(false);
                aCol.setType(CsvColInfo.TYPE_UNKNOWN);

                // add column to csvAllColumns:
                if (!tempList.contains(aCol.getName())) {
                    tempList.add(aCol.getName());
                } else {
                    errors.add("global", new ActionMessage("error.import.column"));
                }
                csvAllColumns.add(aCol);
                colNum++;
                this.csvMaxUsedColumn = colNum;
            }
        }

    } catch (Exception e) {
        AgnUtils.logger().error("parseFirstline: " + e);
    }

    return errors;
}

From source file:com.netscape.cms.logging.LogFile.java

/**
 * This method actually does the logging, and is not overridden
 * by subclasses, so you can call it and know that it will do exactly
 * what you see below.//  w ww. jav  a  2 s . c  o  m
 */
private synchronized void doLog(ILogEvent event, boolean noFlush) throws ELogException {

    String entry = logEvt2String(event);

    if (mLogWriter == null) {
        String[] params = { mFileName, entry };

        if (mLogSigning) {
            ConsoleError.send(new SystemEvent(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params)));
            // Failed to write to audit log, shut down CMS
            shutdownCMS();
        }
        throw new ELogException(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params));
    } else {
        try {
            mLogWriter.write(entry, 0/*offset*/, entry.length());

            if (mLogSigning == true) {
                if (mSignature != null) {
                    // include newline for calculating MAC
                    mSignature.update(entry.getBytes("UTF-8"));
                } else {
                    CMS.debug("LogFile: mSignature is not yet ready... null in log()");
                }
            }
            if (mTrace) {
                CharArrayWriter cw = new CharArrayWriter(200);
                PrintWriter pw = new PrintWriter(cw);
                Exception e = new Exception();
                e.printStackTrace(pw);
                char[] c = cw.toCharArray();
                cw.close();
                pw.close();

                CharArrayReader cr = new CharArrayReader(c);
                LineNumberReader lr = new LineNumberReader(cr);

                String text = null;
                String method = null;
                String fileAndLine = null;
                if (lr.ready()) {
                    text = lr.readLine();
                    do {
                        text = lr.readLine();
                    } while (text.indexOf("logging") != -1);
                    int p = text.indexOf("(");
                    fileAndLine = text.substring(p);

                    String classandmethod = text.substring(0, p);
                    int q = classandmethod.lastIndexOf(".");
                    method = classandmethod.substring(q + 1);
                    mLogWriter.write(fileAndLine, 0/*offset*/, fileAndLine.length());
                    mLogWriter.write(" ", 0/*offset*/, " ".length());
                    mLogWriter.write(method, 0/*offset*/, method.length());
                }
            }
            mLogWriter.newLine();

            if (mLogSigning == true) {
                if (mSignature != null) {
                    mSignature.update(LINE_SEP_BYTE);
                } else {
                    CMS.debug("LogFile: mSignature is null in log() 2");
                }
            }
        } catch (IOException e) {
            ConsoleError.send(new SystemEvent(
                    CMS.getUserMessage("CMS_LOG_WRITE_FAILED", mFileName, entry, e.toString())));
            if (mLogSigning) {
                // Failed to write to audit log, shut down CMS
                e.printStackTrace();
                shutdownCMS();
            }
        } catch (IllegalStateException e) {
            CMS.debug("LogFile: exception thrown in log(): " + e.toString());
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, e.toString())));
        } catch (GeneralSecurityException gse) {
            // DJN: handle error
            CMS.debug("LogFile: exception thrown in log(): " + gse.toString());
            gse.printStackTrace();
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, gse.toString())));
        } catch (Exception ee) { // Make darn sure we got everything
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, ee.toString())));
            if (mLogSigning) {
                // Failed to write to audit log, shut down CMS
                ee.printStackTrace();
                shutdownCMS();
            }

        }

        // XXX
        // Although length will be in Unicode dual-bytes, the PrintWriter
        // will only print out 1 byte per character.  I suppose this could
        // be dependent on the encoding of your log file, but it ain't that
        // smart yet.  Also, add one for the newline. (hmm, on NT, CR+LF)
        int nBytes = entry.length() + 1;

        mBytesWritten += nBytes;
        mBytesUnflushed += nBytes;

        if (mBufferSize > 0 && mBytesUnflushed > mBufferSize && !noFlush) {
            flush();
        }
    }
}

From source file:org.apache.maven.doxia.siterenderer.DefaultSiteRenderer.java

/** {@inheritDoc} */
public void copyResources(SiteRenderingContext siteRenderingContext, File outputDirectory) throws IOException {
    if (siteRenderingContext.getSkin() != null) {
        ZipFile file = getZipFile(siteRenderingContext.getSkin().getFile());

        try {//from   ww  w .  jav a 2  s .  co  m
            for (Enumeration<? extends ZipEntry> e = file.entries(); e.hasMoreElements();) {
                ZipEntry entry = e.nextElement();

                if (!entry.getName().startsWith("META-INF/")) {
                    File destFile = new File(outputDirectory, entry.getName());
                    if (!entry.isDirectory()) {
                        if (destFile.exists()) {
                            // don't override existing content: avoids extra rewrite with same content or extra site
                            // resource
                            continue;
                        }

                        destFile.getParentFile().mkdirs();

                        copyFileFromZip(file, entry, destFile);
                    } else {
                        destFile.mkdirs();
                    }
                }
            }
        } finally {
            closeZipFile(file);
        }
    }

    if (siteRenderingContext.isUsingDefaultTemplate()) {
        InputStream resourceList = getClass().getClassLoader()
                .getResourceAsStream(RESOURCE_DIR + "/resources.txt");

        if (resourceList != null) {
            Reader r = null;
            LineNumberReader reader = null;
            try {
                r = ReaderFactory.newReader(resourceList, ReaderFactory.UTF_8);
                reader = new LineNumberReader(r);

                String line;

                while ((line = reader.readLine()) != null) {
                    if (line.startsWith("#") || line.trim().length() == 0) {
                        continue;
                    }

                    InputStream is = getClass().getClassLoader().getResourceAsStream(RESOURCE_DIR + "/" + line);

                    if (is == null) {
                        throw new IOException("The resource " + line + " doesn't exist.");
                    }

                    File outputFile = new File(outputDirectory, line);

                    if (outputFile.exists()) {
                        // don't override existing content: avoids extra rewrite with same content or extra site
                        // resource
                        continue;
                    }

                    if (!outputFile.getParentFile().exists()) {
                        outputFile.getParentFile().mkdirs();
                    }

                    OutputStream os = null;
                    try {
                        // for the images
                        os = new FileOutputStream(outputFile);
                        IOUtil.copy(is, os);
                    } finally {
                        IOUtil.close(os);
                    }

                    IOUtil.close(is);
                }
            } finally {
                IOUtil.close(reader);
                IOUtil.close(r);
            }
        }
    }

    // Copy extra site resources
    for (File siteDirectory : siteRenderingContext.getSiteDirectories()) {
        File resourcesDirectory = new File(siteDirectory, "resources");

        if (resourcesDirectory != null && resourcesDirectory.exists()) {
            copyDirectory(resourcesDirectory, outputDirectory);
        }
    }

    // Check for the existence of /css/site.css
    File siteCssFile = new File(outputDirectory, "/css/site.css");
    if (!siteCssFile.exists()) {
        // Create the subdirectory css if it doesn't exist, DOXIA-151
        File cssDirectory = new File(outputDirectory, "/css/");
        boolean created = cssDirectory.mkdirs();
        if (created && getLogger().isDebugEnabled()) {
            getLogger().debug(
                    "The directory '" + cssDirectory.getAbsolutePath() + "' did not exist. It was created.");
        }

        // If the file is not there - create an empty file, DOXIA-86
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(
                    "The file '" + siteCssFile.getAbsolutePath() + "' does not exist. Creating an empty file.");
        }
        Writer writer = null;
        try {
            writer = WriterFactory.newWriter(siteCssFile, siteRenderingContext.getOutputEncoding());
            //DOXIA-290...the file should not be 0 bytes.
            writer.write("/* You can override this file with your own styles */");
        } finally {
            IOUtil.close(writer);
        }
    }
}

From source file:org.talend.designer.runprocess.java.JavaProcessor.java

/**
 * Find line numbers of the beginning of the code of process nodes.
 * /*from w w  w. j a  v a  2s  . c  o  m*/
 * @param file Code file where we are searching node's code.
 * @param nodes List of nodes searched.
 * @return Line numbers where code of nodes appears.
 * @throws CoreException Search failed.
 */
private static int[] getLineNumbers(IFile file, String[] nodes) throws CoreException {
    List<Integer> lineNumbers = new ArrayList<Integer>();

    // List of code's lines searched in the file
    List<String> searchedLines = new ArrayList<String>();
    for (String node : nodes) {
        searchedLines.add(node);
    }

    LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(file.getContents()));
    try {
        String line = lineReader.readLine();
        while (!searchedLines.isEmpty() && line != null) {
            boolean nodeFound = false;
            for (Iterator<String> i = searchedLines.iterator(); !nodeFound && i.hasNext();) {
                String nodeMain = i.next();
                if (line.indexOf(nodeMain) != -1) {
                    nodeFound = true;
                    i.remove();

                    // Search the first valid code line
                    boolean lineCodeFound = false;
                    line = lineReader.readLine();
                    while (line != null && !lineCodeFound) {
                        if (isCodeLine(line)) {
                            lineCodeFound = true;
                            lineNumbers.add(new Integer(lineReader.getLineNumber() + 1));
                        }
                        line = lineReader.readLine();
                    }
                }
            }
            line = lineReader.readLine();
        }
    } catch (IOException ioe) {
        IStatus status = new Status(IStatus.ERROR, "", IStatus.OK, "Source code read failure.", ioe); //$NON-NLS-1$ //$NON-NLS-2$
        throw new CoreException(status);
    }

    int[] res = new int[lineNumbers.size()];
    int pos = 0;
    for (Integer i : lineNumbers) {
        res[pos++] = i.intValue();
    }
    return res;
}