01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wikitext.widgets;
04:
05: import fitnesse.wikitext.WikiWidget;
06:
07: import java.util.regex.*;
08:
09: public class ImageWidget extends WikiWidget {
10: public static final String REGEXP = "(?:!img(?:-[lr])? \\S+)|(?:"
11: + LinkWidget.REGEXP + ".(?:gif|jpg|GIF|JPG))";
12:
13: private static final Pattern pattern = Pattern
14: .compile("(!img(-[lr])? )?(\\S*)");
15:
16: private String picturePath;
17:
18: private String alignment;
19:
20: private boolean usesBangImg;
21:
22: public ImageWidget(ParentWidget parent, String text) {
23: super (parent);
24: Matcher match = pattern.matcher(text);
25: if (match.find()) {
26: picturePath = LinkWidget.makeUrlUsable(match.group(3));
27: usesBangImg = match.group(1) != null;
28: if (usesBangImg)
29: alignment = match.group(2);
30: } else
31: System.err.println("ImagesWidget parse error: " + text);
32: }
33:
34: public String render() throws Exception {
35: StringBuffer html = new StringBuffer("<img src=\"");
36: html.append(picturePath).append("\"");
37: if (alignment != null) {
38: html.append(" class=\"");
39: if ("-l".equals(alignment))
40: html.append("left");
41: else
42: html.append("right");
43: html.append("\"");
44: }
45: html.append(">");
46:
47: return html.toString();
48: }
49:
50: public String asWikiText() throws Exception {
51: String pathString = picturePath;
52: if (pathString.startsWith("/files"))
53: pathString = "http:/" + pathString;
54:
55: if (usesBangImg) {
56: final String alignmentString = (alignment == null ? ""
57: : alignment);
58: return "!img" + alignmentString + " " + pathString;
59: } else
60: return pathString;
61: }
62:
63: }
|