KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > searcher > HitDetails


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.searcher;
5
6 import java.io.DataInput JavaDoc;
7 import java.io.DataOutput JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import net.nutch.io.*;
12 import net.nutch.html.Entities;
13 import net.nutch.util.LogFormatter;
14
15 /** Data stored in the index for a hit.
16  *
17  * <p>Represented as a list of name/value pairs.
18  */

19 public final class HitDetails implements Writable {
20   private static final Logger JavaDoc LOG =
21     LogFormatter.getLogger("net.nutch.searcher.HitDetails");
22
23   private int length;
24   private String JavaDoc[] fields;
25   private String JavaDoc[] values;
26
27   public HitDetails() {}
28
29   /** Construct from field names and values arrays. */
30   public HitDetails(String JavaDoc[] fields, String JavaDoc[] values) {
31     this.length = fields.length;
32     this.fields = fields;
33     this.values = values;
34   }
35
36   /** Construct minimal details from a segment name and document number. */
37   public HitDetails(String JavaDoc segment, String JavaDoc docNo) {
38     this(new String JavaDoc[2], new String JavaDoc[2]);
39     this.fields[0] = "segment";
40     this.values[0] = segment;
41     this.fields[1] = "docNo";
42     this.values[1] = docNo;
43   }
44
45   /** Returns the number of fields contained in this. */
46   public int getLength() { return length; }
47
48   /** Returns the name of the <code>i</code><sup>th</sup> field. */
49   public String JavaDoc getField(int i) { return fields[i]; }
50
51   /** Returns the value of the <code>i</code><sup>th</sup> field. */
52   public String JavaDoc getValue(int i) { return values[i]; }
53   
54   /** Returns the value of the first field with the specified name. */
55   public String JavaDoc getValue(String JavaDoc field) {
56     for (int i = 0; i < length; i++) {
57       if (fields[i].equals(field))
58         return values[i];
59     }
60     return null;
61   }
62
63   // javadoc from Writable
64
public void write(DataOutput JavaDoc out) throws IOException JavaDoc {
65     out.writeInt(length);
66     for (int i = 0; i < length; i++) {
67       out.writeUTF(fields[i]);
68       out.writeUTF(values[i]);
69     }
70   }
71   
72   /** Constructs, reads and returns an instance. */
73   public static HitDetails read(DataInput JavaDoc in) throws IOException JavaDoc {
74     HitDetails result = new HitDetails();
75     result.readFields(in);
76     return result;
77   }
78
79   // javadoc from Writable
80
public void readFields(DataInput JavaDoc in) throws IOException JavaDoc {
81     length = in.readInt();
82     fields = new String JavaDoc[length];
83     values = new String JavaDoc[length];
84     for (int i = 0; i < length; i++) {
85       fields[i] = in.readUTF();
86       values[i] = in.readUTF();
87     }
88   }
89
90   /** Display as a string. */
91   public String JavaDoc toString() {
92     return getValue("segment") + "/" + getValue("docNo");
93   }
94
95   /** Display as HTML. */
96   public String JavaDoc toHtml() {
97     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
98     buffer.append("<ul>\n");
99     for (int i = 0; i < length; i++) {
100       buffer.append("<li>");
101       buffer.append(fields[i]);
102       buffer.append(" = ");
103       buffer.append(Entities.encode(values[i]));
104       buffer.append("</li>\n");
105     }
106     buffer.append("</ul>\n");
107     return buffer.toString();
108   }
109   
110
111
112 }
113
Popular Tags