You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2005/07/04 16:52:27 UTC

svn commit: r209089 [4/7] - in /incubator/jackrabbit/trunk: commons/ commons/src/java/org/apache/jackrabbit/ commons/src/java/org/apache/jackrabbit/core/ commons/src/java/org/apache/jackrabbit/name/ commons/src/java/org/apache/jackrabbit/util/ commons/...

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BinaryValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BinaryValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BinaryValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BinaryValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,193 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * A <code>BinaryValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a binary value.
+ */
+public class BinaryValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.BINARY;
+
+    // those fields are mutually exclusive, i.e. only one can be non-null
+    private byte[] streamData = null;
+    private String text = null;
+
+    /**
+     * Constructs a <code>BinaryValue</code> object based on a string.
+     *
+     * @param text the string this <code>BinaryValue</code> should represent
+     */
+    public BinaryValue(String text) {
+        super(TYPE);
+        this.text = text;
+    }
+
+    /**
+     * Constructs a <code>BinaryValue</code> object based on a stream.
+     *
+     * @param stream the stream this <code>BinaryValue</code> should represent
+     */
+    public BinaryValue(InputStream stream) {
+        super(TYPE);
+        this.stream = stream;
+    }
+
+    /**
+     * Constructs a <code>BinaryValue</code> object based on a stream.
+     *
+     * @param data the stream this <code>BinaryValue</code> should represent
+     */
+    public BinaryValue(byte[] data) {
+        super(TYPE);
+        streamData = data;
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>BinaryValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof BinaryValue) {
+            BinaryValue other = (BinaryValue) obj;
+            if (text == other.text && stream == other.stream
+                    && streamData == other.streamData) {
+                return true;
+            }
+            // stream, streamData and text are mutually exclusive,
+            // i.e. only one of them can be non-null
+            if (stream != null) {
+                return stream.equals(other.stream);
+            } else if (streamData != null) {
+                return streamData.equals(other.streamData);
+            } else {
+                return text.equals(other.text);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * Gets the string representation of this binary value.
+     *
+     * @return string representation of this binary value.
+     *
+     * @throws javax.jcr.ValueFormatException
+     * @throws javax.jcr.RepositoryException  if another error occurs
+     */
+    public String getInternalString()
+            throws ValueFormatException, RepositoryException {
+        // build text value if necessary
+        if (streamData != null) {
+            try {
+                text = new String(streamData, DEFAULT_ENCODING);
+            } catch (UnsupportedEncodingException e) {
+                throw new RepositoryException(DEFAULT_ENCODING
+                        + " not supported on this platform", e);
+            }
+            streamData = null;
+        } else if (stream != null) {
+            try {
+                ByteArrayOutputStream out = new ByteArrayOutputStream();
+                byte[] buffer = new byte[8192];
+                int read;
+                while ((read = stream.read(buffer)) > 0) {
+                    out.write(buffer, 0, read);
+                }
+                byte[] data = out.toByteArray();
+                text = new String(data, DEFAULT_ENCODING);
+            } catch (UnsupportedEncodingException e) {
+                throw new RepositoryException(DEFAULT_ENCODING
+                        + " not supported on this platform", e);
+            } catch (IOException e) {
+                throw new RepositoryException("conversion from stream to string failed", e);
+            } finally {
+                try {
+                    if (stream != null) {
+                        stream.close();
+                    }
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+            stream = null;
+        }
+
+        if (text != null) {
+            return text;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public InputStream getStream()
+            throws IllegalStateException, RepositoryException {
+        setStreamConsumed();
+
+        // build stream value if necessary
+        if (streamData != null) {
+            stream = new ByteArrayInputStream(streamData);
+            streamData = null;
+        } else if (text != null) {
+            try {
+                stream = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING));
+            } catch (UnsupportedEncodingException e) {
+                throw new RepositoryException(DEFAULT_ENCODING
+                        + " not supported on this platform", e);
+            }
+            text = null;
+        }
+
+        return super.getStream();
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BinaryValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BooleanValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BooleanValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BooleanValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BooleanValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+
+/**
+ * A <code>BooleanValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a boolean value.
+ */
+public class BooleanValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.BOOLEAN;
+
+    private final Boolean bool;
+
+    /**
+     * Constructs a <code>BooleanValue</code> object representing a boolean.
+     *
+     * @param bool the boolean this <code>BooleanValue</code> should represent
+     */
+    public BooleanValue(Boolean bool) {
+        super(TYPE);
+        this.bool = bool;
+    }
+
+    /**
+     * Constructs a <code>BooleanValue</code> object representing a boolean.
+     *
+     * @param bool the boolean this <code>BooleanValue</code> should represent
+     */
+    public BooleanValue(boolean bool) {
+        super(TYPE);
+        this.bool = new Boolean(bool);
+    }
+
+    /**
+     * Returns a new <code>BooleanValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>BooleanValue</code> representing the
+     *         the specified value.
+     */
+    public static BooleanValue valueOf(String s) {
+        return new BooleanValue(Boolean.valueOf(s));
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>BooleanValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof BooleanValue) {
+            BooleanValue other = (BooleanValue) obj;
+            if (bool == other.bool) {
+                return true;
+            } else if (bool != null && other.bool != null) {
+                return bool.equals(other.bool);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (bool != null) {
+            return bool.toString();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to date failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to long failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (bool != null) {
+            return bool.booleanValue();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to double failed: inconvertible types");
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/BooleanValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DateValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DateValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DateValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DateValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import org.apache.jackrabbit.util.ISO8601;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+
+/**
+ * A <code>DateValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a date value.
+ */
+public class DateValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.DATE;
+
+    private final Calendar date;
+
+    /**
+     * Constructs a <code>DateValue</code> object representing a date.
+     *
+     * @param date the date this <code>DateValue</code> should represent
+     */
+    public DateValue(Calendar date) {
+        super(TYPE);
+        this.date = date;
+    }
+
+    /**
+     * Returns a new <code>DateValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     * <p/>
+     * The specified <code>String</code> must be a ISO8601-compliant date/time
+     * string.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>DateValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> is not a valid
+     *                              ISO8601-compliant date/time string.
+     * @see ISO8601
+     */
+    public static DateValue valueOf(String s) throws ValueFormatException {
+        Calendar cal = ISO8601.parse(s);
+        if (cal != null) {
+            return new DateValue(cal);
+        } else {
+            throw new ValueFormatException("not a valid date format");
+        }
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>DateValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof DateValue) {
+            DateValue other = (DateValue) obj;
+            if (date == other.date) {
+                return true;
+            } else if (date != null && other.date != null) {
+                return date.equals(other.date);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (date != null) {
+            return ISO8601.format(date);
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (date != null) {
+            return date;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (date != null) {
+            return date.getTimeInMillis();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (date != null) {
+            throw new ValueFormatException("cannot convert date to boolean");
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (date != null) {
+            long ms = date.getTimeInMillis();
+            if (ms <= Double.MAX_VALUE) {
+                return ms;
+            }
+            throw new ValueFormatException("conversion from date to double failed: inconvertible types");
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DateValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DoubleValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DoubleValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DoubleValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DoubleValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * A <code>DoubleValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a double value.
+ */
+public class DoubleValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.DOUBLE;
+
+    private final Double dblNumber;
+
+    /**
+     * Constructs a <code>DoubleValue</code> object representing a double.
+     *
+     * @param dblNumber the double this <code>DoubleValue</code> should represent
+     */
+    public DoubleValue(Double dblNumber) {
+        super(TYPE);
+        this.dblNumber = dblNumber;
+    }
+
+    /**
+     * Constructs a <code>DoubleValue</code> object representing a double.
+     *
+     * @param dbl the double this <code>DoubleValue</code> should represent
+     */
+    public DoubleValue(double dbl) {
+        super(TYPE);
+        this.dblNumber = new Double(dbl);
+    }
+
+    /**
+     * Returns a new <code>DoubleValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>DoubleValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> does not
+     *                                        contain a parsable <code>double</code>.
+     */
+    public static DoubleValue valueOf(String s) throws ValueFormatException {
+        try {
+            return new DoubleValue(Double.parseDouble(s));
+        } catch (NumberFormatException e) {
+            throw new ValueFormatException("invalid format", e);
+        }
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>DoubleValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof DoubleValue) {
+            DoubleValue other = (DoubleValue) obj;
+            if (dblNumber == other.dblNumber) {
+                return true;
+            } else if (dblNumber != null && other.dblNumber != null) {
+                return dblNumber.equals(other.dblNumber);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (dblNumber != null) {
+            return dblNumber.toString();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (dblNumber != null) {
+            // loosing timezone information...
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(new Date(dblNumber.longValue()));
+            return cal;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (dblNumber != null) {
+            return dblNumber.longValue();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to boolean failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (dblNumber != null) {
+            return dblNumber.doubleValue();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/DoubleValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/LongValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/LongValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/LongValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/LongValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * A <code>LongValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a long value.
+ */
+public class LongValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.LONG;
+
+    private final Long lNumber;
+
+    /**
+     * Constructs a <code>LongValue</code> object representing a long.
+     *
+     * @param lNumber the long this <code>LongValue</code> should represent
+     */
+    public LongValue(Long lNumber) {
+        super(TYPE);
+        this.lNumber = lNumber;
+    }
+
+    /**
+     * Constructs a <code>LongValue</code> object representing a long.
+     *
+     * @param l the long this <code>LongValue</code> should represent
+     */
+    public LongValue(long l) {
+        super(TYPE);
+        this.lNumber = new Long(l);
+    }
+
+    /**
+     * Returns a new <code>LongValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>LongValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> does not
+     *                                        contain a parsable <code>long</code>.
+     */
+    public static LongValue valueOf(String s) throws ValueFormatException {
+        try {
+            return new LongValue(Long.parseLong(s));
+        } catch (NumberFormatException e) {
+            throw new ValueFormatException("invalid format", e);
+        }
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>LongValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof LongValue) {
+            LongValue other = (LongValue) obj;
+            if (lNumber == other.lNumber) {
+                return true;
+            } else if (lNumber != null && other.lNumber != null) {
+                return lNumber.equals(other.lNumber);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (lNumber != null) {
+            return lNumber.toString();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (lNumber != null) {
+            // loosing timezone information...
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(new Date(lNumber.longValue()));
+            return cal;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (lNumber != null) {
+            return lNumber.longValue();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to boolean failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        if (lNumber != null) {
+            return lNumber.doubleValue();
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/LongValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/NameValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/NameValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/NameValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/NameValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.QName;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+
+/**
+ * A <code>NameValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a <code>NAME</code> value
+ * (a string that is namespace-qualified).
+ */
+public class NameValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.NAME;
+
+    private final String name;
+
+    /**
+     * Returns a new <code>NameValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     * <p/>
+     * The specified <code>String</code> must be a valid JCR name.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>NameValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> is not a valid
+     *                              name.
+     */
+    public static NameValue valueOf(String s) throws ValueFormatException {
+        if (s != null) {
+            try {
+                QName.checkFormat(s);
+            } catch (IllegalNameException ine) {
+                throw new ValueFormatException(ine.getMessage());
+            }
+            return new NameValue(s);
+        } else {
+            throw new ValueFormatException("not a valid name format");
+        }
+    }
+
+    /**
+     * Protected constructor creating a <code>NameValue</code> object
+     * without validating the name.
+     *
+     * @param name the name this <code>NameValue</code> should represent
+     * @see #valueOf
+     */
+    protected NameValue(String name) {
+        super(TYPE);
+        this.name = name;
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>NameValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof NameValue) {
+            NameValue other = (NameValue) obj;
+            if (name == other.name) {
+                return true;
+            } else if (name != null && other.name != null) {
+                return name.equals(other.name);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (name != null) {
+            return name;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to date failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to long failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to boolean failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to double failed: inconvertible types");
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/NameValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/PathValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/PathValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/PathValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/PathValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import org.apache.jackrabbit.name.MalformedPathException;
+import org.apache.jackrabbit.name.Path;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+
+/**
+ * A <code>PathValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a <code>PATH</code> value
+ * (an absolute or relative workspace path).
+ */
+public class PathValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.PATH;
+
+    private final String path;
+
+    /**
+     * Returns a new <code>PathValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     * <p/>
+     * The specified <code>String</code> must be a valid absolute or relative
+     * path.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>PathValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> is not a valid
+     *                              absolute or relative path.
+     */
+    public static PathValue valueOf(String s) throws ValueFormatException {
+        if (s != null) {
+            try {
+                Path.checkFormat(s);
+            } catch (MalformedPathException mpe) {
+                throw new ValueFormatException(mpe.getMessage());
+            }
+            return new PathValue(s);
+        } else {
+            throw new ValueFormatException("not a valid path format");
+        }
+    }
+
+    /**
+     * Protected constructor creating a <code>PathValue</code> object
+     * without validating the path.
+     *
+     * @param path the path this <code>PathValue</code> should represent
+     * @see #valueOf
+     */
+    protected PathValue(String path) {
+        super(TYPE);
+        this.path = path;
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>PathValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof PathValue) {
+            PathValue other = (PathValue) obj;
+            if (path == other.path) {
+                return true;
+            } else if (path != null && other.path != null) {
+                return path.equals(other.path);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (path != null) {
+            return path;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to date failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to long failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to boolean failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to double failed: inconvertible types");
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/PathValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ReferenceValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ReferenceValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ReferenceValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ReferenceValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import org.apache.jackrabbit.uuid.UUID;
+
+import javax.jcr.Node;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+
+/**
+ * A <code>ReferenceValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a <code>REFERENCE</code> value
+ * (a UUID of an existing node).
+ */
+public class ReferenceValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.REFERENCE;
+
+    private final String uuid;
+
+    /**
+     * Constructs a <code>ReferenceValue</code> object representing the UUID of
+     * an existing node.
+     *
+     * @param target the node to be referenced
+     * @throws IllegalArgumentException If <code>target</code> is nonreferenceable.
+     * @throws javax.jcr.RepositoryException      If another error occurs.
+     */
+    public ReferenceValue(Node target) throws RepositoryException {
+        super(TYPE);
+        try {
+            this.uuid = target.getUUID();
+        } catch (UnsupportedRepositoryOperationException ure) {
+            throw new IllegalArgumentException("target is nonreferenceable.");
+        }
+    }
+
+    /**
+     * Returns a new <code>ReferenceValue</code> initialized to the value
+     * represented by the specified <code>String</code>.
+     * <p/>
+     * The specified <code>String</code> must denote the UUID of an existing
+     * node.
+     *
+     * @param s the string to be parsed.
+     * @return a newly constructed <code>ReferenceValue</code> representing the
+     *         the specified value.
+     * @throws javax.jcr.ValueFormatException If the <code>String</code> is not a valid
+     *                              not a valid UUID format.
+     */
+    public static ReferenceValue valueOf(String s) throws ValueFormatException {
+        if (s != null) {
+            try {
+                UUID.fromString(s);
+            } catch (IllegalArgumentException iae) {
+                throw new ValueFormatException("not a valid UUID format");
+            }
+            return new ReferenceValue(s);
+        } else {
+            throw new ValueFormatException("not a valid UUID format");
+        }
+    }
+
+    /**
+     * Protected constructor creating a <code>ReferenceValue</code> object
+     * without validating the UUID format.
+     *
+     * @param uuid the UUID of the node to be referenced
+     * @see #valueOf
+     */
+    protected ReferenceValue(String uuid) {
+        super(TYPE);
+        this.uuid = uuid;
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>ReferenceValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof ReferenceValue) {
+            ReferenceValue other = (ReferenceValue) obj;
+            if (uuid == other.uuid) {
+                return true;
+            } else if (uuid != null && other.uuid != null) {
+                return uuid.equals(other.uuid);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (uuid != null) {
+            return uuid;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+
+    //----------------------------------------------------------------< Value >
+    /**
+     * {@inheritDoc}
+     */
+    public Calendar getDate()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to date failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLong()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to long failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean getBoolean()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to boolean failed: inconvertible types");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getDouble()
+            throws ValueFormatException, IllegalStateException,
+            RepositoryException {
+        setValueConsumed();
+
+        throw new ValueFormatException("conversion to double failed: inconvertible types");
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ReferenceValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/StringValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/StringValue.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/StringValue.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/StringValue.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import javax.jcr.PropertyType;
+import javax.jcr.ValueFormatException;
+
+/**
+ * A <code>StringValue</code> provides an implementation
+ * of the <code>Value</code> interface representing a string value.
+ */
+public class StringValue extends BaseValue {
+
+    public static final int TYPE = PropertyType.STRING;
+
+    private final String text;
+
+    /**
+     * Constructs a <code>StringValue</code> object representing a string.
+     *
+     * @param text the string this <code>StringValue</code> should represent
+     */
+    public StringValue(String text) {
+        super(TYPE);
+        this.text = text;
+    }
+
+    /**
+     * Indicates whether some other object is "equal to" this one.
+     * <p/>
+     * The result is <code>true</code> if and only if the argument is not
+     * <code>null</code> and is a <code>StringValue</code> object that
+     * represents the same value as this object.
+     *
+     * @param obj the reference object with which to compare.
+     * @return <code>true</code> if this object is the same as the obj
+     *         argument; <code>false</code> otherwise.
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof StringValue) {
+            StringValue other = (StringValue) obj;
+            if (text == other.text) {
+                return true;
+            } else if (text != null && other.text != null) {
+                return text.equals(other.text);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns zero to satisfy the Object equals/hashCode contract.
+     * This class is mutable and not meant to be used as a hash key.
+     *
+     * @return always zero
+     * @see Object#hashCode()
+     */
+    public int hashCode() {
+        return 0;
+    }
+
+    //------------------------------------------------------------< BaseValue >
+    /**
+     * {@inheritDoc}
+     */
+    protected String getInternalString() throws ValueFormatException {
+        if (text != null) {
+            return text;
+        } else {
+            throw new ValueFormatException("empty value");
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/StringValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ValueHelper.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ValueHelper.java?rev=209089&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ValueHelper.java (added)
+++ incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ValueHelper.java Mon Jul  4 07:52:19 2005
@@ -0,0 +1,579 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.value;
+
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.MalformedPathException;
+import org.apache.jackrabbit.name.Path;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.util.Base64;
+import org.apache.jackrabbit.util.Text;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+/**
+ * The <code>ValueHelper</code> class provides several <code>Value</code>
+ * related utility methods.
+ */
+public class ValueHelper {
+
+    /**
+     * empty private constructor
+     */
+    private ValueHelper() {
+    }
+
+    /**
+     * @param srcValue
+     * @param targetType
+     * @return
+     * @throws javax.jcr.ValueFormatException
+     * @throws IllegalArgumentException
+     */
+    public static Value convert(String srcValue, int targetType)
+            throws ValueFormatException, IllegalArgumentException {
+        if (srcValue == null) {
+            return null;
+        } else {
+            return convert(new StringValue(srcValue), targetType);
+        }
+    }
+
+    /**
+     * @param srcValues
+     * @param targetType
+     * @return
+     * @throws javax.jcr.ValueFormatException
+     * @throws IllegalArgumentException
+     */
+    public static Value[] convert(String[] srcValues, int targetType)
+            throws ValueFormatException, IllegalArgumentException {
+        if (srcValues == null) {
+            return null;
+        }
+        Value[] newValues = new Value[srcValues.length];
+        for (int i = 0; i < srcValues.length; i++) {
+            newValues[i] = convert(srcValues[i], targetType);
+        }
+        return newValues;
+    }
+
+    /**
+     * @param srcValues
+     * @param targetType
+     * @return
+     * @throws javax.jcr.ValueFormatException
+     * @throws IllegalArgumentException
+     */
+    public static Value[] convert(Value[] srcValues, int targetType)
+            throws ValueFormatException, IllegalArgumentException {
+        if (srcValues == null) {
+            return null;
+        }
+
+        Value[] newValues = new Value[srcValues.length];
+        int srcValueType = PropertyType.UNDEFINED;
+        for (int i = 0; i < srcValues.length; i++) {
+            if (srcValues[i] == null) {
+                newValues[i] = null;
+                continue;
+            }
+            // check type of values
+            if (srcValueType == PropertyType.UNDEFINED) {
+                srcValueType = srcValues[i].getType();
+            } else if (srcValueType != srcValues[i].getType()) {
+                // inhomogeneous types
+                String msg = "inhomogeneous type of values";
+                throw new ValueFormatException(msg);
+            }
+
+            newValues[i] = convert(srcValues[i], targetType);
+        }
+        return newValues;
+    }
+
+    /**
+     * @param srcValue
+     * @param targetType
+     * @return
+     * @throws javax.jcr.ValueFormatException
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     */
+    public static Value convert(Value srcValue, int targetType)
+            throws ValueFormatException, IllegalStateException,
+            IllegalArgumentException {
+        if (srcValue == null) {
+            return null;
+        }
+
+        Value val;
+        int srcType = srcValue.getType();
+
+        if (srcType == targetType) {
+            // no conversion needed, return original value
+            return srcValue;
+        }
+
+        switch (targetType) {
+            case PropertyType.STRING:
+                // convert to STRING
+                try {
+                    val = new StringValue(srcValue.getString());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.BINARY:
+                // convert to BINARY
+                try {
+                    val = new BinaryValue(srcValue.getStream());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.BOOLEAN:
+                // convert to BOOLEAN
+                try {
+                    val = new BooleanValue(srcValue.getBoolean());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.DATE:
+                // convert to DATE
+                try {
+                    val = new DateValue(srcValue.getDate());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.DOUBLE:
+                // convert to DOUBLE
+                try {
+                    val = new DoubleValue(srcValue.getDouble());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.LONG:
+                // convert to LONG
+                try {
+                    val = new LongValue(srcValue.getLong());
+                } catch (RepositoryException re) {
+                    throw new ValueFormatException("conversion failed: "
+                            + PropertyType.nameFromValue(srcType) + " to "
+                            + PropertyType.nameFromValue(targetType), re);
+                }
+                break;
+
+            case PropertyType.PATH:
+                // convert to PATH
+                switch (srcType) {
+                    case PropertyType.PATH:
+                        // no conversion needed, return original value
+                        // (redundant code, just here for the sake of clarity)
+                        return srcValue;
+
+                    case PropertyType.BINARY:
+                    case PropertyType.STRING:
+                    case PropertyType.NAME: // a name is always also a relative path
+                        // try conversion via string
+                        String path;
+                        try {
+                            // get string value
+                            path = srcValue.getString();
+                        } catch (RepositoryException re) {
+                            // should never happen
+                            throw new ValueFormatException("failed to convert source value to PATH value",
+                                    re);
+                        }
+                        try {
+                            // check path format
+                            Path.checkFormat(path);
+                        } catch (MalformedPathException mpe) {
+                            throw new ValueFormatException("source value " + path
+                                    + " does not represent a valid path", mpe);
+                        }
+                        val = PathValue.valueOf(path);
+                        break;
+
+                    case PropertyType.BOOLEAN:
+                    case PropertyType.DATE:
+                    case PropertyType.DOUBLE:
+                    case PropertyType.LONG:
+                    case PropertyType.REFERENCE:
+                        throw new ValueFormatException("conversion failed: "
+                                + PropertyType.nameFromValue(srcType) + " to "
+                                + PropertyType.nameFromValue(targetType));
+
+                    default:
+                        throw new IllegalArgumentException("not a valid type constant: " + srcType);
+                }
+                break;
+
+            case PropertyType.NAME:
+                // convert to NAME
+                switch (srcType) {
+                    case PropertyType.NAME:
+                        // no conversion needed, return original value
+                        // (redundant code, just here for the sake of clarity)
+                        return srcValue;
+
+                    case PropertyType.BINARY:
+                    case PropertyType.STRING:
+                    case PropertyType.PATH: // path might be a name (relative path of length 1)
+                        // try conversion via string
+                        String name;
+                        try {
+                            // get string value
+                            name = srcValue.getString();
+                        } catch (RepositoryException re) {
+                            // should never happen
+                            throw new ValueFormatException("failed to convert source value to NAME value",
+                                    re);
+                        }
+                        try {
+                            // check name format
+                            QName.checkFormat(name);
+                        } catch (IllegalNameException ine) {
+                            throw new ValueFormatException("source value "
+                                    + name
+                                    + " does not represent a valid name", ine);
+                        }
+                        val = NameValue.valueOf(name);
+                        break;
+
+                    case PropertyType.BOOLEAN:
+                    case PropertyType.DATE:
+                    case PropertyType.DOUBLE:
+                    case PropertyType.LONG:
+                    case PropertyType.REFERENCE:
+                        throw new ValueFormatException("conversion failed: "
+                                + PropertyType.nameFromValue(srcType) + " to "
+                                + PropertyType.nameFromValue(targetType));
+
+                    default:
+                        throw new IllegalArgumentException("not a valid type constant: " + srcType);
+                }
+                break;
+
+            case PropertyType.REFERENCE:
+                // convert to REFERENCE
+                switch (srcType) {
+                    case PropertyType.REFERENCE:
+                        // no conversion needed, return original value
+                        // (redundant code, just here for the sake of clarity)
+                        return srcValue;
+
+                    case PropertyType.BINARY:
+                    case PropertyType.STRING:
+                        // try conversion via string
+                        String uuid;
+                        try {
+                            // get string value
+                            uuid = srcValue.getString();
+                        } catch (RepositoryException re) {
+                            // should never happen
+                            throw new ValueFormatException("failed to convert source value to REFERENCE value",
+                                    re);
+                        }
+                        val = ReferenceValue.valueOf(uuid);
+                        break;
+
+                    case PropertyType.BOOLEAN:
+                    case PropertyType.DATE:
+                    case PropertyType.DOUBLE:
+                    case PropertyType.LONG:
+                    case PropertyType.PATH:
+                    case PropertyType.NAME:
+                        throw new ValueFormatException("conversion failed: "
+                                + PropertyType.nameFromValue(srcType) + " to "
+                                + PropertyType.nameFromValue(targetType));
+
+                    default:
+                        throw new IllegalArgumentException("not a valid type constant: " + srcType);
+                }
+                break;
+
+            default:
+                throw new IllegalArgumentException("not a valid type constant: " + targetType);
+        }
+
+        return val;
+    }
+
+    /**
+     * @param srcValue
+     * @return
+     * @throws IllegalStateException
+     */
+    public static Value copy(Value srcValue) throws IllegalStateException {
+        if (srcValue == null) {
+            return null;
+        }
+
+        Value newVal = null;
+        try {
+            switch (srcValue.getType()) {
+                case PropertyType.BINARY:
+                    newVal = new BinaryValue(srcValue.getStream());
+                    break;
+
+                case PropertyType.BOOLEAN:
+                    newVal = new BooleanValue(srcValue.getBoolean());
+                    break;
+
+                case PropertyType.DATE:
+                    newVal = new DateValue(srcValue.getDate());
+                    break;
+
+                case PropertyType.DOUBLE:
+                    newVal = new DoubleValue(srcValue.getDouble());
+                    break;
+
+                case PropertyType.LONG:
+                    newVal = new LongValue(srcValue.getLong());
+                    break;
+
+                case PropertyType.PATH:
+                    newVal = PathValue.valueOf(srcValue.getString());
+                    break;
+
+                case PropertyType.NAME:
+                    newVal = NameValue.valueOf(srcValue.getString());
+                    break;
+
+                case PropertyType.REFERENCE:
+                    newVal = ReferenceValue.valueOf(srcValue.getString());
+                    break;
+
+                case PropertyType.STRING:
+                    newVal = new StringValue(srcValue.getString());
+                    break;
+            }
+        } catch (RepositoryException re) {
+            // should never get here
+        }
+        return newVal;
+    }
+
+    /**
+     * @param srcValues
+     * @return
+     * @throws IllegalStateException
+     */
+    public static Value[] copy(Value[] srcValues) throws IllegalStateException {
+        if (srcValues == null) {
+            return null;
+        }
+
+        Value[] newValues = new Value[srcValues.length];
+        for (int i = 0; i < srcValues.length; i++) {
+            newValues[i] = copy(srcValues[i]);
+        }
+        return newValues;
+    }
+
+    /**
+     * Serializes the given value to a <code>String</code>. The serialization
+     * format is the same as used by Document & System View XML, i.e.
+     * binary values will be Base64-encoded whereas for all others
+     * <code>{@link javax.jcr.Value#getString()}</code> will be used.
+     *
+     * @param value        the value to be serialized
+     * @param encodeBlanks if <code>true</code> space characters will be encoded
+     *                     as <code>"_x0020_"</code> within he output string.
+     * @return a string representation of the given value.
+     * @throws IllegalStateException if the given value is in an illegal state
+     * @throws javax.jcr.RepositoryException   if an error occured during the serialization.
+     */
+    public static String serialize(Value value, boolean encodeBlanks)
+            throws IllegalStateException, RepositoryException {
+        StringWriter writer = new StringWriter();
+        try {
+            serialize(value, encodeBlanks, writer);
+        } catch (IOException ioe) {
+            throw new RepositoryException("failed to serialize value",
+                    ioe);
+        }
+        return writer.toString();
+    }
+
+    /**
+     * Outputs the serialized value to a <code>Writer</code>. The serialization
+     * format is the same as used by Document & System View XML, i.e.
+     * binary values will be Base64-encoded whereas for all others
+     * <code>{@link javax.jcr.Value#getString()}</code> will be used for
+     * serialization.
+     *
+     * @param value        the value to be serialized
+     * @param encodeBlanks if <code>true</code> space characters will be encoded
+     *                     as <code>"_x0020_"</code> within he output string.
+     * @param writer       writer to output the encoded data
+     * @throws IllegalStateException if the given value is in an illegal state
+     * @throws java.io.IOException           if an i/o error occured during the
+     *                               serialization
+     * @throws javax.jcr.RepositoryException   if an error occured during the serialization.
+     */
+    public static void serialize(Value value, boolean encodeBlanks,
+                                 Writer writer)
+            throws IllegalStateException, IOException, RepositoryException {
+        if (value.getType() == PropertyType.BINARY) {
+            // binary data, base64 encoding required;
+            // the encodeBlanks flag can be ignored since base64-encoded
+            // data cannot contain space characters
+            InputStream in = value.getStream();
+            try {
+                Base64.encode(in, writer);
+                // no need to close StringWriter
+                //writer.close();
+            } finally {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+        } else {
+            String textVal = value.getString();
+            if (encodeBlanks) {
+                // enocde blanks in string
+                textVal = Text.replace(textVal, " ", "_x0020_");
+            }
+            writer.write(textVal);
+        }
+    }
+
+    /**
+     * Deserializes the given string to a <code>Value</code> of the given type.
+     *
+     * @param value        string to be deserialized
+     * @param type         type of value
+     * @param decodeBlanks if <code>true</code> <code>"_x0020_"</code>
+     *                     character sequences will be decoded to single space
+     *                     characters each.
+     * @return the deserialized <code>Value</code>
+     * @throws javax.jcr.ValueFormatException if the string data is not of the required
+     *                              format
+     * @throws javax.jcr.RepositoryException  if an error occured during the
+     *                              deserialization.
+     */
+    public static Value deserialize(String value, int type,
+                                    boolean decodeBlanks)
+            throws ValueFormatException, RepositoryException {
+        if (type == PropertyType.BINARY) {
+            // base64 encoded binary value;
+            // the encodeBlanks flag can be ignored since base64-encoded
+            // data cannot contain encoded space characters
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            try {
+                Base64.decode(value, baos);
+                // no need to close ByteArrayOutputStream
+                //baos.close();
+            } catch (IOException ioe) {
+                throw new RepositoryException("failed to decode binary value",
+                        ioe);
+            }
+            return new BinaryValue(baos.toByteArray());
+        } else {
+            if (decodeBlanks) {
+                // decode encoded blanks in value
+                value = Text.replace(value, "_x0020_", " ");
+            }
+            return convert(value, type);
+        }
+    }
+
+    /**
+     * Deserializes the string data read from the given reader to a
+     * <code>Value</code> of the given type.
+     *
+     * @param reader       reader for the string data to be deserialized
+     * @param type         type of value
+     * @param decodeBlanks if <code>true</code> <code>"_x0020_"</code>
+     *                     character sequences will be decoded to single space
+     *                     characters each.
+     * @return the deserialized <code>Value</code>
+     * @throws java.io.IOException          if an i/o error occured during the
+     *                              serialization
+     * @throws javax.jcr.ValueFormatException if the string data is not of the required
+     *                              format
+     * @throws javax.jcr.RepositoryException  if an error occured during the
+     *                              deserialization.
+     */
+    public static Value deserialize(Reader reader, int type,
+                                    boolean decodeBlanks)
+            throws IOException, ValueFormatException, RepositoryException {
+        if (type == PropertyType.BINARY) {
+            // base64 encoded binary value;
+            // the encodeBlanks flag can be ignored since base64-encoded
+            // data cannot contain encoded space characters
+/*
+            // @todo decode to temp file and pass FileInputStream to BinaryValue constructor
+            File tmpFile = File.createTempFile("bin", null);
+            FileOutputStream out = new FileOutputStream(tmpFile);
+            tmpFile.deleteOnExit();
+            Base64.decode(reader, out);
+            out.close();
+            return new BinaryValue(new FileInputStream(tmpFile));
+*/
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            Base64.decode(reader, baos);
+            // no need to close ByteArrayOutputStream
+            //baos.close();
+            return new BinaryValue(baos.toByteArray());
+        } else {
+            char[] chunk = new char[8192];
+            int read;
+            StringBuffer buf = new StringBuffer();
+            while ((read = reader.read(chunk)) > -1) {
+                buf.append(chunk, 0, read);
+            }
+            String value = buf.toString();
+            if (decodeBlanks) {
+                // decode encoded blanks in value
+                value = Text.replace(value, "_x0020_", " ");
+            }
+            return convert(value, type);
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/commons/src/java/org/apache/jackrabbit/value/ValueHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native