Clarity

Expand all | Collapse all

GEL Script: Append lines to a file using

  • 1.  GEL Script: Append lines to a file using

    Posted Jul 26, 2013 07:15 AM
    Hello all,
    I'm a newbie using gel scripting and I want to do a simple thing.

    Writing lines into a log file. The lines should be appended to the file but every line is overwritten when I use the following construct.

    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">


    <file:line>



    <file:column value="Text 1 in first line "/>

    </file:line>
    </file:writeFile>

    ... < here is scripting code including file:writes into other files (error, output etc.) > ...

    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">


    <file:line>



    <file:column value="Text 2 in first line "/>

    </file:line>
    </file:writeFile>
    --------------------

    In this case line 1 is overwritten by line 2.
    Is there a possibility to append lines in a file ?

    Thank you very much for your help
    christiane


  • 2.  RE: GEL Script: Append lines to a file using

    Posted Jul 26, 2013 07:51 AM
    It is not that the "write line to file" command is overwriting each line, it is that you are re-opening the file each time ; the <file:writeFile> command is the "open file for writing" command, not the "write line to file" command!

    So your code should be a bit more like;
    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    
    
    <file:line>
    
    
    <file:column value="FIRST line in file "/>
            </file:line>
    
    ...do some other stuff....
    
    
    
    <file:line>
    
    
    <file:column value="Line 2"/>
            </file:line>
    
    ...do some other stuff....
    
    
    
    <file:line>
    
    
    <file:column value="Line 3"/>
            </file:line>
    
    ...do some other stuff....  (etc)
    
    
    
    <file:line>
    
    
    <file:column value="LAST line in file "/>
            </file:line>
    </file:writeFile>
    --

    i.e. in your example;
    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    <file:line>
    <file:column value="Text 1 in first line "/>
    </file:line>
    [s]</file:writeFile>[s]

    ... < here is scripting code including file:writes into other files (error, output etc.) > ...

    [s]<file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">[s]
    <file:line>
    <file:column value="Text 2 in first line "/>
    </file:line>
    </file:writeFile>


  • 3.  RE: GEL Script: Append lines to a file using

    Posted Jul 26, 2013 08:13 AM
    Hello Dave,

    yes, I just thought this.
    But I want to write to several files and I can not find a why how to distinguish them without closing one and re-open it when I need to write another line.

    Example:
    ... do some stuff and write to LOG file ...>

    .<file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    <file:line> <file:column value="FIRST line in log file "/> </file:line>
    </file:writeFile>

    ... do some stuff and write to output file

    .<file:writeFile commentIndicator="#" delimiter=";" fileName="${v_outputFile}">
    <file:line> <file:column value="FIRST line in OUTPUT file "/> </file:line>
    </file:writeFile>


    ... do some stuff and write again a new line to log file

    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    <file:line> <file:column value="SECONDline in log file "/> </file:line>
    </file:writeFile>
    -----------------------------

    I think it is not possible to "nest" the writeFile like

    .<file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    <file:line> <file:column value="FIRST line in log file "/> </file:line>

    < do stuff here ...and write to OUTPUT file.>

    .<file:writeFile commentIndicator="#" delimiter=";" fileName="${v_outputFile}">
    <file:line> <file:column value="FIRST line in OUTPUT file "/> </file:line>

    <do stuff here and write again to log file ???? >
    <file:line> ?!!! ... I can not find a way to know in which file I write.


    </file:writeFile>
    </file:writeFile>

    That's why I search a possibility of "append", ... if this exists?

    Thank you very much


  • 4.  RE: GEL Script: Append lines to a file using

    Posted Jul 26, 2013 11:08 AM
    OK so I don't have a clever answer to "writing to multiple files" or "opening files in append mode" :sad

    I did manage to get multiple files written to as below, by "nesting" the writes, but I couldn't get it to write to one file then the other then the first one again.
    <gel:script 
    xmlns:core="jelly:core" 
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
    xmlns:util="jelly:util">
    
    <core:set value="my_logfile.txt" var="v_logFile"/>
    <core:set value="my_outputfile.txt" var="v_outputFile"/>
    
    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_logFile}">
    
    <file:line> <file:column value="FIRST line in log file "/> </file:line>
    
    <file:writeFile commentIndicator="#" delimiter=";" fileName="${v_outputFile}">
    <file:line> <file:column value="FIRST line in output file "/> </file:line>
    <file:line> <file:column value="SECOND line in output file "/> </file:line>
    </file:writeFile>
    
    <file:line> <file:column value="SECOND line in log file "/> </file:line>
    
    </file:writeFile>
    
    </gel:script>
    Not sure if this is true but it appears that GEL (using those stock FileTagLibrary methods) can only manage one output file at a time - someone please correct me if this statement is wrong! :unsure:

    --

    You could try building up your various messages into GEL variables just using <gel:set> (hint &#xA; will write a carriage return) and writing them out to distinct files at the end of the script I guess?


  • 5.  RE: GEL Script: Append lines to a file using

    Posted Jul 29, 2013 11:45 AM
    Hello Dave,

    thank you for your answer.

    I think you are right, that using fileWrite it is only possible to write to one file at a time.
    I tried also to "nest two fileWrites" but then all the text was written to the file opened by the second fileWrite.

    But I found another way to append text to a file =>

    <core:file append="true" trim="false" omitXmlDeclaration="true" name="${v_logFile}">This is the FIRST text in ${v_logFile}


    </core:file>





    <core:file append="true" omitXmlDeclaration="true" name="${v_errorFile}">



    This is the FIRST text in ${v_errorFile}


    </core:file>





    <core:file append="true" trim="false" omitXmlDeclaration="true" name="${v_logFile}">This is the SECOND text in ${v_logFile}</core:file>





    <core:file append="true" omitXmlDeclaration="true" name="${v_errorFile}">



    This is the SECOND text in ${v_errorFile}


    </core:file>

    This works, the only problem is the "formatting" (linefeed, whitespace).
    I'm just trying to find a "good looking solution"


  • 6.  RE: GEL Script: Append lines to a file using

    Posted Jul 29, 2013 11:57 AM
    Cool - you have found the answer* I was struggling with then! cool)

    ( * Using <core> methods instead of <file> methods )

    For completeness ; <core:file> is documented here; http://commons.apache.org/proper/commons-jelly/tags.html#core:file

    while I can't find any documentation on the FileTagLibrary methods :mad:


  • 7.  RE: GEL Script: Append lines to a file using

    Posted Jul 30, 2013 05:44 AM
    Hi Dave,

    than we can close the issue with the summary:

    Append lines to a file using <file:writeFile ... is NOT possible

    BUT
    it is possible to solve this problem using <core:file> methods.
    Example: <core:file append="true" trim="false" omitXmlDeclaration="true" name="${v_logFile}">This is the FIRST text in ${v_logFile}</core:file>
    <core:file append="true" trim="false" omitXmlDeclaration="true" name="${v_logFile}">This is the SECOND text in ${v_logFile}</core:file>

    Have a good day
    Christiane


  • 8.  RE: GEL Script: Append lines to a file using

    Posted Aug 07, 2013 11:19 PM
    I am also very new to GEL and soon as I get a properties.xml file from OnDemand I will be able to try out these scripts.
    As for having two files open for write at the same time, I have this Jelly script and it works with the commons-jelly-1.0 engine.

    Would this run via Clarity GEL or is there limitations on creating objects in GEL?

    <?xml version="1.0" encoding="utf-8"?>
    <core:script

    xmlns:core="jelly:core"

    xmlns:util="jelly:util"

    xmlns:log="jelly:log"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


    <!-- Setup our two files -->

    <core:new className="java.io.FileWriter" var="FileOne">


    <core:arg type="java.lang.String" value="FileOne.txt"/>

    </core:new>


    <core:new className="java.io.FileWriter" var="FileTwo">


    <core:arg type="java.lang.String" value="FileTwo.txt"/>

    </core:new>


    <core:new className="java.io.PrintWriter" var="PrintOne">


    <core:arg type="java.io.FileWriter" value="${FileOne}"/>

    </core:new>


    <core:new className="java.io.PrintWriter" var="PrintTwo">


    <core:arg type="java.io.FileWriter" value="${FileTwo}"/>

    </core:new>


    <!-- Loop 10 times and write to both files to test our writing to both files -->

    <core:set var="loopCount" value="0"/>

    <core:set var="anotherCount" value="1001"></core:set>

    <core:while test="${loopCount lt 10}">


    <core:invoke method="println" on="${PrintOne}">



    <core:arg value="${loopCount}" />


    </core:invoke>


    <core:invoke method="println" on="${PrintTwo}">



    <core:arg value="${anotherCount}" />


    </core:invoke>


    <core:set var="loopCount" value="${loopCount + 1}"/>


    <core:set var="anotherCount" value="${anotherCount + 1}"/>

    </core:while>


    <!-- Close our files -->

    <core:invoke method="close" on="${PrintOne}">

    </core:invoke>

    <core:invoke method="close" on="${PrintTwo}">

    </core:invoke>

    </core:script>

    V/r,
    Gene


  • 9.  RE: GEL Script: Append lines to a file using

    Posted Aug 22, 2013 06:26 PM
    I re-factored my jelly into GEL and this worked within the Process engine.
    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
    
    xmlns:core="jelly:core"
    
    xmlns:util="jelly:util"
    
    xmlns:log="jelly:log"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    <core:new className="java.io.PrintWriter" var="FileOne">
    
    
    <core:arg type="java.lang.String" value="/fs0/clarity1/share/FileOne.txt"/>
    
    </core:new>
    
    
    <core:new className="java.io.PrintWriter" var="FileTwo">
    
    
    <core:arg type="java.lang.String" value="/fs0/clarity1/share/FileTwo.txt"/>
    
    </core:new>
    
    
    <!-- Loop 100 times and test our writing to two open files -->
    
    <core:set var="loopCount" value="0"/>
    
    <core:while test="${loopCount lt 100}">
    
    
    
    
    
    <!-- using the invoke tag -->
    
    
    <core:invoke method="println" on="${FileOne}">
    
    
    
    <core:arg value="${loopCount}" />
    
    
    </core:invoke>
    
    
    
    
    
    <!-- Test with an expression statement -->
    
    
    <core:mute>${FileTwo.println(loopCount * 1000)}</core:mute>
    
    
    
    
    
    <core:set var="loopCount" value="${loopCount + 1}"/>
    
    </core:while>
    
    
    <!-- Close our files -->
    
    <core:invoke method="close" on="${FileOne}" />
    
    <core:invoke method="close" on="${FileTwo}" />
    
    
    </gel:script>
    V/r,
    Gene


  • 10.  Re: GEL Script: Append lines to a file using

    Posted Apr 15, 2015 11:17 AM

    How do you write tab or comma delimited columns on one line with CORE method?



  • 11.  Re: GEL Script: Append lines to a file using

    Posted Apr 15, 2015 11:27 AM

    One way I suspect is by using the print() method instead of println() (which automatically adds line termination characters) for most of the outputs including your field delimiters (tabs/commas), and change your looping structure to only call println() when appropriate.