001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.db;
007:
008: import com.sun.portal.search.rdm.*;
009: import com.sun.portal.search.soif.*;
010: import com.sun.portal.search.util.*;
011:
012: import com.google.soap.search.*;
013:
014: import java.util.*;
015:
016: /**
017: *
018: * Google search result set implementation using RDMResultSet
019: *
020: */
021: public class GoogleResultSet extends RDMResultSet {
022:
023: /**
024: * Creates a new instance of GoogleResultSet
025: */
026: GoogleResultSet(SToken st, RDMDb database, String query,
027: RDMTransaction t) {
028: super (st, database, query, t);
029: }
030:
031: /**
032: * Prepare Google Search Results
033: */
034: void setGoogleSearchResult(GoogleSearchResult gsr) {
035: this .gsr = gsr;
036: startHit = gsr.getStartIndex();
037: endHit = gsr.getEndIndex();
038: hitCount = gsr.getEstimatedTotalResultsCount();
039: resultCount = endHit - startHit + 1;
040: docCount = 2000000000;
041: }
042:
043: /**
044: * Add view to RDMResultSet
045: */
046: void setView(Set view) {
047: this .view = view;
048: }
049:
050: /**
051: * Default view attribute set
052: */
053: static Set defaultView = new HashSet();
054: static {
055: defaultView.add("title");
056: defaultView.add("description");
057: defaultView.add("content-length");
058: }
059:
060: static private void configureHighlighting() {
061:
062: // set up default highlighting from search config file
063: String p = SearchConfig.getValue(SearchConfig.HIGHLIGHTS);
064: if (p != null)
065: highlightsEnabled = p.equalsIgnoreCase("true");
066:
067: p = SearchConfig.getValue(SearchConfig.HIGHLIGHT_TAGS);
068: if (p != null) {
069: String[] tags = String2Array.string2Array(p, ',');
070: if (tags != null && tags.length == 6)
071: defaultHighlightTags = tags;
072: }
073:
074: p = SearchConfig.getValue(SearchConfig.HIGHLIGHT_TAGS_FIELD);
075: if (p != null) {
076: String[] tags = String2Array.string2Array(p, ',');
077: if (tags != null && tags.length == 6)
078: defaultFldHighlightTags = tags;
079: }
080:
081: }
082:
083: private void hlinit(String[] hltags) {
084:
085: if (!hlconfigured) {
086: synchronized (this .getClass()) {
087: configureHighlighting();
088: hlconfigured = true;
089: }
090: }
091:
092: if (hlinited)
093: return;
094:
095: highlightTags = (String[]) defaultHighlightTags.clone();
096: fldHighlightTags = (String[]) defaultFldHighlightTags.clone();
097:
098: if (hltags != null && hltags.length >= 6)
099: System.arraycopy(hltags, 0, highlightTags, 0, 6);
100: if (hltags != null && hltags.length == 12)
101: System.arraycopy(hltags, 6, fldHighlightTags, 0, 6);
102: hlinited = true;
103: }
104:
105: /**
106: * Retrieve Search Results
107: * @param i Document index
108: * @param view Attributes
109: * @param hltags Highlight tags
110: * @return a SOIF hit, highlighted and filtered by view
111: */
112: public SOIF getResult(int i, Set view, String[] hltags) {
113: if (hits == null) {
114: hits = gsr.getResultElements();
115: }
116:
117: if (i >= hits.length) {
118: return null;
119: //return null when the document index requested is
120: //greater than the google result count
121: //A temporary solution to avoid ArrayIndexOutOfBound
122: //for Google search returns only 10 documents per search.
123: }
124:
125: GoogleSearchResultElement r = hits[i];
126:
127: hlinit(hltags);
128:
129: SOIF s = new SOIF("DOCUMENT", r.getURL());
130:
131: // insert/map the attributes
132: if (view == null)
133: view = this .view;
134: if (view == null)
135: view = defaultView;
136: if (view.contains("hl-description"))
137: s.insert("hl-description", convertHighlight(r.getSnippet(),
138: true));
139: if (view.contains("description"))
140: s.insert("description", convertHighlight(r.getSnippet(),
141: false));
142: if (view.contains("hl-title"))
143: s.insert("hl-title", convertFieldHighlight(r.getTitle(),
144: true));
145: if (view.contains("title"))
146: s.insert("title",
147: convertFieldHighlight(r.getTitle(), false));
148: if (view.contains("hl-url"))
149: s.insert("hl-url", r.getURL());
150: if (view.contains("content-length"))
151: s.insert("content-length", r.getCachedSize());
152: if (view.contains("score"))
153: s.insert("score", "100");
154: return s;
155:
156: }
157:
158: /**
159: * Get search hit count
160: * @return total number of hits for this search
161: */
162: public long getHitCount() {
163: return hitCount;
164: }
165:
166: /**
167: * Get document count
168: * @return number of docs searched
169: */
170: public long getDocCount() {
171: return docCount;
172: }
173:
174: /**
175: * Transform the result set to string
176: * @return tring representation of this result set
177: */
178: public java.lang.String toString() {
179: return super .toString();
180: }
181:
182: /**
183: * Get search result count
184: * @return current number of hits in this set
185: */
186: public int getResultCount() {
187: return resultCount;
188: }
189:
190: /**
191: * Convert google highlight to portal style highlight
192: * @param s string with highlights to be converted
193: * @param hl true convert highlights, false remove highlights
194: * @return the highlight converted string
195: */
196: public String convertHighlight(String s, boolean hl) {
197: if (highlightsEnabled && hl) {
198: // convert google highlights to RDM highlights
199: // treat each google hit as a passage - could do better...
200: String t1 = s.replaceAll("(?i)<B>\\.\\.\\.</B>", "...");
201: String t2 = t1.replaceAll("(?i)<B>", highlightTags[2]
202: + highlightTags[4]);
203: String t3 = t2.replaceAll("(?i)</B>", highlightTags[5]
204: + highlightTags[3]);
205: String t4 = t3.replaceAll("(?i)<BR>", "");
206: return t4;
207: } else {
208: // strip google highlights
209: String t1 = s.replaceAll("(?i)<B>", "");
210: String t2 = t1.replaceAll("(?i)</B>", "");
211: String t3 = t2.replaceAll("(?i)<BR>", "");
212: return t3;
213: }
214: }
215:
216: /**
217: * Convert google highlight to portal style field highlight
218: * @param s string with highlights to be converted
219: * @param hl true convert highlights, false remove highlights
220: * @return the highlight converted string
221: */
222: public String convertFieldHighlight(String s, boolean hl) {
223: if (highlightsEnabled && hl) {
224: // convert google highlights to RDM highlights
225: String t1 = s.replaceAll("(?i)<B>\\.\\.\\.</B>", "...");
226: String t2 = t1.replaceAll("(?i)<B>", fldHighlightTags[4]);
227: String t3 = t2.replaceAll("(?i)</B>", fldHighlightTags[5]);
228: return t3;
229: } else {
230: // strip google highlights
231: String t1 = s.replaceAll("(?i)<B>", "");
232: String t2 = t1.replaceAll("(?i)</B>", "");
233: return t2;
234: }
235: }
236:
237: private GoogleSearchResult gsr;
238: private GoogleSearchResultElement[] hits;
239: int startHit;
240: int endHit;
241: long hitCount;
242: long docCount;
243: int resultCount;
244: Set view;
245: boolean hlinited = false;
246: String[] highlightTags;
247: String[] fldHighlightTags;
248:
249: // highlighting configuration
250: static boolean hlconfigured = false;
251: static boolean highlightsEnabled = true;
252: static String[] defaultHighlightTags = { "... ", " ...",
253: "<font color=bb0033>", "</font>", "<b>", "</b>" };
254: static String[] defaultFldHighlightTags = { "", "", "", "", "<b>",
255: "</b>" };
256:
257: }
|