KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2004 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.logging.Logger;
33 import org.radeox.util.i18n.ResourceManager;
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 xref searches in SnipSnap. The macro
47  * displays the snips reference to the snip for the input string.
48  *
49  * @author Mario Ivankovits
50  * @version $Id: SnipXrefMacro.java 1606 2004-05-17 10:56:18Z leo $
51  */

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