r1683 - xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro
Marta Girdea
marta_girdea at users.forge.objectweb.org
Wed Dec 6 15:08:07 CET 2006
Author: marta_girdea
Date: 2006-12-06 15:08:05 +0100 (Wed, 06 Dec 2006)
New Revision: 1683
Modified:
xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/AttachMacro.java
xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/ImageMacro.java
Log:
XWIKI-406: Add support for external documents in the attach and image macros
New syntax for the attach macro:
{attach:text|file|document|title|rel|id|fromIncludingDoc}
New syntax for the image macro:
{image:text|height|width|align|document|alt|title|link|fromIncludingDoc}
Changes:
- The macros can use a different document as the container of the image/attachment
- If a parameter is not specified, and it's default position contains another parameter, it is no longer an error
- Support for specifying more attributes
Modified: xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/AttachMacro.java
===================================================================
--- xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/AttachMacro.java 2006-12-06 09:24:34 UTC (rev 1682)
+++ xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/AttachMacro.java 2006-12-06 14:08:05 UTC (rev 1683)
@@ -16,58 +16,138 @@
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- *
- * @author sdumitriu
*/
+/**
+ * Macro that outputs links to document attachments.
+ *
+ * Syntax: {attach:text|file|document|title|rel|id|fromIncludingDoc}
+ * <ul>
+ * <li>text: The link text. If missing, the file name is used.</li>
+ * <li>file: The target filename. If missing, the link text is used.</li>
+ * <li>document: The document to which the file is attached. If missing,
+ * the current document is used.</li>
+ * <li>title: The title attribute of the link. If missing, no title.</li>
+ * <li>rel: The rel attribute of the link. If missing, no rel.</li>
+ * <li>id: The id attribute of the link. If missing, no id.</li>
+ * <li>fromIncludingDoc: If present, when the current document is included using
+ * #include*, use the top level document instead of the included one.
+ * This is useful for templates, for example.</li>
+ * </ul>
+ */
+
package com.xpn.xwiki.render.macro;
-import com.xpn.xwiki.XWikiContext;
-import com.xpn.xwiki.doc.XWikiDocument;
-import com.xpn.xwiki.render.XWikiRadeoxRenderEngine;
+import java.io.IOException;
+import java.io.Writer;
+
import org.radeox.api.engine.RenderEngine;
import org.radeox.api.engine.context.RenderContext;
import org.radeox.macro.BaseLocaleMacro;
import org.radeox.macro.parameter.MacroParameter;
import org.radeox.util.Encoder;
-import java.io.IOException;
-import java.io.Writer;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.render.XWikiRadeoxRenderEngine;
-public class AttachMacro extends BaseLocaleMacro {
- public String getLocaleKey() {
- return "macro.attach";
- }
+public class AttachMacro extends BaseLocaleMacro {
+ public String getLocaleKey() {
+ return "macro.attach";
+ }
- public void execute(Writer writer, MacroParameter params)
- throws IllegalArgumentException, IOException {
+ public void execute(Writer writer, MacroParameter params) throws IllegalArgumentException, IOException {
- RenderContext context = params.getContext();
- RenderEngine engine = context.getRenderEngine();
+ RenderContext context = params.getContext();
+ RenderEngine engine = context.getRenderEngine();
- String text = params.get("text", 0);
- String filename = params.get("file", 1);
+ // Read the parameters
+ String text = params.get("text", 0);
+ if (null == text || text.indexOf("=") != -1) {
+ text = null;
+ }
+ String filename = params.get("file", 1);
+ if (null == filename || filename.indexOf("=") != -1) {
+ filename = null;
+ }
+ String document = params.get("document", 2);
+ if (null == document || document.indexOf("=") != -1) {
+ document = null;
+ }
+ String title = params.get("title", 3);
+ if (null == title || title.indexOf("=") != -1 || title.equals("")) {
+ title = null;
+ }
+ String rel = params.get("rel", 4);
+ if (null == rel || rel.indexOf("=") != -1 || rel.equals("")) {
+ rel = null;
+ }
+ String id = params.get("id", 5);
+ if (null == id || id.indexOf("=") != -1 || id.equals("")) {
+ id = null;
+ }
+ String useIncluder = params.get("fromIncludingDoc", 6);
+ if (null == useIncluder || useIncluder.indexOf("=") != -1 || useIncluder.toLowerCase().startsWith("f")) {
+ useIncluder = null;
+ }
- if(params.getLength() == 1) {
- filename = text;
- text = Encoder.toEntity(text.charAt(0)) + Encoder.escape(text.substring(1));
- }
+ // Fix missing text or filename parameters
+ if (text == null && filename != null) {
+ text = filename;
+ } else if (text != null && filename == null) {
+ filename = text;
+ } else {
+ return;
+ }
+ text = Encoder.escape(text);
+ XWikiContext xcontext = ((XWikiRadeoxRenderEngine) engine).getContext();
- XWikiContext xcontext = ((XWikiRadeoxRenderEngine)engine).getContext();
- XWikiDocument doc = xcontext.getDoc();
+ // Get the target document
+ XWikiDocument doc = null;
+ try {
+ if (document == null || "".equals(document)) {
+ if (useIncluder == null) {
+ doc = (XWikiDocument) xcontext.get("sdoc");
+ }
+ } else {
+ doc = xcontext.getWiki().getDocument(xcontext.getDoc().getSpace(), document, xcontext);
+ }
+ } catch (Exception ex) {
+ // NullPointer or ClassCast
+ }
+ if (doc == null) {
+ doc = xcontext.getDoc();
+ }
- StringBuffer str = new StringBuffer();
- str.append("<a href=\"");
- str.append(doc.getAttachmentURL(filename, "download", xcontext));
- str.append("\" />");
- str.append(text);
- str.append("</a>");
- writer.write(str.toString());
+ // Create the link code
+ StringBuffer str = new StringBuffer();
+ str.append("<a href=\"");
+ if (filename.indexOf("tp://") != -1) {
+ str.append(filename);
+ } else {
+ str.append(doc.getAttachmentURL(filename, "download", xcontext));
+ }
+ str.append("\"");
+ if (rel != null) {
+ str.append(" rel=\"" + rel + "\"");
+ }
+ if (title != null) {
+ str.append(" title=\"" + title + "\"");
+ }
+ if (id != null) {
+ str.append(" id=\"" + id + "\"");
+ }
+ str.append(">");
+ str.append(text);
+ str.append("</a>");
- // if (!"none".equals(img) && engine instanceof ImageRenderEngine) {
- // writer.write(((ImageRenderEngine) engine).getExternalImageLink());
- // }
+ // All done, flush the StringBufer
+ writer.write(str.toString());
- }
+ // if (!"none".equals(img) && engine instanceof ImageRenderEngine) {
+ // writer.write(((ImageRenderEngine) engine).getExternalImageLink());
+ // }
+
+ }
}
Modified: xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/ImageMacro.java
===================================================================
--- xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/ImageMacro.java 2006-12-06 09:24:34 UTC (rev 1682)
+++ xwiki/trunk/core/src/main/java/com/xpn/xwiki/render/macro/ImageMacro.java 2006-12-06 14:08:05 UTC (rev 1683)
@@ -1,4 +1,4 @@
-/*
+/**
* Copyright 2006, XpertNet SARL, and individual contributors as indicated
* by the contributors.txt.
*
@@ -22,51 +22,148 @@
package com.xpn.xwiki.render.macro;
-import com.xpn.xwiki.XWikiContext;
-import com.xpn.xwiki.doc.XWikiDocument;
-import com.xpn.xwiki.render.XWikiRadeoxRenderEngine;
+import java.io.IOException;
+import java.io.Writer;
+
import org.radeox.api.engine.RenderEngine;
import org.radeox.api.engine.context.RenderContext;
import org.radeox.macro.BaseLocaleMacro;
import org.radeox.macro.parameter.MacroParameter;
-import java.io.IOException;
-import java.io.Writer;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.render.XWikiRadeoxRenderEngine;
-public class ImageMacro extends BaseLocaleMacro {
- public String getLocaleKey() {
- return "macro.image";
- }
+/**
+ * Macro that displays images.
+ *
+ * Syntax: {image:text|height|width|align|document|alt|title|link|fromIncludingDoc}
+ * <ul>
+ * <li>text: The target filename.</li>
+ * <li>height: The height attribute of the img.</li>
+ * <li>width: The width attribute of the img.</li>
+ * <li>align: The align attribute of the img.</li>
+ * <li>document: The document to which the file is attached. If missing,
+ * the current document is used.</li>
+ * <li>alt: The alt attribute of the link. If missing, title is used.
+ * If both are missing, use the filename.</li>
+ * <li>title: The title attribute of the link. If missing, alt is used.
+ * If both are missing, use the filename.</li>
+ * <li>link: Also link to the image.</li>
+ * <li>fromIncludingDoc: If present, when the current document is included using
+ * #include*, use the top level document instead of the included one.
+ * This is useful for templates, for example.</li>
+ * </ul>
+ * @author sdumitriu
+ */
+public class ImageMacro extends BaseLocaleMacro {
+ public String getLocaleKey() {
+ return "macro.image";
+ }
- public void execute(Writer writer, MacroParameter params)
- throws IllegalArgumentException, IOException {
+ public void execute(Writer writer, MacroParameter params) throws IllegalArgumentException, IOException {
- RenderContext context = params.getContext();
- RenderEngine engine = context.getRenderEngine();
+ RenderContext context = params.getContext();
+ RenderEngine engine = context.getRenderEngine();
- String img = params.get("text", 0);
- String height = params.get("height", 1);
- String width = params.get("width", 2);
- String align = params.get("align", 3);
- XWikiContext xcontext = ((XWikiRadeoxRenderEngine)engine).getContext();
- XWikiDocument doc = xcontext.getDoc();
+ // Read the parameters
+ String img = params.get("text", 0);
+ if (null == img || img.indexOf("=") != -1) {
+ return;
+ }
+ String height = params.get("height", 1);
+ if (null == height || height.indexOf("=") != -1) {
+ height = null;
+ }
+ String width = params.get("width", 2);
+ if (null == width || width.indexOf("=") != -1) {
+ width = null;
+ }
+ String align = params.get("align", 3);
+ if (null == align || align.indexOf("=") != -1) {
+ align = null;
+ }
+ String document = params.get("document", 4);
+ if (null == document || document.indexOf("=") != -1) {
+ document = null;
+ }
+ String alt = params.get("alt", 5);
+ if (null == alt || alt.indexOf("=") != -1) {
+ alt = null;
+ }
+ String title = params.get("title", 6);
+ if (null == title || title.indexOf("=") != -1) {
+ title = null;
+ }
+ String link = params.get("link", 7);
+ if (null == link || link.indexOf("=") != -1 || link.toLowerCase().startsWith("f")) {
+ link = null;
+ }
+ String useIncluder = params.get("fromIncludingDoc", 8);
+ if (null == useIncluder || useIncluder.indexOf("=") != -1 || useIncluder.toLowerCase().startsWith("f")) {
+ useIncluder = null;
+ }
- StringBuffer str = new StringBuffer();
- str.append("<img src=\"");
- str.append(doc.getAttachmentURL(img, "download", xcontext));
- str.append("\" ");
- if ((!"none".equals(height))&&(height!=null)&&(!"".equals(height.trim()))){
- str.append("height=\"" + height.trim() + "\" ");
+ // Fix missing alt or title parameters
+ if (alt == null && title != null) {
+ alt = title;
+ } else if (alt != null && title == null) {
+ title = alt;
+ } else {
+ alt = title = img;
+ }
+
+ XWikiContext xcontext = ((XWikiRadeoxRenderEngine) engine).getContext();
+
+ // Get the target document
+ XWikiDocument doc = null;
+ try {
+ if (document == null || "".equals(document)) {
+ if (useIncluder == null) {
+ doc = (XWikiDocument) xcontext.get("sdoc");
+ }
+ } else {
+ doc = xcontext.getWiki().getDocument(xcontext.getDoc().getSpace(), document, xcontext);
+ }
+ } catch (Exception ex) {
+ // NullPointer or ClassCast
+ }
+ if (doc == null) {
+ doc = xcontext.getDoc();
+ }
+
+ // Create the img code
+ StringBuffer str = new StringBuffer();
+ if (link != null) {
+ str.append("<a href=\"" + doc.getAttachmentURL(img, "download", xcontext) + "\">");
+ }
+ str.append("<img src=\"");
+
+ if (img.indexOf("tp://") != -1) {
+ str.append(img);
+ } else {
+ str.append(doc.getAttachmentURL(img, "download", xcontext));
+ }
+ str.append("\" ");
+ if ((!"none".equals(height)) && (height != null) && (!"".equals(height.trim()))) {
+ str.append("height=\"" + height.trim() + "\" ");
+ }
+ if ((!"none".equals(width)) && (width != null) && (!"".equals(width.trim()))) {
+ str.append("width=\"" + width.trim() + "\" ");
+ }
+ if ((!"none".equals(align)) && (align != null) && (!"".equals(align.trim()))) {
+ str.append("align=\"" + align.trim() + "\" ");
+ }
+ str.append("alt=\"");
+ str.append(alt);
+ str.append("\" title=\"");
+ str.append(title);
+ str.append("\"/>");
+ if (link != null) {
+ str.append("</a>");
+ }
+
+ // All done, flush the StringBufer
+ writer.write(str.toString());
}
- if ((!"none".equals(width))&&(width!=null)&&(!"".equals(width.trim()))){
- str.append("width=\"" + width.trim() + "\" ");
- }
- if ((!"none".equals(align))&&(align!=null)&&(!"".equals(align.trim()))){
- str.append("align=\"" + align.trim() + "\" ");
- }
- str.append("alt=\"");
- str.append(img);
- str.append("\" />");
- writer.write(str.toString());
- }
}
\ No newline at end of file
More information about the Xwiki-notifications
mailing list