[jboss-cvs] jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui ...
Christian Bauer
christian.bauer at jboss.com
Wed Feb 28 13:25:09 EST 2007
User: cbauer
Date: 07/02/28 13:25:09
Modified: examples/wiki/src/org/jboss/seam/wiki/core/ui
UIWikiFormattedText.java JSFUtil.java
Added: examples/wiki/src/org/jboss/seam/wiki/core/ui
FileServlet.java FileMetaMap.java
Log:
Basic file attachment/image embedding support
Revision Changes Path
1.3 +12 -3 jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/UIWikiFormattedText.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: UIWikiFormattedText.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/UIWikiFormattedText.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- UIWikiFormattedText.java 16 Feb 2007 16:26:46 -0000 1.2
+++ UIWikiFormattedText.java 28 Feb 2007 18:25:09 -0000 1.3
@@ -8,8 +8,8 @@
import javax.faces.context.FacesContext;
import org.jboss.seam.text.SeamTextLexer;
-import org.jboss.seam.text.SeamTextParser;
import org.jboss.seam.wiki.core.links.WikiTextParser;
+import org.jboss.seam.contexts.Contexts;
import antlr.ANTLRException;
@@ -28,11 +28,16 @@
Reader r = new StringReader((String) getValue());
SeamTextLexer lexer = new SeamTextLexer(r);
+ // TODO: This getAttribute() stuff is not NPE safe!
+
// Use the WikiTextParser to resolve links
- SeamTextParser parser =
+ WikiTextParser parser =
new WikiTextParser(lexer,
getAttributes().get("linkStyleClass").toString(),
- getAttributes().get("brokenLinkStyleClass").toString());
+ getAttributes().get("brokenLinkStyleClass").toString(),
+ getAttributes().get("attachmentLinkStyleClass").toString(),
+ getAttributes().get("inlineLinkStyleClass").toString()
+ );
try {
parser.startRule();
@@ -40,7 +45,11 @@
catch (ANTLRException re) {
throw new RuntimeException(re);
}
+
context.getResponseWriter().write(parser.toString());
+
+ // Put attachments (wiki links...) into the event context for later rendering
+ Contexts.getEventContext().set("wikiTextAttachments", parser.getAttachments());
}
1.2 +18 -1 jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/JSFUtil.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: JSFUtil.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/JSFUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- JSFUtil.java 27 Feb 2007 13:21:45 -0000 1.1
+++ JSFUtil.java 28 Feb 2007 18:25:09 -0000 1.2
@@ -1,12 +1,29 @@
package org.jboss.seam.wiki.core.ui;
+import org.jboss.seam.annotations.Name;
+
+import javax.faces.component.UIData;
import java.util.Collection;
/**
- * Adds stuff to JSF that should be there but isn't (big surprise).
+ * Adds stuff to and for JSF that should be there but isn't.
*/
+ at Name("jsfUtil")
public class JSFUtil {
+ /**
+ * Need to bind UI components to non-conversational backing beans.
+ * That this is even needed makes no sense. Why can't I call the UI components
+ * in the EL directly? Don't try components['id'], it won't work.
+ */
+ private UIData datatable;
+ public UIData getDatatable() { return datatable; }
+ public void setDatatable(UIData datatable) { this.datatable = datatable; }
+
+ /**
+ * Can't use col.size() in a value binding. Why can't I call arbitrary methods, even
+ * with arguments, in a value binding? Java needs properties badly.
+ */
public static int sizeOf(Collection col) {
return col.size();
}
1.1 date: 2007/02/28 18:25:09; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/FileServlet.java
Index: FileServlet.java
===================================================================
package org.jboss.seam.wiki.core.ui;
import org.jboss.seam.wiki.core.dao.NodeDAO;
import org.jboss.seam.wiki.core.prefs.GlobalPreferences;
import org.jboss.seam.util.Transactions;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.swing.*;
import javax.transaction.UserTransaction;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class FileServlet extends HttpServlet {
private static final String DOWNLOAD_PATH = "/download";
/**
* The maximum width allowed for image rescaling
*/
private static final int MAX_IMAGE_WIDTH = 3000;
private byte[] noImage;
public FileServlet() {
InputStream in = getClass().getResourceAsStream("/img/filenotfound.png");
if (in != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
try {
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
noImage = out.toByteArray();
}
catch (IOException e) {
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (DOWNLOAD_PATH.equals(request.getPathInfo())) {
String id = request.getParameter("fileId");
org.jboss.seam.wiki.core.node.File file;
// TODO: Seam should use its transaction interceptor for java beans: http://jira.jboss.com/jira/browse/JBSEAM-957
NodeDAO nodeDAO = (NodeDAO) org.jboss.seam.Component.getInstance("nodeDAO");
UserTransaction userTx = null;
boolean startedTx = false;
try {
userTx = Transactions.getUserTransaction();
if (userTx.getStatus() != javax.transaction.Status.STATUS_ACTIVE) {
startedTx = true;
userTx.begin();
}
file = (!"".equals(id)) ? nodeDAO.findFile(Long.parseLong(id)) : null;
if (startedTx) userTx.commit();
} catch (Exception ex) {
try {
if (startedTx) userTx.rollback();
} catch (Exception rbEx) {
rbEx.printStackTrace();
}
throw new RuntimeException(ex);
}
String contentType = null;
byte[] data = null;
if (file != null && file.getData() != null && file.getData().length > 0) {
contentType = file.getContentType();
data = file.getData();
} else if (noImage != null) {
contentType = "image/png";
data = noImage;
}
if (data != null) {
response.setContentType(contentType);
boolean rescale = false;
int width = 0;
ImageIcon icon = null;
// Check if the image needs to be rescaled (and if the file is an image)
if (request.getParameter("width") != null && file.getImageMetaInfo() != null) {
width = Math.min(MAX_IMAGE_WIDTH, Integer.parseInt(request.getParameter("width")));
icon = new ImageIcon(data);
if (width > 0 && width != icon.getIconWidth())
rescale = true;
}
// Rescale the image if required
if (rescale) {
double ratio = (double) width / icon.getIconWidth();
int height = (int) (icon.getIconHeight() * ratio);
int imageType = "image/png".equals(contentType) ?
BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
BufferedImage bImg = new BufferedImage(width, height, imageType);
Graphics2D g2d = bImg.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(icon.getImage(), 0, 0, width, height, null);
g2d.dispose();
String formatName = "";
if ("image/png".equals(contentType))
formatName = "png";
else if ("image/jpeg".equals(contentType))
formatName = "jpeg";
ImageIO.write(bImg, formatName, response.getOutputStream());
} else {
response.getOutputStream().write(data);
}
}
response.getOutputStream().flush();
}
}
}
1.1 date: 2007/02/28 18:25:09; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/ui/FileMetaMap.java
Index: FileMetaMap.java
===================================================================
package org.jboss.seam.wiki.core.ui;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.AutoCreate;
import java.util.HashMap;
import java.util.Map;
@Name("fileMetaMap")
@AutoCreate
public class FileMetaMap {
public String contentType;
private Map<String, FileMetaInfo> metamap = new HashMap<String, FileMetaInfo>() {
{
put("image/jpg", new FileMetaInfo("icon.fileimg.gif", true));
put("image/jpeg", new FileMetaInfo("icon.fileimg.gif", true));
put("image/gif", new FileMetaInfo("icon.fileimg.gif", true));
put("image/png", new FileMetaInfo("icon.fileimg.gif", true));
put("text/plain", new FileMetaInfo("icon.filetxt.gif", false));
put("application/pdf", new FileMetaInfo("icon.filepdf.gif", false));
put("application/octet-stream", new FileMetaInfo("icon.filegeneric.gif", false));
put("generic", new FileMetaInfo("icon.filegeneric.gif", false));
}
};
@Unwrap
public Map<String, FileMetaInfo> getFielMetaMap() {
return metamap;
}
public class FileMetaInfo {
public String displayIcon;
public boolean image;
public FileMetaInfo(String displayIcon, boolean image) {
this.displayIcon = displayIcon;
this.image = image;
}
public String getDisplayIcon() { return displayIcon; }
public boolean isImage() { return image; }
}
}
More information about the jboss-cvs-commits
mailing list