001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.wiki.macros;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.interfaces.KernelService;
039:
040: import org.libresource.web.wiki.LibresourceRenderContext;
041:
042: import org.radeox.macro.BaseMacro;
043: import org.radeox.macro.parameter.MacroParameter;
044:
045: import java.io.IOException;
046: import java.io.Writer;
047:
048: import java.net.URI;
049:
050: public class ImageMacro extends BaseMacro {
051: private String scheme;
052: private String host;
053: private KernelService kernelService;
054:
055: public ImageMacro() throws Exception {
056: kernelService = (KernelService) Libresource
057: .getService(KernelConstants.SERVICE);
058: scheme = kernelService.getUriScheme();
059: host = kernelService.getUriHost();
060: }
061:
062: public String getName() {
063: return "image";
064: }
065:
066: public void execute(Writer writer, MacroParameter params)
067: throws IllegalArgumentException, IOException {
068: try {
069: if (params.getLength() < 1) {
070: throw new IllegalArgumentException(
071: "syntax : uri|align|width|height");
072: }
073:
074: // tout ca n'est qu'un vieux hack
075: String uri = params.get(0);
076: String httpLinkPattern = "([^\"'=]|^)((http|ftp)s?://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
077: String LibresourceLinkPattern = "([^\"'=]|^)("
078: + scheme
079: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
080: String imageUrl = null;
081: URI currentUri = ((LibresourceRenderContext) params
082: .getContext()).getUri();
083:
084: String align = (String) params.getParams().get("align");
085:
086: if (align == null) {
087: align = "";
088: }
089:
090: if (uri.matches(httpLinkPattern)) {
091: imageUrl = uri;
092: } else {
093: // i assume that it's a Libresource link
094: // absolute Libresource link ??
095: if (uri.matches(LibresourceLinkPattern)) {
096: imageUrl = kernelService.normalizeURI(new URI(uri))
097: .getPath().substring(1)
098: + "?action=download&nodecorator";
099: } else {
100: // so, it's an relative Libresource link
101: URI relativeLink;
102:
103: if (uri.startsWith("/")) {
104: relativeLink = new URI(scheme + "://" + host
105: + uri);
106: } else {
107: relativeLink = new URI(uri);
108: }
109:
110: URI resolved = currentUri.resolve(relativeLink);
111: imageUrl = ""
112: + kernelService.normalizeURI(resolved)
113: .getPath().substring(1)
114: + "?action=download&nodecorator";
115: }
116: }
117:
118: writer.write("<img src=\"" + imageUrl + "\" class=\"image"
119: + align + "\"");
120:
121: String width = (String) params.getParams().get("width");
122:
123: if (width != null) {
124: writer.write(" width=\"" + width + "\" ");
125: }
126:
127: String height = (String) params.getParams().get("height");
128:
129: if (height != null) {
130: writer.write(" height=\"" + height + "\" ");
131: }
132:
133: writer.write("/>");
134: } catch (IOException e) {
135: throw e;
136: } catch (IllegalArgumentException e) {
137: throw e;
138: } catch (Exception e) {
139: throw new IllegalArgumentException("error in image : "
140: + e.getMessage());
141: }
142: }
143:
144: public String getDescription() {
145: return "Display an image.";
146: }
147:
148: public String[] getParamDescription() {
149: return new String[] { "1. the image URI",
150: "2. align (right ,left or center) (optional)",
151: "3. width (optional)", "4. height (optional)" };
152: }
153: }
|