001 /* 002 * JBoss, Home of Professional Open Source. 003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors 004 * as indicated by the @author tags. See the copyright.txt file in the 005 * distribution for a full listing of individual contributors. 006 * 007 * This is free software; you can redistribute it and/or modify it 008 * under the terms of the GNU Lesser General Public License as 009 * published by the Free Software Foundation; either version 2.1 of 010 * the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this software; if not, write to the Free 019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 021 */ 022 package org.jboss.dna.graph.properties.basic; 023 024 import java.io.IOException; 025 import java.io.InputStream; 026 import java.io.Reader; 027 import java.io.UnsupportedEncodingException; 028 import java.math.BigDecimal; 029 import java.net.URI; 030 import java.util.Calendar; 031 import java.util.Date; 032 import java.util.UUID; 033 import net.jcip.annotations.Immutable; 034 import org.jboss.dna.common.text.TextDecoder; 035 import org.jboss.dna.common.util.IoUtil; 036 import org.jboss.dna.graph.GraphI18n; 037 import org.jboss.dna.graph.properties.Binary; 038 import org.jboss.dna.graph.properties.DateTime; 039 import org.jboss.dna.graph.properties.IoException; 040 import org.jboss.dna.graph.properties.Name; 041 import org.jboss.dna.graph.properties.Path; 042 import org.jboss.dna.graph.properties.PropertyType; 043 import org.jboss.dna.graph.properties.Reference; 044 import org.jboss.dna.graph.properties.ValueFactory; 045 import org.jboss.dna.graph.properties.ValueFormatException; 046 047 /** 048 * Teh standard {@link ValueFactory} for {@link PropertyType#BINARY} values. 049 * 050 * @author Randall Hauch 051 * @author John Verhaeg 052 */ 053 @Immutable 054 public class InMemoryBinaryValueFactory extends AbstractValueFactory<Binary> { 055 056 private static final String CHAR_SET_NAME = "UTF-8"; 057 058 public InMemoryBinaryValueFactory( TextDecoder decoder, 059 ValueFactory<String> stringValueFactory ) { 060 super(PropertyType.BINARY, decoder, stringValueFactory); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public Binary create( String value ) { 067 if (value == null) return null; 068 try { 069 return create(value.getBytes(CHAR_SET_NAME)); 070 } catch (UnsupportedEncodingException err) { 071 throw new ValueFormatException(value, getPropertyType(), 072 GraphI18n.errorConvertingType.text(String.class.getSimpleName(), 073 Binary.class.getSimpleName(), 074 value), err); 075 } 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 public Binary create( String value, 082 TextDecoder decoder ) { 083 if (value == null) return null; 084 return create(getDecoder(decoder).decode(value)); 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 public Binary create( int value ) { 091 // Convert the value to a string, then to a binary ... 092 return create(this.getStringValueFactory().create(value)); 093 } 094 095 /** 096 * {@inheritDoc} 097 */ 098 public Binary create( long value ) { 099 // Convert the value to a string, then to a binary ... 100 return create(this.getStringValueFactory().create(value)); 101 } 102 103 /** 104 * {@inheritDoc} 105 */ 106 public Binary create( boolean value ) { 107 // Convert the value to a string, then to a binary ... 108 return create(this.getStringValueFactory().create(value)); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 public Binary create( float value ) { 115 // Convert the value to a string, then to a binary ... 116 return create(this.getStringValueFactory().create(value)); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public Binary create( double value ) { 123 // Convert the value to a string, then to a binary ... 124 return create(this.getStringValueFactory().create(value)); 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 public Binary create( BigDecimal value ) { 131 // Convert the value to a string, then to a binary ... 132 return create(this.getStringValueFactory().create(value)); 133 } 134 135 /** 136 * {@inheritDoc} 137 */ 138 public Binary create( Calendar value ) { 139 // Convert the value to a string, then to a binary ... 140 return create(this.getStringValueFactory().create(value)); 141 } 142 143 /** 144 * {@inheritDoc} 145 */ 146 public Binary create( Date value ) { 147 // Convert the value to a string, then to a binary ... 148 return create(this.getStringValueFactory().create(value)); 149 } 150 151 /** 152 * {@inheritDoc} 153 * 154 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.DateTime) 155 */ 156 public Binary create( DateTime value ) throws ValueFormatException { 157 // Convert the value to a string, then to a binary ... 158 return create(this.getStringValueFactory().create(value)); 159 } 160 161 /** 162 * {@inheritDoc} 163 */ 164 public Binary create( Name value ) { 165 // Convert the value to a string, then to a binary ... 166 return create(this.getStringValueFactory().create(value)); 167 } 168 169 /** 170 * {@inheritDoc} 171 */ 172 public Binary create( Path value ) { 173 // Convert the value to a string, then to a binary ... 174 return create(this.getStringValueFactory().create(value)); 175 } 176 177 /** 178 * {@inheritDoc} 179 */ 180 public Binary create( Reference value ) { 181 // Convert the value to a string, then to a binary ... 182 return create(this.getStringValueFactory().create(value)); 183 } 184 185 /** 186 * {@inheritDoc} 187 */ 188 public Binary create( URI value ) { 189 // Convert the value to a string, then to a binary ... 190 return create(this.getStringValueFactory().create(value)); 191 } 192 193 /** 194 * {@inheritDoc} 195 * 196 * @see org.jboss.dna.graph.properties.ValueFactory#create(java.util.UUID) 197 */ 198 public Binary create( UUID 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 public Binary create( byte[] value ) { 207 return new InMemoryBinary(value); 208 } 209 210 /** 211 * {@inheritDoc} 212 * 213 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.Binary) 214 */ 215 public Binary create( Binary value ) throws ValueFormatException, IoException { 216 return value; 217 } 218 219 /** 220 * {@inheritDoc} 221 */ 222 public Binary create( InputStream stream, 223 long approximateLength ) throws IoException { 224 if (stream == null) return null; 225 try { 226 byte[] value = IoUtil.readBytes(stream); 227 return create(value); 228 } catch (IOException err) { 229 throw new IoException( 230 GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(), Binary.class.getSimpleName()), 231 err); 232 } 233 } 234 235 /** 236 * {@inheritDoc} 237 */ 238 public Binary create( Reader reader, 239 long approximateLength ) throws IoException { 240 if (reader == null) return null; 241 // Convert the value to a string, then to a binary ... 242 try { 243 String value = IoUtil.read(reader); 244 return create(this.getStringValueFactory().create(value)); 245 } catch (IOException err) { 246 throw new IoException(GraphI18n.errorConvertingIo.text(Reader.class.getSimpleName(), Binary.class.getSimpleName()), err); 247 } 248 } 249 250 /** 251 * {@inheritDoc} 252 */ 253 @Override 254 protected Binary[] createEmptyArray( int length ) { 255 return new Binary[length]; 256 } 257 }