View Javadoc

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.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   * The standard {@link ValueFactory} for {@link PropertyType#URI} values.
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       * {@inheritDoc}
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       * {@inheritDoc}
82       */
83      public URI create( String value,
84                         TextDecoder decoder ) {
85          // 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
86          return create(getDecoder(decoder).decode(value));
87      }
88  
89      /**
90       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
167      * 
168      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.DateTime)
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      * {@inheritDoc}
179      */
180     public URI create( Name value ) {
181         if (value == null) return null;
182         return create("./" + value.getString(this.namespaceRegistry));
183     }
184 
185     /**
186      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
216      * 
217      * @see org.modeshape.graph.property.ValueFactory#create(java.util.UUID)
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      * {@inheritDoc}
227      */
228     public URI create( URI value ) {
229         return value;
230     }
231 
232     /**
233      * {@inheritDoc}
234      */
235     public URI create( byte[] value ) {
236         // First attempt to create a string from the value, then a long from the string ...
237         return create(getStringValueFactory().create(value));
238     }
239 
240     /**
241      * {@inheritDoc}
242      * 
243      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
244      */
245     public URI create( Binary value ) throws ValueFormatException, IoException {
246         // First create a string and then create the boolean from the string value ...
247         return create(getStringValueFactory().create(value));
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     public URI create( InputStream stream,
254                        long approximateLength ) throws IoException {
255         // First attempt to create a string from the value, then a double from the string ...
256         return create(getStringValueFactory().create(stream, approximateLength));
257     }
258 
259     /**
260      * {@inheritDoc}
261      */
262     public URI create( Reader reader,
263                        long approximateLength ) throws IoException {
264         // First attempt to create a string from the value, then a double from the string ...
265         return create(getStringValueFactory().create(reader, approximateLength));
266     }
267 
268     /**
269      * {@inheritDoc}
270      */
271     @Override
272     protected URI[] createEmptyArray( int length ) {
273         return new URI[length];
274     }
275 
276 }