Search in sources :

Example 1 with HistoryEntry

use of org.opensolaris.opengrok.history.HistoryEntry in project OpenGrok by OpenGrok.

the class HistoryContext method getHistoryContext.

/**
     * Writes matching History log entries from 'in' to 'out' or to 'hits'
     * @param in the history to fetch entries from
     * @param out to write matched context
     * @param path path to the file
     * @param hits list of hits
     * @param wcontext web context - beginning of url
     */
private boolean getHistoryContext(History in, String path, Writer out, List<Hit> hits, String wcontext) {
    if ((out == null) == (hits == null)) {
        // none or both are specified, it's a bug.
        throw new IllegalArgumentException("Exactly one of out and hits should be non-null");
    }
    if (m == null) {
        return false;
    }
    int matchedLines = 0;
    Iterator<HistoryEntry> it = in.getHistoryEntries().iterator();
    try {
        HistoryEntry he;
        HistoryEntry nhe = null;
        String nrev;
        while ((it.hasNext() || (nhe != null)) && matchedLines < 10) {
            if (nhe == null) {
                he = it.next();
            } else //nhe is the lookahead revision
            {
                he = nhe;
            }
            String line = he.getLine();
            String rev = he.getRevision();
            if (//this prefetch mechanism is here because of the diff link generation
            it.hasNext()) //this prefetch mechanism is here because of the diff link generation
            {
                nhe = it.next();
            } else // we currently generate the diff to previous revision
            {
                nhe = null;
            }
            if (nhe == null) {
                nrev = null;
            } else {
                nrev = nhe.getRevision();
            }
            tokens.reInit(line);
            String token;
            int matchState;
            int start = -1;
            while ((token = tokens.next()) != null) {
                for (int i = 0; i < m.length; i++) {
                    matchState = m[i].match(token);
                    if (matchState == LineMatcher.MATCHED) {
                        if (start < 0) {
                            start = tokens.getMatchStart();
                        }
                        int end = tokens.getMatchEnd();
                        if (out == null) {
                            StringBuilder sb = new StringBuilder();
                            writeMatch(sb, line, start, end, true, path, wcontext, nrev, rev);
                            hits.add(new Hit(path, sb.toString(), "", false, false));
                        } else {
                            writeMatch(out, line, start, end, false, path, wcontext, nrev, rev);
                        }
                        matchedLines++;
                        break;
                    } else if (matchState == LineMatcher.WAIT) {
                        if (start < 0) {
                            start = tokens.getMatchStart();
                        }
                    } else {
                        start = -1;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Could not get history context for " + path, e);
    }
    return matchedLines > 0;
}
Also used : Hit(org.opensolaris.opengrok.search.Hit) HistoryEntry(org.opensolaris.opengrok.history.HistoryEntry) IOException(java.io.IOException) HistoryException(org.opensolaris.opengrok.history.HistoryException)

Aggregations

IOException (java.io.IOException)1 HistoryEntry (org.opensolaris.opengrok.history.HistoryEntry)1 HistoryException (org.opensolaris.opengrok.history.HistoryException)1 Hit (org.opensolaris.opengrok.search.Hit)1