Check if a list's value contains in a string

12,065

Solution 1

Do you have an example that fails with some example xml? I ask because this (which you say at the top of your question crashes) actually works:

def vrs=["6.0","6.1","6.1.0"]
def test=[ version:'6.1 HotFix11' ]

vrs.each { ver-> println test.version.contains( ver ) }

and prints:

false
true
false

But I cannot find a problem with your other code, as I don't know what 'E://Projects/agent6.1.xml' contains...

Solution 2

I guess your code is as follows:

class XmlHandler {
    private Map params
    private def root

    private def nineBelow

    XmlHandler(String xml)
    {
        nineBelow=["6.0","6.1","6.1.0"]

        params=[:]
        root=new XmlParser().parseText(xml)
    }

    def getParams()
    {
        if(root.product.version.size()>0)
        {
            params.version=root.product.version.text()
        }

        nineBelow.each {
            println params.version  //even this throws java.lang.StackOverflowError
           //println "$it , ${params.version}"
           //println ver.getClass()+", "+params.version.getClass()
        }
   }
}

def doc = """
    <root>
        <product>
            <version>1.0</version>
        <version>2.0</version>
        <version>3.0</version>
        <version>4.0</version>
        <version>5.0</version>
    </product>
</root>
"""

XmlHandler handler = new XmlHandler(doc)
handler.getParams()

Therefore, in println params.version you are actually invoking getParams().version thereby entering in an endless recursive call.

Check Groovy @ symbol before fields. As tim_yates says there, ¿bug or feature?

Share:
12,065

Related videos on Youtube

AabinGunz
Author by

AabinGunz

Quiet and fun loving, here to learn and share. :) SOreadytohelp Got to share some amazing things. Thanks SO.

Updated on June 04, 2022

Comments

  • AabinGunz
    AabinGunz almost 2 years

    I have a list def vrs=["6.0","6.1","6.1.0"] (versions) and I get a map in a function with this value def test=[version:6.1 HotFix11].

    How can I check whether test's version value matches with the list vrs?

    I tried this vrs.each{ver-> println test.version.contains(ver)} but it gives Exception in thread "main" java.lang.StackOverflowError

    Update

    Turns out, there was something wrong with my code. I tried the test case in a small Groovy script and it works.

    Here is the full code:

    private Map params
    private def root
    
    private def nineBelow
    
    XmlHandler(String xml)
    {
        nineBelow=["6.0","6.1","6.1.0"]
    
        params=[:]
        root=new XmlParser().parseText(xml)
    }
    def getParams()
    {
        if(root.product.version.size()>0)
        {
            params.version=root.product.version.text()
        }
    
        /*nineBelow.each {
            println params.version  //even this throws java.lang.StackOverflowError
            //println "$it , ${params.version}"
            //println ver.getClass()+", "+params.version.getClass()
             }*/
    
        println nineBelow.each{ver-> println params.version.contains(ver)}
    
        /*I need to check whether `params.version` matches with `nineBelow` list, so i'll check for condition here*/
    
        params
    }
    

    Another class which calls getParams()

    static main(args) {
    
        String fileContents = new File('E://Projects/agent6.1.xml').text
        XmlHandler xm=new XmlHandler(fileContents)  
    
        def params=xm.getParams()
        println params
    }
    

    Update

    Even println nineBelow.each { println params.version} gives me Exception in thread "main" java.lang.StackOverflowError

    More Update

    It worked only after the below code

    def ver=params.version
        println nineBelow.each { println ver.contains(it) }
    

    What is the problem here?

  • AabinGunz
    AabinGunz almost 12 years
    I tried an example, which works perfectly fine too. But anyways the problem is resolved. see More Updates in my question
  • AabinGunz
    AabinGunz almost 12 years
    Can you please solve another question here?
  • AabinGunz
    AabinGunz almost 12 years
    I was using wrong jar instead of groovy-all-1.8.6.jar i was using groovy-1.8.6.jar It solved my problem for the other question. thanks