View Javadoc

1   package com.atlassian.bonnie.search.summary;
2   
3   import java.util.ArrayList;
4   
5   /**
6    * Originally from org.apache.nutch.searcher.Summary v 0.7 (Revision: <a href="http://svn.apache.org/viewcvs.cgi/lucene/nutch/trunk/src/java/org/apache/nutch/searcher/Summary.java?rev=179640&view=markup">179640</a>)
7    * <p />
8    * A document summary dynamically generated to match a query.
9    */
10  public class Summary {
11  
12    /** A fragment of text within a summary. */
13    public static class Fragment {
14      private String text;
15  
16      /** Constructs a fragment for the given text. */
17      public Fragment(String text) { this.text = text; }
18  
19      /** Returns the text of this fragment. */
20      public String 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 toString() { return text; }
30  
31        public boolean equals(Object o)
32        {
33            if (this == o) return true;
34            if (o == null || getClass() != o.getClass()) return false;
35  
36            final Fragment fragment = (Fragment) o;
37  
38            if (!text.equals(fragment.text)) return false;
39            if (isHighlight() != fragment.isHighlight()) return false;
40            if (isEllipsis() != fragment.isEllipsis()) return false;
41  
42            return true;
43        }
44  
45        public int hashCode()
46        {
47            return text.hashCode();
48        }
49    }
50  
51    /** A highlighted fragment of text within a summary. */
52    public static class Highlight extends Fragment {
53      /** Constructs a highlighted fragment for the given text. */
54      public Highlight(String text) { super(text); }
55  
56      /** Returns true. */
57      public boolean isHighlight() { return true; }
58    }
59  
60    /** An ellipsis fragment within a summary. */
61    public static class Ellipsis extends Fragment {
62      /** Constructs an ellipsis fragment for the given text. */
63      public Ellipsis() { super(" ... "); }
64  
65      /** Returns true. */
66      public boolean isEllipsis() { return true; }
67  
68      /** Returns an HTML representation of this fragment. */
69      public String toString() { return " ... "; }
70    }
71  
72    private ArrayList fragments = new ArrayList();
73  
74    private static final Fragment[] FRAGMENT_PROTO = new Fragment[0];
75  
76    /** Constructs an empty Summary.*/
77    public Summary() {}
78  
79    /** Adds a fragment to a summary.*/
80    public void add(Fragment fragment) { fragments.add(fragment); }
81  
82    /** Returns an array of all of this summary's fragments.*/
83    public Fragment[] getFragments() {
84      return (Fragment[])fragments.toArray(FRAGMENT_PROTO);
85    }
86  
87    /** Returns an HTML representation of this fragment. */
88    public String toString() {
89      StringBuffer buffer = new StringBuffer();
90      for (int i = 0; i < fragments.size(); i++) {
91        buffer.append(fragments.get(i));
92      }
93      return buffer.toString();
94    }
95  
96  
97  }