summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java
diff options
context:
space:
mode:
authoradrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68>2009-08-17 06:24:15 +0000
committeradrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68>2009-08-17 06:24:15 +0000
commit53c043ac18ca8dd9d7f823bf7a399058c305003d (patch)
tree5b8184792065accca1e5a68092d0960db455cda6 /branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java
parenta4f020f2df42181bf54587acddb3b748ff5f698f (diff)
committing domain-search changes from patch tuscany_2552_phillipe_ramalho_08_16_2009.patch
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@804871 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java293
1 files changed, 147 insertions, 146 deletions
diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java
index fd1d0e102b..cd253c4859 100644
--- a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java
+++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/HighlightingUtil.java
@@ -37,151 +37,152 @@ import org.apache.tuscany.sca.domain.search.Result;
* @version $Rev$ $Date$
*/
final public class HighlightingUtil {
-
- public static void highlightResult(Result result, Query query) {
- highlightResult(result, query, new SimpleFragmenter(70));
- }
-
- public static void highlightResult(Result result, Query query, Fragmenter fragmenter) {
- Map<String, Result> contents = result.getContents();
-
- if (contents != null) {
-
- for (Result content : contents.values()) {
- highlightResult(content, query, fragmenter);
- }
-
- }
-
- try {
- String highlightedText = HighlightingUtil.bestFragmentHighlighted(
- result.getField(), query, result.getValue(), fragmenter);
-
- // checks if something was highlighted before resetting the value
- if (highlightedText != null && highlightedText.length() > 0) {
- result.setValue(highlightedText);
- }
-
- } catch (IOException e) {
- // ignore highlighting
- }
-
- }
-
- public static String highlight(String field, Query query, String text) throws IOException {
- String highlightedText = bestFragmentHighlighted(field, query, text, new NullFragmenter());
-
- if (highlightedText == null || text.length() >= highlightedText.length()) {
- return text;
- }
-
- return highlightedText;
-
- }
-
- public static String bestFragmentHighlighted(String field, Query query, String text) throws IOException {
- return bestFragmentHighlighted(field, query, text, new SimpleFragmenter(100));
- }
-
- public static String bestFragmentHighlighted(String field, Query query, String text, Fragmenter fragmenter) throws IOException {
- CachingTokenFilter tokenStream = new CachingTokenFilter(new DomainSearchAnalyzer().tokenStream(
- field, new StringReader(text)));
-
- Highlighter highlighter = new Highlighter(new DomainSearchFormatter(), new SpanScorer(query, field, tokenStream, ""));
- highlighter.setTextFragmenter(fragmenter);
- tokenStream.reset();
-
- try {
- return highlighter.getBestFragments(tokenStream, text, 2, " ... ");
-
- } catch (InvalidTokenOffsetsException e) {
-
- // could not create fragments, return empty string
- return "";
-
- }
-
- }
-
- public static String replaceHighlightMarkupBy(CharSequence text,
- String startHighlight, String endHighlight) {
- StringBuilder sb = new StringBuilder();
- int start = 0;
- int end = 0;
-
- for (int i = 0; i < text.length(); i++) {
- char c = text.charAt(i);
-
- if (start > 0) {
-
- if (c == DomainSearchFormatter.HIGHLIGHT_START.charAt(start)) {
- start++;
-
- if (start == DomainSearchFormatter.HIGHLIGHT_START.length()) {
- sb.append(startHighlight);
- start = 0;
-
- }
-
- } else {
-
- for (int j = 0; j < start; j++) {
- sb.append(DomainSearchFormatter.HIGHLIGHT_START.charAt(j));
- }
-
- start = 0;
-
- }
-
- } else if (end > 0) {
-
- if (c == DomainSearchFormatter.HIGHLIGHT_END.charAt(end)) {
- end++;
-
- if (end == DomainSearchFormatter.HIGHLIGHT_END.length()) {
- sb.append(endHighlight);
- end = 0;
-
- }
-
- } else {
-
- for (int j = 0; j < end; j++) {
- sb.append(DomainSearchFormatter.HIGHLIGHT_END.charAt(j));
- }
-
- end = 0;
-
- }
-
- } else if (c == DomainSearchFormatter.HIGHLIGHT_START.charAt(0)) {
- start = 1;
-
- } else if (c == DomainSearchFormatter.HIGHLIGHT_END.charAt(0)) {
- end = 1;
-
- } else {
- sb.append(c);
- }
-
- }
-
- if (start > 0) {
-
- for (int j = 0; j < start; j++) {
- sb.append(DomainSearchFormatter.HIGHLIGHT_START.charAt(j));
- }
-
- } else if (end > 0) {
-
- for (int j = 0; j < start; j++) {
- sb.append(DomainSearchFormatter.HIGHLIGHT_END.charAt(j));
- }
-
- }
-
- return sb.toString();
-
- }
+
+ public static void highlightResult(Result result, Query query) {
+ highlightResult(result, query, new SimpleFragmenter(70));
+ }
+
+ public static void highlightResult(Result result, Query query, Fragmenter fragmenter) {
+ Map<String, Result> contents = result.getContents();
+
+ if (contents != null) {
+
+ for (Result content : contents.values()) {
+ highlightResult(content, query, fragmenter);
+ }
+
+ }
+
+ try {
+ String highlightedText =
+ HighlightingUtil.bestFragmentHighlighted(result.getField(), query, result.getValue(), fragmenter);
+
+ // checks if something was highlighted before resetting the value
+ if (highlightedText != null && highlightedText.length() > 0) {
+ result.setValue(highlightedText);
+ }
+
+ } catch (IOException e) {
+ // ignore highlighting
+ }
+
+ }
+
+ public static String highlight(String field, Query query, String text) throws IOException {
+ String highlightedText = bestFragmentHighlighted(field, query, text, new NullFragmenter());
+
+ if (highlightedText == null || text.length() >= highlightedText.length()) {
+ return text;
+ }
+
+ return highlightedText;
+
+ }
+
+ public static String bestFragmentHighlighted(String field, Query query, String text) throws IOException {
+ return bestFragmentHighlighted(field, query, text, new SimpleFragmenter(100));
+ }
+
+ public static String bestFragmentHighlighted(String field, Query query, String text, Fragmenter fragmenter)
+ throws IOException {
+ CachingTokenFilter tokenStream =
+ new CachingTokenFilter(new DomainSearchAnalyzer().tokenStream(field, new StringReader(text)));
+
+ Highlighter highlighter =
+ new Highlighter(new DomainSearchFormatter(), new SpanScorer(query, field, tokenStream, ""));
+ highlighter.setTextFragmenter(fragmenter);
+ tokenStream.reset();
+
+ try {
+ return highlighter.getBestFragments(tokenStream, text, 2, " ... ");
+
+ } catch (InvalidTokenOffsetsException e) {
+
+ // could not create fragments, return empty string
+ return "";
+
+ }
+
+ }
+
+ public static String replaceHighlightMarkupBy(CharSequence text, String startHighlight, String endHighlight) {
+ StringBuilder sb = new StringBuilder();
+ int start = 0;
+ int end = 0;
+
+ for (int i = 0; i < text.length(); i++) {
+ char c = text.charAt(i);
+
+ if (start > 0) {
+
+ if (c == DomainSearchFormatter.HIGHLIGHT_START.charAt(start)) {
+ start++;
+
+ if (start == DomainSearchFormatter.HIGHLIGHT_START.length()) {
+ sb.append(startHighlight);
+ start = 0;
+
+ }
+
+ } else {
+
+ for (int j = 0; j < start; j++) {
+ sb.append(DomainSearchFormatter.HIGHLIGHT_START.charAt(j));
+ }
+
+ start = 0;
+
+ }
+
+ } else if (end > 0) {
+
+ if (c == DomainSearchFormatter.HIGHLIGHT_END.charAt(end)) {
+ end++;
+
+ if (end == DomainSearchFormatter.HIGHLIGHT_END.length()) {
+ sb.append(endHighlight);
+ end = 0;
+
+ }
+
+ } else {
+
+ for (int j = 0; j < end; j++) {
+ sb.append(DomainSearchFormatter.HIGHLIGHT_END.charAt(j));
+ }
+
+ end = 0;
+
+ }
+
+ } else if (c == DomainSearchFormatter.HIGHLIGHT_START.charAt(0)) {
+ start = 1;
+
+ } else if (c == DomainSearchFormatter.HIGHLIGHT_END.charAt(0)) {
+ end = 1;
+
+ } else {
+ sb.append(c);
+ }
+
+ }
+
+ if (start > 0) {
+
+ for (int j = 0; j < start; j++) {
+ sb.append(DomainSearchFormatter.HIGHLIGHT_START.charAt(j));
+ }
+
+ } else if (end > 0) {
+
+ for (int j = 0; j < start; j++) {
+ sb.append(DomainSearchFormatter.HIGHLIGHT_END.charAt(j));
+ }
+
+ }
+
+ return sb.toString();
+
+ }
}