001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package soif;
012:
013: import java.util.StringTokenizer;
014:
015: /**
016: Example of a pretty print class for handling a result set.
017: Provides a base class that handles output and manages
018: related services.
019: *
020: */
021: public class Results {
022: private int MAXSCORE = 100;
023: private int NOSCORE = -1;
024:
025: private String[] scoreGif;
026: private boolean[] hasScoreGif;
027:
028: private String displayFields[];
029: private int displayCount;
030: private String displayString;
031: private int displayScore;
032: private boolean displayURL[];
033:
034: /**
035: * Constructor.
036: */
037: public Results() {
038: scoreGif = new String[MAXSCORE + 1];
039: hasScoreGif = new boolean[MAXSCORE + 1];
040: displayFields = null;
041: displayCount = 0;
042: displayString = "";
043: displayScore = NOSCORE;
044: displayURL = null;
045: }
046:
047: /**
048: * Set the display.
049: * @param s comma delimited list of fields to display
050: */
051: public void setDisplay(String s) {
052: StringTokenizer st = new StringTokenizer(s, ",");
053:
054: displayCount = st.countTokens();
055: displayFields = new String[displayCount];
056: displayURL = new boolean[displayCount];
057: displayString = s;
058: displayScore = NOSCORE;
059:
060: for (int i = 0; i < displayCount; i++) {
061: displayURL[i] = false;
062: displayFields[i] = st.nextToken();
063: if (displayFields[i].equalsIgnoreCase("score")) {
064: displayScore = i;
065: }
066: }
067: }
068:
069: /**
070: * Set fields which should be displayed as links (e.g. title).
071: * @param s field
072: * @param b display or not
073: */
074: public void setDisplayAsLink(String s, boolean b) {
075: for (int i = 0; i < displayCount; i++) {
076: if (displayFields[i].equalsIgnoreCase(s)) {
077: displayURL[i] = b;
078: return;
079: }
080: }
081: }
082:
083: /**
084: * Return comma delimited display set.
085: */
086: public String getDisplay() {
087: return displayString;
088: }
089:
090: /**
091: * Set the gif for a range of scores.
092: * @param low low end of the range, inclusive.
093: * @param high high end of the range, inclusive.
094: * @param gif url for gif
095: */
096: public void setScoreGif(int low, int high, String gif) {
097: boolean has = (gif.length() != 0);
098:
099: for (int i = low; i <= high; i++) {
100: hasScoreGif[i] = has;
101: scoreGif[i] = gif;
102: }
103: }
104:
105: /**
106: * Given a score, format it returning both percent and image, if any.
107: * @param s score, range 0.00 <= s <= 1.00
108: */
109: public String toScoreGifHTML(String s) {
110: try {
111: float f = Float.valueOf(s).floatValue();
112: return toScoreGifHTML(f);
113: } catch (NumberFormatException e) {
114: return "";
115: }
116: }
117:
118: /**
119: * Given a score, format it returning both percentage and image, if any.
120: * @param f score, range 0.00 <= f <= 1.00
121: */
122: public String toScoreGifHTML(float f) {
123: int i = (int) (f * 100);
124:
125: if ((i < 0) || (i >= MAXSCORE)) {
126: return "";
127: }
128:
129: if (!hasScoreGif[i]) {
130: return i + "%";
131: }
132:
133: return i + "%<img src=\"" + scoreGif[i] + "\">";
134: }
135:
136: /**
137: * Format result SOIF into HTML.
138: * Handles URL hrefs and score formatting/images.
139: * @param soif target
140: * @param preField HTML string to prepend each field with, e.g. "<td>"
141: * @param postField HTML string to append each field with, e.g. "</td>"
142: */
143: public String resultToHTML(SOIF soif, String preField,
144: String postField) {
145: StringBuffer sb = new StringBuffer();
146: String url = soif.getValue("URL");
147: String s;
148:
149: for (int i = 0; i < displayCount; i++) {
150: sb.append(preField);
151:
152: s = soif.getValue(displayFields[i]);
153: if (s != null) {
154: if (i == displayScore) {
155: s = toScoreGifHTML(s);
156: }
157:
158: if ((url != null) && (displayURL[i])) {
159: sb.append("<a href=\"" + url + "\">" + s + "</a>");
160: } else {
161: sb.append(s);
162: }
163: }
164:
165: sb.append(postField);
166: }
167:
168: return sb.toString();
169: }
170:
171: /**
172: * Format results list SOIF set into HTML.
173: * Handles URL hrefs and score formatting/images.
174: * @param soif target
175: * @param preResult HTML string to prepend each result with, e.g. "<tr>"
176: * @param postResult HTML string to append each result with, e.g. "</tr>"
177: * @param preField HTML string to prepend each field with, e.g. "<td>"
178: * @param postField HTML string to append each field with, e.g. "</td>"
179: */
180: public String resultsToHTML(SOIF soif, String preResult,
181: String postResult, String preField, String postField) {
182: StringBuffer sb = new StringBuffer();
183:
184: for (SOIF i = soif; i != null; i = i.next) {
185: sb.append(preResult);
186: sb.append(resultToHTML(i, preField, postField));
187: sb.append(postResult);
188: }
189:
190: return sb.toString();
191: }
192:
193: public String toString() {
194: StringBuffer sb = new StringBuffer();
195: int i;
196:
197: sb.append("Output instance: (" + Header.VERSION + ")\n"
198: + "\tdisplayString\t= [" + displayString + "]\n"
199: + "\tdisplayCount\t= [" + displayCount + "]\n"
200: + "\tdisplayScore\t= [" + displayScore + "]\n");
201:
202: sb.append("\tdisplayURL[], displayFields[]:\n");
203: for (i = 0; i < displayCount; i++) {
204: sb.append("\t[" + i + "]:\t" + displayURL[i] + "\t"
205: + displayFields[i] + "\n");
206: }
207:
208: sb.append("\thasScoreGif[], scoreGif[]:\n");
209: for (i = 0; i < MAXSCORE; i++) {
210: sb.append("\t[" + i + "]:\t" + hasScoreGif[i] + "\t"
211: + scoreGif[i] + "\n");
212: }
213:
214: return sb.toString();
215: }
216: }
|