Summary.java revision 457
349N/A/**
349N/A * Copyright 2005 The Apache Software Foundation
349N/A *
349N/A * Licensed under the Apache License, Version 2.0 (the "License");
349N/A * you may not use this file except in compliance with the License.
349N/A * You may obtain a copy of the License at
349N/A *
349N/A * http://www.apache.org/licenses/LICENSE-2.0
349N/A *
349N/A * Unless required by applicable law or agreed to in writing, software
349N/A * distributed under the License is distributed on an "AS IS" BASIS,
349N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
349N/A * See the License for the specific language governing permissions and
349N/A * limitations under the License.
349N/A */
349N/Apackage org.opensolaris.opengrok.search;
349N/A
349N/Aimport java.util.ArrayList;
349N/Aimport java.util.List;
349N/A
349N/A/** A document summary dynamically generated to match a query. */
349N/Apublic class Summary {
349N/A
349N/A public static String htmlize(String q) {
349N/A StringBuilder sb = new StringBuilder(q.length() * 2);
349N/A char c;
350N/A for(int i=0; i < q.length() ; i++) {
350N/A c = q.charAt(i);
350N/A if (c == '&') {
350N/A sb.append("&amp;");
350N/A } else if(c == '>') {
350N/A sb.append("&gt;");
349N/A } else if(c == '<') {
349N/A sb.append("&lt;");
349N/A } else {
349N/A sb.append(c);
349N/A }
349N/A }
349N/A return sb.toString();
350N/A }
349N/A
349N/A /** A fragment of text within a summary. */
349N/A public static class Fragment {
349N/A private final String text;
349N/A
349N/A /** Constructs a fragment for the given text. */
349N/A public Fragment(String text) { this.text = text; }
349N/A
349N/A /** Returns the text of this fragment. */
349N/A public String getText() { return text; }
349N/A
349N/A /** Returns true iff this fragment is to be highlighted. */
349N/A public boolean isHighlight() { return false; }
349N/A
349N/A /** Returns true iff this fragment is an ellipsis. */
349N/A public boolean isEllipsis() { return false; }
349N/A
349N/A /** Returns an HTML representation of this fragment. */
349N/A public String toString() { return htmlize(text); }
349N/A }
349N/A
349N/A /** A highlighted fragment of text within a summary. */
349N/A public static class Highlight extends Fragment {
349N/A /** Constructs a highlighted fragment for the given text. */
349N/A public Highlight(String text) { super(text); }
349N/A
349N/A /** Returns true. */
349N/A public boolean isHighlight() { return true; }
349N/A
349N/A /** Returns an HTML representation of this fragment. */
349N/A public String toString() { return "<b>" + super.toString() + "</b>"; }
349N/A }
349N/A
349N/A /** An ellipsis fragment within a summary. */
349N/A public static class Ellipsis extends Fragment {
349N/A /** Constructs an ellipsis fragment for the given text. */
349N/A public Ellipsis() { super(" ... "); }
349N/A
349N/A /** Returns true. */
349N/A public boolean isEllipsis() { return true; }
349N/A
349N/A /** Returns an HTML representation of this fragment. */
349N/A public String toString() { return "<b> ... </b>"; }
349N/A }
349N/A
349N/A private final List<Fragment> fragments = new ArrayList<Fragment>();
349N/A
349N/A private static final Fragment[] FRAGMENT_PROTO = new Fragment[0];
349N/A
349N/A /** Constructs an empty Summary.*/
349N/A public Summary() {}
349N/A
349N/A /** Adds a fragment to a summary.*/
349N/A public void add(Fragment fragment) { fragments.add(fragment); }
349N/A
349N/A /** Returns an array of all of this summary's fragments.*/
349N/A public Fragment[] getFragments() {
349N/A return (Fragment[])fragments.toArray(FRAGMENT_PROTO);
349N/A }
349N/A
349N/A /** Returns an HTML representation of this fragment. */
349N/A public String toString() {
349N/A StringBuffer buffer = new StringBuffer();
349N/A for (int i = 0; i < fragments.size(); i++) {
349N/A buffer.append(fragments.get(i));
349N/A }
349N/A return buffer.toString();
349N/A }
350N/A}
349N/A