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