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