KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > filter > links > BackLinks


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.render.filter.links;
27
28 import org.radeox.util.Encoder;
29 import org.radeox.util.i18n.ResourceManager;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.snip.Links;
32 import org.snipsnap.snip.SnipLink;
33 import org.snipsnap.util.URLEncoderDecoder;
34
35 import java.io.IOException JavaDoc;
36 import java.io.Writer JavaDoc;
37 import java.io.UnsupportedEncodingException JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.InetAddress JavaDoc;
41 import java.net.UnknownHostException JavaDoc;
42
43 /**
44  * Renders backlinks
45  *
46  * @author Stephan J. Schmidt
47  * @version $Id: BackLinks.java 1629 2004-06-08 07:40:23Z leo $
48  */

49
50 public class BackLinks {
51   private static UrlFormatter formatter = new CutLengthFormatter();
52   private static InetAddress JavaDoc googleHost;
53
54   static {
55     try {
56       googleHost = InetAddress.getByName("www.google.com");
57     } catch (UnknownHostException JavaDoc e) {
58       Logger.warn("unable to resolve google.com: ", e);
59     }
60   }
61
62   public static void appendTo(Writer JavaDoc writer, Links backLinks, int count) {
63     Iterator JavaDoc iterator = backLinks.iterator();
64
65     try {
66       if (iterator.hasNext()) {
67         writer.write("<span class=\"caption\">");
68         writer.write(ResourceManager.getString("i18n.messages", "backlinks.title"));
69         writer.write("</span>\n");
70         writer.write("<ul class=\"list\">\n");
71         while (iterator.hasNext() && --count >= 0) {
72           String JavaDoc url = (String JavaDoc) iterator.next();
73           writer.write("<li>");
74           writer.write("<span class=\"count\">");
75           writer.write("" + backLinks.getIntCount(url));
76           writer.write("</span>");
77           writer.write(" <span class=\"content\"><a HREF=\"");
78           writer.write(Encoder.escape(url));
79           writer.write("\">");
80           renderView(writer, url);
81           writer.write("</a></span></li>\n");
82         }
83         writer.write("</ul>");
84       }
85     } catch (IOException JavaDoc e) {
86       Logger.warn("unable write to writer", e);
87     }
88
89   }
90
91   private static void renderView(Writer JavaDoc writer, String JavaDoc url) throws IOException JavaDoc {
92     URL JavaDoc urlInfo = new URL JavaDoc(url);
93     String JavaDoc info = null;
94     try {
95       if(googleHost != null && googleHost.equals(InetAddress.getByName(urlInfo.getHost()))) {
96         info = getQuery(urlInfo.getQuery(), "q");
97         if(info != null) {
98           info = urlInfo.getHost() + ": " + info;
99         }
100       }
101     } catch (UnknownHostException JavaDoc e) {
102       // TODO maybe delete such hosts
103
// ignore unknown hosts
104
}
105
106     if(null == info) {
107       info = url;
108     }
109
110     writer.write(Encoder.toEntity(info.charAt(0)));
111     writer.write(Encoder.escape(SnipLink.cutLength(info.substring(1), 90)));
112   }
113
114   private static String JavaDoc getQuery(String JavaDoc query, String JavaDoc id) {
115     if(null != query) {
116       String JavaDoc vars[] = query.split("&");
117       for(int v = 0; v < vars.length; v++) {
118         if(vars[v].startsWith(id+"=")) {
119           try {
120             String JavaDoc value = URLEncoderDecoder.decode(vars[v].substring(id.length()+1), "UTF-8");
121             return value.replace('+', ' ');
122           } catch (UnsupportedEncodingException JavaDoc e) {
123             return null;
124           }
125         }
126       }
127     }
128     return null;
129   }
130 }
Popular Tags