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