You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by Michael Wechner <mi...@wyona.com> on 2005/02/27 01:23:58 UTC

getString for multi-valued properties

When using Property.getString() for multi-valued properties I receive

     [java] javax.jcr.ValueFormatException: 
/jcr:system/jcr:versionStorage/jcr:mixinTypes is multi-valued and can 
therefore only be retrieved as an array of values
     [java]     at 
org.apache.jackrabbit.core.PropertyImpl.getString(PropertyImpl.java:383)

Is this a feature or a bug?

As I workaround I do

if (p.getDefinition().isMultiple()) {
                Value[] values = p.getValues();


Is this the way how to do it?

Just being curious, why does Value or Property of the "cqfs" implementation
not implement the "toString()" method for convenience?

Thanks

Michi

-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
Jukka Zitting wrote:

>
>Among other updates to the First Steps document, I recently changed
>property display code to the following. The changes are already visible
>on the web site.
>  
>

thanks very much. I saw the changes, but after I have sent my original 
email ;-)


Michi



-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
David Nuescheler wrote:

>>It's not as straightforward as one might wish, but this is what the spec
>>requires. Should we start a "JCR code idioms" document somewhere (the
>>dormant wiki perhaps)? 
>>    
>>
>how about an "examples"-contrib in svn? i think a sample
>webapp could help people to kickstart their work with 
>jackrabbit.
>  
>

yes, that would be nice and I would be happy to contribute
to it.

I have extended the Firststeps code, such that one can import
a directory/file from the filesystem into Jackrabbit with quick and
dirty command line util.

I am currently cleaning the ant build file a bit, but otherwise it's
quite ready. The reason I am not using Maven is that Lenya doesn't use
Maven (maybe it does sometime).

Thanks

Michi


-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by David Nuescheler <da...@gmail.com>.
> I agree. An unexpected property format (svp/mvp) is (in my opinion) a
> type safety issue and throwing a ValueFormatException is a reasonable
> course of action. It's unfortunate that API structure gives few hints
> about this problem (other value conversions are mostly allowed).
> Hopefully the API documentation together with illustrative code examples
> will help people avoid this trap.
... and of course suggestion for improved verbage or examples 
in the spec are always welcome at jsr-170-comments@jcp.org

regards,
david

Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
Jukka Zitting wrote:

>Hi,
>
>Michael Wechner wrote about contrib/examples:
>  
>
>>Have you added this already? At least I cannot find it yet.
>>    
>>
>
>Just committed it. 
>

thanks

>I still haven't figured out what would be the best
>way to setup and run the examples. Ideas are welcome.
>  
>

I guess some ant build or maven would do for the beginning

Michi

-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by Jukka Zitting <jz...@yukatan.fi>.
Hi,

Michael Wechner wrote about contrib/examples:
> Have you added this already? At least I cannot find it yet.

Just committed it. I still haven't figured out what would be the best
way to setup and run the examples. Ideas are welcome.

So far the contrib/examples package only contains a simple skeleton
structure and a org.apache.jackrabbit.examples.FirstSteps class that
contains a slightly modified version of the last example code from the
First Steps document.

Everyone's free to add or modify the examples. I'm planning on adding
some JCR-RMI example code under o.a.j.examples.rmi during the next few
days.

BR,

Jukka Zitting


Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
Jukka Zitting wrote:

>Hi,
>
>David Nuescheler wrote:
>  
>
>>how about an "examples"-contrib in svn? i think a sample
>>webapp could help people to kickstart their work with 
>>jackrabbit.
>>    
>>
>
>Sounds fine. I'll create the contrib/examples package and copy the First
>Steps examples as a start.
>  
>

Have you added this already? At least I cannot find it yet.

Thanks

Michi

-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by Jukka Zitting <jz...@yukatan.fi>.
Hi,

David Nuescheler wrote:
> how about an "examples"-contrib in svn? i think a sample
> webapp could help people to kickstart their work with 
> jackrabbit.

Sounds fine. I'll create the contrib/examples package and copy the First
Steps examples as a start.

