1 23 24 28 29 34 35 package com.sun.enterprise.management.support; 36 37 import java.util.Map ; 38 import java.util.Iterator ; 39 import java.util.HashMap ; 40 import java.util.Collections ; 41 42 import java.text.CharacterIterator ; 43 import java.text.StringCharacterIterator ; 44 45 import com.sun.appserv.management.util.jmx.AttributeNameMangler; 46 47 52 public final class ParamNameMapper implements AttributeNameMangler 53 { 54 final Map <String , String > mMappings; 55 final String mWordDelim; 56 57 60 public static final String DEFAULT_WORD_DELIM = "_"; 61 62 63 70 public 71 ParamNameMapper( final Map <String ,String > overrides ) 72 { 73 this( overrides, DEFAULT_WORD_DELIM ); 74 } 75 76 81 public 82 ParamNameMapper( 83 final Map <String ,String > overrides, 84 final String wordDelim ) 85 { 86 mMappings = new HashMap <String ,String >(); 87 if ( overrides != null ) 88 { 89 mMappings.putAll( overrides ); 90 } 91 92 mWordDelim = wordDelim; 93 } 94 95 98 public 99 ParamNameMapper( final String wordDelim ) 100 { 101 this( null, wordDelim ); 102 } 103 104 107 public 108 ParamNameMapper( ) 109 { 110 this( null, DEFAULT_WORD_DELIM ); 111 } 112 113 114 private String 115 getUpperCaseSequence( final CharacterIterator iter ) 116 { 117 final StringBuffer buf = new StringBuffer (); 118 119 char c; 120 121 while ( (c=iter.current()) != CharacterIterator.DONE ) 122 { 123 if ( Character.isLowerCase( c ) ) 124 { 125 break; 126 } 127 128 iter.next(); 129 buf.append( c ); 130 } 131 132 return( buf.toString() ); 133 } 134 private String 135 getLowerCaseSequence( final StringCharacterIterator iter ) 136 { 137 final StringBuffer buf = new StringBuffer (); 138 139 char c; 140 141 while ( (c=iter.current()) != CharacterIterator.DONE ) 142 { 143 if ( Character.isUpperCase( c ) ) 144 { 145 break; 146 } 147 148 iter.next(); 149 buf.append( c ); 150 } 151 152 return( buf.toString() ); 153 } 154 155 private String 156 newToOld( final String newName ) 157 { 158 final StringCharacterIterator iter = new StringCharacterIterator ( newName ); 159 final StringBuffer buf = new StringBuffer (); 160 161 while ( iter.current() != CharacterIterator.DONE ) 162 { 163 final String uppercase = getUpperCaseSequence( iter ); 164 if ( uppercase.length() <= 1 || 165 iter.current() == CharacterIterator.DONE ) 166 { 167 buf.append( uppercase.toLowerCase() ); 168 } 169 else 170 { 171 final int acronymLength = uppercase.length() - 1; 175 final String acronym = uppercase.substring( 0, acronymLength ); 176 final char firstOfNext = uppercase.charAt( acronymLength ); 177 buf.append( acronym.toLowerCase() ); 178 buf.append( mWordDelim ); 179 buf.append( Character.toLowerCase( firstOfNext ) ); 180 } 181 182 buf.append( getLowerCaseSequence( iter ) ); 183 if ( iter.current() != CharacterIterator.DONE ) 184 { 185 buf.append( mWordDelim ); 186 } 187 } 188 189 return( buf.toString() ); 190 } 191 192 193 194 public String 195 toString() 196 { 197 final StringBuffer buf = new StringBuffer (); 198 final Iterator iter = mMappings.keySet().iterator(); 199 200 while ( iter.hasNext() ) 201 { 202 final String newName = (String )iter.next(); 203 204 buf.append( newName + "=" + mMappings.get( newName ) + "\n"); 205 } 206 return( buf.toString() ); 207 } 208 209 210 215 public String 216 mangleAttributeName( final String newName ) 217 { 218 String result = (String )mMappings.get( newName ); 219 220 if ( result == null ) 221 { 222 result = newToOld( newName ); 224 225 mMappings.put( newName, result ); 227 } 228 229 return( result ); 230 } 231 } 232 233 234 235 236 237 238 239 240 241 242 243 | Popular Tags |