1 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 ; 38 import java.io.UnsupportedEncodingException ; 39 import java.io.Writer ; 40 import java.net.URL ; 41 import java.text.MessageFormat ; 42 import java.util.Arrays ; 43 import java.util.List ; 44 45 51 52 public class SnipLink { 53 54 public static NameFormatter formatter = new PathRemoveFormatter(); 55 56 64 public static Writer appendUrlWithBase(Writer writer, String base, String name, String target) 65 throws IOException { 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 appendUrl(Writer writer, String name, String target) throws IOException { 77 return appendUrlWithBase(writer, getSpaceRoot(), name, target); 78 } 79 80 public static Writer appendUrl(Writer writer, String name) throws IOException { 81 return appendUrlWithBase(writer, getSpaceRoot(), name, null); 82 } 83 84 87 public static Writer appendCreateLink(Writer writer, String name) throws IOException { 88 writer.write(makeCreateLink(name)); 89 return writer; 90 } 91 92 public static StringBuffer appendCreateLink(StringBuffer buffer, String name) { 93 return buffer.append(makeCreateLink(name)); 94 } 95 96 private static String makeCreateLink(String name) { 97 MessageFormat formatter = new MessageFormat (ResourceManager.getString("i18n.messages", "sniplink.create"), 98 ResourceManager.getLocale("i18n.messages")); 99 100 StringBuffer link = new StringBuffer (); 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 []{link.toString()}); 108 } 109 110 public static String createLink(String name) { 111 StringBuffer buffer = new StringBuffer (); 112 return appendLink(buffer, name, name).toString(); 113 } 114 115 public static String createLink(String name, String view) { 116 StringBuffer buffer = new StringBuffer (); 117 return appendLink(buffer, name, view).toString(); 118 } 119 120 public static String createLink(String root, String name, String view) { 121 StringBuffer buffer = new StringBuffer (); 122 return appendLinkWithRoot(buffer, root, encode(name), view).toString(); 123 } 124 125 public static Writer appendLink(Writer writer, Snip snip) throws IOException { 126 return appendLink(writer, snip.getName()); 127 } 128 129 public static StringBuffer appendLink(StringBuffer buffer, Snip snip) { 130 return appendLink(buffer, snip.getName()); 131 } 132 133 public static Writer appendLink(Writer writer, String name) throws IOException { 134 StringBuffer buffer = new StringBuffer (); 135 appendLink(buffer, name, name); 136 writer.write(buffer.toString()); 137 return writer; 138 } 139 140 public static StringBuffer appendLink(StringBuffer buffer, String name) { 141 return appendLink(buffer, name, name); 142 } 143 144 public static Writer appendLink(Writer writer, String name, String view) throws IOException { 145 return appendLinkWithRoot(writer, getSpaceRoot(), encode(name), view); 146 } 147 148 public static StringBuffer appendLink(StringBuffer buffer, String name, String view, String target) { 149 return appendLinkWithRoot(buffer, getSpaceRoot(), encode(name) + "#" + target, view); 150 } 151 152 public static StringBuffer appendLink(StringBuffer buffer, String name, String view) { 153 return appendLinkWithRoot(buffer, getSpaceRoot(), encode(name), view); 154 } 155 156 public static Writer appendLinkWithRoot(Writer writer, String root, String name, String view) throws IOException { 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 171 public static StringBuffer appendLinkWithRoot(StringBuffer buffer, String root, String name, String 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 191 public static String getImageRoot() { 192 return getCanonicalUrl("theme/images"); 193 } 194 195 public static String getSpaceRoot() { 196 return getCanonicalUrl("space"); 197 } 198 199 public static String getExecRoot() { 200 return getCanonicalUrl("exec"); 201 } 202 203 public static String getCommentsRoot() { 204 return getCanonicalUrl("comments"); 205 } 206 207 private static String getCanonicalUrl(String url) { 208 URL baseUrl = (URL ) Application.get().getObject(Application.URL); 209 if (null != baseUrl) { 210 return baseUrl.toExternalForm() + "/" + url; 211 } 212 return url; 213 } 214 215 private static List extensions = Arrays.asList(new String []{"png", "jpg", "jpeg", "gif"}); 216 217 public static Writer appendImage(Writer writer, String name, String alt) throws IOException { 218 return appendImageWithRoot(writer, getImageRoot(), name, alt, "png", null); 219 } 220 221 public static Writer appendImage(Writer writer, String name, String alt, String ext) throws IOException { 222 return appendImageWithRoot(writer, getImageRoot(), name, alt, ext, null); 223 } 224 225 public static Writer appendImage(Writer writer, Snip snip, String name, String alt, String ext, String position) throws IOException { 226 return appendImageWithRoot(writer, getSpaceRoot() + "/" + snip.getNameEncoded(), name, alt, ext, position); 227 } 228 229 239 public static Writer appendImageWithRoot(Writer writer, String root, String name, 240 String alt, String ext, String position) 241 throws IOException { 242 int dotIndex = name.lastIndexOf('.'); 244 if (dotIndex != -1) { 245 String 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 public static String encode(String s) { 282 try { 283 Configuration config = Application.get().getConfiguration(); 284 String 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 e) { 291 Logger.log(Logger.FATAL, "unsupported encoding: " + e); 292 return s; 293 } 294 } 295 296 public static String decode(String s) { 297 try { 298 Configuration config = Application.get().getConfiguration(); 299 String 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 e) { 305 Logger.log(Logger.FATAL, "unsupported encoding: " + e); 306 return s; 307 } 308 } 309 310 public static String cutLength(String 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 |