velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anagha <anajo...@gmail.com>
Subject Re: Can we use File object in VTL?
Date Wed, 08 Feb 2006 05:50:00 GMT
Christoph,
Thanks for suggestions.
Currently I'm using vel-1.4 and tried a sample macro from your script below.

#macro( fileWrite $filename $text )
  #set( $out = $Class.newInstance("java.io.FileOutputStream", $filename) )
  #set( $data = $text.getBytes() )
  #call( $out.write($data) )
  #call( $out.close() )
#end
##
#macro( call $foo )#if($foo)#**##end#end

#fileWrite( "SampJava" "intercae ABC{}" )

I'm expecting that a file "SampJava" should be created with the text
"intercae ABC{}" in it.
Here I see "Class: variable is not set.
This is not working, Pls have a look for this macro.

Also, in vel-1.4 I did see "ContextTool" class, pls. let me know its
package.

Thanks,
Anagha


On 2/7/06, apache@recks.org <apache@recks.org> wrote:
>
> Hi Anagha,
>
> I sometimes use velocity to generate diverse ASCII files from
> CSV or other inputs. I use my own TemplateTool that has some
> few goodies in the context - like the ClassTool used in the macro
> below.
>
> ##
> ------------------------------------------------------------------------
> ## Macro to write a text to a file.
> ##
> ------------------------------------------------------------------------
> #macro( fileWrite $filename $text )
>    #set( $out = $Class.newInstance("java.io.FileOutputStream", $filename)
> )
>    #set( $data = $text.getBytes() )
>    #call( $out.write($data) )
>    #call( $out.close() )
> #end
> ##
> ##
> ------------------------------------------------------------------------
> ## convenience directive to invoke a method and ignore the return value
> ##
> ------------------------------------------------------------------------
> #macro( call $foo )#if($foo)#**##end#end
> ##
> ##
> ------------------------------------------------------------------------
> ## Some helpful local context tools:
> ##
> ------------------------------------------------------------------------
> #set( $LF = $Context.formDecode("%0A") )
> #set( $Integer = 1 )
> #set( $Long = $Integer.longValue() )
> #set( $now = $date.clone() )
> ##
> ##
> ------------------------------------------------------------------------
> ## Some simple formatters
> ##
> ------------------------------------------------------------------------
> #macro( digits4 $number )#*
>    *##set( $id = "$number" )#*         -- access the macro paramter,
> ensure its a String
>    *##set( $id = "0000$id.trim()" )#*  -- prepend the padding, clipping
> whitespace artefacts
>    *##set( $len = $id.length() - 4 )#* -- compute the offset for the rest
> length
>    *#$id.substring($len)##             -- emit the resulting padded string
> #end
> #macro( digits2 $number )#*
>    *##set( $id = "$number" )#*
>    *##set( $id = "00$id.trim()" )#*
>    *##set( $len = $id.length() - 2 )#*
>    *#$id.substring($len)##
> #end
> #macro( dateStr $d )#*
>    *##set( $y = $d.year + 1900 )#*
>
> *##digits4($y)#digits2($d.month)#digits2($d.date)#digits2($d.hours)#digits2($d.minutes)#digits2($d.seconds)##
> #end
>
>
> You can do things like:
>
> ## -- read file
> #set( $text = "#parse($inputName)" )
> #set( $lines = $text.trim().split($LF) )
> #foreach( $line in $lines )
>    ...
> #end
> ...
> #set( $filename =
> "E${statelliteId}_${orderId}_#digits4($nr)_#dateStr($now).ORD" )
> writing output to: $filename
> #fileWrite( $filename $order )##
>
> This does things that maybe should have been done in perl ;)
>
> Cheers,
> Christoph
>
> Anagha wrote:
> > Hi,
> > I'm using VTL to build Java file. I need to change/insert  some
> keywords,
> > variables in the Java file based on context paramters.
> > Instead of putting the whole Java file in ".vm" file, can I load the
> file in
> > some test program and put the "File" descriptor in the context?
> > Later in ".vm" I'll retrieve this file descriptor from context and
> output
> > some lines of the file.
> >
> > I'have tried in similar way for ".xml' where I load and built Document
> > object from XML in test program. I put the "root" element of the xml
> > in the context and later processed the doc in ".vm" by extracting root
> from
> > context.
> >
> > I want to know can we do on similar lines for File descriptor object.
> > Any help is welcome.
> >
> > --
> > Thanks & Regards,
> > Anagha
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Thanks & Regards,
Anagha

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message