KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > ParamNameMapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/ParamNameMapper.java,v 1.3 2006/03/09 20:30:48 llc Exp $
31  * $Revision: 1.3 $
32  * $Date: 2006/03/09 20:30:48 $
33  */

34
35 package com.sun.enterprise.management.support;
36
37 import java.util.Map JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Collections JavaDoc;
41
42 import java.text.CharacterIterator JavaDoc;
43 import java.text.StringCharacterIterator JavaDoc;
44
45 import com.sun.appserv.management.util.jmx.AttributeNameMangler;
46
47 /**
48     Translates parameters from the new Attribute names to the old
49     ones. When instantiated, optional override mappings may be supplied;
50     these will be used in lieu of the algorithmic translation.
51  */

52 public final class ParamNameMapper implements AttributeNameMangler
53 {
54     final Map JavaDoc<String JavaDoc, String JavaDoc> mMappings;
55     final String JavaDoc mWordDelim;
56     
57     /**
58         The default delimiter used between words.
59      */

60     public static final String JavaDoc DEFAULT_WORD_DELIM = "_";
61     
62     
63     /**
64         Create a new instance.
65         
66         The 'overrides' map should map a new name to an old one.
67         
68         @param overrides any special mappings that override the algorithmic mapping (may be null)
69      */

70         public
71     ParamNameMapper( final Map JavaDoc<String JavaDoc,String JavaDoc> overrides )
72     {
73         this( overrides, DEFAULT_WORD_DELIM );
74     }
75     
76     /**
77         Create a new instance
78         
79         @param overrides any special mappings that override the algorithmic mapping (may be null)
80      */

81         public
82     ParamNameMapper(
83         final Map JavaDoc<String JavaDoc,String JavaDoc> overrides,
84         final String JavaDoc wordDelim )
85     {
86         mMappings = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
87         if ( overrides != null )
88         {
89             mMappings.putAll( overrides );
90         }
91         
92         mWordDelim = wordDelim;
93     }
94     
95     /**
96         Create a new instance with no overrides.
97      */

98         public
99     ParamNameMapper( final String JavaDoc wordDelim )
100     {
101         this( null, wordDelim );
102     }
103     
104     /**
105         Create a new instance with no overrides.
106      */

107         public
108     ParamNameMapper( )
109     {
110         this( null, DEFAULT_WORD_DELIM );
111     }
112
113         
114         private String JavaDoc
115     getUpperCaseSequence( final CharacterIterator JavaDoc iter )
116     {
117         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc
135     getLowerCaseSequence( final StringCharacterIterator JavaDoc iter )
136     {
137         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc
156     newToOld( final String JavaDoc newName )
157     {
158         final StringCharacterIterator JavaDoc iter = new StringCharacterIterator JavaDoc( newName );
159         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
160
161         while ( iter.current() != CharacterIterator.DONE )
162         {
163             final String JavaDoc uppercase = getUpperCaseSequence( iter );
164             if ( uppercase.length() <= 1 ||
165                     iter.current() == CharacterIterator.DONE )
166             {
167                 buf.append( uppercase.toLowerCase() );
168             }
169             else
170             {
171                 // a run of an uppercase acronym of length N. The first N-1 characters
172
// are the acronym and the last character is actually the first letter of
173
// the next word.
174
final int acronymLength = uppercase.length() - 1;
175                 final String JavaDoc 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 JavaDoc
195     toString()
196     {
197         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
198         final Iterator JavaDoc iter = mMappings.keySet().iterator();
199         
200         while ( iter.hasNext() )
201         {
202             final String JavaDoc newName = (String JavaDoc)iter.next();
203             
204             buf.append( newName + "=" + mMappings.get( newName ) + "\n");
205         }
206         return( buf.toString() );
207     }
208
209         
210     /**
211         Convert an Attribute name from the new one to the old one.
212         
213         Any special casing should override this.
214      */

215         public String JavaDoc
216     mangleAttributeName( final String JavaDoc newName )
217     {
218         String JavaDoc result = (String JavaDoc)mMappings.get( newName );
219         
220         if ( result == null )
221         {
222             // use algorithmic conversion
223
result = newToOld( newName );
224             
225             // cache for later
226
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