001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property.basic;
025    
026    import java.io.InputStream;
027    import java.io.Reader;
028    import java.math.BigDecimal;
029    import java.net.URI;
030    import java.util.Calendar;
031    import java.util.Date;
032    import java.util.UUID;
033    import net.jcip.annotations.Immutable;
034    import org.jboss.dna.common.text.TextDecoder;
035    import org.jboss.dna.graph.GraphI18n;
036    import org.jboss.dna.graph.property.Binary;
037    import org.jboss.dna.graph.property.DateTime;
038    import org.jboss.dna.graph.property.IoException;
039    import org.jboss.dna.graph.property.Name;
040    import org.jboss.dna.graph.property.Path;
041    import org.jboss.dna.graph.property.PropertyType;
042    import org.jboss.dna.graph.property.Reference;
043    import org.jboss.dna.graph.property.ValueFactory;
044    import org.jboss.dna.graph.property.ValueFormatException;
045    
046    /**
047     * The standard {@link ValueFactory} for {@link PropertyType#REFERENCE} values.
048     * 
049     * @author Randall Hauch
050     * @author John Verhaeg
051     */
052    @Immutable
053    public class UuidReferenceValueFactory extends AbstractValueFactory<Reference> {
054    
055        public UuidReferenceValueFactory( TextDecoder decoder,
056                                          ValueFactory<String> stringValueFactory ) {
057            super(PropertyType.REFERENCE, decoder, stringValueFactory);
058        }
059    
060        /**
061         * {@inheritDoc}
062         */
063        public Reference create( String value ) {
064            if (value == null) return null;
065            try {
066                UUID uuid = UUID.fromString(value);
067                return new UuidReference(uuid);
068            } catch (IllegalArgumentException err) {
069                throw new ValueFormatException(value, getPropertyType(),
070                                               GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
071                                                                                Reference.class.getSimpleName(),
072                                                                                value), err);
073            }
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        public Reference create( String value,
080                                 TextDecoder decoder ) {
081            // this probably doesn't really need to call the decoder, but by doing so then we don't care at all what the decoder does
082            return create(getDecoder(decoder).decode(value));
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public Reference create( int value ) {
089            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
090                                                                                                      Integer.class.getSimpleName(),
091                                                                                                      value));
092        }
093    
094        /**
095         * {@inheritDoc}
096         */
097        public Reference create( long value ) {
098            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
099                                                                                                      Long.class.getSimpleName(),
100                                                                                                      value));
101        }
102    
103        /**
104         * {@inheritDoc}
105         */
106        public Reference create( boolean value ) {
107            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
108                                                                                                      Boolean.class.getSimpleName(),
109                                                                                                      value));
110        }
111    
112        /**
113         * {@inheritDoc}
114         */
115        public Reference create( float value ) {
116            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
117                                                                                                      Float.class.getSimpleName(),
118                                                                                                      value));
119        }
120    
121        /**
122         * {@inheritDoc}
123         */
124        public Reference create( double value ) {
125            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
126                                                                                                      Double.class.getSimpleName(),
127                                                                                                      value));
128        }
129    
130        /**
131         * {@inheritDoc}
132         */
133        public Reference create( BigDecimal value ) {
134            throw new ValueFormatException(value, getPropertyType(),
135                                           GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
136                                                                            BigDecimal.class.getSimpleName(),
137                                                                            value));
138        }
139    
140        /**
141         * {@inheritDoc}
142         */
143        public Reference create( Calendar value ) {
144            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
145                                                                                                      Calendar.class.getSimpleName(),
146                                                                                                      value));
147        }
148    
149        /**
150         * {@inheritDoc}
151         */
152        public Reference create( Date value ) {
153            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
154                                                                                                      Date.class.getSimpleName(),
155                                                                                                      value));
156        }
157    
158        /**
159         * {@inheritDoc}
160         * 
161         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.DateTime)
162         */
163        public Reference create( DateTime value ) throws ValueFormatException {
164            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
165                                                                                                      DateTime.class.getSimpleName(),
166                                                                                                      value));
167        }
168    
169        /**
170         * {@inheritDoc}
171         */
172        public Reference create( Name value ) {
173            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
174                                                                                                      Name.class.getSimpleName(),
175                                                                                                      value));
176        }
177    
178        /**
179         * {@inheritDoc}
180         */
181        public Reference create( Path value ) {
182            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
183                                                                                                      Path.class.getSimpleName(),
184                                                                                                      value));
185        }
186    
187        /**
188         * {@inheritDoc}
189         */
190        public Reference create( Reference value ) {
191            return value;
192        }
193    
194        /**
195         * {@inheritDoc}
196         * 
197         * @see org.jboss.dna.graph.property.ValueFactory#create(java.util.UUID)
198         */
199        public Reference create( UUID value ) {
200            if (value == null) return null;
201            return new UuidReference(value);
202        }
203    
204        /**
205         * {@inheritDoc}
206         */
207        public Reference create( URI value ) {
208            throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
209                                                                                                      Date.class.getSimpleName(),
210                                                                                                      value));
211        }
212    
213        /**
214         * {@inheritDoc}
215         */
216        public Reference create( byte[] value ) {
217            // First attempt to create a string from the value, then a long from the string ...
218            return create(getStringValueFactory().create(value));
219        }
220    
221        /**
222         * {@inheritDoc}
223         * 
224         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.Binary)
225         */
226        public Reference create( Binary value ) throws ValueFormatException, IoException {
227            // First create a string and then create the boolean from the string value ...
228            return create(getStringValueFactory().create(value));
229        }
230    
231        /**
232         * {@inheritDoc}
233         */
234        public Reference create( InputStream stream,
235                                 long approximateLength ) throws IoException {
236            // First attempt to create a string from the value, then a double from the string ...
237            return create(getStringValueFactory().create(stream, approximateLength));
238        }
239    
240        /**
241         * {@inheritDoc}
242         */
243        public Reference create( Reader reader,
244                                 long approximateLength ) throws IoException {
245            // First attempt to create a string from the value, then a double from the string ...
246            return create(getStringValueFactory().create(reader, approximateLength));
247        }
248    
249        /**
250         * {@inheritDoc}
251         */
252        @Override
253        protected Reference[] createEmptyArray( int length ) {
254            return new Reference[length];
255        }
256    
257    }