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