KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > SearchMacro


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26
27 package org.snipsnap.render.macro;
28
29 import org.apache.lucene.search.Hits;
30 import org.radeox.macro.BaseMacro;
31 import org.radeox.macro.parameter.MacroParameter;
32 import org.radeox.util.i18n.ResourceManager;
33 import org.radeox.util.logging.Logger;
34 import org.snipsnap.app.Application;
35 import org.snipsnap.container.Components;
36 import org.snipsnap.snip.SnipLink;
37 import org.snipsnap.snip.SnipSpace;
38 import org.snipsnap.snip.SnipSpaceFactory;
39 import org.snipsnap.user.AuthenticationService;
40
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43 import java.text.MessageFormat JavaDoc;
44
45 /*
46  * Macro for fulltext searches in SnipSnap. The macro
47  * displays the search results for the input string. Can be
48  * used in snips to "store" searches. For user defined
49  * searches use a {field} macro combined with the {search}
50  * macro.
51  *
52  * @author stephan
53  * @version $Id: SearchMacro.java 1828 2005-04-28 10:07:57Z leo $
54  */

55
56 public class SearchMacro extends BaseMacro {
57   private SnipSpace space;
58   public SearchMacro() {
59     space = SnipSpaceFactory.getInstance();
60   }
61
62   public String JavaDoc getName() {
63     return "search";
64   }
65
66   public String JavaDoc getDescription() {
67     return ResourceManager.getString("i18n.messages", "macro.search.description");
68   }
69
70   public String JavaDoc[] getParamDescription() {
71     return ResourceManager.getString("i18n.messages", "macro.search.params").split(";");
72   }
73
74   public void execute(Writer JavaDoc writer, MacroParameter params)
75       throws IllegalArgumentException JavaDoc, IOException JavaDoc {
76
77     if (params.getLength() == 1 || params.getLength() == 2) {
78       int maxHits = 10;
79       if (params.getLength() == 2) {
80         maxHits = Integer.parseInt(params.get("1"));
81       }
82       String JavaDoc searchString = params.get("0");
83
84       Hits hits = null;
85       try {
86         hits = space.search(searchString);
87       } catch (Exception JavaDoc e) {
88         Logger.warn("SearchMacro: exception while searching: " + e);
89       }
90
91       if (hits != null && hits.length() > 0) {
92         writer.write("<div class=\"list\"><div class=\"list-title\">");
93         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "macro.search.title"),
94                                              ResourceManager.getLocale("i18n.messages"));
95         writer.write(mf.format(new Object JavaDoc[] { searchString, new Integer JavaDoc(hits.length()) }));
96         writer.write("</div>");
97
98         int start = 0;
99         int end = Math.min(maxHits, hits.length());
100         writer.write("<blockquote>");
101         try {
102           for (int i = start; i < end; i++) {
103             SnipLink.appendLink(writer, hits.doc(i).get("title"));
104             if (i < end - 1) {
105               writer.write(", ");
106             }
107           }
108           writer.write("</blockquote></div>");
109         } catch (IOException JavaDoc e) {
110           Logger.warn("I/O error while iterating over search results.");
111         }
112       } else {
113         writer.write(ResourceManager.getString("i18n.messages", "macro.search.notfound"));
114       }
115       AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
116
117       if (searchString != null && searchString.length() > 0 &&
118           !SnipSpaceFactory.getInstance().exists(searchString) &&
119           service.isAuthenticated(Application.get().getUser())) {
120         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "macro.search.create"),
121                                              ResourceManager.getLocale("i18n.messages"));
122         writer.write("<p>");
123         writer.write(mf.format(new String JavaDoc[] { searchString, SnipLink.appendCreateLink(new StringBuffer JavaDoc(), searchString).toString() }));
124         writer.write("</p>");
125       }
126
127       return;
128     } else {
129       throw new IllegalArgumentException JavaDoc("Number of arguments does not match");
130     }
131   }
132 }
133
Popular Tags