[jboss-cvs] jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/links ...
Christian Bauer
christian.bauer at jboss.com
Wed Feb 28 13:25:07 EST 2007
User: cbauer
Date: 07/02/28 13:25:07
Modified: examples/wiki/src/org/jboss/seam/wiki/core/links
WikiTextParser.java WikiLinkResolver.java
Log:
Basic file attachment/image embedding support
Revision Changes Path
1.3 +66 -12 jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/links/WikiTextParser.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WikiTextParser.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/links/WikiTextParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- WikiTextParser.java 16 Feb 2007 16:26:44 -0000 1.2
+++ WikiTextParser.java 28 Feb 2007 18:25:07 -0000 1.3
@@ -2,39 +2,93 @@
import org.jboss.seam.text.SeamTextParser;
import org.jboss.seam.Component;
+import org.jboss.seam.wiki.core.node.File;
import antlr.TokenStream;
import java.util.Map;
import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
public class WikiTextParser extends SeamTextParser {
private String linkClass;
private String brokenLinkClass;
+ private String attachmentLinkClass;
+ private String inlineLinkClass;
- private Map<String, WikiLinkResolver.WikiLink> links = new HashMap<String, WikiLinkResolver.WikiLink>();
private WikiLinkResolver resolver;
- public WikiTextParser(TokenStream tokenStream, String linkClass, String brokenLinkClass) {
+ private List<WikiLinkResolver.WikiLink> attachments = new ArrayList<WikiLinkResolver.WikiLink>();
+ private Map<String, WikiLinkResolver.WikiLink> links = new HashMap<String, WikiLinkResolver.WikiLink>();
+
+ public WikiTextParser(TokenStream tokenStream,
+ String linkClass, String brokenLinkClass, String attachmentLinkClass, String inlineLinkClass) {
super(tokenStream);
this.linkClass = linkClass;
this.brokenLinkClass = brokenLinkClass;
+ this.attachmentLinkClass = attachmentLinkClass;
+ this.inlineLinkClass = inlineLinkClass;
resolver = (WikiLinkResolver)Component.getInstance(WikiLinkResolver.class);
}
- // TODO: Not a pretty dependency... this needs to be called first
- protected String linkUrl(String linkText) {
+ protected String linkTag(String descriptionText, String linkText) {
+
+ // Resolve the link
resolver.resolveWikiLink(links, linkText.trim());
- return links.get(linkText).url;
+ WikiLinkResolver.WikiLink link = links.get((linkText));
+
+ String finalDescriptionText =
+ (descriptionText!=null && descriptionText.length() > 0 ? descriptionText : link.description);
+
+ // Link to file (inline or attached)
+ if (WikiLinkResolver.isFile(link.node)) {
+ File file = (File)link.node;
+
+ if (file.getImageMetaInfo() == null || 'A' == file.getImageMetaInfo().getThumbnail()) {
+ // It's an attachment
+ if (!attachments.contains(link)) attachments.add(link);
+ return "<a href=\"#attachment"
+ + (attachments.indexOf(link)+1)
+ + "\" class=\""
+ + attachmentLinkClass
+ + "\">"
+ + finalDescriptionText
+ + "</a>";
+ } else {
+ // It's an image and we need to show it inline
+ int thumbnailWidth;
+ switch(file.getImageMetaInfo().getThumbnail()) {
+ case 'S': thumbnailWidth = 80; break;
+ case 'M': thumbnailWidth = 160; break;
+ case 'L': thumbnailWidth = 320; break;
+ default: thumbnailWidth = file.getImageMetaInfo().getSizeX();
}
+ String thumbnailUrl = link.url + "&width=" + thumbnailWidth;
- // then this needs to be called by the parser
- protected String linkDescription(String descriptionText, String linkText) {
- if (descriptionText != null && descriptionText.length() >0) return descriptionText;
- return links.get(linkText).description;
+ return "<a href=\""
+ + link.url
+ + "\" class=\""
+ + inlineLinkClass
+ + "\"><img src=\""
+ + thumbnailUrl
+ + "\"/></a>";
+ }
}
- protected String linkClass(String linkText) {
- return links.get(linkText).broken ? brokenLinkClass : linkClass;
+ // Regular link
+ return "<a href=\""
+ + link.url
+ + "\" class=\""
+ + (link.broken ? brokenLinkClass : linkClass)
+ + "\">"
+ + finalDescriptionText
+ + "</a>";
}
+
+
+ public List<WikiLinkResolver.WikiLink> getAttachments() {
+ return attachments;
+ }
+
}
1.4 +33 -10 jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/links/WikiLinkResolver.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WikiLinkResolver.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/links/WikiLinkResolver.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- WikiLinkResolver.java 21 Feb 2007 16:24:11 -0000 1.3
+++ WikiLinkResolver.java 28 Feb 2007 18:25:07 -0000 1.4
@@ -3,9 +3,11 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Transactional;
+import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.wiki.core.node.Document;
import org.jboss.seam.wiki.core.node.Directory;
import org.jboss.seam.wiki.core.node.Node;
+import org.jboss.seam.wiki.core.node.File;
import org.jboss.seam.wiki.core.prefs.GlobalPreferences;
import org.jboss.seam.wiki.core.dao.NodeDAO;
import org.jboss.seam.Component;
@@ -16,6 +18,7 @@
import java.util.Map;
@Name("wikiLinkResolver")
+ at AutoCreate
public class WikiLinkResolver {
// Prepended to primary keys in the database, e.g. [This is a stored link=>wiki://5]
@@ -45,17 +48,18 @@
Pattern.quote("[") + "([^" + Pattern.quote("]") + "|" + Pattern.quote("[") + "]*)" +
"=>" + WIKI_PROTOCOL + Pattern.quote("]");
- @In(create = true)
+ @In
private NodeDAO nodeDAO;
+ @In
+ protected Directory wikiRoot;
+
@In(required = false)
private Document currentDocument;
@In(required = false)
private Directory currentDirectory;
- @In(create = true)
- protected Directory wikiRoot;
public static String convertToWikiName(String realName) {
return realName.replaceAll(WIKINAME_REMOVECHARACTERS, "");
@@ -127,7 +131,7 @@
// Find the node by PK
Node node = nodeDAO.findNode(Long.valueOf(wikiUrlMatcher.group(1)));
if (node != null) {
- wikiLink = new WikiLink(node.getId(), false, renderURL(node), node.getName());
+ wikiLink = new WikiLink(node, false, renderURL(node), node.getName());
} else {
wikiLink = new WikiLink(null, true, BROKENLINK_URL, BROKENLINK_DESCRIPTION);
}
@@ -139,7 +143,7 @@
Node node = resolveCrossAreaLinkText(currentDirectory, linkText);
if (node!=null) {
- wikiLink = new WikiLink(node.getId(), false, renderURL(node), node.getName());
+ wikiLink = new WikiLink(node, false, renderURL(node), node.getName());
// Run the converter again and UPDATE the currentDocument (yes, this happens during rendering!)
currentDocument.setContent(convertToWikiLinks(currentDirectory, currentDocument.getContent()));
// This should be updated in the database during the next flush()
@@ -179,18 +183,21 @@
}
public class WikiLink {
- Long nodeId;
+ Node node;
boolean broken = false;
String url;
String description;
- public WikiLink(Long nodeId, boolean broken, String url, String description) {
- this.nodeId = nodeId;
+ public WikiLink(Node node, boolean broken, String url, String description) {
+ this.node = node;
this.url = url;
this.broken = broken;
this.description = description;
}
-
+ public Node getNode() { return node; }
+ public boolean isBroken() { return broken; }
+ public String getUrl() { return url; }
+ public String getDescription() { return description; }
public String toString() {
return "Description: " + description + " URL: " + url;
}
@@ -198,9 +205,12 @@
public static String renderURL(Node node) {
GlobalPreferences globalPrefs = (GlobalPreferences) Component.getInstance("globalPrefs");
-
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
+ if (isFile(node)) {
+ return contextPath + "/files/download?fileId=" + node.getId();
+ }
+
if (globalPrefs.getDefaultURLRendering().equals(GlobalPreferences.URLRendering.PERMLINK)) {
return contextPath + "/" + node.getId() + globalPrefs.getPermlinkSuffix();
} else {
@@ -210,4 +220,17 @@
}
}
+ // Replacement for missing instaceOf in EL (can't use string comparison, might be proxy)
+ public static boolean isDirectory(Node node) {
+ return node != null && Directory.class.isAssignableFrom(node.getClass());
+ }
+
+ public static boolean isDocument(Node node) {
+ return node != null && Document.class.isAssignableFrom(node.getClass());
+ }
+
+ public static boolean isFile(Node node) {
+ return node != null && File.class.isAssignableFrom(node.getClass());
+ }
+
}
More information about the jboss-cvs-commits
mailing list