595N/A/*
0N/A * Copyright 2005 The Apache Software Foundation
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A */
0N/Apackage org.opensolaris.opengrok.search;
0N/A
0N/Aimport java.util.ArrayList;
457N/Aimport java.util.List;
0N/A
1469N/Aimport org.opensolaris.opengrok.web.Util;
1469N/A
0N/A/** A document summary dynamically generated to match a query. */
0N/Apublic class Summary {
1190N/A
0N/A /** A fragment of text within a summary. */
0N/A public static class Fragment {
456N/A private final String text;
1190N/A
1469N/A /** Constructs a fragment for the given text.
1469N/A * @param text text for this fragment
1469N/A */
0N/A public Fragment(String text) { this.text = text; }
1190N/A
1469N/A /** Get the text of this fragment.
1469N/A * @return the text of this fragment */
0N/A public String getText() { return text; }
1190N/A
1469N/A /** Check whether this fragment is to be highlighted.
1469N/A * @return {@code true} if highlighting is enabled */
1469N/A @SuppressWarnings("static-method")
0N/A public boolean isHighlight() { return false; }
1190N/A
1469N/A /** Check, whether this fragment is an ellipsis.
1469N/A * @return {@code true} if this is an ellipsis */
1469N/A @SuppressWarnings("static-method")
0N/A public boolean isEllipsis() { return false; }
1190N/A
1469N/A /** Get an HTML representation of this fragment.
1469N/A * @return the htmlized text of this fragment */
1469N/A @Override
1469N/A public String toString() { return Util.htmlize(text); }
0N/A }
1190N/A
0N/A /** A highlighted fragment of text within a summary. */
0N/A public static class Highlight extends Fragment {
1469N/A /** Constructs a highlighted fragment for the given text.
1469N/A * @param text text for this fragment
1469N/A */
0N/A public Highlight(String text) { super(text); }
1190N/A
1469N/A /**
1469N/A * {@inheritDoc}
1469N/A */
1469N/A @Override
0N/A public boolean isHighlight() { return true; }
1190N/A
1469N/A /**
1469N/A * {@inheritDoc}
1469N/A */
1469N/A @Override
0N/A public String toString() { return "<b>" + super.toString() + "</b>"; }
0N/A }
1190N/A
0N/A /** An ellipsis fragment within a summary. */
0N/A public static class Ellipsis extends Fragment {
0N/A /** Constructs an ellipsis fragment for the given text. */
0N/A public Ellipsis() { super(" ... "); }
1190N/A
1469N/A /**
1469N/A * {@inheritDoc}
1469N/A */
1469N/A @Override
0N/A public boolean isEllipsis() { return true; }
1190N/A
1469N/A /**
1469N/A * {@inheritDoc}
1469N/A */
1469N/A @Override
0N/A public String toString() { return "<b> ... </b>"; }
0N/A }
1190N/A
457N/A private final List<Fragment> fragments = new ArrayList<Fragment>();
1190N/A
0N/A private static final Fragment[] FRAGMENT_PROTO = new Fragment[0];
1190N/A
1469N/A /** Adds a fragment to a summary.
1469N/A * @param fragment fragment ot add */
0N/A public void add(Fragment fragment) { fragments.add(fragment); }
1190N/A
1469N/A /** Get an array of all of this summary's fragments.
1469N/A * @return a possible empty array */
0N/A public Fragment[] getFragments() {
1469N/A return fragments.toArray(FRAGMENT_PROTO);
0N/A }
1190N/A
1469N/A /** Get concatenation of all HTMLized fragments of this instance. */
1469N/A @Override
0N/A public String toString() {
1185N/A StringBuilder buffer = new StringBuilder();
0N/A for (int i = 0; i < fragments.size(); i++) {
0N/A buffer.append(fragments.get(i));
0N/A }
0N/A return buffer.toString();
0N/A }
0N/A}