Results.java revision 456
1252N/A/*
493N/A * CDDL HEADER START
493N/A *
919N/A * The contents of this file are subject to the terms of the
919N/A * Common Development and Distribution License (the "License").
919N/A * You may not use this file except in compliance with the License.
919N/A *
919N/A * See LICENSE.txt included in this distribution for the specific
493N/A * language governing permissions and limitations under the License.
919N/A *
919N/A * When distributing Covered Code, include this CDDL HEADER in each
919N/A * file and include the License file at LICENSE.txt.
493N/A * If applicable, add the following below this CDDL HEADER, with the
919N/A * fields enclosed by brackets "[]" replaced with your own identifying
919N/A * information: Portions Copyright [yyyy] [name of copyright owner]
919N/A *
919N/A * CDDL HEADER END
919N/A */
919N/A
919N/A/*
493N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
493N/A * Use is subject to license terms.
1276N/A */
1276N/Apackage org.opensolaris.opengrok.search;
1276N/A
1276N/Aimport java.io.BufferedReader;
1276N/Aimport java.io.FileReader;
1276N/Aimport java.io.IOException;
822N/Aimport java.io.Reader;
1124N/Aimport java.io.Writer;
1124N/Aimport java.util.ArrayList;
1124N/Aimport java.util.LinkedHashMap;
235N/Aimport java.util.Map;
1276N/Aimport org.apache.lucene.document.Document;
1276N/Aimport org.apache.lucene.document.Fieldable;
1276N/Aimport org.apache.lucene.search.Hits;
1276N/Aimport org.opensolaris.opengrok.analysis.Definitions;
1276N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
1276N/Aimport org.opensolaris.opengrok.search.context.Context;
1276N/Aimport org.opensolaris.opengrok.search.context.HistoryContext;
822N/Aimport org.opensolaris.opengrok.web.EftarFileReader;
822N/Aimport org.opensolaris.opengrok.web.Util;
822N/A
822N/Apublic final class Results {
822N/A
235N/A private Results() {
822N/A // Util class, should not be constructed
1276N/A }
822N/A
822N/A public static void prettyPrintHTML(Hits hits, int start, int end, Writer out,
822N/A Context sourceContext, HistoryContext historyContext,
822N/A Summarizer summer, String urlPrefix,
822N/A String morePrefix,
822N/A String srcRoot,
822N/A String dataRoot,
822N/A EftarFileReader desc) throws IOException, ClassNotFoundException {
822N/A char[] content = new char[1024*8];
822N/A LinkedHashMap<String, ArrayList<Document>> dirHash = new LinkedHashMap<String, ArrayList<Document>>();
822N/A for (int i = start; i < end; i++) {
822N/A Document doc = hits.doc(i);
822N/A String rpath = doc.get("path");
822N/A String parent = rpath.substring(0,rpath.lastIndexOf('/'));
822N/A ArrayList<Document> dirDocs = dirHash.get(parent);
1276N/A if(dirDocs == null) {
1296N/A dirDocs = new ArrayList<Document>();
1276N/A dirHash.put(parent, dirDocs);
1276N/A }
1276N/A dirDocs.add(doc);
1088N/A }
1088N/A
1124N/A for (Map.Entry<String, ArrayList<Document>> entry: dirHash.entrySet()) {
1124N/A String parent = entry.getKey();
1124N/A String tag = (desc != null) ? " - <i>" + desc.get(parent) + "</i>": "";
1088N/A
1088N/A out.write("<tr class=\"dir\"><td colspan=\"2\">&nbsp;&nbsp;<a href=\"");
235N/A out.write(Util.URIEncodePath(urlPrefix + parent));
1296N/A out.write("/\">" + parent + "/</a>" + tag + "</td></tr>");
1252N/A
1088N/A boolean alt = false;
1124N/A for (Document doc: entry.getValue()) {
1088N/A String rpath = doc.get("path");
1088N/A String self = rpath.substring(rpath.lastIndexOf('/')+1, rpath.length());
1088N/A String selfUrl = Util.URIEncodePath(urlPrefix + rpath);
235N/A out.write("<tr ");
1088N/A if(alt) {
235N/A out.write(" class=\"alt\"");
1088N/A }
822N/A alt ^= true;
1252N/A out.write("><td class=\"f\"><a href=\"" +
822N/A selfUrl + "\">"+self+"</a>&nbsp;</td><td><tt class=\"con\">");
606N/A if (sourceContext != null) {
606N/A String genre = doc.get("t");
1252N/A Definitions tags = null;
235N/A Fieldable tagsField = doc.getFieldable("tags");
606N/A if (tagsField != null) {
1088N/A tags = Definitions.deserialize(tagsField.binaryValue());
1296N/A }
1296N/A try {
1296N/A if ("p".equals(genre) && srcRoot != null) {
1296N/A sourceContext.getContext(new FileReader(srcRoot + rpath), out, urlPrefix, morePrefix, rpath,
1296N/A tags, true, null);
1088N/A } else if("x".equals(genre) && dataRoot != null && summer != null){
1088N/A Reader r = new TagFilter(new BufferedReader(new FileReader(dataRoot + "/xref" + rpath)));
1088N/A int len = r.read(content);
1088N/A out.write(summer.getSummary(new String(content, 0, len)).toString());
1088N/A r.close();
1088N/A } else if("h".equals(genre) && srcRoot != null && summer != null){
1088N/A Reader r = new TagFilter(new BufferedReader(new FileReader(srcRoot + rpath)));
1088N/A int len = r.read(content);
1296N/A out.write(summer.getSummary(new String(content, 0, len)).toString());
1296N/A r.close();
1296N/A } else {
1296N/A sourceContext.getContext(null, out, urlPrefix, morePrefix, rpath, tags, true, null);
1296N/A }
1296N/A } catch (IOException e) {
606N/A
1276N/A }
606N/A //out.write("Genre = " + genre);
606N/A }
606N/A if(historyContext != null) {
1088N/A historyContext.getContext(srcRoot + parent, self, rpath, out);
1276N/A }
606N/A out.write("</tt></td></tr>\n");
606N/A }
1276N/A }
1276N/A }
1276N/A}
1276N/A