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