001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.graph.property.basic; 025 026 import java.io.InputStream; 027 import java.io.Reader; 028 import java.math.BigDecimal; 029 import java.net.URI; 030 import java.net.URISyntaxException; 031 import java.util.Calendar; 032 import java.util.Date; 033 import java.util.UUID; 034 import net.jcip.annotations.Immutable; 035 import org.jboss.dna.common.text.TextDecoder; 036 import org.jboss.dna.common.util.CheckArg; 037 import org.jboss.dna.graph.GraphI18n; 038 import org.jboss.dna.graph.property.Binary; 039 import org.jboss.dna.graph.property.DateTime; 040 import org.jboss.dna.graph.property.IoException; 041 import org.jboss.dna.graph.property.Name; 042 import org.jboss.dna.graph.property.NamespaceRegistry; 043 import org.jboss.dna.graph.property.Path; 044 import org.jboss.dna.graph.property.PropertyType; 045 import org.jboss.dna.graph.property.Reference; 046 import org.jboss.dna.graph.property.ValueFactory; 047 import org.jboss.dna.graph.property.ValueFormatException; 048 049 /** 050 * The standard {@link ValueFactory} for {@link PropertyType#URI} values. 051 * 052 * @author Randall Hauch 053 * @author John Verhaeg 054 */ 055 @Immutable 056 public class UriValueFactory extends AbstractValueFactory<URI> { 057 058 private final NamespaceRegistry namespaceRegistry; 059 060 public UriValueFactory( NamespaceRegistry namespaceRegistry, 061 TextDecoder decoder, 062 ValueFactory<String> stringValueFactory ) { 063 super(PropertyType.URI, decoder, stringValueFactory); 064 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry"); 065 this.namespaceRegistry = namespaceRegistry; 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 public URI create( String value ) { 072 if (value == null) return null; 073 try { 074 return new URI(value); 075 } catch (URISyntaxException err) { 076 throw new ValueFormatException(value, getPropertyType(), 077 GraphI18n.errorConvertingType.text(String.class.getSimpleName(), 078 URI.class.getSimpleName(), 079 value), err); 080 } 081 } 082 083 /** 084 * {@inheritDoc} 085 */ 086 public URI create( String value, 087 TextDecoder decoder ) { 088 // 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 089 return create(getDecoder(decoder).decode(value)); 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 public URI create( int value ) { 096 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 097 Integer.class.getSimpleName(), 098 value)); 099 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 public URI create( long value ) { 105 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 106 Long.class.getSimpleName(), 107 value)); 108 } 109 110 /** 111 * {@inheritDoc} 112 */ 113 public URI create( boolean value ) { 114 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 115 Boolean.class.getSimpleName(), 116 value)); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public URI create( float value ) { 123 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 124 Float.class.getSimpleName(), 125 value)); 126 } 127 128 /** 129 * {@inheritDoc} 130 */ 131 public URI create( double value ) { 132 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 133 Double.class.getSimpleName(), 134 value)); 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 public URI create( BigDecimal value ) { 141 throw new ValueFormatException(value, getPropertyType(), 142 GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 143 BigDecimal.class.getSimpleName(), 144 value)); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 public URI create( Calendar value ) { 151 throw new ValueFormatException(value, getPropertyType(), 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.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.DateTime) 169 */ 170 public URI create( DateTime value ) throws ValueFormatException { 171 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 172 DateTime.class.getSimpleName(), 173 value)); 174 } 175 176 /** 177 * {@inheritDoc} 178 */ 179 public URI create( Name value ) { 180 if (value == null) return null; 181 return create("./" + value.getString(this.namespaceRegistry)); 182 } 183 184 /** 185 * {@inheritDoc} 186 */ 187 public URI create( Path value ) { 188 if (value == null) return null; 189 if (value.isAbsolute()) { 190 return create("/" + value.getString(this.namespaceRegistry)); 191 } 192 return create("./" + value.getString(this.namespaceRegistry)); 193 } 194 195 /** 196 * {@inheritDoc} 197 */ 198 public URI create( Reference value ) { 199 throw new ValueFormatException(value, getPropertyType(), 200 GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 201 Reference.class.getSimpleName(), 202 value)); 203 } 204 205 /** 206 * {@inheritDoc} 207 * 208 * @see org.jboss.dna.graph.property.ValueFactory#create(java.util.UUID) 209 */ 210 public URI create( UUID value ) { 211 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(), 212 UUID.class.getSimpleName(), 213 value)); 214 } 215 216 /** 217 * {@inheritDoc} 218 */ 219 public URI create( URI value ) { 220 return value; 221 } 222 223 /** 224 * {@inheritDoc} 225 */ 226 public URI create( byte[] value ) { 227 // First attempt to create a string from the value, then a long from the string ... 228 return create(getStringValueFactory().create(value)); 229 } 230 231 /** 232 * {@inheritDoc} 233 * 234 * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.Binary) 235 */ 236 public URI create( Binary value ) throws ValueFormatException, IoException { 237 // First create a string and then create the boolean from the string value ... 238 return create(getStringValueFactory().create(value)); 239 } 240 241 /** 242 * {@inheritDoc} 243 */ 244 public URI create( InputStream stream, 245 long approximateLength ) throws IoException { 246 // First attempt to create a string from the value, then a double from the string ... 247 return create(getStringValueFactory().create(stream, approximateLength)); 248 } 249 250 /** 251 * {@inheritDoc} 252 */ 253 public URI create( Reader reader, 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(reader, approximateLength)); 257 } 258 259 /** 260 * {@inheritDoc} 261 */ 262 @Override 263 protected URI[] createEmptyArray( int length ) { 264 return new URI[length]; 265 } 266 267 }