1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.property.basic;
25
26 import java.io.InputStream;
27 import java.io.Reader;
28 import java.math.BigDecimal;
29 import java.net.URI;
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.UUID;
33 import net.jcip.annotations.Immutable;
34 import org.modeshape.common.text.TextDecoder;
35 import org.modeshape.graph.GraphI18n;
36 import org.modeshape.graph.property.Binary;
37 import org.modeshape.graph.property.DateTime;
38 import org.modeshape.graph.property.IoException;
39 import org.modeshape.graph.property.Name;
40 import org.modeshape.graph.property.Path;
41 import org.modeshape.graph.property.PropertyType;
42 import org.modeshape.graph.property.Reference;
43 import org.modeshape.graph.property.ValueFactory;
44 import org.modeshape.graph.property.ValueFormatException;
45
46 /**
47 * The standard {@link ValueFactory} for {@link PropertyType#REFERENCE} values.
48 */
49 @Immutable
50 public class UuidReferenceValueFactory extends AbstractValueFactory<Reference> {
51
52 public UuidReferenceValueFactory( TextDecoder decoder,
53 ValueFactory<String> stringValueFactory ) {
54 super(PropertyType.REFERENCE, decoder, stringValueFactory);
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 public Reference create( String value ) {
61 if (value == null) return null;
62 try {
63 UUID uuid = UUID.fromString(value);
64 return new UuidReference(uuid);
65 } catch (IllegalArgumentException err) {
66 throw new ValueFormatException(value, getPropertyType(),
67 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
68 Reference.class.getSimpleName(),
69 value), err);
70 }
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 public Reference create( String value,
77 TextDecoder decoder ) {
78 // 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
79 return create(getDecoder(decoder).decode(value));
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public Reference create( int value ) {
86 throw new ValueFormatException(value, getPropertyType(),
87 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
88 Integer.class.getSimpleName(),
89 value));
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 public Reference create( long value ) {
96 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
97 Long.class.getSimpleName(),
98 value));
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public Reference create( boolean value ) {
105 throw new ValueFormatException(value, getPropertyType(),
106 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
107 Boolean.class.getSimpleName(),
108 value));
109 }
110
111 /**
112 * {@inheritDoc}
113 */
114 public Reference create( float value ) {
115 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
116 Float.class.getSimpleName(),
117 value));
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public Reference create( double value ) {
124 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
125 Double.class.getSimpleName(),
126 value));
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 public Reference create( BigDecimal value ) {
133 throw new ValueFormatException(value, getPropertyType(),
134 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
135 BigDecimal.class.getSimpleName(),
136 value));
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 public Reference create( Calendar value ) {
143 throw new ValueFormatException(value, getPropertyType(),
144 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.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.DateTime)
162 */
163 public Reference create( DateTime value ) throws ValueFormatException {
164 throw new ValueFormatException(value, getPropertyType(),
165 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
166 DateTime.class.getSimpleName(),
167 value));
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 public Reference create( Name value ) {
174 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
175 Name.class.getSimpleName(),
176 value));
177 }
178
179 /**
180 * {@inheritDoc}
181 */
182 public Reference create( Path value ) {
183 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
184 Path.class.getSimpleName(),
185 value));
186 }
187
188 /**
189 * {@inheritDoc}
190 */
191 public Reference create( Reference value ) {
192 return value;
193 }
194
195 /**
196 * {@inheritDoc}
197 *
198 * @see org.modeshape.graph.property.ValueFactory#create(java.util.UUID)
199 */
200 public Reference create( UUID value ) {
201 if (value == null) return null;
202 return new UuidReference(value);
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 public Reference create( URI value ) {
209 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
210 Date.class.getSimpleName(),
211 value));
212 }
213
214 /**
215 * {@inheritDoc}
216 */
217 public Reference create( byte[] value ) {
218 // First attempt to create a string from the value, then a long from the string ...
219 return create(getStringValueFactory().create(value));
220 }
221
222 /**
223 * {@inheritDoc}
224 *
225 * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
226 */
227 public Reference create( Binary value ) throws ValueFormatException, IoException {
228 // First create a string and then create the boolean from the string value ...
229 return create(getStringValueFactory().create(value));
230 }
231
232 /**
233 * {@inheritDoc}
234 */
235 public Reference create( InputStream stream,
236 long approximateLength ) throws IoException {
237 // First attempt to create a string from the value, then a double from the string ...
238 return create(getStringValueFactory().create(stream, approximateLength));
239 }
240
241 /**
242 * {@inheritDoc}
243 */
244 public Reference create( Reader reader,
245 long approximateLength ) throws IoException {
246 // First attempt to create a string from the value, then a double from the string ...
247 return create(getStringValueFactory().create(reader, approximateLength));
248 }
249
250 /**
251 * {@inheritDoc}
252 */
253 @Override
254 protected Reference[] createEmptyArray( int length ) {
255 return new Reference[length];
256 }
257
258 }