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( Reference 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( URI 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      * @see org.modeshape.graph.property.ValueFactory#create(java.util.UUID)
199      */
200     public Binary create( UUID value ) {
201         // Convert the value to a string, then to a binary ...
202         return create(this.getStringValueFactory().create(value));
203     }
204 
205     /**
206      * {@inheritDoc}
207      * 
208      * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary)
209      */
210     public Binary create( Binary value ) throws ValueFormatException, IoException {
211         return value;
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     public Binary create( InputStream stream,
218                           long approximateLength ) throws IoException {
219         if (stream == null) return null;
220         try {
221             byte[] value = IoUtil.readBytes(stream);
222             return create(value);
223         } catch (IOException err) {
224             throw new IoException(GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(),
225                                                                    Binary.class.getSimpleName()), err);
226         }
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     public Binary create( Reader reader,
233                           long approximateLength ) throws IoException {
234         if (reader == null) return null;
235         // Convert the value to a string, then to a binary ...
236         try {
237             String value = IoUtil.read(reader);
238             return create(this.getStringValueFactory().create(value));
239         } catch (IOException err) {
240             throw new IoException(GraphI18n.errorConvertingIo.text(Reader.class.getSimpleName(), Binary.class.getSimpleName()),
241                                   err);
242         }
243     }
244 
245     /**
246      * {@inheritDoc}
247      * 
248      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.File)
249      */
250     public Binary create( File file ) throws ValueFormatException, IoException {
251         if (file == null) return null;
252         if (!file.canRead()) return null;
253         if (!file.isFile()) return null;
254         try {
255             byte[] value = IoUtil.readBytes(file);
256             return create(value);
257         } catch (IOException err) {
258             throw new IoException(GraphI18n.errorConvertingIo.text(file, Binary.class.getSimpleName()), err);
259         }
260     }
261 
262     /**
263      * {@inheritDoc}
264      * <p>
265      * This implementation does not manage or share the in-memory binary values, so this implementation is identical to calling
266      * {@link #create(Reader, long)}.
267      * </p>
268      * 
269      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.Reader, long, byte[])
270      */
271     public Binary create( Reader reader,
272                           long approximateLength,
273                           byte[] secureHash ) throws ValueFormatException, IoException {
274         return create(reader, approximateLength);
275     }
276 
277     /**
278      * {@inheritDoc}
279      * <p>
280      * This implementation does not manage or share the in-memory binary values, so this implementation is identical to calling
281      * {@link #create(InputStream, long)}.
282      * </p>
283      * 
284      * @see org.modeshape.graph.property.BinaryFactory#create(java.io.InputStream, long, byte[])
285      */
286     public Binary create( InputStream stream,
287                           long approximateLength,
288                           byte[] secureHash ) throws ValueFormatException, IoException {
289         return create(stream, approximateLength);
290     }
291 
292     /**
293      * {@inheritDoc}
294      * <p>
295      * This method always returns null, since the in-memory binary values are not managed or shared.
296      * </p>
297      * 
298      * @see org.modeshape.graph.property.BinaryFactory#find(byte[])
299      */
300     public Binary find( byte[] secureHash ) {
301         return null;
302     }
303 
304     /**
305      * {@inheritDoc}
306      */
307     @Override
308     protected Binary[] createEmptyArray( int length ) {
309         return new Binary[length];
310     }
311 }