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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to