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.resource.loader; 018 019import java.util.Locale; 020 021import org.apache.wicket.Component; 022import org.apache.wicket.markup.html.form.Form; 023import org.apache.wicket.markup.html.form.FormComponent; 024import org.apache.wicket.markup.html.form.validation.FormValidatorAdapter; 025import org.apache.wicket.markup.html.form.validation.IFormValidator; 026import org.apache.wicket.validation.IValidator; 027import org.apache.wicket.validation.ValidatorAdapter; 028 029 030/** 031 * This is one of Wicket's default string resource loaders. 032 * <p> 033 * The validator string resource loader checks resource bundles attached to validators (eg 034 * MinimumValidator.properties). The validator list is pulled from the form / form component in error. 035 * <p> 036 * This implementation is fully aware of both locale and style values when trying to obtain the 037 * appropriate resources. 038 * <p> 039 * 040 * @author igor.vaynberg 041 */ 042public class ValidatorStringResourceLoader extends ComponentStringResourceLoader 043{ 044 /** 045 * Create and initialize the resource loader. 046 */ 047 public ValidatorStringResourceLoader() 048 { 049 } 050 051 @Override 052 public String loadStringResource(Class<?> clazz, final String key, final Locale locale, 053 final String style, final String variation) 054 { 055 // only care about IValidator/IFormValidator subclasses 056 if ( 057 clazz == null || 058 !(IValidator.class.isAssignableFrom(clazz) || IFormValidator.class.isAssignableFrom(clazz))) 059 { 060 return null; 061 } 062 063 return super.loadStringResource(clazz, key, locale, style, variation); 064 } 065 066 @Override 067 public String loadStringResource(final Component component, final String key, 068 final Locale locale, final String style, final String variation) 069 { 070 071 final String resource; 072 if (component instanceof FormComponent) 073 { 074 resource = loadStringResource((FormComponent) component, key, locale, style, variation); 075 } 076 else if (component instanceof Form) 077 { 078 resource = loadStringResource((Form) component, key, locale, style, variation); 079 } 080 else 081 { 082 resource = null; 083 } 084 085 return resource; 086 } 087 088 089 private String loadStringResource(Form<?> form, final String key, 090 final Locale locale, final String style, final String variation) 091 { 092 for (IFormValidator validator : form.getFormValidators()) 093 { 094 Class<?> scope = getScope(validator); 095 String resource = loadStringResource(scope, key, locale, style, 096 variation); 097 if (resource != null) 098 { 099 return resource; 100 } 101 } 102 return null; 103 } 104 105 private String loadStringResource(FormComponent<?> fc, final String key, 106 final Locale locale, final String style, final String variation) 107 { 108 for (IValidator<?> validator : fc.getValidators()) 109 { 110 Class<?> scope = getScope(validator); 111 String resource = loadStringResource(scope, key, locale, style, 112 variation); 113 if (resource != null) 114 { 115 return resource; 116 } 117 } 118 return null; 119 } 120 121 private Class<? extends IValidator> getScope(IValidator<?> validator) 122 { 123 Class<? extends IValidator> scope; 124 if (validator instanceof ValidatorAdapter) 125 { 126 scope = ((ValidatorAdapter) validator).getValidator().getClass(); 127 } 128 else 129 { 130 scope = validator.getClass(); 131 } 132 return scope; 133 } 134 135 private Class<? extends IFormValidator> getScope(IFormValidator formValidator) 136 { 137 Class<? extends IFormValidator> scope; 138 if (formValidator instanceof FormValidatorAdapter) 139 { 140 scope = ((FormValidatorAdapter) formValidator).getValidator().getClass(); 141 } 142 else 143 { 144 scope = formValidator.getClass(); 145 } 146 return scope; 147 } 148}