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.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 ; 42 import java.io.Writer ; 43 import java.text.MessageFormat ; 44 45 52 53 public class SnipXrefMacro extends BaseMacro { 54 private SnipSpace space; 55 56 public SnipXrefMacro() { 57 space = SnipSpaceFactory.getInstance(); 58 } 59 60 public String getName() { 61 return "snip-xref"; 62 } 63 64 public String [] getParamDescription() { 65 return ResourceManager.getString("i18n.messages", "macro.snipxref.params").split(";"); 66 } 67 68 public String getDescription() { 69 return ResourceManager.getString("i18n.messages", "macro.snipxref.description"); 70 } 71 72 public void execute(Writer writer, MacroParameter params) 73 throws IllegalArgumentException , IOException { 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 searchString = params.get("0"); 81 82 Hits hits = null; 83 try { 84 hits = space.search("reference:\"" + searchString + "\""); 85 } catch (Exception 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 mf = new MessageFormat (ResourceManager.getString("i18n.messages", "macro.snipxref.title")); 93 writer.write(mf.format(new Object [] { searchString, new Integer (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 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 mf = new MessageFormat (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 ("Number of arguments does not match"); 127 } 128 } 129 } 130 | Popular Tags |