Paul King | 1 May 2010 05:50
Picon
Favicon
Gravatar

Re: [groovy-user] Creating XML


It is close to ConfigSlurper format. You could convert using:

new ConfigSlurper().parse(str.replaceAll(/=\s*\{/, / \{/))

And from there output into XML using perhaps MarkupBuilder.

Paul.

On 1/05/2010 8:30 AM, citron wrote:
>
> Hi,
>
> I need to transform strings in this format into xml, any ideas how to do
> this?
>
> Thanks!
>
> def str ="""rootElement={
>                      someComplexSubElement={
>                              string1="sdfsf"
>                              string2="sdsssss"
>                      }
>                      someOtherComplexSubElement={
>                              string1="sdfsf"
>                              string2="sdsssss"
>                      }
>                      string1="yert"
>                      string2="selskdjf"
>            }"""
(Continue reading)

uli42 | 1 May 2010 07:30
Favicon

Re: [groovy-user] Unable to create a child instance when using a closure accessing private fields of the parent within the constructor


Hi Jochen,

Jochen Theodorou wrote:
> 
> 
> this is actually a design error in the MOP. Each way that would fix 
> this, will create other problems, so there cannot be a backwarts 
> compatible fix. We decided then to keep that till Groovy 2.0 and solve 
> this issue there once and for all. assigning to a local variable fixes 
> the problem. I am aware that is a somewhat ugly workaround, but better 
> than nothing.
> 
> 

thanks a lot for this information.  Does this mean the behavior will
probably change when this item of the roadmap is done:

* New Meta-Object Protocol

Currently, the roadmap shows ETAs up to version 1.8.  I guess we cannot
expect 2.0 to show up in 2010 or early 2011, can we?

Best regards + many thanks, Uli
--

-- 
View this message in context: http://old.nabble.com/Unable-to-create-a-child-instance-when-using-a-closure-accessing-private-fields-of-the-parent-within-the-constructor-tp28413103p28418916.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:
(Continue reading)

Anders Viklund | 1 May 2010 09:22
Picon
Favicon

RE: [groovy-user] Creating XML

Thanks Paul, this got me a little bit further, but I still need some assistance.

def str ="""rootElement={
                    someComplexSubElement={
                            string1="sdfsf"
                            string2="sdsssss"
                    }
                    someOtherComplexSubElement={
                            string1="sdfsf"
                            string2="sdsssss"
                    }
                    string1="yert"
                    string2="selskdjf"
          }"""


def rvTreeMap =new ConfigSlurper().parse(str.replaceAll(/=\s*\{/, / \{/)).flatten()

def xml=new StringWriter()
def builder = new groovy.xml.MarkupBuilder(xml)
def root =rvTreeMap.toString().substring(1,rvTreeMap.toString().indexOf("."))

builder."${root}"{ 
        rvTreeMap.each(){ key, value -> 
          "${key}""${value}" 
        } 

println xml

OUTPUT
<rootElement>
  <rootElement.someComplexSubElement.string1>sdfsf</rootElement.someComplexSubElement.string1>
  <rootElement.someComplexSubElement.string2>sdsssss</rootElement.someComplexSubElement.string2>
  <rootElement.someOtherComplexSubElement.string1>sdfsf</rootElement.someOtherComplexSubElement.string1>
  <rootElement.someOtherComplexSubElement.string2>sdsssss</rootElement.someOtherComplexSubElement.string2>
  <rootElement.string1>yert</rootElement.string1>
  <rootElement.string2>selskdjf</rootElement.string2>
</rootElement>




> Date: Sat, 1 May 2010 13:50:44 +1000
> From: paulk-V+QuBFElvc30CCvOHzKKcA@public.gmane.org
> To: user-i9PBDF1N6cxnkHa44VUL00B+6BGkLq7r@public.gmane.org
> Subject: Re: [groovy-user] Creating XML
>
>
> It is close to ConfigSlurper format. You could convert using:
>
> new ConfigSlurper().parse(str.replaceAll(/=\s*\{/, / \{/))
>
> And from there output into XML using perhaps MarkupBuilder.
>
> Paul.
>
> On 1/05/2010 8:30 AM, citron wrote:
> >
> > Hi,
> >
> > I need to transform strings in this format into xml, any ideas how to do
> > this?
> >
> > Thanks!
> >
> > def str ="""rootElement={
> > someComplexSubElement={
> > string1="sdfsf"
> > string2="sdsssss"
> > }
> > someOtherComplexSubElement={
> > string1="sdfsf"
> > string2="sdsssss"
> > }
> > string1="yert"
> > string2="selskdjf"
> > }"""
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>

Hotmail: Trusted email with powerful SPAM protection. Sign up now.
Jochen Theodorou | 1 May 2010 12:05
Picon
Gravatar

Re: [groovy-user] Unable to create a child instance when using a closure accessing private fields of the parent within the constructor

uli42 wrote:
[...]
> thanks a lot for this information.  Does this mean the behavior will
> probably change when this item of the roadmap is done:
> 
> * New Meta-Object Protocol
> 
> Currently, the roadmap shows ETAs up to version 1.8.  I guess we cannot
> expect 2.0 to show up in 2010 or early 2011, can we?

Groovy 2.0 won't come in 2010, that is quite sure. We will spend 2010 to 
  complete 1.8. We use 1.8 also to collect information on how to make a 
more efficient MOP, which then would go into Groovy 2.0. I think the 
most early date for Groovy 2.0 would be end of 2011.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

gmurph03 | 1 May 2010 17:47
Picon

Re: [groovy-user] Pre-compiled classes


Anyone know how to use the MOP to add a toString method to a class that acts
as the native toString method would?

For example

MyClass.metaClass.toString = {//method}

So that this would act as the default toString, other than having to be
called like

def a = new Myclass
println a.toString

--

-- 
View this message in context: http://old.nabble.com/Pre-compiled-classes-tp28394606p28421627.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

J. David Beutel | 2 May 2010 03:58

[groovy-user] default unique() not consistent with equals

DefaultGroovyMethods.unique() removes "all duplicated items, using the 
default comparator".  For non-Comparable classes, this defaults to 
comparing hashCode().  But, hashCode() is not unique and does not imply 
equals():

         def a = TimeZone.getTimeZone("US/Hawaii")
         def b = TimeZone.getTimeZone("Pacific/Honolulu")
         assert a != b
         assert a.hashCode() == b.hashCode()
         assert [a, b].unique() == [a, b]                 // <--------- 
this fails

The relevant part of NumberAwareComparator seems to be this:

             // since the object does not have a valid compareTo method
             // we compare using the hashcodes. null cases are handled by
             // DefaultTypeTransformation.compareTo
             int x1 = o1.hashCode();
             int x2 = o2.hashCode();
             if (x1==x2) return 0;
             if (x1<x2)  return -1;
             return 1;

Is there some reason for ignoring equals() on lowly, non-Comparable 
classes?  Why not do the following?  (Any problem for sort()?)

             if (o1.equals(o2)) return 0;
             int x1 = o1.hashCode(); // arbitrary comparison, but stable
             int x2 = o2.hashCode();
             if (x1==x2) { // fall back to arbitrary comparison of 
instance identity
                 x1 = System.identityHashCode(o1);
                 x2 = System.identityHashCode(o2);
             }
             if (x1<x2)  return -1;
             return 1;

Cheers,
11011011

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Roshan Dawrani | 2 May 2010 06:57
Picon

Re: [groovy-user] default unique() not consistent with equals

This is an already reported issue - http://jira.codehaus.org/browse/GROOVY-4101.

You may want to add your inputs there and also follow the progress.


On Sun, May 2, 2010 at 7:28 AM, J. David Beutel <list-MMhEdAkqs3YAvxtiuMwx3w@public.gmane.org> wrote:
DefaultGroovyMethods.unique() removes "all duplicated items, using the default comparator".  For non-Comparable classes, this defaults to comparing hashCode().  But, hashCode() is not unique and does not imply equals():

       def a = TimeZone.getTimeZone("US/Hawaii")
       def b = TimeZone.getTimeZone("Pacific/Honolulu")
       assert a != b
       assert a.hashCode() == b.hashCode()
       assert [a, b].unique() == [a, b]                 // <--------- this fails

The relevant part of NumberAwareComparator seems to be this:

           // since the object does not have a valid compareTo method
           // we compare using the hashcodes. null cases are handled by
           // DefaultTypeTransformation.compareTo
           int x1 = o1.hashCode();
           int x2 = o2.hashCode();
           if (x1==x2) return 0;
           if (x1<x2)  return -1;
           return 1;

Is there some reason for ignoring equals() on lowly, non-Comparable classes?  Why not do the following?  (Any problem for sort()?)

           if (o1.equals(o2)) return 0;
           int x1 = o1.hashCode(); // arbitrary comparison, but stable
           int x2 = o2.hashCode();
           if (x1==x2) { // fall back to arbitrary comparison of instance identity
               x1 = System.identityHashCode(o1);
               x2 = System.identityHashCode(o2);
           }
           if (x1<x2)  return -1;
           return 1;

Cheers,
11011011

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Roshan Dawrani | 2 May 2010 07:28
Picon

Re: [groovy-user] Pre-compiled classes

It doesn't look like you can do what you are after - because implicit toString() call does not seem to be going through MOP.

The issue is discussed here - GROOVY-2732 and GROOVY-2599

It seems routing toString() through MOP has been tried because the comment in the code at that very point says
---------------------------------------------------------------------------------------------------------------------
"// TODO: For GROOVY-2599 we need something like below but it breaks other things"
.....code to call toString() via MOP......
---------------------------------------------------------------------------------------------------------------------

Someone else may be able to throw more light on the exact issues faced in doing it ...

rgds,
Roshan

On Sat, May 1, 2010 at 9:17 PM, gmurph03 <garethmurphy73-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

Anyone know how to use the MOP to add a toString method to a class that acts
as the native toString method would?


For example

MyClass.metaClass.toString = {//method}

So that this would act as the default toString, other than having to be
called like

def a = new Myclass
println a.toString

--
View this message in context: http://old.nabble.com/Pre-compiled-classes-tp28394606p28421627.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



uli42 | 2 May 2010 09:27
Favicon

Re: [groovy-user] Unable to create a child instance when using a closure accessing private fields of the parent within the constructor


Hi Jochen,

Jochen Theodorou wrote:
> 
> Groovy 2.0 won't come in 2010, that is quite sure. We will spend 2010 to 
>   complete 1.8. We use 1.8 also to collect information on how to make a 
> more efficient MOP, which then would go into Groovy 2.0. I think the 
> most early date for Groovy 2.0 would be end of 2011.
> 

this is a good plan. But it means that I cannot depend on the fix for my
current project, which is fine. I thought a little bit about your remarks
related to MOP etc. and came to the conclusion that there might be a chance
to get the original approach working by using GroovyPP/Groovy++. Indeed, it
works. Basically, you only have to add the " <at> Typed" annotation to the
constructor.

So I think I'll do some more experiments based on this. Maybe in the end, I
will use groovy++ to compile all the library parts of the application and
groovy for the remaining things. On the other hand, things definitely get
more complex by doing this.

Thanks a lot for helping and good luck for the 1.8 release. I'm looking
forward to it.

Best regards, Uli
--

-- 
View this message in context: http://old.nabble.com/Unable-to-create-a-child-instance-when-using-a-closure-accessing-private-fields-of-the-parent-within-the-constructor-tp28413103p28425220.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Roshan Dawrani | 2 May 2010 11:25
Picon

Re: [groovy-user] Removing a metaClass method

In any case, you should raise a JIRA - because implementation and documentation are not in sync.

I looked into it a bit and it is not doing anything resembling removal of meta methods when a meta property is set to null.

-- Roshan

On Fri, Apr 30, 2010 at 9:49 PM, Tim Yates <tim.yates-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hiya,

Spotted a question on stackoverflow about removing a metaclass method

The page here

http://groovy.codehaus.org/JN3525-MetaClasses

Says you should be able to do it by setting it to null, but we get this:

String.metaClass.foo = { "${delegate}foo" }
assert 'kung'.foo() == 'kungfoo'
String.metaClass.foo = null
assert 'woo'.foo() == 'woofoo'

That last assert should fail (according to the docs), but it passes

Is this a documentation issue? Or should I raise a JIRA?

Cheers.

Tim


Gmane