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.InputStream; 025 import java.io.Reader; 026 import java.math.BigDecimal; 027 import java.net.URI; 028 import java.net.URISyntaxException; 029 import java.util.Calendar; 030 import java.util.Date; 031 import java.util.UUID; 032 import net.jcip.annotations.Immutable; 033 import org.jboss.dna.common.text.TextDecoder; 034 import org.jboss.dna.common.util.CheckArg; 035 import org.jboss.dna.graph.GraphI18n; 036 import org.jboss.dna.graph.properties.Binary; 037 import org.jboss.dna.graph.properties.DateTime; 038 import org.jboss.dna.graph.properties.IoException; 039 import org.jboss.dna.graph.properties.Name; 040 import org.jboss.dna.graph.properties.NamespaceRegistry; 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 * The standard {@link ValueFactory} for {@link PropertyType#URI} values. 049 * 050 * @author Randall Hauch 051 * @author John Verhaeg 052 */ 053 @Immutable 054 public class UriValueFactory extends AbstractValueFactory<URI> { 055 056 private final NamespaceRegistry namespaceRegistry; 057 058 public UriValueFactory( NamespaceRegistry namespaceRegistry, 059 TextDecoder decoder, 060 ValueFactory<String> stringValueFactory ) { 061 super(PropertyType.URI, decoder, stringValueFactory); 062 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry"); 063 this.namespaceRegistry = namespaceRegistry; 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 public URI create( String value ) { 070 if (value == null) return null; 071 try { 072 return new URI(value); 073 } catch (URISyntaxException err) { 074 throw new ValueFormatException(value, getPropertyType(), 075 GraphI18n.errorConvertingType.text(String.class.getSimpleName(), 076 URI.class.getSimpleName(), 077 value), err); 078 } 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 public URI create( String value, 085 TextDecoder decoder ) { 086 // 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 087 return create(getDecoder(decoder).decode(value)); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 public URI create( int value ) { 094 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 095 Integer.class.getSimpleName(), 096 value)); 097 } 098 099 /** 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(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 113 Boolean.class.getSimpleName(), 114 value)); 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 public URI create( float value ) { 121 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 122 Float.class.getSimpleName(), 123 value)); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 public URI create( double value ) { 130 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 131 Double.class.getSimpleName(), 132 value)); 133 } 134 135 /** 136 * {@inheritDoc} 137 */ 138 public URI create( BigDecimal value ) { 139 throw new ValueFormatException(value, getPropertyType(), 140 GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 141 BigDecimal.class.getSimpleName(), 142 value)); 143 } 144 145 /** 146 * {@inheritDoc} 147 */ 148 public URI create( Calendar value ) { 149 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 150 Calendar.class.getSimpleName(), 151 value)); 152 } 153 154 /** 155 * {@inheritDoc} 156 */ 157 public URI create( Date value ) { 158 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 159 Date.class.getSimpleName(), 160 value)); 161 } 162 163 /** 164 * {@inheritDoc} 165 * 166 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.DateTime) 167 */ 168 public URI create( DateTime value ) throws ValueFormatException { 169 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 170 DateTime.class.getSimpleName(), 171 value)); 172 } 173 174 /** 175 * {@inheritDoc} 176 */ 177 public URI create( Name value ) { 178 if (value == null) return null; 179 return create("./" + value.getString(this.namespaceRegistry)); 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 public URI create( Path value ) { 186 if (value == null) return null; 187 if (value.isAbsolute()) { 188 return create("/" + value.getString(this.namespaceRegistry)); 189 } 190 return create("./" + value.getString(this.namespaceRegistry)); 191 } 192 193 /** 194 * {@inheritDoc} 195 */ 196 public URI create( Reference value ) { 197 throw new ValueFormatException(value, getPropertyType(), 198 GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 199 Reference.class.getSimpleName(), 200 value)); 201 } 202 203 /** 204 * {@inheritDoc} 205 * 206 * @see org.jboss.dna.graph.properties.ValueFactory#create(java.util.UUID) 207 */ 208 public URI create( UUID value ) { 209 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 210 UUID.class.getSimpleName(), 211 value)); 212 } 213 214 /** 215 * {@inheritDoc} 216 */ 217 public URI create( URI value ) { 218 return value; 219 } 220 221 /** 222 * {@inheritDoc} 223 */ 224 public URI create( byte[] value ) { 225 // First attempt to create a string from the value, then a long from the string ... 226 return create(getStringValueFactory().create(value)); 227 } 228 229 /** 230 * {@inheritDoc} 231 * 232 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.Binary) 233 */ 234 public URI create( Binary value ) throws ValueFormatException, IoException { 235 // First create a string and then create the boolean from the string value ... 236 return create(getStringValueFactory().create(value)); 237 } 238 239 /** 240 * {@inheritDoc} 241 */ 242 public URI create( InputStream stream, 243 long approximateLength ) throws IoException { 244 // First attempt to create a string from the value, then a double from the string ... 245 return create(getStringValueFactory().create(stream, approximateLength)); 246 } 247 248 /** 249 * {@inheritDoc} 250 */ 251 public URI create( Reader reader, 252 long approximateLength ) throws IoException { 253 // First attempt to create a string from the value, then a double from the string ... 254 return create(getStringValueFactory().create(reader, approximateLength)); 255 } 256 257 /** 258 * {@inheritDoc} 259 */ 260 @Override 261 protected URI[] createEmptyArray( int length ) { 262 return new URI[length]; 263 } 264 265 }