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.LibresourceSecurityException;
039: import org.libresource.kernel.URINotExistException;
040: import org.libresource.kernel.interfaces.KernelService;
041:
042: import org.libresource.web.wiki.LibresourceRenderContext;
043:
044: import org.libresource.wiki.WikiConstants;
045: import org.libresource.wiki.ejb.model.PageResourceValue;
046: import org.libresource.wiki.interfaces.LibresourceWikiService;
047:
048: import org.radeox.macro.BaseMacro;
049: import org.radeox.macro.parameter.MacroParameter;
050:
051: import java.io.IOException;
052: import java.io.Writer;
053:
054: import java.net.URI;
055:
056: import java.util.StringTokenizer;
057:
058: public class SummaryMacro extends BaseMacro {
059: private String scheme;
060: private String host;
061: private KernelService kernelService;
062:
063: public SummaryMacro() throws Exception {
064: kernelService = (KernelService) Libresource
065: .getService(KernelConstants.SERVICE);
066: scheme = kernelService.getUriScheme();
067: host = kernelService.getUriHost();
068: }
069:
070: public String getName() {
071: return "summary";
072: }
073:
074: public void execute(Writer writer, MacroParameter params)
075: throws IllegalArgumentException, IOException {
076: try {
077: if (params.getLength() > 1) {
078: throw new IllegalArgumentException(
079: "syntax : uri(optional)");
080: }
081:
082: String libresourceLinkPattern = "([^\"'=]|^)("
083: + scheme
084: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
085: String pageUri = null;
086: URI currentUri = ((LibresourceRenderContext) params
087: .getContext()).getUri();
088:
089: String page = params.get(0);
090:
091: if (page == null) {
092: pageUri = currentUri.getPath();
093: } else if (page.matches(libresourceLinkPattern)) {
094: pageUri = kernelService.normalizeURI(new URI(page))
095: .getPath().substring(1);
096: } else {
097: URI relativeLink;
098:
099: if (page.startsWith("/")) {
100: relativeLink = new URI(scheme + "://" + host + page);
101: } else {
102: relativeLink = new URI(page);
103: }
104:
105: URI resolved = currentUri.resolve(relativeLink);
106: pageUri = ""
107: + kernelService.normalizeURI(resolved)
108: .getPath().substring(1);
109: }
110:
111: writer.write("<table><tr><td><div class=\"summary\">");
112:
113: LibresourceWikiService libresourcewikiservice = (LibresourceWikiService) Libresource
114: .getService(WikiConstants.SERVICE);
115: PageResourceValue pageResourceValue = null;
116:
117: try {
118: pageResourceValue = libresourcewikiservice
119: .getPage(new URI(pageUri));
120: } catch (LibresourceSecurityException libresourceSecurityException) {
121: writer
122: .write("<span class=\"error\">Error : Permission denied at "
123: + page + "</span>");
124: } catch (URINotExistException uriNotExistException) {
125: writer.write("<span class=\"error\">Error : wiki page "
126: + page + " does not exist</span>");
127: }
128:
129: if (pageResourceValue != null) {
130: String pageName = pageResourceValue.getName();
131: String pageContent = pageResourceValue.getContent();
132:
133: if ((pageName == null)
134: || (pageName.trim().length() == 0)) {
135: pageName = pageUri;
136: }
137:
138: if (pageUri.endsWith("/")) {
139: pageUri = pageUri
140: .substring(0, pageUri.length() - 1);
141: }
142:
143: writer.write("<h3>" + pageName + "</h3>");
144:
145: StringTokenizer st = new StringTokenizer(pageContent,
146: "\n\r");
147:
148: while (st.hasMoreTokens()) {
149: String line = st.nextToken();
150:
151: // write title
152: if (line
153: .matches("\\A[\\p{Space}]*(T1)[\\p{Space}]+(.*?)\\Z")) {
154: String title = line.substring(
155: line.indexOf("1") + 1, line.length())
156: .trim();
157: writer.write("<a class=\"head-1\" href=\""
158: + pageUri + "#" + title + "\">" + title
159: + "</a><br/>");
160: }
161:
162: // write subtitle
163: if (line
164: .matches("\\A[\\p{Space}]*(T1\\.1)[\\p{Space}]+(.*?)\\Z")) {
165: String subTitle = line.substring(
166: line.indexOf("1.1") + 3, line.length())
167: .trim();
168: writer.write("<a class=\"head-1-1\" href=\""
169: + pageUri + "#" + subTitle + "\">"
170: + subTitle + "</a><br/>");
171: }
172: }
173: }
174:
175: writer.write("</div></td></tr></table>");
176: } catch (IOException ioe) {
177: throw ioe;
178: } catch (IllegalArgumentException iae) {
179: throw iae;
180: } catch (Exception e) {
181: throw new IllegalArgumentException("error in summary : "
182: + e.getMessage());
183: }
184: }
185:
186: public String getDescription() {
187: return "Create the summary of a wiki page.";
188: }
189:
190: public String[] getParamDescription() {
191: return (new String[] { "1. the page URI (optional)" });
192: }
193: }
|