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.membership.interfaces.MembershipService;
038:
039: import org.libresource.web.servlets.BaseServlet;
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: import javax.servlet.jsp.jstl.fmt.LocalizationContext;
051:
052: public class SearchMacro extends BaseMacro {
053: public String getName() {
054: return "searchBox";
055: }
056:
057: public void execute(Writer writer, MacroParameter params)
058: throws IllegalArgumentException, IOException {
059: try {
060: if ((params.getLength() != 0) && (params.getLength() != 1)) {
061: throw new IllegalArgumentException("syntax : searchBox");
062: }
063:
064: MembershipService membershipService = (MembershipService) Libresource
065: .getService("Membership");
066: String lang = (String) membershipService.getProfile()
067: .getInfos().get("libresource-web.lang");
068: LocalizationContext local = BaseServlet
069: .getLocalisation((lang == null) ? "en" : lang);
070:
071: URI currentUri = ((LibresourceRenderContext) params
072: .getContext()).getUri();
073: String uriPattern = currentUri.toString();
074:
075: if (uriPattern.endsWith("/")) {
076: uriPattern = uriPattern.substring(0, uriPattern
077: .length() - 1)
078: + "/*";
079: } else {
080: uriPattern = uriPattern + "*";
081: }
082:
083: String text = params.get(0);
084:
085: if (text == null) {
086: text = "Search from here";
087: }
088:
089: writer
090: .write("<div class=\"searchBox\"><form action=\"Search\" method=\"POST\"><input type=\"hidden\" name=\"resourceType\" value=\"All\"/><input type=\"hidden\" name=\"uriPattern\" value=\""
091: + uriPattern
092: + "\"/><span class=\"searchLabel\">"
093: + text
094: + "</span> <input type=\"text\" name=\"query\" class=\"input\"/> <input type=\"submit\" class=\"button\" value=\""
095: + local.getResourceBundle().getString(
096: "main.tabs.search")
097: + "\"/></form></div>");
098: } catch (IOException e) {
099: throw e;
100: } catch (IllegalArgumentException e) {
101: throw e;
102: } catch (Exception e) {
103: throw new IllegalArgumentException("error in searchBox : "
104: + e.getMessage());
105: }
106: }
107:
108: public String getDescription() {
109: return "Display a search box.";
110: }
111:
112: public String[] getParamDescription() {
113: return new String[] { "1. the URI pattern" };
114: }
115: }
|