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