1 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 ; 42 import java.io.Writer ; 43 import java.text.MessageFormat ; 44 45 55 56 public class SearchMacro extends BaseMacro { 57 private SnipSpace space; 58 public SearchMacro() { 59 space = SnipSpaceFactory.getInstance(); 60 } 61 62 public String getName() { 63 return "search"; 64 } 65 66 public String getDescription() { 67 return ResourceManager.getString("i18n.messages", "macro.search.description"); 68 } 69 70 public String [] getParamDescription() { 71 return ResourceManager.getString("i18n.messages", "macro.search.params").split(";"); 72 } 73 74 public void execute(Writer writer, MacroParameter params) 75 throws IllegalArgumentException , IOException { 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 searchString = params.get("0"); 83 84 Hits hits = null; 85 try { 86 hits = space.search(searchString); 87 } catch (Exception 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 mf = new MessageFormat (ResourceManager.getString("i18n.messages", "macro.search.title"), 94 ResourceManager.getLocale("i18n.messages")); 95 writer.write(mf.format(new Object [] { searchString, new Integer (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 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 mf = new MessageFormat (ResourceManager.getString("i18n.messages", "macro.search.create"), 121 ResourceManager.getLocale("i18n.messages")); 122 writer.write("<p>"); 123 writer.write(mf.format(new String [] { searchString, SnipLink.appendCreateLink(new StringBuffer (), searchString).toString() })); 124 writer.write("</p>"); 125 } 126 127 return; 128 } else { 129 throw new IllegalArgumentException ("Number of arguments does not match"); 130 } 131 } 132 } 133 | Popular Tags |