1 16 package org.apache.commons.betwixt.strategy; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.apache.commons.betwixt.ElementDescriptor; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 34 public class DefaultPluralStemmer implements PluralStemmer { 35 36 37 protected static Log log = LogFactory.getLog( DefaultPluralStemmer.class ); 38 39 57 public ElementDescriptor findPluralDescriptor( String propertyName, Map map ) { 58 int foundKeyCount = 0; 59 String keyFound = null; 60 ElementDescriptor answer = (ElementDescriptor) map.get( propertyName + "s" ); 61 62 if ( answer == null && !propertyName.endsWith( "y" )) { 63 answer = (ElementDescriptor) map.get( propertyName + "es" ); 64 } 65 66 if ( answer == null ) { 67 int length = propertyName.length(); 68 if ( propertyName.endsWith( "y" ) && length > 1 ) { 69 String key = propertyName.substring(0, length - 1) + "ies"; 70 answer = (ElementDescriptor) map.get( key ); 71 } 72 73 if ( answer == null ) { 74 for ( Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { 76 String key = (String ) iter.next(); 77 if ( key.startsWith( propertyName ) ) { 78 if (answer == null) { 79 answer = (ElementDescriptor) map.get(key); 80 if (key.equals(propertyName)) { 81 break; 83 } 84 foundKeyCount++; 85 keyFound = key; 86 87 } else { 88 if (keyFound.length() > key.length()) { 90 answer = (ElementDescriptor) map.get(key); 91 keyFound = key; 92 } 93 foundKeyCount++; 94 95 } 96 } 97 } 98 } 99 } 100 if (foundKeyCount > 1) { 101 log.warn("More than one type matches, using closest match "+answer.getQualifiedName()); 102 } 103 return answer; 104 105 } 106 } 107 | Popular Tags |