001 /* 002 * Copyright 2003-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.convert1.string; 017 018 019 import org.apache.commons.convert1.ConversionException; 020 import org.apache.commons.convert1.Converter; 021 022 import java.net.URL; 023 import java.net.MalformedURLException; 024 025 026 027 /** 028 * <p>Standard {@link Converter} implementation that converts an incoming 029 * String into a <code>java.net.URL</code> object, optionally using a 030 * default value or throwing a {@link ConversionException} if a conversion 031 * error occurs.</p> 032 * 033 * @author Henri Yandell 034 * @version $Id: URLConverter.java 155441 2005-02-26 13:19:22Z dirkv $ 035 * @since 0.1 036 */ 037 038 public final class URLConverter implements Converter { 039 040 041 // ----------------------------------------------------------- Constructors 042 043 044 /** 045 * Create a {@link Converter} that will throw a {@link ConversionException} 046 * if a conversion error occurs. 047 */ 048 public URLConverter() { 049 050 this.defaultValue = null; 051 this.useDefault = false; 052 053 } 054 055 056 /** 057 * Create a {@link Converter} that will return the specified default value 058 * if a conversion error occurs. 059 * 060 * @param defaultValue The default value to be returned 061 */ 062 public URLConverter(Object defaultValue) { 063 064 this.defaultValue = defaultValue; 065 this.useDefault = true; 066 067 } 068 069 070 // ----------------------------------------------------- Instance Variables 071 072 073 /** 074 * The default value specified to our Constructor, if any. 075 */ 076 private Object defaultValue = null; 077 078 079 /** 080 * Should we return the default value on conversion errors? 081 */ 082 private boolean useDefault = true; 083 084 085 // --------------------------------------------------------- Public Methods 086 087 088 /** 089 * Convert the specified input object into an output object of the 090 * specified type. 091 * 092 * @param type Data type to which this value should be converted 093 * @param value The input value to be converted 094 * 095 * @exception ConversionException if conversion cannot be performed 096 * successfully 097 */ 098 public Object convert(Class type, Object value) { 099 100 if (value == null) { 101 if (useDefault) { 102 return (defaultValue); 103 } else { 104 throw new ConversionException("No value specified"); 105 } 106 } 107 108 if (value instanceof URL) { 109 return (value); 110 } 111 112 try { 113 return new URL(value.toString()); 114 } catch(MalformedURLException murle) { 115 if (useDefault) { 116 return (defaultValue); 117 } else { 118 throw new ConversionException(murle); 119 } 120 } 121 122 } 123 124 125 }