Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:org.tolweb.content.preparers.EolContentPreparer.java

private List<String> createReferencesList(MappedPage mpage) {
    List<String> references = new ArrayList<String>();
    String input = mpage.getReferences();

    if (input != null) {
        LineNumberReader reader = null;
        String currentLine;/* ww w.  ja v a 2 s .co m*/
        try {
            reader = new LineNumberReader(new StringReader(input));
            while ((currentLine = reader.readLine()) != null) {
                if (StringUtils.notEmpty(currentLine)) {
                    references.add(currentLine);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
    return references;
}

From source file:edu.stanford.muse.index.Indexer.java

public static Set<String> readStreamAndInternStrings(Reader r) {
    Set<String> result = new LinkedHashSet<String>();
    try {//from   www  . ja v  a 2s  .  co m
        LineNumberReader lnr = new LineNumberReader(r);
        while (true) {
            String word = lnr.readLine();
            if (word == null) {
                lnr.close();
                break;
            }
            word = word.trim();
            if (word.startsWith("#") || word.length() == 0)
                continue;
            word = IndexUtils.canonicalizeMultiWordTerm(word, false); // TOFIX: not really sure if stemming shd be off
            word = InternTable.intern(word);
            result.add(word);
        }
    } catch (IOException e) {
        log.warn("Exception reading reader " + r + ": " + e + Util.stackTrace(e));
    }

    return result;
}

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

/**
 * Executes all commands read from the given input stream.<p>
 * /* w w w. j ava  2  s.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:edu.stanford.muse.index.Indexer.java

public static Set<String> readFileAndInternStrings(String file) {
    Set<String> result = new LinkedHashSet<String>();
    try {//from   w ww  . jav a2 s .co  m
        Reader r = null;
        if (file.toLowerCase().endsWith(".gz"))
            r = new InputStreamReader(new GZIPInputStream(new FileInputStream(file)));
        else
            r = new FileReader(file);

        LineNumberReader lnr = new LineNumberReader(r);
        while (true) {
            String word = lnr.readLine();
            if (word == null) {
                lnr.close();
                break;
            }
            word = word.trim();
            if (word.startsWith("#") || word.length() == 0)
                continue;
            word = IndexUtils.canonicalizeMultiWordTerm(word, false); // TOFIX: not really sure if stemming shd be on
            word = InternTable.intern(word);
            result.add(word);
        }
    } catch (IOException e) {
        log.warn("Exception reading file " + file + ": " + e + Util.stackTrace(e));
    }

    return result;
}

From source file:org.apache.jackrabbit.oak.plugins.blob.MarkSweepGarbageCollector.java

/**
 * Returns the stats related to GC for all repos
 * //  w  w w . ja v  a  2 s . c o m
 * @return a list of GarbageCollectionRepoStats objects
 * @throws Exception
 */
@Override
public List<GarbageCollectionRepoStats> getStats() throws Exception {
    List<GarbageCollectionRepoStats> stats = newArrayList();
    if (SharedDataStoreUtils.isShared(blobStore)) {
        // Get all the references available
        List<DataRecord> refFiles = ((SharedDataStore) blobStore)
                .getAllMetadataRecords(SharedStoreRecordType.REFERENCES.getType());
        Map<String, DataRecord> references = Maps.uniqueIndex(refFiles, new Function<DataRecord, String>() {
            @Override
            public String apply(DataRecord input) {
                return SharedStoreRecordType.REFERENCES.getIdFromName(input.getIdentifier().toString());
            }
        });

        // Get all the markers available
        List<DataRecord> markerFiles = ((SharedDataStore) blobStore)
                .getAllMetadataRecords(SharedStoreRecordType.MARKED_START_MARKER.getType());
        Map<String, DataRecord> markers = Maps.uniqueIndex(markerFiles, new Function<DataRecord, String>() {
            @Override
            public String apply(DataRecord input) {
                return SharedStoreRecordType.MARKED_START_MARKER
                        .getIdFromName(input.getIdentifier().toString());
            }
        });

        // Get all the repositories registered
        List<DataRecord> repoFiles = ((SharedDataStore) blobStore)
                .getAllMetadataRecords(SharedStoreRecordType.REPOSITORY.getType());

        for (DataRecord repoRec : repoFiles) {
            String id = SharedStoreRecordType.REFERENCES.getIdFromName(repoRec.getIdentifier().toString());
            GarbageCollectionRepoStats stat = new GarbageCollectionRepoStats();
            stat.setRepositoryId(id);
            if (id != null && id.equals(repoId)) {
                stat.setLocal(true);
            }

            if (references.containsKey(id)) {
                DataRecord refRec = references.get(id);
                stat.setEndTime(refRec.getLastModified());
                stat.setLength(refRec.getLength());

                if (markers.containsKey(id)) {
                    stat.setStartTime(markers.get(id).getLastModified());
                }

                LineNumberReader reader = null;
                try {
                    reader = new LineNumberReader(new InputStreamReader(refRec.getStream()));
                    while (reader.readLine() != null) {
                    }
                    stat.setNumLines(reader.getLineNumber());
                } finally {
                    Closeables.close(reader, true);
                }
            }
            stats.add(stat);
        }
    }
    return stats;
}

From source file:com.repeatability.pdf.PDFTextStripper.java

/**
 * This method parses the bidi file provided as inputstream.
 * //from   w  ww. ja va2  s  .  c  om
 * @param inputStream - The bidi file as inputstream
 * @throws IOException if any line could not be read by the LineNumberReader
 */
private static void parseBidiFile(InputStream inputStream) throws IOException {
    LineNumberReader rd = new LineNumberReader(new InputStreamReader(inputStream));

    do {
        String s = rd.readLine();
        if (s == null) {
            break;
        }

        int comment = s.indexOf('#'); // ignore comments
        if (comment != -1) {
            s = s.substring(0, comment);
        }

        if (s.length() < 2) {
            continue;
        }

        StringTokenizer st = new StringTokenizer(s, ";");
        int nFields = st.countTokens();
        Character[] fields = new Character[nFields];
        for (int i = 0; i < nFields; i++) {
            fields[i] = (char) Integer.parseInt(st.nextToken().trim(), 16);
        }

        if (fields.length == 2) {
            // initialize the MIRRORING_CHAR_MAP
            MIRRORING_CHAR_MAP.put(fields[0], fields[1]);
        }

    } while (true);
}

From source file:org.opencms.util.CmsRfsFileViewer.java

/**
 * Internally sets the member <code>m_windowPos</code> to the last available 
 * window of <code>m_windowSize</code> windows to let further calls to 
 * <code>{@link #readFilePortion()}</code> display the end of the file. <p> 
 * //from  w w w  . j  a  v  a 2  s.co  m
 * This method is triggered when a new file is chosen 
 * (<code>{@link #setFilePath(String)}</code>) because the amount of lines changes. 
 * This method is also triggered when a different window size is chosen 
 * (<code>{@link #setWindowSize(int)}</code>) because the amount of lines to display change. 
 * 
 * @return the amount of lines in the file to view
 */
private int scrollToFileEnd() {

    int lines = 0;
    if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
        // no scrolling if system not yet fully initialized
    } else {
        LineNumberReader reader = null;
        // shift the window position to the end of the file: this is expensive but OK for ocs logfiles as they 
        // are ltd. to 2 MB
        try {
            reader = new LineNumberReader(
                    new BufferedReader(new InputStreamReader(new FileInputStream(m_filePath))));
            while (reader.readLine() != null) {
                lines++;
            }
            reader.close();
            // if 11.75 windows are available, we don't want to end on window nr. 10 
            int availWindows = (int) Math.ceil((double) lines / (double) m_windowSize);
            // we start with window 0
            m_windowPos = availWindows - 1;
        } catch (IOException ioex) {
            LOG.error("Unable to scroll file " + m_filePath + " to end. Ensure that it exists. ");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Throwable f) {
                    LOG.info("Unable to close reader of file " + m_filePath, f);
                }
            }
        }
    }
    return lines;
}

From source file:de.innovationgate.wgpublisher.WGPDeployer.java

public String preprocessCode(WGTMLModule mod, String code, int codeOffset) throws WGAPIException {

    // First pass. Process preprocessor tags
    try {/*from   ww w .j a v a 2 s . c o m*/
        PPTagsPreProcessor preProcessor = new PPTagsPreProcessor(_core, mod);
        code = WGUtils.strReplace(code, "{%", preProcessor, true);
    } catch (RuntimeException e) {
        LOG.error("Error preprocessing WebTML module " + mod.getDocumentKey(), e);
    }

    // Process <@ preprocessor if enabled
    if (mod.isPreprocess()) {
        try {
            //code = code.replaceAll("@@([\\w|\\$]+)", "<tml:item name=\"$1\"/>");
            PPPreProcessor pppreProcessor = new PPPreProcessor(_core, mod);
            code = WGUtils.strReplace(code, "@{", pppreProcessor, true);
        } catch (RuntimeException e) {
            LOG.error("Error preprocessing WebTML module " + mod.getDocumentKey(), e);
        }
    }

    // Second pass. Process WebTML tags
    LineNumberReader reader = new LineNumberReader(new StringReader(code));
    StringBuffer out = new StringBuffer();
    TMLTagsPreProcessor linePreProcessor = new TMLTagsPreProcessor();
    String line;
    boolean firstLine = true;
    try {
        while ((line = reader.readLine()) != null) {
            if (!firstLine) {
                out.append("\n");
            } else {
                firstLine = false;
            }
            linePreProcessor.setLineNumber(codeOffset + reader.getLineNumber());
            out.append(WGUtils.strReplace(line, "<tml:", linePreProcessor, true));
        }
        code = out.toString();
    } catch (IOException e) {
        LOG.error("Error adding line numbers to WebTML code", e);
    }

    // Third pass. Process WebTML close tags
    code = WGUtils.strReplace(code, "</tml:", new TMLCloseTagsPreProcessor(), true);

    return code;
}

From source file:org.opencms.setup.CmsSetupDb.java

/**
 * Internal method to parse and execute a setup script.<p>
 *
 * @param inputReader an input stream reader on the setup script
 * @param replacers the replacements to perform in the script
 * @param abortOnError if a error occurs this flag indicates if to continue or to abort
 *///from   w  w  w . j a  v  a 2s .  co  m
private void executeSql(Reader inputReader, Map<String, String> replacers, boolean abortOnError) {

    String statement = "";
    LineNumberReader reader = null;
    String line = null;

    // parse the setup script
    try {
        reader = new LineNumberReader(inputReader);

        while (true) {
            line = reader.readLine();
            if (line == null) {
                break;
            }
            StringTokenizer st = new StringTokenizer(line);

            while (st.hasMoreTokens()) {
                String currentToken = st.nextToken();

                // comment! Skip rest of the line
                if (currentToken.startsWith("#")) {
                    break;
                }

                // not to be executed
                if (currentToken.startsWith("prompt")) {
                    break;
                }

                // add token to query
                statement += " " + currentToken;

                // query complete (terminated by ';')
                if (currentToken.endsWith(";")) {
                    // cut of ';' at the end
                    statement = statement.substring(0, (statement.length() - 1));

                    // normal statement, execute it
                    try {
                        if (replacers != null) {
                            statement = replaceTokens(statement, replacers);
                            executeStatement(statement);
                        } else {
                            executeStatement(statement);
                        }
                    } catch (SQLException e) {
                        if (!abortOnError) {
                            if (m_errorLogging) {
                                m_errors.add("Error executing SQL statement: " + statement);
                                m_errors.add(CmsException.getStackTraceAsString(e));
                            }
                        } else {
                            throw e;
                        }
                    }

                    // reset
                    statement = "";
                }
            }

            statement += " \n";
        }
    } catch (SQLException e) {
        if (m_errorLogging) {
            m_errors.add("Error executing SQL statement: " + statement);
            m_errors.add(CmsException.getStackTraceAsString(e));
        }
    } catch (Exception e) {
        if (m_errorLogging) {
            m_errors.add("Error parsing database setup SQL script in line: " + line);
            m_errors.add(CmsException.getStackTraceAsString(e));
        }
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            // noop
        }
    }
}

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

/**
 * Find line numbers of the beginning of the code of process nodes.
 * /*  www  .j av  a2s .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;
}