1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.net.URISyntaxException;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.UUID;
34 import net.jcip.annotations.Immutable;
35 import org.modeshape.common.text.TextDecoder;
36 import org.modeshape.common.util.CheckArg;
37 import org.modeshape.graph.GraphI18n;
38 import org.modeshape.graph.property.Binary;
39 import org.modeshape.graph.property.DateTime;
40 import org.modeshape.graph.property.IoException;
41 import org.modeshape.graph.property.Name;
42 import org.modeshape.graph.property.NamespaceRegistry;
43 import org.modeshape.graph.property.Path;
44 import org.modeshape.graph.property.PropertyType;
45 import org.modeshape.graph.property.Reference;
46 import org.modeshape.graph.property.ValueFactory;
47 import org.modeshape.graph.property.ValueFormatException;
48
49
50
51
52 @Immutable
53 public class UriValueFactory extends AbstractValueFactory<URI> {
54
55 private final NamespaceRegistry namespaceRegistry;
56
57 public UriValueFactory( NamespaceRegistry namespaceRegistry,
58 TextDecoder decoder,
59 ValueFactory<String> stringValueFactory ) {
60 super(PropertyType.URI, decoder, stringValueFactory);
61 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
62 this.namespaceRegistry = namespaceRegistry;
63 }
64
65
66
67
68 public URI create( String value ) {
69 if (value == null) return null;
70 try {
71 return new URI(value);
72 } catch (URISyntaxException err) {
73 throw new ValueFormatException(value, getPropertyType(),
74 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
75 URI.class.getSimpleName(),
76 value), err);
77 }
78 }
79
80
81
82
83 public URI create( String value,
84 TextDecoder decoder ) {
85
86 return create(getDecoder(decoder).decode(value));
87 }
88
89
90
91
92 public URI create( int value ) {
93 throw new ValueFormatException(value, getPropertyType(),
94 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
95 Integer.class.getSimpleName(),
96 value));
97 }
98
99
100
101
102 public URI create( long value ) {
103 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
104 Long.class.getSimpleName(),
105 value));
106 }
107
108
109
110
111 public URI create( boolean value ) {
112 throw new ValueFormatException(value, getPropertyType(),
113 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
114 Boolean.class.getSimpleName(),
115 value));
116 }
117
118
119
120
121 public URI create( float value ) {
122 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
123 Float.class.getSimpleName(),
124 value));
125 }
126
127
128
129
130 public URI create( double value ) {
131 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
132 Double.class.getSimpleName(),
133 value));
134 }
135
136
137
138
139 public URI create( BigDecimal value ) {
140 throw new ValueFormatException(value, getPropertyType(),
141 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
142 BigDecimal.class.getSimpleName(),
143 value));
144 }
145
146
147
148
149 public URI create( Calendar value ) {
150 throw new ValueFormatException(value, getPropertyType(),
151 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
152 Calendar.class.getSimpleName(),
153 value));
154 }
155
156
157
158
159 public URI create( Date value ) {
160 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
161 Date.class.getSimpleName(),
162 value));
163 }
164
165
166
167
168
169
170 public URI create( DateTime value ) throws ValueFormatException {
171 throw new ValueFormatException(value, getPropertyType(),
172 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
173 DateTime.class.getSimpleName(),
174 value));
175 }
176
177
178
179
180 public URI create( Name value ) {
181 if (value == null) return null;
182 return create("./" + value.getString(this.namespaceRegistry));
183 }
184
185
186
187
188 public URI create( Path value ) {
189 if (value == null) return null;
190 if (value.isAbsolute()) {
191 return create("/" + value.getString(this.namespaceRegistry));
192 }
193 return create("./" + value.getString(this.namespaceRegistry));
194 }
195
196
197
198
199 public URI create( Path.Segment value ) {
200 if (value == null) return null;
201 return create("./" + value.getString(this.namespaceRegistry));
202 }
203
204
205
206
207 public URI create( Reference value ) {
208 throw new ValueFormatException(value, getPropertyType(),
209 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
210 Reference.class.getSimpleName(),
211 value));
212 }
213
214
215
216
217
218
219 public URI create( UUID value ) {
220 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
221 UUID.class.getSimpleName(),
222 value));
223 }
224
225
226
227
228 public URI create( URI value ) {
229 return value;
230 }
231
232
233
234
235 public URI create( byte[] value ) {
236
237 return create(getStringValueFactory().create(value));
238 }
239
240
241
242
243
244
245 public URI create( Binary value ) throws ValueFormatException, IoException {
246
247 return create(getStringValueFactory().create(value));
248 }
249
250
251
252
253 public URI create( InputStream stream,
254 long approximateLength ) throws IoException {
255
256 return create(getStringValueFactory().create(stream, approximateLength));
257 }
258
259
260
261
262 public URI create( Reader reader,
263 long approximateLength ) throws IoException {
264
265 return create(getStringValueFactory().create(reader, approximateLength));
266 }
267
268
269
270
271 @Override
272 protected URI[] createEmptyArray( int length ) {
273 return new URI[length];
274 }
275
276 }