File Source: UrlCatcherPlugin.java
/*
P/P * Method: com.dmdirc.addons.urlcatcher.UrlCatcherPlugin__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.urlcatcher;
24
25 import com.dmdirc.actions.ActionManager;
26 import com.dmdirc.actions.interfaces.ActionType;
27 import com.dmdirc.actions.CoreActionType;
28 import com.dmdirc.commandparser.CommandManager;
29 import com.dmdirc.interfaces.ActionListener;
30 import com.dmdirc.plugins.Plugin;
31 import com.dmdirc.ui.messages.Styliser;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36 /**
37 *
38 * @author chris
39 */
/*
P/P * Method: void com.dmdirc.addons.urlcatcher.UrlCatcherPlugin()
*
* Postconditions:
* this.command == &new UrlListCommand(UrlCatcherPlugin#2)
* this.urls == &new HashMap(UrlCatcherPlugin#1)
* new HashMap(UrlCatcherPlugin#1) num objects == 1
* new UrlListCommand(UrlCatcherPlugin#2) num objects == 1
* this.command.plugin == this
* this.command.plugin != null
*/
40 public class UrlCatcherPlugin extends Plugin implements ActionListener {
41
42 private final Map<String, Integer> urls = new HashMap<String, Integer>();
43
44 private final UrlListCommand command = new UrlListCommand(this);
45
46 /** {@inheritDoc} */
47 @Override
48 public void onLoad() {
/*
P/P * Method: void onLoad()
*
* Preconditions:
* init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* this.command != null
*
* Presumptions:
* init'ed(com.dmdirc.actions.CoreActionType.CLIENT_LINE_ADDED)
*/
49 ActionManager.addListener(this, CoreActionType.CLIENT_LINE_ADDED);
50 CommandManager.registerCommand(command);
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public void onUnload() {
/*
P/P * Method: void onUnload()
*
* Preconditions:
* init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* this.command != null
*/
56 ActionManager.removeListener(this);
57 CommandManager.unregisterCommand(command);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public void processEvent(final ActionType type, final StringBuffer format,
63 final Object... arguments) {
/*
P/P * Method: void processEvent(ActionType, StringBuffer, Object[])
*
* Preconditions:
* arguments != null
* arguments.length >= 2
* init'ed(arguments[1])
*
* Presumptions:
* com.dmdirc.ui.messages.Styliser:doLinks(...)@64 != null
*
* Test Vectors:
* java.lang.String:indexOf(...)@66: {-231..-1}, {0..232-1}
*/
64 final String message = Styliser.doLinks((String) arguments[1]);
65
66 if (message.indexOf(Styliser.CODE_HYPERLINK) > -1) {
67 final String[] parts = message.split("" + Styliser.CODE_HYPERLINK);
68
69 for (int i = 1; i < parts.length; i += 2) {
70 addURL(parts[i]);
71 }
72 }
73 }
74
75 /**
76 * Adds an URL to the list of tracked URLs, or increases its counter by one.
77 *
78 * @param url The URL to be added
79 */
80 private void addURL(final String url) {
/*
P/P * Method: void addURL(String)
*
* Preconditions:
* this.urls != null
*
* Presumptions:
* java.lang.Integer:intValue(...)@82 <= 232-2
* java.util.Map:get(...)@82 != null
*
* Test Vectors:
* java.util.Map:containsKey(...)@81: {0}, {1}
*/
81 if (urls.containsKey(url)) {
82 urls.put(url, urls.get(url) + 1);
83 } else {
84 urls.put(url, 1);
85 }
86 }
87
88 /**
89 * Retrieves the URLs that have been recorded.
90 *
91 * @return A map of URLs to the number of times they've been seen.
92 */
93 public Map<String, Integer> getURLS() {
/*
P/P * Method: Map getURLS()
*
* Postconditions:
* return_value == this.urls
* init'ed(return_value)
*/
94 return urls;
95 }
96
97 }
SofCheck Inspector Build Version : 2.17854
| UrlCatcherPlugin.java |
2009-Jun-25 01:54:24 |
| UrlCatcherPlugin.class |
2009-Sep-02 17:04:16 |