velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Christian <si...@stoutstick.com>
Subject Re: beginner question
Date Thu, 09 Sep 2004 13:40:03 GMT
Hi Mike,

I believe this happens because the example closes the Writer to 
System.out, and for some reason can't reopen it - or possibly the shell 
won't allow it to be reopened. Sorry I can't be more precise in that area...

Anyway you could get around that in various ways, perhaps most 
'correctly' by defining the Writer in the main() method, passing to the 
XMLTest contructor, and then taking care to flush and close it 
afterwards. Something like the basic example below:

------------------------------------------------------------

public class XMLTest
{
     public XMLTest( Writer writer, String templateFile)
     {
         try
         {
		// do Velocity stuff here, using the writer
		// which was passed as a parameter
         }
         catch( Exception e )
         {
            System.out.println("Exception : " + e);
         }
         finally
         {
             if ( writer != null)
             {
                 try
                 {
                     writer.flush();
		    // don't close the writer
                 }
                 catch( Exception ee )
                 {
                     System.out.println("Exception : " + ee );
                 }
             }
         }
     }


     public static void main(String[] args)
     {
         Writer writer = null;

         XMLTest t = new XMLTest(writer, args[0]);
         XMLTest u = new XMLTest(writer, args[0]);

         if( writer != null )
         {
                 try
                 {
                         writer.close();
                 }
                 catch( Exception ee )
                 {
                         System.out.println("Exception : " + ee );
                 }
         }
     }
}
		
------------------------------------------------------------

Note this isn't very 'real world' - there are surely much better ways of 
doing the same sort of thing.

- simon



Mikael Larsson wrote:
> Hi
> I have tried to use a slightly modified version of the
> "velocity-1.4/examples/xmlapp_example"
> the modification I have made is that I have added this line
> u = new XMLTest(args[0]) into the main.
> 
> I excpect that I should see the output twice, but it is only printed once. 
> why??
> 
> //Mike
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message