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.forum.ForumConstants;
038: import org.libresource.forum.ejb.model.MessageResourceValue;
039: import org.libresource.forum.interfaces.LibresourceForumService;
040:
041: import org.libresource.kernel.KernelConstants;
042: import org.libresource.kernel.URINotExistException;
043: import org.libresource.kernel.interfaces.KernelService;
044:
045: import org.libresource.web.wiki.LibresourceRenderContext;
046:
047: import org.radeox.macro.Preserved;
048: import org.radeox.macro.parameter.MacroParameter;
049:
050: import java.io.IOException;
051: import java.io.Writer;
052:
053: import java.net.URI;
054:
055: public class QuoteMessageMacro extends Preserved {
056: private LibresourceForumService forumService;
057: private String scheme;
058: private String host;
059: private KernelService kernelService;
060:
061: public QuoteMessageMacro() throws Exception {
062: forumService = (LibresourceForumService) Libresource
063: .getService(ForumConstants.SERVICE);
064: kernelService = (KernelService) Libresource
065: .getService(KernelConstants.SERVICE);
066: scheme = kernelService.getUriScheme();
067: host = kernelService.getUriHost();
068: addSpecial('[');
069: addSpecial(']');
070: addSpecial('{');
071: addSpecial('}');
072: addSpecial('*');
073: addSpecial('-');
074: addSpecial('\\');
075: }
076:
077: public String getName() {
078: return "quoteMessage";
079: }
080:
081: public void execute(Writer writer, MacroParameter params)
082: throws IllegalArgumentException, IOException {
083: try {
084: if (params.getLength() != 1) {
085: throw new IllegalArgumentException(
086: "syntax : You must specify the message to quote");
087: }
088:
089: String LibresourceLinkPattern = "([^\"'=]|^)("
090: + scheme
091: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
092: URI currentUri = ((LibresourceRenderContext) params
093: .getContext()).getUri();
094: String uri = params.get(0);
095: URI resolvedUri = null;
096:
097: // i assume that it's a Libresource link
098: // absolute Libresource link ??
099: if (uri.matches(LibresourceLinkPattern)) {
100: resolvedUri = kernelService.normalizeURI(new URI(uri));
101: } else {
102: // so, it's an relative Libresource link
103: URI relativeLink;
104:
105: if (uri.startsWith("/")) {
106: relativeLink = new URI(scheme + "://" + host + uri);
107: } else {
108: relativeLink = new URI(uri);
109: }
110:
111: resolvedUri = kernelService.normalizeURI(currentUri
112: .resolve(relativeLink));
113: }
114:
115: try {
116: MessageResourceValue message = forumService
117: .getMessage(resolvedUri);
118:
119: String quoted = message.getBody(); //replace(message.getBody());
120: writer.write("<div class=\"quotedMessage\">");
121: writer.write(quoted);
122: writer.write("</div>");
123: } catch (URINotExistException e) {
124: writer.write("<div class=\"quotedMessage\">");
125: writer.write("(quoted message not found : "
126: + resolvedUri + ")");
127: writer.write("</div>");
128: }
129: } catch (IOException e) {
130: throw e;
131: } catch (IllegalArgumentException e) {
132: throw e;
133: } catch (Exception e) {
134: throw new IllegalArgumentException(
135: "error in quoteMessage : " + e.getMessage());
136: }
137: }
138:
139: public String getDescription() {
140: return "Quote a message in another message by referencing only the message URI.";
141: }
142:
143: public String[] getParamDescription() {
144: return new String[] { "1. The message URI to quote." };
145: }
146: }
|