1 25 package org.snipsnap.render.macro; 26 27 import org.radeox.api.engine.ImageRenderEngine; 28 import org.radeox.api.engine.RenderEngine; 29 import org.radeox.util.Encoder; 30 import org.radeox.util.i18n.ResourceManager; 31 import org.snipsnap.app.Application; 32 import org.snipsnap.config.Configuration; 33 import org.snipsnap.render.macro.parameter.SnipMacroParameter; 34 import org.snipsnap.snip.Snip; 35 import org.snipsnap.snip.SnipLink; 36 import org.snipsnap.snip.SnipSpaceFactory; 37 38 import java.io.IOException ; 39 import java.io.Writer ; 40 import java.util.ResourceBundle ; 41 42 49 50 public class ImageMacro extends SnipMacro { 51 Configuration config; 52 53 public String getName() { 54 return "image"; 55 } 56 57 public String getDescription() { 58 return ResourceManager.getString("i18n.messages", "macro.image.description"); 59 } 60 61 public String [] getParamDescription() { 62 return ResourceManager.getString("i18n.messages", "macro.image.params").split(";"); 63 } 64 65 public ImageMacro() { 66 config = Application.get().getConfiguration(); 67 } 68 69 public void execute(Writer writer, SnipMacroParameter params) 70 throws IllegalArgumentException , IOException { 71 72 RenderEngine engine = params.getContext().getRenderEngine(); 73 74 if (engine instanceof ImageRenderEngine) { 75 ImageRenderEngine imageEngine = (ImageRenderEngine) engine; 76 77 if (params.getLength() > 0) { 78 String img = params.get("img"); 79 String alt = null, ext = null, align = null, target = null; 80 boolean qualifiedParams = img != null; 81 if (qualifiedParams) { 82 alt = params.get("alt"); 83 ext = params.get("ext"); 84 align = params.get("align"); 85 target = params.get("target"); 86 } else { 87 img = params.get(0); 88 alt = params.get(1); 89 ext = params.get(2); 90 align = params.get(3); 91 target = params.get(4); 92 } 93 94 String link = params.get("link"); 95 if (link != null) { 96 writer.write("<a HREF=\"" + Encoder.escape(link) + "\""); 97 if (target != null) { 98 writer.write("target=\""+ Encoder.escape(target) +"\""); 99 } 100 writer.write(">"); 101 } 102 103 String imageName = img; 104 105 if (imageName.startsWith("http://")) { 106 if (config.allow(Configuration.APP_PERM_EXTERNALIMAGES)) { 107 appendExternalImage(writer, imageName, align); 108 } 109 } else { 110 int dotIndex = imageName.lastIndexOf('.'); 112 if (-1 != dotIndex) { 113 ext = imageName.substring(dotIndex + 1); 114 imageName = imageName.substring(0, dotIndex); 115 } 116 117 Snip snip = params.getSnipRenderContext().getSnip(); 118 int slashIndex = imageName.lastIndexOf('/'); 119 if(-1 != slashIndex) { 120 String snipName = imageName.substring(0, slashIndex); 121 snip = SnipSpaceFactory.getInstance().load(snipName); 122 imageName = imageName.substring(slashIndex + 1); 123 } 124 125 if ("svg".equals(ext)) { 126 writer.write("<object data=\""); 128 writer.write(snip.getNameEncoded() + "/" + imageName); 129 writer.write("."); 130 writer.write(ext); 131 writer.write("\" type=\"image/svg+xml\" width=\"400\" height=\"400\"></object>"); 132 } else { 133 SnipLink.appendImage(writer, snip, imageName, alt, ext, align); 134 } 135 } 136 137 if (link != null) { 138 writer.write("</a>"); 139 } 140 } 141 } else { 142 throw new IllegalArgumentException ("Number of arguments does not match"); 143 } 144 return; 145 } 146 147 148 public static Writer appendExternalImage(Writer writer, String url, String position) throws IOException { 149 writer.write("<img SRC=\""); 150 writer.write(url); 151 writer.write("\" "); 152 if (position != null) { 153 writer.write("class=\""); 154 writer.write(position); 155 writer.write("\" "); 156 } 157 writer.write("border=\"0\"/>"); 158 return writer; 159 } 160 } 161 | Popular Tags |