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.taglibs.JspFunctions;
041: import org.libresource.web.wiki.LibresourceRenderContext;
042:
043: import org.radeox.macro.BaseMacro;
044: import org.radeox.macro.parameter.MacroParameter;
045:
046: import java.io.IOException;
047: import java.io.Writer;
048:
049: import java.net.URI;
050:
051: public class MenuMacro extends BaseMacro {
052: KernelService kernelService = null;
053: private String scheme;
054: private String host;
055:
056: public MenuMacro() throws Exception {
057: kernelService = (KernelService) Libresource
058: .getService(KernelConstants.SERVICE);
059: scheme = kernelService.getUriScheme();
060: host = kernelService.getUriHost();
061: }
062:
063: public String getName() {
064: return "menu";
065: }
066:
067: public void execute(Writer writer, MacroParameter params)
068: throws IllegalArgumentException, IOException {
069: try {
070: if (params.getLength() != 1) {
071: throw new IllegalArgumentException(
072: "You must specify the URI of the menu");
073: }
074:
075: // tout ca n'est qu'un vieux hack
076: String menuUri = params.get(0);
077: String LibresourceLinkPattern = "([^\"'=]|^)("
078: + scheme
079: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
080: URI currentUri = ((LibresourceRenderContext) params
081: .getContext()).getUri();
082: URI resolvedUri = null;
083:
084: // i assume that it's a Libresource link
085: // absolute Libresource link ??
086: if (menuUri.matches(LibresourceLinkPattern)) {
087: resolvedUri = kernelService.normalizeURI(new URI(
088: menuUri));
089: } else {
090: // so, it's an relative Libresource link
091: URI relativeLink;
092:
093: if (menuUri.startsWith("/")) {
094: relativeLink = new URI(scheme + "://" + host
095: + menuUri);
096: } else {
097: relativeLink = new URI(menuUri);
098: }
099:
100: resolvedUri = kernelService.normalizeURI(currentUri
101: .resolve(relativeLink));
102: }
103:
104: String menuContent = kernelService.getProperty(resolvedUri,
105: "libresource-web.menu", true);
106: URI menuUri2 = kernelService.getNodeDefiningProperty(
107: resolvedUri, "libresource-web.menu");
108: writer.write(JspFunctions.render(menuUri2, menuContent));
109: } catch (IOException e) {
110: throw e;
111: } catch (IllegalArgumentException e) {
112: throw e;
113: } catch (Exception e) {
114: throw new IllegalArgumentException(e.getMessage());
115: }
116: }
117:
118: public String getDescription() {
119: return "Include the menu of the specified URI";
120: }
121:
122: public String[] getParamDescription() {
123: return new String[] { "1. The uri for the menu to be include" };
124: }
125: }
|