1 /** 2 * Copyright (C) 2005-2009 Alfresco Software Limited. 3 * 4 * This file is part of the Spring Surf Extension project. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.springframework.extensions.surf.util; 20 21 import org.springframework.extensions.surf.exception.PlatformRuntimeException; 22 23 /** 24 * Helper class for for use when checking properties. This class uses 25 * I18N for its messages. 26 * 27 * @author Derek Hulley 28 */ 29 public class PropertyCheck 30 { 31 public static final String ERR_PROPERTY_NOT_SET = "system.err.property_not_set"; 32 33 /** 34 * Checks that the property with the given name is not null. 35 * 36 * @param target the object on which the property must have been set 37 * @param propertyName the name of the property 38 * @param value of the property value 39 */ 40 public static void mandatory(Object target, String propertyName, Object value) 41 { 42 if (value == null) 43 { 44 throw new PlatformRuntimeException( 45 ERR_PROPERTY_NOT_SET, 46 new Object[] {propertyName, target, target.getClass()}); 47 } 48 } 49 50 /** 51 * Checks that the given string is not: 52 * <ul> 53 * <li>null</li> 54 * <li>empty</li> 55 * <li>a placeholder of form '${...}'</li> 56 * </ul> 57 * 58 * @param value the value to check 59 * @return <tt>true</tt> if the checks all pass 60 */ 61 public static boolean isValidPropertyString(String value) 62 { 63 if (value == null || value.length() == 0) 64 { 65 return false; 66 } 67 if (value.startsWith("${") && value.endsWith("}")) 68 { 69 return false; 70 } 71 else 72 { 73 return true; 74 } 75 } 76 77 /** 78 * Dig out the property name from a placeholder-style property of form 79 * <b>${prop.name}</b>, which will yield <b>prop.name</b>. If the placeholders 80 * are not there, the value is returned directly. <tt>null</tt> values are 81 * not allowed, but empty strings are. 82 * 83 * @param value The property with or without property placeholders 84 * @return Returns the core property without the property placeholders 85 * <b>${</b> and <b>}</b>. 86 * @throws IllegalArgumentException if the value is <tt>null</tt> 87 */ 88 public static String getPropertyName(String value) 89 { 90 if (value == null) 91 { 92 throw new IllegalArgumentException("'value' is a required argument."); 93 } 94 if (!value.startsWith("${")) 95 { 96 return value; 97 } 98 if (!value.endsWith("}")) 99 { 100 return value; 101 } 102 int strLen = value.length(); 103 return value.substring(2, strLen - 1); 104 } 105 }