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.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.Reader;
30  import java.io.UnsupportedEncodingException;
31  import java.math.BigDecimal;
32  import java.net.URI;
33  import java.util.Calendar;
34  import java.util.Date;
35  import java.util.UUID;
36  import net.jcip.annotations.Immutable;
37  import org.modeshape.common.text.TextDecoder;
38  import org.modeshape.common.util.IoUtil;
39  import org.modeshape.graph.GraphI18n;
40  import org.modeshape.graph.property.Binary;
41  import org.modeshape.graph.property.BinaryFactory;
42  import org.modeshape.graph.property.DateTime;
43  import org.modeshape.graph.property.IoException;
44  import org.modeshape.graph.property.Name;
45  import org.modeshape.graph.property.Path;
46  import org.modeshape.graph.property.PropertyType;
47  import org.modeshape.graph.property.Reference;
48  import org.modeshape.graph.property.ValueFactory;
49  import org.modeshape.graph.property.ValueFormatException;
50  
51  /**
52   * An abstract {@link BinaryFactory} implementation that contains many general methods that are likely to be appropriate for many
53   * concrete implementations.
54   */
55  @Immutable
56  public abstract class AbstractBinaryValueFactory extends AbstractValueFactory<Binary> implements BinaryFactory {
57  
58      private static final String CHAR_SET_NAME = "UTF-8";
59  
60      protected AbstractBinaryValueFactory( TextDecoder decoder,
61                                            ValueFactory<String> stringValueFactory ) {
62          super(PropertyType.BINARY, decoder, stringValueFactory);
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public Binary create( String value ) {
69          if (value == null) return null;
70          try {
71              return create(value.getBytes(CHAR_SET_NAME));
72          } catch (UnsupportedEncodingException err) {
73              throw new ValueFormatException(value, getPropertyType(),
74                                             GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
75                                                                                Binary.class.getSimpleName(),
76                                                                                value), err);
77          }
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public Binary create( String value,
84                            TextDecoder decoder ) {
85          if (value == null) return null;
86          return create(getDecoder(decoder).decode(value));
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public Binary create( int value ) {
93          // Convert the value to a string, then to a binary ...
94          return create(this.getStringValueFactory().create(value));
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public Binary create( long value ) {
101         // Convert the value to a string, then to a binary ...
102         return create(this.getStringValueFactory().create(value));
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     public Binary create( boolean value ) {
109         // Convert the value to a string, then to a binary ...
110         return create(this.getStringValueFactory().create(value));
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     public Binary create( float value ) {
117         // Convert the value to a string, then to a binary ...
118         return create(this.getStringValueFactory().create(value));
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     public Binary create( double value ) {
125         // Convert the value to a string, then to a binary ...
126         return create(this.getStringValueFactory().create(value));
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public Binary create( BigDecimal value ) {
133         // Convert the value to a string, then to a binary ...
134         return create(this.getStringValueFactory().create(value));
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     public Binary create( Calendar value ) {
141         // Convert the value to a string, then to a binary ...
142         return create(this.getStringValueFactory().create(value));
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     public Binary create( Date value ) {
149         // Convert the value to a string, then to a binary ...
150         return create(this.getStringValueFactory().create(value));
151     }
152 
153     /**
154      * {@inheritDoc}
155      * 
156      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.DateTime)
157      */
158     public Binary create( DateTime value ) throws ValueFormatException {
159         // Convert the value to a string, then to a binary ...
160         return create(this.getStringValueFactory().create(value));
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     public Binary create( Name value ) {
167         // Convert the value to a string, then to a binary ...
168         return create(this.getStringValueFactory().create(value));
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     public Binary create( Path value ) {
175         // Convert the value to a string, then to a binary ...
176         return create(this.getStringValueFactory().create(value));
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     public Binary create( Path.Segment value ) {
183         // Convert the value to a string, then to a binary ...
184         return create(this.getStringValueFactory().create(value));
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     public Binary create( Reference value ) {
191         // Convert the value to a string, then to a binary ...
192         return create(this.getStringValueFactory().create(value));
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     public Binary create( URI value ) {
199         // Convert the value to a string, then to a binary ...
200         return create(this.getStringValueFactory().create(value));
201     }
202 
203     /**
204      * {@inheritDoc}
205      * 
206      * @see org.modeshape.graph.property.ValueFactory#create(java.util.UUID)
207      */
208     public Binary create( UUID value ) {
209         // Convert the value to a string, then to a binary ...
210         return create(this.getStringValueFactory().create(value));
211     }
212 
213     /**
214      * {@inheritDoc}
215      * 
216      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
217      */
218     public Binary create( Binary value ) throws ValueFormatException, IoException {
219         return value;
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     public Binary create( InputStream stream,
226                           long approximateLength ) throws IoException {
227         if (stream == null) return null;
228         try {
229             byte[] value = IoUtil.readBytes(stream);
230             return create(value);
231         } catch (IOException err) {
232             throw new IoException(GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(),
233                                                                    Binary.class.getSimpleName()), err);
234         }
235     }
236 
237     /**
238      * {@inheritDoc}
239      */
240     public Binary create( Reader reader,
241                           long approximateLength ) throws IoException {
242         if (reader == null) return null;
243         // Convert the value to a string, then to a binary ...
244         try {
245             String value = IoUtil.read(reader);
246             return create(this.getStringValueFactory().create(value));
247         } catch (IOException err) {
248             throw new IoException(GraphI18n.errorConvertingIo.text(Reader.class.getSimpleName(), Binary.class.getSimpleName()),
249                                   err);
250         }
251     }
252 
253     /**
254      * {@inheritDoc}
255      * 
256      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.File)
257      */
258     public Binary create( File file ) throws ValueFormatException, IoException {
259         if (file == null) return null;
260         if (!file.canRead()) return null;
261         if (!file.isFile()) return null;
262         try {
263             byte[] value = IoUtil.readBytes(file);
264             return create(value);
265         } catch (IOException err) {
266             throw new IoException(GraphI18n.errorConvertingIo.text(file, Binary.class.getSimpleName()), err);
267         }
268     }
269 
270     /**
271      * {@inheritDoc}
272      * <p>
273      * This implementation does not manage or share the in-memory binary values, so this implementation is identical to calling
274      * {@link #create(Reader, long)}.
275      * </p>
276      * 
277      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.Reader, long, byte[])
278      */
279     public Binary create( Reader reader,
280                           long approximateLength,
281                           byte[] secureHash ) throws ValueFormatException, IoException {
282         return create(reader, approximateLength);
283     }
284 
285     /**
286      * {@inheritDoc}
287      * <p>
288      * This implementation does not manage or share the in-memory binary values, so this implementation is identical to calling
289      * {@link #create(InputStream, long)}.
290      * </p>
291      * 
292      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.InputStream, long, byte[])
293      */
294     public Binary create( InputStream stream,
295                           long approximateLength,
296                           byte[] secureHash ) throws ValueFormatException, IoException {
297         return create(stream, approximateLength);
298     }
299 
300     /**
301      * {@inheritDoc}
302      * <p>
303      * This method always returns null, since the in-memory binary values are not managed or shared.
304      * </p>
305      * 
306      * @see org.modeshape.graph.property.BinaryFactory#find(byte[])
307      */
308     public Binary find( byte[] secureHash ) {
309         return null;
310     }
311 
312     /**
313      * {@inheritDoc}
314      */
315     @Override
316     protected Binary[] createEmptyArray( int length ) {
317         return new Binary[length];
318     }
319 }