001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.convert.converter; 018 019import java.text.Format; 020import java.text.ParsePosition; 021import java.util.Locale; 022 023import org.apache.wicket.util.convert.ConversionException; 024import org.apache.wicket.util.convert.IConverter; 025 026 027/** 028 * Base class for locale aware type converters. 029 * 030 * @author Eelco Hillenius 031 * @param <C> 032 */ 033public abstract class AbstractConverter<C> implements IConverter<C> 034{ 035 private static final long serialVersionUID = 1L; 036 037 /** 038 * Parses a value using one of the java.util.text format classes. 039 * 040 * @param format 041 * The format to use 042 * @param value 043 * The object to parse 044 * @param locale 045 * The locale to use to parse. 046 * @return The object 047 * @throws ConversionException 048 * Thrown if parsing fails 049 */ 050 @SuppressWarnings("unchecked") 051 protected C parse(final Format format, final Object value, final Locale locale) 052 { 053 final ParsePosition position = new ParsePosition(0); 054 final String stringValue = value.toString(); 055 final C result = (C)format.parseObject(stringValue, position); 056 057 if (position.getIndex() != stringValue.length()) 058 { 059 throw newConversionException("Cannot parse '" + value + "' using format " + format, 060 value, locale).setFormat(format); 061 } 062 return result; 063 } 064 065 /** 066 * Creates a conversion exception for throwing 067 * 068 * @param message 069 * The message 070 * @param value 071 * The value that didn't convert 072 * @param locale 073 * The locale 074 * @return The ConversionException 075 */ 076 protected ConversionException newConversionException(final String message, final Object value, 077 final Locale locale) 078 { 079 return new ConversionException(message).setSourceValue(value) 080 .setTargetType(getTargetType()) 081 .setConverter(this) 082 .setLocale(locale); 083 } 084 085 /** 086 * @return The target type of this type converter 087 */ 088 protected abstract Class<C> getTargetType(); 089 090 /** 091 * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object, Locale) 092 */ 093 @Override 094 public String convertToString(final C value, final Locale locale) 095 { 096 if (value == null) 097 { 098 return null; 099 } 100 return value.toString(); 101 } 102}