Results.java revision 1390
306N/A/*
306N/A * CDDL HEADER START
810N/A *
810N/A * The contents of this file are subject to the terms of the
306N/A * Common Development and Distribution License (the "License").
306N/A * You may not use this file except in compliance with the License.
306N/A *
306N/A * See LICENSE.txt included in this distribution for the specific
306N/A * language governing permissions and limitations under the License.
306N/A *
306N/A * When distributing Covered Code, include this CDDL HEADER in each
306N/A * file and include the License file at LICENSE.txt.
306N/A * If applicable, add the following below this CDDL HEADER, with the
306N/A * fields enclosed by brackets "[]" replaced with your own identifying
306N/A * information: Portions Copyright [yyyy] [name of copyright owner]
306N/A *
306N/A * CDDL HEADER END
306N/A */
306N/A/*
306N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
306N/A *
306N/A * Portions Copyright 2011 Jens Elkner.
306N/A */
306N/Apackage org.opensolaris.opengrok.search;
306N/A
306N/Aimport java.io.BufferedReader;
306N/Aimport java.io.File;
306N/Aimport java.io.FileReader;
306N/Aimport java.io.IOException;
306N/Aimport java.io.Reader;
306N/Aimport java.io.Writer;
493N/Aimport java.util.ArrayList;
493N/Aimport java.util.LinkedHashMap;
306N/Aimport java.util.Map;
493N/Aimport java.util.logging.Level;
306N/Aimport java.util.logging.Logger;
493N/A
493N/Aimport org.apache.lucene.document.Document;
810N/Aimport org.apache.lucene.document.Fieldable;
810N/Aimport org.apache.lucene.index.CorruptIndexException;
810N/Aimport org.apache.lucene.search.IndexSearcher;
493N/Aimport org.apache.lucene.search.ScoreDoc;
493N/Aimport org.opensolaris.opengrok.analysis.Definitions;
493N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer.Genre;
493N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
493N/Aimport org.opensolaris.opengrok.analysis.XrefReader;
306N/Aimport org.opensolaris.opengrok.history.HistoryException;
306N/Aimport org.opensolaris.opengrok.util.IOUtils;
493N/Aimport org.opensolaris.opengrok.web.Prefix;
306N/Aimport org.opensolaris.opengrok.web.SearchHelper;
306N/Aimport org.opensolaris.opengrok.web.Util;
561N/A
306N/A/**
306N/A * @author Chandan slightly rewritten by Lubos Kosco
306N/A */
306N/Apublic final class Results {
306N/A private static final Logger logger = Logger.getLogger(Results.class.getName());
306N/A private Results() {
636N/A // Util class, should not be constructed
636N/A }
636N/A
636N/A /**
636N/A * Create a has map keyed by the directory of the document found.
636N/A * @param searcher searcher to use.
715N/A * @param hits hits produced by the given searcher's search
715N/A * @param startIdx the index of the first hit to check
* @param stopIdx the index of the last hit to check
* @return a (directory, hitDocument) hashmap
* @throws CorruptIndexException
* @throws IOException
*/
private static Map<String, ArrayList<Document>>
createMap(IndexSearcher searcher, ScoreDoc[] hits, int startIdx, int stopIdx)
throws CorruptIndexException, IOException
{
LinkedHashMap<String, ArrayList<Document>> dirHash =
new LinkedHashMap<String, ArrayList<Document>>();
for (int i = startIdx; i < stopIdx; i++ ) {
int docId = hits[i].doc;
Document doc = searcher.doc(docId);
String rpath = doc.get("path");
String parent = rpath.substring(0, rpath.lastIndexOf('/'));
ArrayList<Document> dirDocs = dirHash.get(parent);
if (dirDocs == null) {
dirDocs = new ArrayList<Document>();
dirHash.put(parent, dirDocs);
}
dirDocs.add(doc);
}
return dirHash;
}
private static String getTags(File basedir, String path, boolean isXref) {
char[] content = new char[1024 * 8];
Reader r = null;
// Grrrrrrrrrrrrr - TagFilter takes Readers, only!!!!
// Why? Is it CS sensible?
File file = new File(basedir, path);
try {
if (isXref) {
r = new TagFilter(new XrefReader(file));
} else {
r = new TagFilter(new BufferedReader(new FileReader(file)));
}
int len = r.read(content);
return new String(content, 0, len);
} catch (Exception e) {
logger.warning("An error reading tags from '" + file + "': "
+ e.getMessage());
logger.log(Level.FINE, "getTags", e);
} finally {
IOUtils.close(r);
}
return "";
}
/**
* Prints out results in html form. The following search helper fields are
* required to be properly initialized:
* <ul>
* <li>{@link SearchHelper#dataRoot}</li>
* <li>{@link SearchHelper#contextPath}</li>
* <li>{@link SearchHelper#searcher}</li>
* <li>{@link SearchHelper#hits}</li>
* <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
* <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
* <li>{@link SearchHelper#summerizer} (if sourceContext is not {@code null})</li>
* <li>{@link SearchHelper#sourceRoot} (if sourceContext or historyContext
* is not {@code null})</li>
* </ul>
*
* @param out write destination
* @param sh search helper which has all required fields set
* @param start index of the first hit to print
* @param end index of the last hit to print
* @throws HistoryException
* @throws IOException
* @throws ClassNotFoundException
*/
public static void prettyPrint(Writer out, SearchHelper sh, int start,
int end)
throws HistoryException, IOException, ClassNotFoundException
{
String ctxE = Util.URIEncodePath(sh.contextPath);
String xrefPrefix = sh.contextPath + Prefix.XREF_P;
String morePrefix = sh.contextPath + Prefix.MORE_P;
String xrefPrefixE = ctxE + Prefix.XREF_P;
String histPrefixE = ctxE + Prefix.HIST_L;
String rawPrefixE = ctxE + Prefix.RAW_P;
File xrefDataDir = new File(sh.dataRoot, Prefix.XREF_P.toString());
for (Map.Entry<String, ArrayList<Document>> entry :
createMap(sh.searcher, sh.hits, start, end).entrySet())
{
String parent = entry.getKey();
out.write("<tr class=\"rsd\"><td colspan=\"3\" class=\"rsdl\"><a href=\"");
out.write(xrefPrefixE);
out.write(Util.URIEncodePath(parent));
out.write("/\">");
out.write(parent); // htmlize ???
out.write("/</a>");
if (sh.desc != null) {
out.write(" - <span class=\"rsdd\">");
out.write(sh.desc.get(parent)); // htmlize ???
out.write("</span>");
}
out.write("</td></tr>");
for (Document doc : entry.getValue()) {
String rpath = doc.get("path");
String rpathE = Util.URIEncodePath(rpath);
out.write("<tr><td class=\"rsq\"><a href=\"");
out.write(histPrefixE);
out.write(rpathE);
out.write("\" title=\"History\">H</a> <a href=\"");
out.write(xrefPrefixE);
out.write(rpathE);
out.write("?a=true\" title=\"Annotate\">A</a> <a href=\"");
out.write(rawPrefixE);
out.write(rpathE);
out.write("\" title=\"Download\">D</a>");
out.write("</td>");
out.write("<td class=\"rsf\"><a href=\"");
out.write(xrefPrefixE);
out.write(rpathE);
out.write("\">");
out.write(rpath.substring(rpath.lastIndexOf('/') + 1)); // htmlize ???
out.write("</a></td><td><tt class=\"rscon\">");
if (sh.sourceContext != null) {
Genre genre = Genre.get(doc.get("t"));
Definitions tags = null;
Fieldable tagsField = doc.getFieldable("tags");
if (tagsField != null) {
tags = Definitions.deserialize(tagsField.getBinaryValue());
}
if (Genre.XREFABLE == genre && sh.summerizer != null) {
String xtags = getTags(xrefDataDir, rpath, true);
// FIXME use Highlighter from lucene contrib here,
// instead of summarizer, we'd also get rid of
// apache lucene in whole source ...
out.write(sh.summerizer.getSummary(xtags).toString());
} else if (Genre.HTML == genre && sh.summerizer != null) {
String htags = getTags(sh.sourceRoot, rpath, false);
out.write(sh.summerizer.getSummary(htags).toString());
} else {
FileReader r = genre == Genre.PLAIN
? new FileReader(new File(sh.sourceRoot, rpath))
: null;
sh.sourceContext.getContext(r, out, xrefPrefix,
morePrefix, rpath, tags, true, null);
}
}
if (sh.historyContext != null) {
sh.historyContext.getContext(new File(sh.sourceRoot, rpath),
rpath, out, sh.contextPath);
}
out.write("</tt></td></tr>\n");
}
}
}
}