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.bean.validation; 018 019import org.apache.wicket.WicketRuntimeException; 020import org.apache.wicket.markup.html.form.FormComponent; 021import org.apache.wicket.model.IPropertyReflectionAwareModel; 022import org.apache.wicket.model.PropertyModel; 023 024import java.lang.reflect.Field; 025import java.lang.reflect.Method; 026import java.util.Locale; 027 028/** 029 * Default property resolver. This resolver supports common Wicket models like the 030 * {@link PropertyModel}, and other implementations of {@link IPropertyReflectionAwareModel} 031 * 032 * @author igor 033 * 034 */ 035public class DefaultPropertyResolver implements IPropertyResolver 036{ 037 038 @Override 039 public Property resolveProperty(FormComponent<?> component) 040 { 041 IPropertyReflectionAwareModel<?> delegate = ValidationModelResolver.resolvePropertyModelFrom(component); 042 if (delegate == null) 043 { 044 return null; 045 } 046 047 String name; 048 Method getter = delegate.getPropertyGetter(); 049 if (getter != null) 050 { 051 String methodName = getter.getName(); 052 if (methodName.startsWith("get")) 053 { 054 name = methodName.substring(3, 4).toLowerCase(Locale.ROOT) + 055 methodName.substring(4); 056 } 057 else if (methodName.startsWith("is")) 058 { 059 name = methodName.substring(2, 3).toLowerCase(Locale.ROOT) + 060 methodName.substring(3); 061 } 062 else 063 { 064 throw new WicketRuntimeException("Invalid name for a getter method: '" 065 + methodName + "'. It must start either with 'get' or 'is'."); 066 } 067 return new Property(getter.getDeclaringClass(), name); 068 } 069 070 Field field = delegate.getPropertyField(); 071 if (field != null) 072 { 073 return new Property(field.getDeclaringClass(), field.getName()); 074 } 075 076 return null; 077 } 078 079}