KickJava   Java API By Example, From Geeks To Geeks.

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


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.Encoder;
29 import org.radeox.util.i18n.ResourceManager;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.config.Configuration;
33 import org.snipsnap.snip.name.NameFormatter;
34 import org.snipsnap.snip.name.PathRemoveFormatter;
35 import org.snipsnap.util.URLEncoderDecoder;
36
37 import java.io.IOException JavaDoc;
38 import java.io.UnsupportedEncodingException JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.text.MessageFormat JavaDoc;
42 import java.util.Arrays JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * Generates links for snips
47  *
48  * @author stephan
49  * @version $Id: SnipLink.java 1832 2005-08-08 13:15:47Z leo $
50  */

51
52 public class SnipLink {
53
54   public static NameFormatter formatter = new PathRemoveFormatter();
55
56   /**
57    * Append a URL String that contains a base a name-to-be-encoded and an optional anchor target.
58    *
59    * @param writer the writer to append to
60    * @param base the url base
61    * @param name the name (to be encoded)
62    * @param target the anchor target
63    */

64   public static Writer JavaDoc appendUrlWithBase(Writer JavaDoc writer, String JavaDoc base, String JavaDoc name, String JavaDoc target)
65           throws IOException JavaDoc {
66     writer.write(base);
67     writer.write("/");
68     writer.write(encode(name));
69     if (target != null) {
70       writer.write("#");
71       writer.write(target);
72     }
73     return writer;
74   }
75
76   public static Writer JavaDoc appendUrl(Writer JavaDoc writer, String JavaDoc name, String JavaDoc target) throws IOException JavaDoc {
77     return appendUrlWithBase(writer, getSpaceRoot(), name, target);
78   }
79
80   public static Writer JavaDoc appendUrl(Writer JavaDoc writer, String JavaDoc name) throws IOException JavaDoc {
81     return appendUrlWithBase(writer, getSpaceRoot(), name, null);
82   }
83
84   /**
85    * Append a create link for the specified name.
86    */

87   public static Writer JavaDoc appendCreateLink(Writer JavaDoc writer, String JavaDoc name) throws IOException JavaDoc {
88     writer.write(makeCreateLink(name));
89     return writer;
90   }
91
92   public static StringBuffer JavaDoc appendCreateLink(StringBuffer JavaDoc buffer, String JavaDoc name) {
93     return buffer.append(makeCreateLink(name));
94   }
95
96   private static String JavaDoc makeCreateLink(String JavaDoc name) {
97     MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "sniplink.create"),
98             ResourceManager.getLocale("i18n.messages"));
99
100     StringBuffer JavaDoc link = new StringBuffer JavaDoc();
101     link.append("<a HREF=\"");
102     link.append(getExecRoot());
103     link.append("/new?name=");
104     link.append(encode(name));
105     link.append("\">").append(Encoder.escape(name)).append("</a>");
106
107     return formatter.format(new Object JavaDoc[]{link.toString()});
108   }
109
110   public static String JavaDoc createLink(String JavaDoc name) {
111     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
112     return appendLink(buffer, name, name).toString();
113   }
114
115   public static String JavaDoc createLink(String JavaDoc name, String JavaDoc view) {
116     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117     return appendLink(buffer, name, view).toString();
118   }
119
120   public static String JavaDoc createLink(String JavaDoc root, String JavaDoc name, String JavaDoc view) {
121     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
122     return appendLinkWithRoot(buffer, root, encode(name), view).toString();
123   }
124
125   public static Writer JavaDoc appendLink(Writer JavaDoc writer, Snip snip) throws IOException JavaDoc {
126     return appendLink(writer, snip.getName());
127   }
128
129   public static StringBuffer JavaDoc appendLink(StringBuffer JavaDoc buffer, Snip snip) {
130     return appendLink(buffer, snip.getName());
131   }
132
133   public static Writer JavaDoc appendLink(Writer JavaDoc writer, String JavaDoc name) throws IOException JavaDoc {
134     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
135     appendLink(buffer, name, name);
136     writer.write(buffer.toString());
137     return writer;
138   }
139
140   public static StringBuffer JavaDoc appendLink(StringBuffer JavaDoc buffer, String JavaDoc name) {
141     return appendLink(buffer, name, name);
142   }
143
144   public static Writer JavaDoc appendLink(Writer JavaDoc writer, String JavaDoc name, String JavaDoc view) throws IOException JavaDoc {
145     return appendLinkWithRoot(writer, getSpaceRoot(), encode(name), view);
146   }
147
148   public static StringBuffer JavaDoc appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view, String JavaDoc target) {
149     return appendLinkWithRoot(buffer, getSpaceRoot(), encode(name) + "#" + target, view);
150   }
151
152   public static StringBuffer JavaDoc appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view) {
153     return appendLinkWithRoot(buffer, getSpaceRoot(), encode(name), view);
154   }
155
156   public static Writer JavaDoc appendLinkWithRoot(Writer JavaDoc writer, String JavaDoc root, String JavaDoc name, String JavaDoc view) throws IOException JavaDoc {
157     writer.write("<a HREF=\"");
158     writer.write(root);
159     writer.write("/");
160     writer.write(name);
161     writer.write("\">");
162     view = formatter.format(view);
163     writer.write(Encoder.escape(view));
164     writer.write("</a>");
165     return writer;
166   }
167
168   /**
169    * Create a link with a root and a special view. The name will not be url encoded!
170    */

