1 16 17 package org.springframework.beans; 18 19 27 public abstract class PropertyAccessorUtils { 28 29 35 public static String getPropertyName(String propertyPath) { 36 int separatorIndex = propertyPath.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); 37 return (separatorIndex != -1 ? propertyPath.substring(0, separatorIndex) : propertyPath); 38 } 39 40 46 public static int getFirstNestedPropertySeparatorIndex(String propertyPath) { 47 return getNestedPropertySeparatorIndex(propertyPath, false); 48 } 49 50 56 public static int getLastNestedPropertySeparatorIndex(String propertyPath) { 57 return getNestedPropertySeparatorIndex(propertyPath, true); 58 } 59 60 67 private static int getNestedPropertySeparatorIndex(String propertyPath, boolean last) { 68 boolean inKey = false; 69 final int length = propertyPath.length(); 70 int i = (last ? length - 1 : 0); 71 while (last ? i >= 0 : i < length) { 72 switch (propertyPath.charAt(i)) { 73 case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR: 74 case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR: 75 inKey = !inKey; 76 break; 77 case PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR: 78 if (!inKey) { 79 return i; 80 } 81 } 82 if (last) 83 i--; 84 else 85 i++; 86 } 87 return -1; 88 } 89 90 97 public static boolean matchesProperty(String registeredPath, String propertyPath) { 98 if (!registeredPath.startsWith(propertyPath)) { 99 return false; 100 } 101 if (registeredPath.length() == propertyPath.length()) { 102 return true; 103 } 104 if (registeredPath.charAt(propertyPath.length()) != PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) { 105 return false; 106 } 107 return (registeredPath.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR, propertyPath.length() + 1) == 108 registeredPath.length() - 1); 109 } 110 111 119 public static String canonicalPropertyName(String propertyName) { 120 if (propertyName == null) { 121 return ""; 122 } 123 124 128 StringBuffer buf = new StringBuffer (propertyName); 129 int searchIndex = 0; 130 while (searchIndex != -1) { 131 int keyStart = buf.toString().indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX, searchIndex); 132 searchIndex = -1; 133 if (keyStart != -1) { 134 int keyEnd = buf.toString().indexOf( 135 PropertyAccessor.PROPERTY_KEY_SUFFIX, keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length()); 136 if (keyEnd != -1) { 137 String key = buf.substring(keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length(), keyEnd); 138 if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) { 139 buf.delete(keyStart + 1, keyStart + 2); 140 buf.delete(keyEnd - 2, keyEnd - 1); 141 keyEnd = keyEnd - 2; 142 } 143 searchIndex = keyEnd + PropertyAccessor.PROPERTY_KEY_SUFFIX.length(); 144 } 145 } 146 } 147 return buf.toString(); 148 } 149 150 157 public static String [] canonicalPropertyNames(String [] propertyNames) { 158 if (propertyNames == null) { 159 return null; 160 } 161 String [] result = new String [propertyNames.length]; 162 for (int i = 0; i < propertyNames.length; i++) { 163 result[i] = canonicalPropertyName(propertyNames[i]); 164 } 165 return result; 166 } 167 168 } 169 | Popular Tags |