[robsite.org]

« JDeveloper's Java Model outside of JDeveloper | Main | Creating a Mac OS X disk image from the command line »

January 30, 2006

JSF Converter for MT Comments.

I've been working on a JSF front end to my blog lately, and needed to display comments. My first instinct was to create a new JSF component, and although that would be a bit of fun, I decided to just create a converter and use an existing ADF Faces component. I am simply displaying the comments in an outputText, using my Moveable Type converter.

The converter simply replaces each line of the comment with <p>line</p>

If you haven't created a JSF converter before, it's really simple and here are the steps:

  1. Create a class that implements javax.faces.convert.Converter.
  2. register your converter with your faces-config.xml file.
  3. Call your converter in your view.

Here is the Converter source:

1      package org.robsite.adfblog.view;
2      
3      import java.io.BufferedReader;
4      import java.io.IOException;
5      import java.io.StringReader;
6      
7      import javax.faces.component.UIComponent;
8      import javax.faces.context.FacesContext;
9      import javax.faces.convert.Converter;
10     
11     import oracle.adf.view.faces.context.AdfFacesContext;
12     import oracle.adf.view.faces.context.Agent;
13     
14     
15     /**
16      * Movable Type comment converter.  
17      * Wraps a comment from Moveble Type (stored in the database as plain text, 
18      * with CR/LF line endings with html paragraph tags for proper display in a 
19      * web page.  
20      * Note: If ADF Faces is in use, the conversion will only happen if the 
21      * ADF Faces's device type is a desktop device.
22      */
23     public class MtCommentConverter implements Converter {
24         private boolean wrapComment;
25         
26         public MtCommentConverter() {
27             try {
28                 Agent agent = AdfFacesContext.getCurrentInstance().getAgent();
29                 wrapComment = ( agent.getType() == Agent.TYPE_DESKTOP );
30             }
31             catch (Exception e) {
32                 wrapComment = true;
33             }
34         }
35     
36         public Object getAsObject(FacesContext context, 
37                                   UIComponent component, String s) {
38             return s;
39         }
40     
41         public String getAsString(FacesContext context, 
42                                   UIComponent component, Object o) {
43             if ( wrapComment && o instanceof String)
44             {
45                 final String commentRaw = (String)o;
46                 try {
47                     BufferedReader reader = new BufferedReader( new StringReader( commentRaw ) );
48                     
49                     final StringBuilder sb = new StringBuilder( (int)(commentRaw.length() * 1.2 ) );
50                     for( String line = reader.readLine(); line != null; ) {
51                         sb.append( "<p>" + line + "</p>" );
52                         line = reader.readLine();
53                         if ( line == null ) {
54                             reader.close();
55                         }
56                     }
57                     return sb.toString();
58                 }
59                 catch (IOException e) {
60                     return commentRaw;
61                 }
62             }
63             return o.toString();
64         }
65     }

 

Now you need to add an entry into your faces-config.xml file:

1        <converter>
2          <converter-id>org.robsite.adfblog.view.MtCommentConverter</converter-id>
3          <converter-class>org.robsite.adfblog.view.MtCommentConverter</converter-class>
4        </converter>

 

Now finally add the tag into your output tag in your JSP or JSPX page:

1                  <af:outputText value="#{row.commentText}"
2                                  escape="false">
3                    <f:converter converterId="org.robsite.adfblog.view.MtCommentConverter" />
4                  </af:outputText>

 

Posted by rcleveng at January 30, 2006 04:22 PM

Comments

Post a comment




Remember Me?