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.ejb.model.ThreadResourceValue;
040: import org.libresource.forum.interfaces.LibresourceForumService;
041:
042: import org.libresource.kernel.KernelConstants;
043: import org.libresource.kernel.interfaces.KernelService;
044:
045: import org.libresource.web.wiki.LibresourceRenderContext;
046:
047: import org.radeox.api.engine.context.RenderContext;
048:
049: import org.radeox.engine.BaseRenderEngine;
050:
051: import org.radeox.macro.Preserved;
052: import org.radeox.macro.parameter.MacroParameter;
053:
054: import java.io.IOException;
055: import java.io.Writer;
056:
057: import java.net.URI;
058:
059: import java.text.SimpleDateFormat;
060:
061: public class NewsMacro extends Preserved {
062: private String scheme;
063: private String host;
064: private KernelService kernelService;
065: private LibresourceForumService forumService;
066:
067: public NewsMacro() throws Exception {
068: kernelService = (KernelService) Libresource
069: .getService(KernelConstants.SERVICE);
070: forumService = (LibresourceForumService) Libresource
071: .getService(ForumConstants.SERVICE);
072: scheme = kernelService.getUriScheme();
073: host = kernelService.getUriHost();
074: addSpecial('[');
075: addSpecial(']');
076: addSpecial('{');
077: addSpecial('}');
078: addSpecial('*');
079: addSpecial('-');
080: addSpecial('\\');
081: }
082:
083: public String getName() {
084: return "news";
085: }
086:
087: public void execute(Writer writer, MacroParameter params)
088: throws IllegalArgumentException, IOException {
089: try {
090: if ((params.getLength() != 1) && (params.getLength() != 2)) {
091: throw new IllegalArgumentException(
092: "syntax : threadUri|(max)");
093: }
094:
095: // tout ca n'est qu'un vieux hack
096: String threadUri = params.get(0);
097: String LibresourceLinkPattern = "([^\"'=]|^)("
098: + scheme
099: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
100: URI currentUri = ((LibresourceRenderContext) params
101: .getContext()).getUri();
102: URI resolvedUri = null;
103:
104: int max = 3;
105:
106: if (params.getLength() > 1) {
107: max = Integer.parseInt(params.get(1));
108: }
109:
110: // i assume that it's a Libresource link
111: // absolute Libresource link ??
112: if (threadUri.matches(LibresourceLinkPattern)) {
113: resolvedUri = kernelService.normalizeURI(new URI(
114: threadUri));
115: } else {
116: // so, it's an relative Libresource link
117: URI relativeLink;
118:
119: if (threadUri.startsWith("/")) {
120: relativeLink = new URI(scheme + "://" + host
121: + threadUri);
122: } else {
123: relativeLink = new URI(threadUri);
124: }
125:
126: resolvedUri = kernelService.normalizeURI(currentUri
127: .resolve(relativeLink));
128: }
129:
130: ThreadResourceValue thread = forumService
131: .getThread(resolvedUri);
132: MessageResourceValue[] messages = forumService
133: .listMessagesInThread(resolvedUri);
134: SimpleDateFormat dateFormat = new SimpleDateFormat(
135: "MMMM, d yyyy");
136:
137: writer.write("<div class=\"news\">");
138: writer
139: .write("<div class=\"title\"><h3 class=\"heading-1\">"
140: + thread.getName() + "</h3></div><br/>");
141:
142: for (int i = 0; ((i < max) && (i < messages.length))
143: || ((max == 0) && (i < messages.length)); i++) {
144: RenderContext renderContext = new LibresourceRenderContext(
145: new URI(messages[i].getUri().getPath() + "/"));
146: String msgTitle = messages[i].getTitle();
147: String msgBody = new BaseRenderEngine().render(
148: messages[i].getBody(), renderContext);
149: writer.write("<div class=\"item\"><a href=\""
150: + messages[i].getUri().getPath().substring(1)
151: + "\"><span class=\"date\">"
152: + dateFormat.format(messages[i].getDate())
153: + "</span> - <span class=\"subject\">"
154: + msgTitle
155: + "</span></a> : <span class=\"body\">"
156: + msgBody + "</span></div><br/>");
157: }
158:
159: writer.write("</div>");
160: } catch (IOException e) {
161: throw e;
162: } catch (IllegalArgumentException e) {
163: throw e;
164: } catch (Exception e) {
165: throw new IllegalArgumentException("error in news : "
166: + e.getMessage());
167: }
168: }
169:
170: public String getDescription() {
171: return "Display a message thread as news.";
172: }
173:
174: public String[] getParamDescription() {
175: return new String[] { "1. the thread URI", "2. Max (optional)" };
176: }
177: }
|