171   public static StringBuffer JavaDoc appendLinkWithRoot(StringBuffer JavaDoc buffer, String JavaDoc root, String JavaDoc name, String JavaDoc view) {
172     buffer.append("<a HREF=\"");
173     buffer.append(root);
174     buffer.append("/");
175     buffer.append(name);
176     buffer.append("\">");
177     view = formatter.format(view);
178     buffer.append(Encoder.escape(view));
179     buffer.append("</a>");
180     return buffer;
181   }
182
183 // private static String absoluteLink(String path) {
184
// Configuration config = Application.get().getConfiguration();
185
// String contextPath = config.getPath();
186
// String prefix = config.getPrefix();
187
// return (contextPath != null ? contextPath : "") +
188
// (prefix != null && !"/".equals(prefix) ? prefix + "/" : "") + path;
189
// }
190

191   public static String JavaDoc getImageRoot() {
192     return getCanonicalUrl("theme/images");
193   }
194
195   public static String JavaDoc getSpaceRoot() {
196     return getCanonicalUrl("space");
197   }
198
199   public static String JavaDoc getExecRoot() {
200     return getCanonicalUrl("exec");
201   }
202
203   public static String JavaDoc getCommentsRoot() {
204     return getCanonicalUrl("comments");
205   }
206
207   private static String JavaDoc getCanonicalUrl(String JavaDoc url) {
208     URL JavaDoc baseUrl = (URL JavaDoc) Application.get().getObject(Application.URL);
209     if (null != baseUrl) {
210       return baseUrl.toExternalForm() + "/" + url;
211     }
212     return url;
213   }
214
215   private static List JavaDoc extensions = Arrays.asList(new String JavaDoc[]{"png", "jpg", "jpeg", "gif"});
216
217   public static Writer JavaDoc appendImage(Writer JavaDoc writer, String JavaDoc name, String JavaDoc alt) throws IOException JavaDoc {
218     return appendImageWithRoot(writer, getImageRoot(), name, alt, "png", null);
219   }
220
221   public static Writer JavaDoc appendImage(Writer JavaDoc writer, String JavaDoc name, String JavaDoc alt, String JavaDoc ext) throws IOException JavaDoc {
222     return appendImageWithRoot(writer, getImageRoot(), name, alt, ext, null);
223   }
224
225   public static Writer JavaDoc appendImage(Writer JavaDoc writer, Snip snip, String JavaDoc name, String JavaDoc alt, String JavaDoc ext, String JavaDoc position) throws IOException JavaDoc {
226     return appendImageWithRoot(writer, getSpaceRoot() + "/" + snip.getNameEncoded(), name, alt, ext, position);
227   }
228
229   /**
230    * Append and image tag to a string buffer. Additionally takes an alternative text to display
231    * if the browser cannot display the image.
232    *
233    * @param writer the writer to append to
234    * @param root the root path for images
235    * @param name the image name
236    * @param alt an alternative text
237    * @return the string buffer
238    */

239   public static Writer JavaDoc appendImageWithRoot(Writer JavaDoc writer, String JavaDoc root, String JavaDoc name,
240                                            String JavaDoc alt, String JavaDoc ext, String JavaDoc position)
241           throws IOException JavaDoc {
242     // extract extension or leave as is, default is to append .png
243
int dotIndex = name.lastIndexOf('.');
244     if (dotIndex != -1) {
245       String JavaDoc imageExt = name.substring(dotIndex + 1);
246       if (extensions.contains(imageExt.toLowerCase())) {
247         ext = imageExt;
248         name = name.substring(0, dotIndex);
249       }
250     }
251     if (null == ext) {
252       ext = "png";
253     }
254
255     writer.write("<img SRC=\"");
256     writer.write(root);
257     writer.write("/");
258     writer.write(name);
259     writer.write(".");
260     writer.write(ext);
261     writer.write("\"");
262     if (alt != null) {
263       writer.write(" alt=\"");
264       writer.write(alt);
265       writer.write("\"");
266     } else {
267       writer.write(" alt=\"");
268       writer.write(name);
269       writer.write("\"");
270     }
271     if (position != null) {
272       writer.write(" class=\"");
273       writer.write(position);
274       writer.write("\"");
275     }
276     writer.write(" border=\"0\"/>");
277     return writer;
278   }
279
280   // TODO 1.4 buffer.append(URLEncoder.encode(key, "iso-8859-1"));
281
public static String JavaDoc encode(String JavaDoc s) {
282     try {
283       Configuration config = Application.get().getConfiguration();
284       String JavaDoc encodedSpace = config.getEncodedSpace();
285       if (null != encodedSpace && encodedSpace.length() > 0) {
286         return URLEncoderDecoder.encode(s, config.getEncoding()).replace(' ', encodedSpace.charAt(0));
287       } else {
288         return URLEncoderDecoder.encode(s, config.getEncoding());
289       }
290     } catch (UnsupportedEncodingException JavaDoc e) {
291       Logger.log(Logger.FATAL, "unsupported encoding: " + e);
292       return s;
293     }
294   }
295
296   public static String JavaDoc decode(String JavaDoc s) {
297     try {
298       Configuration config = Application.get().getConfiguration();
299       String JavaDoc encodedSpace = config.getEncodedSpace();
300       if (null != encodedSpace && encodedSpace.length() > 0) {
301         s = s.replace(encodedSpace.charAt(0), ' ');
302       }
303       return URLEncoderDecoder.decode(s, config.getEncoding());
304     } catch (UnsupportedEncodingException JavaDoc e) {
305       Logger.log(Logger.FATAL, "unsupported encoding: " + e);
306       return s;
307     }
308   }
309
310   public static String JavaDoc cutLength(String JavaDoc url, int len) {
311     if (url != null && len > 3 && url.length() > len) {
312       return url.substring(0, len - 3) + "...";
313     }
314     return url;
315   }
316
317 }
318
Popular Tags