KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > Links


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip;
27
28 import org.radeox.util.logging.Logger;
29
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.SortedSet JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41
42 /**
43  * Manages links to and from snips. Links can be external to internal
44  * and internal to internal.
45  *
46  * @author stephan
47  * @version $Id: Links.java 1633 2004-06-08 09:23:48Z leo $
48  */

49
50 public class Links {
51   private Map JavaDoc linkMap;
52   private String JavaDoc cache = null;
53   private SortedSet JavaDoc links;
54
55   public Links() {
56     cache = "";
57   }
58
59   public Links(String JavaDoc links) {
60     cache = links;
61   }
62
63   public int getSize() {
64     if (null == linkMap) {
65       linkMap = deserialize(cache);
66     }
67     return linkMap.size();
68   }
69
70   public void addLink(String JavaDoc url) {
71     if (null == linkMap) {
72       linkMap = deserialize(cache);
73     }
74     cache = null;
75
76     if (linkMap.containsKey(url)) {
77       int currentCount = 0;
78       Integer JavaDoc tmp = ((Integer JavaDoc) linkMap.get(url));
79       if (tmp != null) {
80         currentCount = tmp.intValue();
81       }
82       currentCount++;
83       linkMap.put(url, new Integer JavaDoc(currentCount));
84     } else {
85       linkMap.put(url, new Integer JavaDoc(1));
86     }
87
88     // If there is a sorted cached key set, then add url
89
if (null != links) {
90       // remove url first to force resorting
91
if (links.contains(url)) {
92         links.remove(url);
93       }
94       links.add(url);
95     }
96   }
97
98   /**
99    * Remove a link using the given regular expression. The regular expression
100    * must match the link fully as defined in String.matches()
101    * @param pattern the regular expression
102    * @return the amount of removed links
103    */

104   public synchronized int removeLinkByPattern(String JavaDoc pattern) {
105     if (null == linkMap) {
106       linkMap = deserialize(cache);
107     }
108     cache = null;
109     int removedLinks = 0;
110     Iterator JavaDoc linkIt = linkMap.keySet().iterator();
111     while(linkIt.hasNext()) {
112       String JavaDoc url = (String JavaDoc) linkIt.next();
113       if(url.matches(pattern)) {
114         linkIt.remove();
115         removedLinks++;
116       }
117     }
118     return removedLinks;
119   }
120
121   public synchronized int removeLink(String JavaDoc domain) {
122     if (null == linkMap) {
123       linkMap = deserialize(cache);
124     }
125     cache = null;
126     int removedLinks = 0;
127     domain = domain.toLowerCase();
128     Iterator JavaDoc linkIt = linkMap.keySet().iterator();
129     while (linkIt.hasNext()) {
130       String JavaDoc url = (String JavaDoc) linkIt.next();
131       try {
132         String JavaDoc host = new URL JavaDoc(url).getHost().toLowerCase();
133         if(host.endsWith(domain)) {
134           linkIt.remove();
135           removedLinks++;
136         }
137       } catch (MalformedURLException JavaDoc e) {
138         Logger.warn("illegal referrer url found: '"+url+"' "+e.getLocalizedMessage());
139         linkIt.remove();
140         removedLinks++;
141       }
142     }
143     return removedLinks;
144   }
145
146   /**
147    * Return an iterator to Links. The iterator is sorted
148    * by the count in the linkMap map. The sorted keyset is
149    * only generated on demand.
150    *
151    * @return Iterator over the urls of Links
152    */

153   public Iterator JavaDoc iterator() {
154     if (null == linkMap) {
155       linkMap = deserialize(cache);
156     }
157     List JavaDoc keys = new LinkedList JavaDoc(linkMap.keySet());
158     Collections.sort(keys, new Comparator JavaDoc() {
159       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
160         // use "-" because we want decreasing order
161
return -((Integer JavaDoc) linkMap.get(o1)).compareTo(((Integer JavaDoc) linkMap.get(o2)));
162       }
163     });
164     return keys.iterator();
165   }
166
167   public int getIntCount(String JavaDoc url) {
168     if (null == linkMap) {
169       linkMap = deserialize(cache);
170     }
171     int currentCount = 0;
172     if (linkMap.containsKey(url)) {
173       currentCount = ((Integer JavaDoc) linkMap.get(url)).intValue();
174     } else {
175       currentCount = -1;
176     }
177     return currentCount;
178   }
179
180   public Map JavaDoc newLinkMap() {
181     return new HashMap JavaDoc();
182   }
183
184   public Map JavaDoc deserialize(String JavaDoc links) {
185     Map JavaDoc linkcounts = newLinkMap();
186     if (links == null || "".equals(links)) {
187       return linkcounts;
188     }
189
190     boolean errors = false;
191     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(links, "|");
192     while (tokenizer.hasMoreTokens()) {
193       String JavaDoc urlString = tokenizer.nextToken();
194       try {
195         Integer JavaDoc count = getCount(urlString);
196         String JavaDoc url = getUrl(urlString);
197         for (int c = 0; c < url.length(); c++) {
198           char ch = urlString.charAt(c);
199           if (ch < 0x20 && !(ch == 0x0a || ch == 0x0d || ch == 0x09)) {
200             errors = true;
201             Logger.warn("ignoring '" + urlString + "' while deserializing: illegal character");
202             break;
203           }
204         }
205         linkcounts.put(url, count);
206       } catch (Exception JavaDoc e) {
207         errors = true;
208         Logger.warn("ignoring '" + urlString + "' while deserializing, format errors");
209         break;
210       }
211     }
212     // make sure correct data is in the cache
213
if (errors) {
214       cache = null;
215     }
216     return linkcounts;
217   }
218
219   private String JavaDoc serialize() {
220     if (null == linkMap || linkMap.isEmpty()) {
221       return "";
222     }
223
224     StringBuffer JavaDoc linkBuffer = new StringBuffer JavaDoc();
225     Iterator JavaDoc iterator = linkMap.keySet().iterator();
226     while (iterator.hasNext()) {
227       String JavaDoc url = (String JavaDoc) iterator.next();
228       linkBuffer.append(url);
229       linkBuffer.append(":");
230       Integer JavaDoc count = (Integer JavaDoc) linkMap.get(url);
231       linkBuffer.append(count);
232       if (iterator.hasNext()) {
233         linkBuffer.append("|");
234       }
235     }
236     return linkBuffer.toString();
237   }
238
239   private String JavaDoc after(String JavaDoc string, String JavaDoc delimiter) {
240     // Split at the last delimiter, e.g. "I:want:some:coke:3"
241
// splits "I:want:some:coke" and "3"
242
return string.substring(string.lastIndexOf(delimiter) + 1);
243   }
244
245   private String JavaDoc before(String JavaDoc string, String JavaDoc delimiter) {
246     return string.substring(0, string.lastIndexOf(delimiter));
247   }
248
249   private String JavaDoc getUrl(String JavaDoc rolesString) {
250     return before(rolesString, ":");
251   }
252
253   private Integer JavaDoc getCount(String JavaDoc urlString) {
254     try {
255       return new Integer JavaDoc(after(urlString, ":"));
256     } catch (NumberFormatException JavaDoc e) {
257       return new Integer JavaDoc(1);
258     }
259   }
260
261   public String JavaDoc toString() {
262     if (null == cache) {
263       cache = serialize();
264     }
265     return cache;
266   }
267 }
268
Popular Tags