About generic value access:
> hmmm... looking at that, i agree that it doesn't 
> look very pretty. i would argue though that 
> there are only few "generic"-applications
> like a general purpose repository browser 
> or an eclipse plug-in that would have to 
> access information in such a manner
> (and even those will probably work differently 
> for mvp & svp). my argument would be 
> that most content applications 
> (hopefully) know their underlying 
> content-model and therefore "know" if
> a property is multi-value or not.
> i think of all the display logic templates
> or content management apps, they 
> should all be aware of the content 
> model that they are operating 
> against? thoughts?

I agree. An unexpected property format (svp/mvp) is (in my opinion) a
type safety issue and throwing a ValueFormatException is a reasonable
course of action. It's unfortunate that API structure gives few hints
about this problem (other value conversions are mostly allowed).
Hopefully the API documentation together with illustrative code examples
will help people avoid this trap.

BR,

Jukka Zitting


Re: getString for multi-valued properties

Posted by David Nuescheler <da...@gmail.com>.
> The First Steps document was probably written when Jackrabbit wasn't
> returning multi-values for the example cases. Nowadays at least the
> jcr:mixinTypes property is multivalued and requires special handling.
i think it was written when getString on an mvp didn't throw... 

> Among other updates to the First Steps document, I recently changed
> property display code to the following. The changes are already visible
> on the web site.
thanks a lot. looks good.

>    Property p=pit.nextProperty();
>    System.out.print(p.getPath() + "=");
>    if (p.getDefinition().isMultiple()) {
>        Value[] values = p.getValues();
>        for (int i = 0; i < values.length; i++) {
>            if (i > 0) System.out.println(",");
>            System.out.println(values[i].getString());
>        }
>    } else {
>        System.out.print(p.getString());
>    }
>    System.out.println();
> 
> It's not as straightforward as one might wish, but this is what the spec
> requires. Should we start a "JCR code idioms" document somewhere (the
> dormant wiki perhaps)? 
how about an "examples"-contrib in svn? i think a sample
webapp could help people to kickstart their work with 
jackrabbit.

> The general-case property value access code would
> be:
> 
>    Property property = ...;
>    if (property.getDefinition().isMultiple()) {
>        Value[] values = property.getValues();
>        for (int i = 0; i < values.length; i++) {
>            processValue(values[i]);
>        }
>    } else {
>        processValue(property.getValue());
>    }

hmmm... looking at that, i agree that it doesn't 
look very pretty. i would argue though that 
there are only few "generic"-applications
like a general purpose repository browser 
or an eclipse plug-in that would have to 
access information in such a manner
(and even those will probably work differently 
for mvp & svp). my argument would be 
that most content applications 
(hopefully) know their underlying 
content-model and therefore "know" if
a property is multi-value or not.
i think of all the display logic templates
or content management apps, they 
should all be aware of the content 
model that they are operating 
against? thoughts?

regards,
david

Re: getString for multi-valued properties

Posted by Jukka Zitting <jz...@yukatan.fi>.
Hi,

Michael Wechner wrote:
> I took it from the first steps example, but I think I still used a 
> rather old version ;-) which used
> 
> System.out.println(p.getPath() + "=" + p.getString());
> 
> for all cases (multivalued and singlevalued)

The First Steps document was probably written when Jackrabbit wasn't
returning multi-values for the example cases. Nowadays at least the
jcr:mixinTypes property is multivalued and requires special handling.

Among other updates to the First Steps document, I recently changed
property display code to the following. The changes are already visible
on the web site.

    Property p=pit.nextProperty();
    System.out.print(p.getPath() + "=");
    if (p.getDefinition().isMultiple()) {
        Value[] values = p.getValues();
        for (int i = 0; i < values.length; i++) {
            if (i > 0) System.out.println(",");
            System.out.println(values[i].getString());
        }
    } else {
        System.out.print(p.getString());
    }
    System.out.println();

It's not as straightforward as one might wish, but this is what the spec
requires. Should we start a "JCR code idioms" document somewhere (the
dormant wiki perhaps)? The general-case property value access code would
be:

    Property property = ...;
    if (property.getDefinition().isMultiple()) {
        Value[] values = property.getValues();
        for (int i = 0; i < values.length; i++) {
            processValue(values[i]);
        }
    } else {
        processValue(property.getValue());
    }

BR,

Jukka Zitting



Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
Tobias Strasser wrote:

>>Is this a feature or a bug?
>>    
>>
>this works as specified:
>
>see 6.2.4:
>[...]
>Property.getValue()
>[...]
>If this property is multi-valued, this method throws a ValueFormatException
>  
>

