Results.java revision 1318
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
407N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A/*
0N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Portions Copyright 2011 Jens Elkner.
0N/A */
0N/Apackage org.opensolaris.opengrok.search;
0N/A
388N/Aimport java.io.BufferedReader;
388N/Aimport java.io.File;
388N/Aimport java.io.FileInputStream;
388N/Aimport java.io.FileReader;
388N/Aimport java.io.IOException;
388N/Aimport java.io.InputStreamReader;
388N/Aimport java.io.Reader;
388N/Aimport java.io.Writer;
388N/Aimport java.util.ArrayList;
388N/Aimport java.util.LinkedHashMap;
388N/Aimport java.util.Map;
350N/Aimport java.util.logging.Level;
0N/Aimport java.util.zip.GZIPInputStream;
388N/A
0N/Aimport org.apache.lucene.document.Document;
388N/Aimport org.apache.lucene.document.Fieldable;
388N/Aimport org.apache.lucene.index.CorruptIndexException;
0N/Aimport org.apache.lucene.search.ScoreDoc;
456N/Aimport org.apache.lucene.search.IndexSearcher;
456N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
456N/Aimport org.opensolaris.opengrok.analysis.Definitions;
456N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer.Genre;
456N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
456N/Aimport org.opensolaris.opengrok.history.HistoryException;
0N/Aimport org.opensolaris.opengrok.util.IOUtils;
0N/Aimport org.opensolaris.opengrok.web.Prefix;
0N/Aimport org.opensolaris.opengrok.web.SearchHelper;
0N/Aimport org.opensolaris.opengrok.web.Util;
0N/A
0N/A/**
350N/A * @author Chandan slightly rewritten by Lubos Kosco
0N/A */
0N/Apublic final class Results {
0N/A private Results() {
0N/A // Util class, should not be constructed
0N/A }
0N/A
0N/A /**
0N/A * Create a has map keyed by the directory of the document found.
0N/A * @param searcher searcher to use.
0N/A * @param hits hits produced by the given searcher's search
0N/A * @param startIdx the index of the first hit to check
0N/A * @param stopIdx the index of the last hit to check
0N/A * @return a (directory, hitDocument) hashmap
0N/A * @throws CorruptIndexException
344N/A * @throws IOException
344N/A */
460N/A private static Map<String, ArrayList<Document>>
460N/A createMap(IndexSearcher searcher, ScoreDoc[] hits, int startIdx, int stopIdx)
151N/A throws CorruptIndexException, IOException
158N/A {
151N/A LinkedHashMap<String, ArrayList<Document>> dirHash =
158N/A new LinkedHashMap<String, ArrayList<Document>>();
0N/A for (int i = startIdx; i < stopIdx; i++ ) {
344N/A int docId = hits[i].doc;
0N/A Document doc = searcher.doc(docId);
0N/A String rpath = doc.get("path");
158N/A String parent = rpath.substring(0, rpath.lastIndexOf('/'));
0N/A ArrayList<Document> dirDocs = dirHash.get(parent);
388N/A if (dirDocs == null) {
0N/A dirDocs = new ArrayList<Document>();
388N/A dirHash.put(parent, dirDocs);
0N/A }
0N/A dirDocs.add(doc);
0N/A }
0N/A return dirHash;
0N/A }
350N/A
350N/A private static String getTags(File basedir, String path, boolean compressed) {
350N/A char[] content = new char[1024 * 8];
350N/A FileInputStream fis = null;
350N/A GZIPInputStream gis = null;
0N/A FileReader fr = null;
0N/A Reader r = null;
0N/A // Grrrrrrrrrrrrr - TagFilter takes Readers, only!!!!
0N/A // Why? Is it CS sensible?
347N/A try {
0N/A if (compressed) {
347N/A fis = new FileInputStream(new File(basedir, path + ".gz"));
0N/A gis = new GZIPInputStream(fis);
372N/A r = new TagFilter(new BufferedReader(new InputStreamReader(gis)));
0N/A } else {
0N/A fr = new FileReader(new File(basedir, path));
347N/A r = new TagFilter(new BufferedReader(fr));
0N/A }
372N/A int len = r.read(content);
0N/A return new String(content, 0, len);
0N/A } catch (Exception e) {
0N/A OpenGrokLogger.getLogger().log(
0N/A Level.WARNING, "An error reading tags from " + basedir + path
0N/A + (compressed ? ".gz" : ""), e);
0N/A } finally {
0N/A IOUtils.close(r);
0N/A IOUtils.close(gis);
0N/A IOUtils.close(fis);
0N/A IOUtils.close(fr);
0N/A }
0N/A return "";
0N/A }
0N/A
0N/A /**
0N/A * 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#compressed} (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=\"dir\"><td colspan=\"3\"><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(" - <i>");
out.write(sh.desc.get(parent)); // htmlize ???
out.write("</i>");
}
out.write("</td></tr>");
for (Document doc : entry.getValue()) {
String rpath = doc.get("path");
String rpathE = Util.URIEncodePath(rpath);
out.write("<tr><td class=\"q\"><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=\"f\"><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=\"con\">");
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, sh.compressed);
// 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");
}
}
}
}