001/* 002 * Copyright (c) 2004-2012, Willem Cazander 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 006 * that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 009 * following disclaimer. 010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 011 * the following disclaimer in the documentation and/or other materials provided with the distribution. 012 * 013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 022 */ 023 024package org.x4o.xml.conv.text; 025 026import java.net.MalformedURLException; 027import java.net.URL; 028import java.util.Locale; 029 030import org.x4o.xml.conv.AbstractStringObjectConverter; 031import org.x4o.xml.conv.ObjectConverter; 032import org.x4o.xml.conv.ObjectConverterException; 033 034/** 035 * URLConverter. 036 * 037 * @author Willem Cazander 038 * @version 1.0 Jan 20, 2012 039 */ 040public class URLConverter extends AbstractStringObjectConverter { 041 042 private static final long serialVersionUID = -611843641266301893L; 043 044 /** 045 * @see org.x4o.xml.conv.ObjectConverter#getObjectClassTo() 046 */ 047 public Class<?> getObjectClassTo() { 048 return URL.class; 049 } 050 051 /** 052 * @see org.x4o.xml.conv.AbstractStringObjectConverter#convertStringTo(java.lang.String, java.util.Locale) 053 */ 054 public Object convertStringTo(String str, Locale locale) throws ObjectConverterException { 055 try { 056 return new URL(str); 057 } catch (MalformedURLException e) { 058 throw new ObjectConverterException(this,e.getMessage(),e); 059 } 060 } 061 062 /** 063 * @see org.x4o.xml.conv.AbstractStringObjectConverter#convertStringBack(java.lang.Object, java.util.Locale) 064 */ 065 public String convertStringBack(Object obj,Locale locale) throws ObjectConverterException { 066 return ((URL)obj).toString(); 067 } 068 069 /** 070 * @see org.x4o.xml.conv.AbstractObjectConverter#clone() 071 */ 072 @Override 073 public ObjectConverter clone() throws CloneNotSupportedException { 074 URLConverter result = new URLConverter(); 075 result.converters=cloneConverters(); 076 return result; 077 } 078}