| 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.krad.util.KRADConstants; | |
| 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="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( |
| 47 | KRADConstants.MESSAGE_RESOURCES); | |
| 48 | 0 | config = removeSpacesAround(propertyConfig); |
| 49 | } | |
| 50 | 0 | return new KualiPropertyMessageResources(this, config, this.returnNull); |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * Removes the spaces around the elements on a csv list of elements. | |
| 55 | * <p> | |
| 56 | * A null input will return a null output. | |
| 57 | * </p> | |
| 58 | * | |
| 59 | * @param csv a list of elements in csv format e.g. foo, bar, baz | |
| 60 | * @return a list of elements in csv format without spaces e.g. foo,bar,baz | |
| 61 | */ | |
| 62 | private String removeSpacesAround(String csv) { | |
| 63 | 0 | if (csv == null) { |
| 64 | 0 | return null; |
| 65 | } | |
| 66 | ||
| 67 | 0 | final StringBuilder result = new StringBuilder(); |
| 68 | 0 | for (final String value : csv.split(",")) { |
| 69 | 0 | if (!"".equals(value.trim())) { |
| 70 | 0 | result.append(value.trim()); |
| 71 | 0 | result.append(","); |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | //remove trailing comma | |
| 76 | 0 | int i = result.lastIndexOf(","); |
| 77 | 0 | if (i != -1) { |
| 78 | 0 | result.deleteCharAt(i); |
| 79 | } | |
| 80 | ||
| 81 | 0 | return result.toString(); |
| 82 | } | |
| 83 | ||
| 84 | } |