1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  * pmHelpRepository.java
29  * Database of help articles
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import java.util.*;
37 
38 import com.sun.admin.pm.server.*;
39 
40 
41 
42 /*
43  * The help repository manages three distinct databases.
44  *
45  * helpItemDB:      String tag -> pmHelpItem
46  *      Returns a pmHelpItem given its unique tag.
47  *      Used to resolve a reference from the app.
48  *
49  * helpKeywordDB:   String -> Vector(of pmHelpItems)
50  *      Returns a Vector containing all pmHelpItems whose `keywords'
51  *      property contains the specifed keyword.
52  *
53  * helpTitleDB:     String -> Vector (of pmHelpItems)
54  *      Returns a Vector containing all pmHelpItems whose `title'
55  *      property is a partial match for the specified string.
56  */
57 
58 final class pmHelpRepository  {
59 
60     private static Hashtable helpItemDB = null;
61     private static Hashtable helpKeywordDB = null;
62     private static BST helpTitleDB = null;
63 
64 
65     // database of HelpItems, by tag string
populateHelpItemDB()66     static void populateHelpItemDB() {
67         helpItemDB = new Hashtable();
68         loadHelpItemDB();
69         // Debug.message("HELP:  helpItemDB: " + helpItemDB);
70     }
71 
72     // database of Vectors of HelpItems, by keyword string
populateHelpKeywordDB()73     static void populateHelpKeywordDB() {
74         if (helpItemDB == null)
75             return;
76 
77 	/*
78 	 * Strategy:
79 	 *  for each item
80 	 *    for each keyword
81 	 *	   if kw not in db
82 	 *		add ititem.tag
83 	 *		    add item to keyword entry
84 	 */
85 
86         helpKeywordDB = new Hashtable();
87 
88         Vector v = null;
89         Enumeration items = helpItemDB.elements();
90         while (items.hasMoreElements()) {
91             pmHelpItem item = (pmHelpItem) items.nextElement();
92             Enumeration keywords = item.keywords.elements();
93             while (keywords.hasMoreElements()) {
94                 String keyword = (String) keywords.nextElement();
95                 v = (Vector) helpKeywordDB.get(keyword);
96                 if (v == null)
97                     helpKeywordDB.put(keyword, v = new Vector());
98                 v.addElement(item);
99             }
100         }
101 
102         // Debug.message("HELP:  KeywordDB: " + helpKeywordDB);
103     }
104 
105 
106     // database of HelpItems, by (partial) title string
populateHelpTitleDB()107     static void populateHelpTitleDB() {
108         if (helpItemDB == null)
109             return;
110 
111 	/*
112 	 * strategy:
113 	 *   assume itemDB is loaded
114 	 *   for each item in itemDB
115 	 *	create an entry in titleDB
116 	 */
117 
118         helpTitleDB = new BST();
119 
120         Enumeration items = helpItemDB.elements();
121         while (items.hasMoreElements()) {
122             pmHelpItem item = (pmHelpItem) items.nextElement();
123             helpTitleDB.insert(item.title, item);
124         }
125     }
126 
127 
helpItemForTag(String tag)128     static public pmHelpItem helpItemForTag(String tag) {
129         if (helpItemDB == null || tag == null)
130             return null;
131         return (pmHelpItem) helpItemDB.get(tag);
132     }
133 
helpItemsForKeyword(String keyword)134     static public Vector helpItemsForKeyword(String keyword) {
135         if (helpKeywordDB == null)
136             return null;
137 
138         return (Vector) helpKeywordDB.get(keyword.toLowerCase());
139     }
140 
141 
helpItemsForString(String partialTitle)142     static public Vector helpItemsForString(String partialTitle)
143         throws pmHelpException {
144 
145         Debug.info("HELP:  helpItemsForString: " + partialTitle);
146 
147         if (helpTitleDB == null)
148             return new Vector();
149 
150         Vector v = new Vector();
151         helpTitleDB.traverse_find_vector(v, partialTitle);
152 
153         Debug.info("HELP:  helpItemsForString: vector contains " +
154                    v.size() + " items");
155 
156         return v;
157     }
158 
159 
160 
161 
162     // this should go in utils...
getResource(String key)163     public static String getResource(String key) {
164         String keyvalue = null;
165         ResourceBundle bundle = null;
166 
167         Debug.message("HELP:  getResource(" + key + ")");
168 
169         try {
170             try {
171                 bundle = ResourceBundle.getBundle(
172                     "com.sun.admin.pm.client.pmHelpResources");
173             } catch (MissingResourceException e) {
174                 Debug.fatal("HELP:  Could not load pmHelpResources file");
175             }
176 
177             try {
178                 keyvalue = bundle.getString(key);
179             } catch (MissingResourceException e) {
180                 keyvalue = bundle.getString("Missing:") + key;
181                 Debug.error("HELP:  Missing: " + key);
182             }
183         } catch (Exception other) {
184             Debug.error("HELP:  getResource(" + key + ") : " + other);
185         }
186 
187 	return keyvalue;
188     }
189 
190 
191     // from resources, presumably
loadHelpItemDB()192     static public void loadHelpItemDB() {
193 
194         // Debug.setDebugLevel(new pmHelpRepository(), Debug.ALL);
195 
196 	/*
197 	 * strategy:
198 	 *   for each tag name (from pmHelpTagNameEnumerator):
199 	 *	get the property values from the resource bundle
200 	 */
201 
202         Debug.message("HELP:  Starting help item load");
203 
204         ResourceBundle bundle = null;
205 
206         try {
207             bundle = ResourceBundle.getBundle(
208                 "com.sun.admin.pm.client.pmHelpResources");
209         } catch (MissingResourceException e) {
210             Debug.fatal("HELP:  Could not load pmHelpResources file");
211             return;
212         }
213         Enumeration e = bundle.getKeys();
214         while (e.hasMoreElements()) {
215             String key = (String) e.nextElement();
216             if (key.endsWith(".tag")) {
217                 String tagName = null;
218                 try {
219                     tagName = bundle.getString(key);
220                 } catch (MissingResourceException x) {
221                     Debug.warning("HELP:  Unable to find tag for " + key);
222                     continue;
223                 }
224 
225                 Debug.message("HELP:  Making new item " + tagName);
226 
227                 pmHelpItem item = new pmHelpItem(tagName);
228 
229                 String theTitle = getResource(tagName + ".title");
230                 item.setTitle(theTitle);
231 
232                 item.setContent(new pmHelpContent(
233                 getResource(tagName + ".content")));
234 
235                 Vector v = null;
236                 StringTokenizer st = null;
237 
238                 String s = getResource(tagName + ".seealso");
239                 if (s != null) {
240                     v = new Vector();
241                     st = new StringTokenizer(s);
242                     while (st.hasMoreTokens())
243                         v.addElement(st.nextToken());
244                     item.setSeeAlso(v);
245                 }
246 
247                 v = new Vector();
248                 s = getResource(tagName + ".keywords");
249                 if (s != null) {
250                     st = new StringTokenizer(s);
251                     while (st.hasMoreTokens()) {
252                         String word = st.nextToken();
253                         String quotelessWord = word.replace('\"', ' ');
254                         v.addElement(quotelessWord.trim());
255                     }
256                 } else
257                     Debug.warning("HELP:  Item " + tagName +
258                                   " keywords is empty");
259 
260 
261                 // insert item's title words into its keywords
262                 st = new StringTokenizer(theTitle);
263                 while (st.hasMoreTokens()) {
264                     String word = (st.nextToken()).toLowerCase();
265 
266                     // ignore useless words
267                     if (ignoreKeyTitleWords.indexOf(word) != -1) {
268                         Debug.message("HELP:  ignoring " + word +
269                                       " from " + theTitle);
270                         continue;
271                     }
272 
273                     Debug.message("HELP:  adding " + word +
274                                   " from " + theTitle);
275 
276                     v.addElement(word);
277                 }
278 
279                 item.setKeywords(v);
280 
281 
282                 Debug.message("HELP:  New item: " + item);
283 
284                 helpItemDB.put(item.tag, item);
285             }
286         }
287     }
288 
289 
290     // these words are not to be treated as keywords when they appear in title
291     static final private String
292 		/* JSTYLED */
293 		ignoreKeyTitleWords = pmUtility.getResource("help.ignore.words");
294 }
295