KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > server > uihandler > TipOfTheDay


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.server.uihandler;
20
21 import org.netbeans.server.uihandler.statistics.*;
22 import org.netbeans.server.uihandler.*;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Queue JavaDoc;
34 import java.util.Random JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38 import javax.xml.parsers.SAXParser JavaDoc;
39 import javax.xml.parsers.SAXParserFactory JavaDoc;
40 import org.openide.util.Exceptions;
41 import org.openide.util.RequestProcessor;
42 import org.xml.sax.Attributes JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44 import org.xml.sax.helpers.DefaultHandler JavaDoc;
45
46 /** Class that provides access to tip of the day functionality provided
47  * by docs team. It is created with URL refering to the page with XML data
48  * and is refereshed very hour.
49  *
50  * @author Jaroslav Tulach
51  */

52 public final class TipOfTheDay implements Runnable JavaDoc {
53     private static final Logger JavaDoc LOG = Logger.getLogger(TipOfTheDay.class.getName());
54     private static RequestProcessor RP = new RequestProcessor("Refresh TipOfTheDay");
55     
56     private Map JavaDoc<String JavaDoc,List JavaDoc<Tip>> tips;
57     private final URL JavaDoc url;
58     private RequestProcessor.Task refresh;
59     
60     private TipOfTheDay(URL JavaDoc url) {
61         this.url = url;
62         if (url == null) {
63             tips = Collections.emptyMap();
64             return;
65         }
66         tips = Collections.emptyMap();
67
68         refresh = RP.create(this);
69         refresh.schedule(0);
70         
71         try {
72             refresh.waitFinished(10000);
73         } catch (InterruptedException JavaDoc ex) {
74             LOG.log(Level.WARNING, ex.getMessage(), ex);
75         }
76     }
77     
78     /** Parses content provided by some URL and create the TipOfTheDay database.
79      * @param url url to read the TipOfTheDay from
80      */

81     public static TipOfTheDay create(URL JavaDoc url) {
82         return new TipOfTheDay(url);
83     }
84     
85     private static TipOfTheDay DEFAULT;
86     /** Gets default tip of the day.
87      */

88     public static TipOfTheDay getDefault() {
89         if (DEFAULT == null) {
90             String JavaDoc tips = Utils.getVariable("tipsOfTheDay", String JavaDoc.class);
91             if (tips != null) {
92                 try {
93                     DEFAULT = new TipOfTheDay(new URL JavaDoc(tips));
94                 } catch (MalformedURLException JavaDoc ex) {
95                     LOG.log(Level.WARNING, ex.getMessage(), ex);
96                 }
97             }
98             if (DEFAULT == null) {
99                 return new TipOfTheDay(null);
100             }
101         }
102         return DEFAULT;
103     }
104
105     /** Refreshes the content of the databases. Re-reads the content of
106      * the provided URL and updates internal structures.
107      */

108     public void run() {
109         LOG.info("Refreshing content of TipOfTheDay: " + url); // NOI18N
110
try {
111             Parser JavaDoc p = new Parser JavaDoc();
112             tips = p.parse(url);
113             return;
114         } catch (SAXException JavaDoc ex) {
115             LOG.log(Level.WARNING, ex.getMessage(), ex);
116         } catch (ParserConfigurationException JavaDoc ex) {
117             LOG.log(Level.WARNING, ex.getMessage(), ex);
118         } catch (IOException JavaDoc ex) {
119             LOG.log(Level.WARNING, ex.getMessage(), ex);
120         } finally {
121             LOG.info("Done refreshing of TipOfTheDay"); // NOI18N
122
refresh.schedule(60 * 1000 * 60);
123         }
124     }
125     
126     /** Finds appropriate tip of the date for given usage of projects.
127      * @param cnts collected info from ProjectTypes
128      * @return randomly selected typ
129      */

130     public Tip find(ProjectTypes.Counts cnts) {
131         List JavaDoc<? extends Tip> all = findAll(cnts);
132         if (all.isEmpty()) {
133             return null;
134         }
135         int r = new Random JavaDoc().nextInt(all.size());
136         return all.get(r);
137     }
138     
139     /** Finds all tips appropriate as tip of the date for given usage of projects.
140      */

141     public List JavaDoc<? extends Tip> findAll(ProjectTypes.Counts cnts) {
142         int max = -1;
143         List JavaDoc<Tip> found = Collections.emptyList();
144         if (cnts == null) {
145             return found;
146         }
147         
148         for (Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc> entry : cnts.getUsages()) {
149             if (entry.getValue() > max) {
150                 List JavaDoc<Tip> f = tips.get(entry.getKey());
151                 if (f != null) {
152                     max = entry.getValue();
153                     found = f;
154                 }
155             }
156         }
157         return found;
158     }
159     
160         
161     static String JavaDoc findProject(String JavaDoc category) throws SAXException JavaDoc {
162         String JavaDoc ret = null;
163         if ("Web and Enterprise Development".equals(category)) { // NOI18N
164
ret = "Ear"; // NOI18N
165
}
166         if ("NetBeans Platform Development".equals(category)) { // NOI18N
167
ret = "NbModule"; // NOI18N
168
}
169         if ("Swing GUI Development".equals(category)) { // NOI18N
170
ret = "j2se"; // NOI18N
171
}
172         if ("Monitoring and Profiling".equals(category)) { // NOI18N
173
ret = "profiler";
174         }
175         if ("Mobile Application Development".equals(category)) { // NOI18N
176
ret = "J2ME"; // NOI18N
177
}
178         if ("Advanced and Miscellaneous".equals(category)) { // NOI18N
179
ret = "J2SE"; // NOI18N
180
}
181         if ("Basic IDE Functionality".equals(category)) { // NOI18N
182
ret = "J2SE"; // NOI18N
183
}
184         
185         if (ret == null) {
186             throw new SAXException JavaDoc("Unexpected category: " + category);
187         }
188         return ret;
189     }
190     
191     
192     /** Represents info about one tip.
193      */

194     public static final class Tip {
195         String JavaDoc category;
196         String JavaDoc description;
197         String JavaDoc url;
198         String JavaDoc title;
199         
200         public String JavaDoc getDescription() {
201             return description;
202         }
203         
204         public String JavaDoc getUrl() {
205             return url;
206         }
207         
208         public String JavaDoc getTitle() {
209             return title;
210         }
211     } // end of Tip
212

213     
214     /* parser for
215     <article>
216         <date>2007-03-05 00:00:00</date>
217         <title>NetBeans IDE 6.0 GUI Builder Demo</title>
218         <description>In this preview the NetBeans IDE 6.0, you will see how much easier it is to develop Java desktop applications with the improved GUI Builder. You'll see the tools which take advantage of both the Swing Application framework(JSR 296) and Beans Binding(JSR 295). Check it out!</description>
219         <category>Swing GUI Development</category>
220         <url>http://www.netbeans.org/download/flash/netbeans_6_gui_builder/netbeans_6_gui_builder.html</url>
221     </article>
222      */

223     private static final class Parser extends DefaultHandler JavaDoc {
224         private Map JavaDoc<String JavaDoc,List JavaDoc<Tip>> tips = new HashMap JavaDoc<String JavaDoc, List JavaDoc<TipOfTheDay.Tip>>();
225         private Tip current;
226         private Queue JavaDoc<StringBuilder JavaDoc> values = new LinkedList JavaDoc<StringBuilder JavaDoc>();
227         
228         public Map JavaDoc<String JavaDoc,List JavaDoc<Tip>> parse(URL JavaDoc url) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
229             SAXParserFactory JavaDoc f = SAXParserFactory.newInstance();
230             SAXParser JavaDoc p = f.newSAXParser();
231             InputStream JavaDoc is = url.openStream();
232             p.parse(is, this);
233             is.close();
234             return tips;
235         }
236         
237         @Override JavaDoc
238         public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName, Attributes JavaDoc args) throws SAXException JavaDoc {
239             if (LOG.isLoggable(Level.FINEST)) {
240                 LOG.finest("startElement uri: " + uri + " local: " + local + " qName: " + qName); // NOI18N
241
}
242             if (qName.equals("article")) {
243                 assert current == null;
244                 current = new Tip();
245             }
246             
247             values.add(new StringBuilder JavaDoc());
248         }
249         
250         @Override JavaDoc
251         public void characters(char[] characters, int from, int len) throws SAXException JavaDoc {
252             assert !values.isEmpty();
253             
254             StringBuilder JavaDoc value = values.peek();
255             if (LOG.isLoggable(Level.FINEST)) {
256                 LOG.finest("characters: " + new String JavaDoc(characters, from, len)); // NOI18N
257
}
258             value.append(characters, from, len);
259         }
260
261         
262         
263         @Override JavaDoc
264         public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName) throws SAXException JavaDoc {
265             assert !values.isEmpty();
266             
267             StringBuilder JavaDoc value = values.poll();
268             for (int i = 0; i < value.length(); i++) {
269                 if (!Character.isWhitespace(value.charAt(i))) {
270                     value.delete(0, i);
271                     break;
272                 }
273             }
274             for (int i = value.length() - 1; i > 0; i--) {
275                 if (!Character.isWhitespace(value.charAt(i))) {
276                     value.delete(i + 1, value.length());
277                     break;
278                 }
279             }
280             
281             if (LOG.isLoggable(Level.FINEST)) {
282                 LOG.finest("endElement uri: " + uri + " local: " + local + " qName: " + qName); // NOI18N
283
}
284             if (qName.equals("article")) { // NOI18N
285
assert current != null;
286                 String JavaDoc prj = findProject(current.category);
287                 List JavaDoc<Tip> arr = tips.get(prj);
288                 if (arr == null) {
289                     arr = new ArrayList JavaDoc<Tip>();
290                     tips.put(prj, arr);
291                 }
292                 arr.add(current);
293                 current = null;
294                 return;
295             }
296             
297             if (qName.equals("description")) { // NOI18N
298
assert current != null;
299                 assert value != null;
300                 current.description = value.toString();
301             }
302             
303             if (qName.equals("title")) { // NOI18N
304
assert current != null;
305                 assert value != null;
306                 current.title = value.toString();
307             }
308
309             if (qName.equals("url")) { // NOI18N
310
assert current != null;
311                 assert value != null;
312                 current.url = value.toString();
313             }
314
315             if (qName.equals("category")) { // NOI18N
316
assert current != null;
317                 assert value != null;
318                 current.category = value.toString();
319             }
320         }
321     } // end of Parser
322
}
323
Popular Tags