On 6/25/08, Carsten Ziegeler <[EMAIL PROTECTED]> wrote:
> Felix Meschberger wrote:
>
> > Hi all,
> >
> > As of SLING-395 JCR Node-based resources may be adapted to a
> > java.util.Map. Each entry in the map is automatically typed from the
> > underlying JCR Property as per the
> JcrResourceUtil.toObject(Property)
> > method.
> >
> > In some use cases, it would be helpful to have helper methods to get a
> > default value, if no actual value is stored in the map (i.e. the named
> > JCR property does not exist) or to specify what exact Java type we want
> > the value to be converted to.
> >
> > The latter functionality would be esp. helpful when content is written
> > to nt:unstructured nodes by means of the SlingPostServlet. Because in
> > this case the value would always be written as a string regardless of
> > the intended data type.
> >
> > So, I propose that in Sling we define an interface ValueMap as follows:
> >
> > package org.apache.sling.api.resource;
> > public interface ValueMap extends Map<String, Object> {
> >
> > // returns the named value or null if not existing
> > @Overwrite
> > Object get(String name);
> >
> > // return named value converted to type T or
> > // null if not existing
> > <T> T get(String name, Class<T> type);
> >
> > // return named value converted to the type T of
> > // the default value or the default value if the
> > // named value does not exist
> > <T> T get(String name, T defaultValue);
> >
> > }
> >
> > This interface will primarily be used to implement the [Value]Map adapter
> for JCR Node based resources but may also be used for other adapters to be
> done in the future - e.g. an adapter for Bundle-based Properties files which
> provide the properties data.
> >
> > The interface would live in the org.apache.sling.api.resource and
> therefore be an enhancement of the Sling API.
> >
> >
> Yes, sounds good to me - I guess it will still be possible to just adapt
> a resource to a plain old Map :)
> Just one question= What happens if the value can't be converted to the
> given type? Will just null be returned or an exception thrown (I imagine the
> latter)?
i think that the underlying JCR value.get*() method is called. eg:
when requesting a 'long' property, the
node.getProperty().getValue().getLong() is called. if the JCR call
fails with a ValueFormatException, it should be converted a respective
runtime exception, either NumberFormatException, or
IllegalArgumentException.
although in a scripting environment, i don't care if the conversion
can't be done. the net effect is, that i don't have the value i
requested, and i need to use some meaningful default. so maybe
ignoring the ValueFormatException and just return the default value
would be more useful.
regards, toby