Results.java revision 460
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
1237N/A/*
1190N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
1185N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.search;
0N/A
388N/Aimport java.io.BufferedReader;
1185N/Aimport java.io.FileReader;
816N/Aimport java.io.IOException;
388N/Aimport java.io.Reader;
388N/Aimport java.io.Writer;
816N/Aimport java.util.ArrayList;
388N/Aimport java.util.LinkedHashMap;
388N/Aimport java.util.Map;
388N/Aimport org.apache.lucene.document.Document;
388N/Aimport org.apache.lucene.document.Fieldable;
388N/Aimport org.apache.lucene.search.Hits;
508N/Aimport org.opensolaris.opengrok.analysis.Definitions;
816N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
1185N/Aimport org.opensolaris.opengrok.search.context.Context;
388N/Aimport org.opensolaris.opengrok.search.context.HistoryContext;
388N/Aimport org.opensolaris.opengrok.web.EftarFileReader;
1185N/Aimport org.opensolaris.opengrok.web.Util;
819N/A
1318N/Apublic final class Results {
508N/A
350N/A private Results() {
1185N/A // Util class, should not be constructed
0N/A }
615N/A
1195N/A public static void prettyPrintHTML(Hits hits, int start, int end, Writer out,
1185N/A Context sourceContext, HistoryContext historyContext,
1185N/A Summarizer summer, String urlPrefix,
388N/A String morePrefix,
0N/A String srcRoot,
1111N/A String dataRoot,
1185N/A EftarFileReader desc) throws IOException, ClassNotFoundException {
1111N/A char[] content = new char[1024*8];
456N/A LinkedHashMap<String, ArrayList<Document>> dirHash = new LinkedHashMap<String, ArrayList<Document>>();
456N/A for (int i = start; i < end; i++) {
456N/A Document doc = hits.doc(i);
456N/A String rpath = doc.get("path");
1185N/A String parent = rpath.substring(0,rpath.lastIndexOf('/'));
1111N/A ArrayList<Document> dirDocs = dirHash.get(parent);
1185N/A if(dirDocs == null) {
1185N/A dirDocs = new ArrayList<Document>();
1185N/A dirHash.put(parent, dirDocs);
1185N/A }
1185N/A dirDocs.add(doc);
1185N/A }
1185N/A
1111N/A for (Map.Entry<String, ArrayList<Document>> entry: dirHash.entrySet()) {
1111N/A String parent = entry.getKey();
1237N/A String tag = (desc == null) ? "" : " - <i>" + desc.get(parent) + "</i>";
1318N/A
1190N/A out.write("<tr class=\"dir\"><td colspan=\"2\">&nbsp;&nbsp;<a href=\"");
615N/A out.write(Util.URIEncodePath(urlPrefix + parent));
1190N/A out.write("/\">" + parent + "/</a>" + tag + "</td></tr>");
1185N/A
1185N/A boolean alt = false;
1185N/A for (Document doc: entry.getValue()) {
819N/A String rpath = doc.get("path");
0N/A String self = rpath.substring(rpath.lastIndexOf('/')+1, rpath.length());
1185N/A String selfUrl = Util.URIEncodePath(urlPrefix + rpath);
0N/A out.write("<tr ");
1185N/A if(alt) {
0N/A out.write(" class=\"alt\"");
0N/A }
0N/A alt ^= true;
0N/A out.write("><td class=\"f\"><a href=\"" +
0N/A selfUrl + "\">"+self+"</a>&nbsp;</td><td><tt class=\"con\">");
1185N/A if (sourceContext != null) {
1185N/A String genre = doc.get("t");
1190N/A Definitions tags = null;
1185N/A Fieldable tagsField = doc.getFieldable("tags");
1185N/A if (tagsField != null) {
1185N/A tags = Definitions.deserialize(tagsField.binaryValue());
1185N/A }
1185N/A try {
1185N/A if ("p".equals(genre) && srcRoot != null) {
1185N/A sourceContext.getContext(new FileReader(srcRoot + rpath), out, urlPrefix, morePrefix, rpath,
1185N/A tags, true, null);
1185N/A } else if("x".equals(genre) && dataRoot != null && summer != null){
1185N/A Reader r = new TagFilter(new BufferedReader(new FileReader(dataRoot + "/xref" + rpath)));
1185N/A int len = r.read(content);
1185N/A out.write(summer.getSummary(new String(content, 0, len)).toString());
1185N/A r.close();
1185N/A } else if("h".equals(genre) && srcRoot != null && summer != null){
1185N/A Reader r = new TagFilter(new BufferedReader(new FileReader(srcRoot + rpath)));
1185N/A int len = r.read(content);
1185N/A out.write(summer.getSummary(new String(content, 0, len)).toString());
1185N/A r.close();
1185N/A } else {
1185N/A sourceContext.getContext(null, out, urlPrefix, morePrefix, rpath, tags, true, null);
1185N/A }
1185N/A } catch (IOException e) {
1185N/A
1185N/A }
1195N/A //out.write("Genre = " + genre);
1195N/A }
1195N/A if(historyContext != null) {
1195N/A historyContext.getContext(srcRoot + parent, self, rpath, out);
1185N/A }
1185N/A out.write("</tt></td></tr>\n");
1185N/A }
460N/A }
1185N/A }
1185N/A}
1185N/A