1 2 3 4 package net.nutch.searcher; 5 6 import java.io.DataInput ; 7 import java.io.DataOutput ; 8 import java.io.IOException ; 9 import java.util.logging.Logger ; 10 11 import net.nutch.io.*; 12 import net.nutch.html.Entities; 13 import net.nutch.util.LogFormatter; 14 15 19 public final class HitDetails implements Writable { 20 private static final Logger LOG = 21 LogFormatter.getLogger("net.nutch.searcher.HitDetails"); 22 23 private int length; 24 private String [] fields; 25 private String [] values; 26 27 public HitDetails() {} 28 29 30 public HitDetails(String [] fields, String [] values) { 31 this.length = fields.length; 32 this.fields = fields; 33 this.values = values; 34 } 35 36 37 public HitDetails(String segment, String docNo) { 38 this(new String [2], new String [2]); 39 this.fields[0] = "segment"; 40 this.values[0] = segment; 41 this.fields[1] = "docNo"; 42 this.values[1] = docNo; 43 } 44 45 46 public int getLength() { return length; } 47 48 49 public String getField(int i) { return fields[i]; } 50 51 52 public String getValue(int i) { return values[i]; } 53 54 55 public String getValue(String 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 public void write(DataOutput out) throws IOException { 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 73 public static HitDetails read(DataInput in) throws IOException { 74 HitDetails result = new HitDetails(); 75 result.readFields(in); 76 return result; 77 } 78 79 public void readFields(DataInput in) throws IOException { 81 length = in.readInt(); 82 fields = new String [length]; 83 values = new String [length]; 84 for (int i = 0; i < length; i++) { 85 fields[i] = in.readUTF(); 86 values[i] = in.readUTF(); 87 } 88 } 89 90 91 public String toString() { 92 return getValue("segment") + "/" + getValue("docNo"); 93 } 94 95 96 public String toHtml() { 97 StringBuffer buffer = new StringBuffer (); 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 |