1 25 26 package org.snipsnap.snip; 27 28 import org.radeox.util.logging.Logger; 29 30 import java.util.Collections ; 31 import java.util.Comparator ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.SortedSet ; 38 import java.util.StringTokenizer ; 39 import java.net.URL ; 40 import java.net.MalformedURLException ; 41 42 49 50 public class Links { 51 private Map linkMap; 52 private String cache = null; 53 private SortedSet links; 54 55 public Links() { 56 cache = ""; 57 } 58 59 public Links(String 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 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 tmp = ((Integer ) linkMap.get(url)); 79 if (tmp != null) { 80 currentCount = tmp.intValue(); 81 } 82 currentCount++; 83 linkMap.put(url, new Integer (currentCount)); 84 } else { 85 linkMap.put(url, new Integer (1)); 86 } 87 88 if (null != links) { 90 if (links.contains(url)) { 92 links.remove(url); 93 } 94 links.add(url); 95 } 96 } 97 98 104 public synchronized int removeLinkByPattern(String pattern) { 105 if (null == linkMap) { 106 linkMap = deserialize(cache); 107 } 108 cache = null; 109 int removedLinks = 0; 110 Iterator linkIt = linkMap.keySet().iterator(); 111 while(linkIt.hasNext()) { 112 String url = (String ) 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 domain) { 122 if (null == linkMap) { 123 linkMap = deserialize(cache); 124 } 125 cache = null; 126 int removedLinks = 0; 127 domain = domain.toLowerCase(); 128 Iterator linkIt = linkMap.keySet().iterator(); 129 while (linkIt.hasNext()) { 130 String url = (String ) linkIt.next(); 131 try { 132 String host = new URL (url).getHost().toLowerCase(); 133 if(host.endsWith(domain)) { 134 linkIt.remove(); 135 removedLinks++; 136 } 137 } catch (MalformedURLException e) { 138 Logger.warn("illegal referrer url found: '"+url+"' "+e.getLocalizedMessage()); 139 linkIt.remove(); 140 removedLinks++; 141 } 142 } 143 return removedLinks; 144 } 145 146 153 public Iterator iterator() { 154 if (null == linkMap) { 155 linkMap = deserialize(cache); 156 } 157 List keys = new LinkedList (linkMap.keySet()); 158 Collections.sort(keys, new Comparator () { 159 public int compare(Object o1, Object o2) { 160 return -((Integer ) linkMap.get(o1)).compareTo(((Integer ) linkMap.get(o2))); 162 } 163 }); 164 return keys.iterator(); 165 } 166 167 public int getIntCount(String url) { 168 if (null == linkMap) { 169 linkMap = deserialize(cache); 170 } 171 int currentCount = 0; 172 if (linkMap.containsKey(url)) { 173 currentCount = ((Integer ) linkMap.get(url)).intValue(); 174 } else { 175 currentCount = -1; 176 } 177 return currentCount; 178 } 179 180 public Map newLinkMap() { 181 return new HashMap (); 182 } 183 184 public Map deserialize(String links) { 185 Map linkcounts = newLinkMap(); 186 if (links == null || "".equals(links)) { 187 return linkcounts; 188 } 189 190 boolean errors = false; 191 StringTokenizer tokenizer = new StringTokenizer (links, "|"); 192 while (tokenizer.hasMoreTokens()) { 193 String urlString = tokenizer.nextToken(); 194 try { 195 Integer count = getCount(urlString); 196 String 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 e) { 207 errors = true; 208 Logger.warn("ignoring '" + urlString + "' while deserializing, format errors"); 209 break; 210 } 211 } 212 if (errors) { 214 cache = null; 215 } 216 return linkcounts; 217 } 218 219 private String serialize() { 220 if (null == linkMap || linkMap.isEmpty()) { 221 return ""; 222 } 223 224 StringBuffer linkBuffer = new StringBuffer (); 225 Iterator iterator = linkMap.keySet().iterator(); 226 while (iterator.hasNext()) { 227 String url = (String ) iterator.next(); 228 linkBuffer.append(url); 229 linkBuffer.append(":"); 230 Integer count = (Integer ) 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 after(String string, String delimiter) { 240 return string.substring(string.lastIndexOf(delimiter) + 1); 243 } 244 245 private String before(String string, String delimiter) { 246 return string.substring(0, string.lastIndexOf(delimiter)); 247 } 248 249 private String getUrl(String rolesString) { 250 return before(rolesString, ":"); 251 } 252 253 private Integer getCount(String urlString) { 254 try { 255 return new Integer (after(urlString, ":")); 256 } catch (NumberFormatException e) { 257 return new Integer (1); 258 } 259 } 260 261 public String toString() { 262 if (null == cache) { 263 cache = serialize(); 264 } 265 return cache; 266 } 267 } 268 | Popular Tags |