xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/Summary.java (revision ff44f24ac10337e272510634599e287cb25d0e9c)
1 /*
2  * Copyright 2005 The Apache Software Foundation
3  * Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.opengrok.indexer.search;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.opengrok.indexer.web.Util;
22 
23 /** A document summary dynamically generated to match a query. */
24 public class Summary {
25 
htmlize(String q)26     protected static String htmlize(String q) {
27         return Util.prehtmlize(q);
28     }
29 
30     /** A fragment of text within a summary. */
31     public static class Fragment {
32         private final String text;
33 
34         /* Constructs a fragment for the given text. */
Fragment(String text)35         public Fragment(String text) {
36             this.text = text;
37         }
38 
39         /* Returns the text of this fragment. */
getText()40         public String getText() {
41             return text;
42         }
43 
44         /* Returns true iff this fragment is to be highlighted. */
isHighlight()45         public boolean isHighlight() {
46             return false;
47         }
48 
49         /* Returns true iff this fragment is an ellipsis. */
isEllipsis()50         public boolean isEllipsis() {
51             return false;
52         }
53 
54         /* Returns an HTML representation of this fragment. */
55         @Override
toString()56         public String toString() {
57             return htmlize(text);
58         }
59     }
60 
61     /** A highlighted fragment of text within a summary. */
62     public static class Highlight extends Fragment {
63         /* Constructs a highlighted fragment for the given text. */
Highlight(String text)64         public Highlight(String text) {
65             super(text);
66         }
67 
68         /* Returns true. */
69         @Override
isHighlight()70         public boolean isHighlight() {
71             return true;
72         }
73 
74         /* Returns an HTML representation of this fragment. */
75         @Override
toString()76         public String toString() {
77             return "<b>" + super.toString() + "</b>";
78         }
79     }
80 
81     /** An ellipsis fragment within a summary. */
82     public static class Ellipsis extends Fragment {
83         /* Constructs an ellipsis fragment for the given text. */
Ellipsis()84         public Ellipsis() {
85             super(" ... ");
86         }
87 
88         /* Returns true. */
89         @Override
isEllipsis()90         public boolean isEllipsis() {
91             return true;
92         }
93 
94         /* Returns an HTML representation of this fragment. */
95         @Override
toString()96         public String toString() {
97             return "<b> ... </b>";
98         }
99     }
100 
101     private final List<Fragment> fragments = new ArrayList<>();
102 
103     private static final Fragment[] FRAGMENT_PROTO = new Fragment[0];
104 
105     /* Adds a fragment to a summary.*/
add(Fragment fragment)106     public void add(Fragment fragment) {
107         fragments.add(fragment);
108     }
109 
110     /**
111      * Returns an array of all of this summary's fragments.
112      * @return fragment array
113      */
getFragments()114     public Fragment[] getFragments() {
115         return fragments.toArray(FRAGMENT_PROTO);
116     }
117 
118     /**
119      * Returns an HTML representation of this fragment.
120      * @return string representation
121      */
122     @Override
toString()123     public String toString() {
124         StringBuilder buffer = new StringBuilder();
125         for (Fragment fragment : fragments) {
126             buffer.append(fragment);
127         }
128         return buffer.toString();
129     }
130 }
131