Return-Path: Delivered-To: apmail-jakarta-velocity-user-archive@www.apache.org Received: (qmail 41007 invoked from network); 8 Feb 2006 05:50:30 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 8 Feb 2006 05:50:30 -0000 Received: (qmail 62332 invoked by uid 500); 8 Feb 2006 05:50:24 -0000 Delivered-To: apmail-jakarta-velocity-user-archive@jakarta.apache.org Received: (qmail 62317 invoked by uid 500); 8 Feb 2006 05:50:23 -0000 Mailing-List: contact velocity-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Velocity Users List" Reply-To: "Velocity Users List" Delivered-To: mailing list velocity-user@jakarta.apache.org Received: (qmail 62306 invoked by uid 99); 8 Feb 2006 05:50:23 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Feb 2006 21:50:23 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of anajoshi@gmail.com designates 66.249.92.205 as permitted sender) Received: from [66.249.92.205] (HELO uproxy.gmail.com) (66.249.92.205) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Feb 2006 21:50:22 -0800 Received: by uproxy.gmail.com with SMTP id k40so185245ugc for ; Tue, 07 Feb 2006 21:50:01 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=jhZObbj+aPI1FcdVimMTmROuIoKPKa4PyWTrjpNxoKlgVf35SktS3UqzJC043ELPsVA2gBwtF0YQ+3dVx4Holr2fG7XsisPhJ2v36b4y/6e+vFnk3DLrs1O8Y7CV8oo4ONqD/WUERjlkRxu9GNphQ8c0U4o+S6GOc6TtOvAsusE= Received: by 10.48.217.10 with SMTP id p10mr1842426nfg; Tue, 07 Feb 2006 21:50:00 -0800 (PST) Received: by 10.49.3.14 with HTTP; Tue, 7 Feb 2006 21:50:00 -0800 (PST) Message-ID: Date: Wed, 8 Feb 2006 11:20:00 +0530 From: Anagha To: Velocity Users List Subject: Re: Can we use File object in VTL? In-Reply-To: <43E8A671.80903@recks.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_3515_24789299.1139377800560" References: <43E8A671.80903@recks.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N ------=_Part_3515_24789299.1139377800560 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 =3D $Class.newInstance("java.io.FileOutputStream", $filename) = ) #set( $data =3D $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 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 =3D $Class.newInstance("java.io.FileOutputStream", $filenam= e) > ) > #set( $data =3D $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 =3D $Context.formDecode("%0A") ) > #set( $Integer =3D 1 ) > #set( $Long =3D $Integer.longValue() ) > #set( $now =3D $date.clone() ) > ## > ## > ------------------------------------------------------------------------ > ## Some simple formatters > ## > ------------------------------------------------------------------------ > #macro( digits4 $number )#* > *##set( $id =3D "$number" )#* -- access the macro paramter, > ensure its a String > *##set( $id =3D "0000$id.trim()" )#* -- prepend the padding, clipping > whitespace artefacts > *##set( $len =3D $id.length() - 4 )#* -- compute the offset for the re= st > length > *#$id.substring($len)## -- emit the resulting padded strin= g > #end > #macro( digits2 $number )#* > *##set( $id =3D "$number" )#* > *##set( $id =3D "00$id.trim()" )#* > *##set( $len =3D $id.length() - 2 )#* > *#$id.substring($len)## > #end > #macro( dateStr $d )#* > *##set( $y =3D $d.year + 1900 )#* > > *##digits4($y)#digits2($d.month)#digits2($d.date)#digits2($d.hours)#digit= s2($d.minutes)#digits2($d.seconds)## > #end > > > You can do things like: > > ## -- read file > #set( $text =3D "#parse($inputName)" ) > #set( $lines =3D $text.trim().split($LF) ) > #foreach( $line in $lines ) > ... > #end > ... > #set( $filename =3D > "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 ------=_Part_3515_24789299.1139377800560--