1 16 17 package org.springframework.context.support; 18 19 import java.util.Locale ; 20 21 import org.springframework.context.HierarchicalMessageSource; 22 import org.springframework.context.MessageSource; 23 import org.springframework.context.MessageSourceResolvable; 24 import org.springframework.context.NoSuchMessageException; 25 26 37 public class DelegatingMessageSource implements HierarchicalMessageSource { 38 39 private MessageSource parentMessageSource; 40 41 42 public void setParentMessageSource(MessageSource parent) { 43 this.parentMessageSource = parent; 44 } 45 46 public MessageSource getParentMessageSource() { 47 return parentMessageSource; 48 } 49 50 51 public String getMessage(String code, Object [] args, String defaultMessage, Locale locale) { 52 if (this.parentMessageSource != null) { 53 return this.parentMessageSource.getMessage(code, args, defaultMessage, locale); 54 } 55 else { 56 return defaultMessage; 57 } 58 } 59 60 public String getMessage(String code, Object [] args, Locale locale) throws NoSuchMessageException { 61 if (this.parentMessageSource != null) { 62 return this.parentMessageSource.getMessage(code, args, locale); 63 } 64 else { 65 throw new NoSuchMessageException(code, locale); 66 } 67 } 68 69 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { 70 if (this.parentMessageSource != null) { 71 return this.parentMessageSource.getMessage(resolvable, locale); 72 } 73 else { 74 if (resolvable.getDefaultMessage() != null) { 75 return resolvable.getDefaultMessage(); 76 } 77 String [] codes = resolvable.getCodes(); 78 String code = (codes != null && codes.length > 0 ? codes[0] : null); 79 throw new NoSuchMessageException(code, locale); 80 } 81 } 82 83 } 84 | Popular Tags |