Re: Table creation helper package for Velocity?

2007-08-08 Thread Familie Laux

Hi,

thanks a lot for the quick responses!

 I haven't actually seen anyone do this sort of thing.  At least, i
 don't recall anyone contributing it.  This would be a good thing to
 have up on our Wiki somewhere (perhaps under CommunityArticles and
 ContributedCode.

Yes, the CommunityArticles and ContributedCode sections
look promising to me as well. I'll try to get something done soon.


I'm not as sure where the VelocityInteractor would fit, but i would be
curious to see/know more about what it does.  In recent months, i've
been mulling over the possibility of a new project (or new package in
the core project) to provide super-easy-to-use wrappers around the
Velocity runtime that are pre-configured (or much more easily and
specifically configured) and enhanced for particular Velocity tasks
(e.g. like a VelocityEmailer).  It's still just a thought at this
point, but a glance at how you're using it makes it look like the
VelocityInteractor could be a such a thing or at least a seed for such
things.


I have attached the source code for the VelocityInteractor. It's
not rocket science, just a wrapper that I found useful for handling
many different VelocityContexts and Templates. Maybe you also find
it useful. Feel free to use or modify.

Thanks,
Matthias

P.S.: I noticed that the mailing list archive seems to discard
attachments, at least I can't see the attachment from my previous
posting in the web interface. Let me know if I should resend it
directly.

/*
 * VelocityException.java
 *
 * Created on 6. August 2007, 09:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author i000698
 */

public class VelocityException extends Exception {

/**
   * Constructs a new codeVelocityException/code exception with 
codenull/code as its
   * detail message.  The cause is not initialized, and may subsequently be
   * initialized by a call to [EMAIL PROTECTED] #initCause}.
   */

public VelocityException() {
super();
}

/**
   * Constructs a new codeVelocityException/code exception with the 
specified detail message.
   * The cause is not initialized, and may subsequently be initialized by a
   * call to [EMAIL PROTECTED] #initCause}.
   * 
   * 
   * @param message   the detail message. The detail message is saved for 
   *  later retrieval by the [EMAIL PROTECTED] #getMessage()} method.
   */

public VelocityException(String message) {
super(message);
}

/**
   * Constructs a new codeVelocityException/code exception with the 
specified detail message and
   * cause.  pNote that the detail message associated with
   * codecause/code is inot/i automatically incorporated in
   * this codeVelocityException/code exception's detail message.
   * 
   * 
   * @param message the detail message (which is saved for later retrieval
   * by the [EMAIL PROTECTED] #getMessage()} method).
   * @param cause the cause (which is saved for later retrieval by the
   * [EMAIL PROTECTED] #getCause()} method).  (A ttnull/tt value is
   * permitted, and indicates that the cause is nonexistent or
   * unknown.)
   */

public VelocityException(String message, Throwable cause) {
super(message, cause);
}

/**
   * Constructs a new codeVelocityException/code exception with the 
specified cause and a
   * detail message of tt(cause==null ? null : cause.toString())/tt
   * (which typically contains the class and detail message of
   * ttcause/tt).  This constructor is useful for 
codeVelocityException/code exceptions
   * that are little more than wrappers for other throwables.
   * 
   * 
   * @param cause the cause (which is saved for later retrieval by the
   * [EMAIL PROTECTED] #getCause()} method).  (A ttnull/tt value is
   * permitted, and indicates that the cause is nonexistent or
   * unknown.)
   */

public VelocityException(Throwable cause) {
super(cause);
}

}



import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;

/**
 * A helper class to encapsulate the interaction with the Velocity template 
engine
 */

public class VelocityInteractor {
  
  private static final String ENCODING = ISO-8859-1;
  
  private MapString,VelocityContext contexts = new 
HashMapString,VelocityContext();
  private MapString,Templatetemplates= new 
HashMapString,Template();
  private MapString,TemplatedefaultTemplates = new 
HashMapString,Template();
  private VelocityContext defaultContext   = new VelocityContext();
  private 

Re: Table creation helper package for Velocity?

2007-08-07 Thread Christopher Schultz
Nathan,

Nathan Bubna wrote:
 I also wonder if there's some way to turn your Table/Cell classes into
 a TableTool of sorts that could go into the VelocityTools project.
 http://velocity.apache.org/tools/devel/

While I think this is an interesting tool, I don't think it's widely
applicable enough to go into the VelocityTools project. Most of the
tools already provided are useul in a very wide range of applications,
but this one seems very narrow.

I like your idea of putting it onto the Contributed Code section of the
Wiki, though.

Just my two cents.
-chris



signature.asc
Description: OpenPGP digital signature


Re: Table creation helper package for Velocity?

2007-08-07 Thread Nathan Bubna
On 8/7/07, Christopher Schultz [EMAIL PROTECTED] wrote:
 Nathan,

 Nathan Bubna wrote:
  I also wonder if there's some way to turn your Table/Cell classes into
  a TableTool of sorts that could go into the VelocityTools project.
  http://velocity.apache.org/tools/devel/

 While I think this is an interesting tool, I don't think it's widely
 applicable enough to go into the VelocityTools project. Most of the
 tools already provided are useul in a very wide range of applications,
 but this one seems very narrow.

Organizing data into tables is pretty common.  things like DisplayTag
are quite popular in the JSP world (actually displaytag is 80% of why
i like to see Velocity and JSP play together better).  While it would
probably take a herculean effort to turn this into a match for that
anytime soon, having something to simplify the process of outputting
tables of data would be a small step in that direction.  i'm not
saying that i'm sure this would fit in VelocityTools, but i'm think it
might with a little work.  i'd at least like to see more of it.

 I like your idea of putting it onto the Contributed Code section of the
 Wiki, though.

 Just my two cents

always appreciated!!

 -chris




-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Table creation helper package for Velocity?

2007-08-06 Thread Familie Laux

Hi,

in my work I quite frequently have the requirement to create somewhat
complex HTML tables using Velocity. complex here refers to cells
spanning multiple rows and columns which are located somewhere inside
the table. That of course leads to interesting challenges in the
Velocity macro to print the proper HTML tags such as tr and
td in those places where they are required.

Until recently, I used hierarchies of #if directives together
with marker variables (#set) e. g. to decide when to print a
tr for a cell that spans multiple rows.

Since that became quite tedious and difficult to maintain I have
searched for an easier way to manage such tables with Velocity. Since
I couldn't find anything really I decided to write some helper classes
myself.

To give you an example of what I mean:

This code snippet

VelocityInteractor interactor = new VelocityInteractor(.);

Table table = new Table(10, 15);
table.setClipping(true);

Cell cell = new Cell(test, 4, 3);
table.setCell(cell, 2, 2);
cell = new Cell(test22, 2, 13);
table.setCell(cell, 8, 6);
cell = new Cell(test33, 10, 1);
table.setCell(cell, 0, 0);
table.setCell(cell, 0, 5);
cell = new Cell(test44, 4, 4);
table.setCell(cell, 0, 11);

BufferedWriter writer = new BufferedWriter(new FileWriter(test.html));

interactor.add(table.vm);
interactor.put(table, table);
interactor.merge(writer);
writer.flush();
writer.close();

creates a table and adds some cells to it. The cells span multiple
rows and columns. Table and Cell are classes that I have created.

The Velocity macro table.vm used here is quite simple:

html
body

#set ( $rowMax = $table.rowNumber - 1 )
#set ( $colMax = $table.colNumber - 1 )

table border=1 cellspacing=0 cellpadding=0

#foreach ( $row in [0..$rowMax])

  tr

  #foreach ( $col in [0..$colMax])

#if ( $table.isVisible($row, $col) )
  #if ( $table.isDefaultCell($row, $col) )
td ($row,$col) /td
  #else
#set ( $cell = $table.getCell($row, $col) )
td rowspan=$cell.rowSpan
colspan=$cell.colSpan
align=center $cell.name
/td
  #end
#end

  #end

#end

/table

/body
/html

and the example HTML output file is attached. All the cells are
as expected with a very simple and clean Velicity macro structure.

There is a lot more that could be said about these helper
classes (for example on how cell data can be transported
into the Velocity macro), but the main question for me is:
is this something that is useful such that it's worth writing
some short article about it for the benefit of others - or has
this been solved a thousand times before (and much better)
but I used the wrong keywords in Google when checking for that?

I realize of course that this is something to complement
Velocity and has nothing to do with enhancing Velocity itself.
Nevertheless, any feedback on the usefulness of this for the
general public would be appreciated. I would then invest more
time in documentation and additional debugging and look for a
place to make this available. Maybe there is a general place for
Velocity add-ons?

Thanks a lot,
Matthias



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]