KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
7 import net.nutch.html.Entities;
8
9 /** A document summary dynamically generated to match a query. */
10 public class Summary {
11
12   /** A fragment of text within a summary. */
13   public static class Fragment {
14     private String JavaDoc text;
15
16     /** Constructs a fragment for the given text. */
17     public Fragment(String JavaDoc text) { this.text = text; }
18
19     /** Returns the text of this fragment. */
20     public String JavaDoc getText() { return text; }
21
22     /** Returns true iff this fragment is to be highlighted. */
23     public boolean isHighlight() { return false; }
24
25     /** Returns true iff this fragment is an ellipsis. */
26     public boolean isEllipsis() { return false; }
27
28     /** Returns an HTML representation of this fragment. */
29     public String JavaDoc toString() { return Entities.encode(text); }
30   }
31
32   /** A highlighted fragment of text within a summary. */
33   public static class Highlight extends Fragment {
34     /** Constructs a highlighted fragment for the given text. */
35     public Highlight(String JavaDoc text) { super(text); }
36
37     /** Returns true. */
38     public boolean isHighlight() { return true; }
39
40     /** Returns an HTML representation of this fragment. */
41     public String JavaDoc toString() { return "<b>" + super.toString() + "</b>"; }
42   }
43
44   /** An ellipsis fragment within a summary. */
45   public static class Ellipsis extends Fragment {
46     /** Constructs an ellipsis fragment for the given text. */
47     public Ellipsis() { super(" ... "); }
48
49     /** Returns true. */
50     public boolean isEllipsis() { return true; }
51
52     /** Returns an HTML representation of this fragment. */
53     public String JavaDoc toString() { return "<b> ... </b>"; }
54   }
55
56   private ArrayList JavaDoc fragments = new ArrayList JavaDoc();
57
58   private static final Fragment[] FRAGMENT_PROTO = new Fragment[0];
59
60   /** Constructs an empty Summary.*/
61   public Summary() {}
62
63   /** Adds a fragment to a summary.*/
64   public void add(Fragment fragment) { fragments.add(fragment); }
65
66   /** Returns an array of all of this summary's fragments.*/
67   public Fragment[] getFragments() {
68     return (Fragment[])fragments.toArray(FRAGMENT_PROTO);
69   }
70
71   /** Returns an HTML representation of this fragment. */
72   public String JavaDoc toString() {
73     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
74     for (int i = 0; i < fragments.size(); i++) {
75       buffer.append(fragments.get(i));
76     }
77     return buffer.toString();
78   }
79 }
80
Popular Tags