right, but I used the Property.getString() method and thought
that this method would just concatenate the values resp. their string 
representation, especially
since there doesn't seem to be a getStrings() method within the API

I took it from the first steps example, but I think I still used a 
rather old version ;-) which used

System.out.println(p.getPath() + "=" + p.getString());

for all cases (multivalued and singlevalued)

but I see from the Javadoc that also the getString method

    A string representation of the value of this property. 
*Throws:*
    |ValueFormatException
    <http://www.day.com/maven/jsr170/javadocs/jcr-0.16.1-pfd/javax/jcr/ValueFormatException.html>|
    - if conversion to a string is not possible or if the property is
    multi-valued.

but does that make sense if there isn't a getStrings() method or would
it make sense to add a getStrings() method to the API?

Sorry, if I might confuse stuff here.

>  
>
>>if (p.getDefinition().isMultiple()) {
>>                Value[] values = p.getValues();
>>
>>Is this the way how to do it?
>>    
>>
>correct.
>
>  
>
>>Just being curious, why does Value or Property of the "cqfs" implementation
>>not implement the "toString()" method for convenience?
>>    
>>
>in general, the toString() should only be used for debugging purposos,
>giving a human readable description about the object.
>

I guess I just thought about very simple String objects
as property values and didn't consider anything else, but I think I
get your point

> IMO, it should
>not modify the state of the object (e.g. read a stream, modify fields)
>or throw uncaught exceptions. so the toString() of a value or property
>should not return the content of the value if it would modify the
>state..
>  
>

I don't fully understand to be honest. Why should getValue
or getString modify the state or whatsoever?

>btw: the Value or Property is implemented by the jcr api and
>jackrabbit. this has nothing to do with 'cqfs'.
>  
>

I thought it might be an implementation bug and didn't check that
ValueFormatException is part of the interface definition

http://www.day.com/maven/jsr170/javadocs/jcr-0.16.1-pfd/javax/jcr/Property.html#getString()

well, maybe I should start using Eclipse ;-)

thanks

Michi

>cheers, tobi
>
>  
>


-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by Tobias Strasser <to...@gmail.com>.
> Is this a feature or a bug?
this works as specified:

see 6.2.4:
[...]
Property.getValue()
[...]
If this property is multi-valued, this method throws a ValueFormatException

> if (p.getDefinition().isMultiple()) {
>                 Value[] values = p.getValues();
> 
> Is this the way how to do it?
correct.

> Just being curious, why does Value or Property of the "cqfs" implementation
> not implement the "toString()" method for convenience?
in general, the toString() should only be used for debugging purposos,
giving a human readable description about the object. IMO, it should
not modify the state of the object (e.g. read a stream, modify fields)
or throw uncaught exceptions. so the toString() of a value or property
should not return the content of the value if it would modify the
state..

btw: the Value or Property is implemented by the jcr api and
jackrabbit. this has nothing to do with 'cqfs'.

cheers, tobi

-- 
------------------------------------------< tobias.strasser@day.com >---
Tobias Strasser, Day Management AG, Barfuesserplatz 6, CH - 4001 Basel
T +41 61 226 98 98, F +41 61 226 98 97 
-----------------------------------------------< http://www.day.com >---

Re: getString for multi-valued properties

Posted by Michael Wechner <mi...@wyona.com>.
David Nuescheler wrote:

>hi michi,
>
>  
>
>>Just being curious, why does Value or Property of the "cqfs" implementation
>>not implement the "toString()" method for convenience?
>>    
>>
>i really wonder: 
>what makes you think that a jsr-170 api call 
>could be impacted by a filesystem implementation 
>used in a persistence manager?
>  
>

please see my other email to Tobi

>still in shock ;)
>  
>

well, please don't worry too much, I am just still learning ;-)

Michi

>david
>
>  
>


-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: getString for multi-valued properties

Posted by David Nuescheler <da...@gmail.com>.
hi michi,

> Just being curious, why does Value or Property of the "cqfs" implementation
> not implement the "toString()" method for convenience?
i really wonder: 
what makes you think that a jsr-170 api call 
could be impacted by a filesystem implementation 
used in a persistence manager?

still in shock ;)
david