View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.opensource.org/licenses/ecl2.php
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
10   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
11   * language governing permissions and limitations under the License.
12   */
13  package org.kuali.rice.kns.web.struts.action;
14  
15  import org.apache.commons.lang.StringUtils;
16  import org.apache.struts.util.MessageResources;
17  import org.apache.struts.util.PropertyMessageResourcesFactory;
18  import org.kuali.rice.core.config.ConfigContext;
19  import org.kuali.rice.kns.util.KNSConstants;
20  import org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResources;
21  
22  /**
23   * A custom MessageResourceFactory that delegates to the KualiConfigurationService's pre-loaded properties. 
24   * 
25   * This factory can be used in struts-config.xml files by specifying a factory attribute in the <message-resources/> tag.  
26   * Example: 
27   *   <message-resources 
28   *       factory="org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResourcesFactory" 
29   *       parameter="SampleApplicationResources" />
30   */
31  public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
32  
33      private static final long serialVersionUID = 9045578011738154255L;
34  
35      /**
36       * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the defalt message set.
37       * 
38       * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
39       */
40      @Override
41      public MessageResources createResources(String config) {
42          if (StringUtils.isBlank(config)) {
43              final String propertyConfig = (String)ConfigContext.getCurrentContextConfig().getProperties().get(KNSConstants.MESSAGE_RESOURCES);
44              config = removeSpacesAround(propertyConfig);
45          }
46          return new KualiPropertyMessageResources(this, config, this.returnNull);
47      }
48      
49      /**
50       * Removes the spaces around the elements on a csv list of elements.
51       * <p>
52       * A null input will return a null output.
53       * </p>
54       * 
55       * @param csv a list of elements in csv format e.g. foo, bar, baz
56       * @return a list of elements in csv format without spaces e.g. foo,bar,baz
57       */
58      private String removeSpacesAround(String csv) {
59      	if (csv == null) {
60      		return null;
61      	}
62      	
63      	final StringBuilder result = new StringBuilder();
64  		for (final String value : csv.split(",")) {
65  			if (!"".equals(value.trim())) {
66  				result.append(value.trim());
67  				result.append(",");
68  			}
69  		}
70  		
71  		//remove trailing comma
72  		int i = result.lastIndexOf(",");
73  		if (i != -1) {
74  			result.deleteCharAt(i);
75  		}
76  		
77  		return result.toString();
78      }
79      
80  }