KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > AttributeNameManglerImpl


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 package com.sun.appserv.management.util.jmx;
24
25 import java.util.Set JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import com.sun.appserv.management.util.misc.GSetUtil;
31
32 /**
33     Implements the mapping by removing illegal characters and
34     attempting to camel-case a following alphabetic character.
35     Optionally capitalizes the first letter
36  */

37 public class AttributeNameManglerImpl implements AttributeNameMangler
38 {
39     private final Map JavaDoc<String JavaDoc,String JavaDoc> mOverrides;
40     private final boolean mCapitalizeFirstLetter;
41     
42     /**
43         @param capitalizeFirstLetter true if first letter should be capitalized
44         @param overrides explicit mappings from the original to a result
45      */

46         public
47     AttributeNameManglerImpl(
48         final boolean capitalizeFirstLetter,
49         final Map JavaDoc<String JavaDoc,String JavaDoc> overrides )
50     {
51         mCapitalizeFirstLetter = capitalizeFirstLetter;
52         mOverrides = overrides;
53     }
54     
55     
56         private String JavaDoc
57     convertOverride( final String JavaDoc name )
58     {
59         String JavaDoc result = name;
60         
61         if ( mOverrides != null )
62         {
63             if ( mOverrides.containsKey( name ) )
64             {
65                 result = (String JavaDoc)mOverrides.get( name );
66             }
67             /*
68             else
69             {
70                 // have to do case-insensitive search
71                 final Iterator iter = mOverrides.keySet().iterator();
72                 while ( iter.hasNext() )
73                 {
74                     final String override = (String)iter.next();
75                     
76                     if ( override.equalsIgnoreCase( name ) )
77                     {
78                         result = (String)mOverrides.get( name );
79                         break;
80                     }
81                 }
82             }
83             */

84         }
85         
86         return( result );
87     }
88
89         public String JavaDoc
90     mangleAttributeName( final String JavaDoc attributeName )
91     {
92         String JavaDoc result = mangleIt( attributeName );
93         
94         return( result );
95     }
96     
97         private String JavaDoc
98     toUpperCase( final char c )
99     {
100         return( ("" + c).toUpperCase() );
101     }
102     
103     /**
104         Note that because we expect a "get" or "set" to be placed in front
105         of the Attribute name, the first character of the Attribute name
106         need only be a valid Java identifier part; it need not be a valid
107         first character.
108      */

109         private String JavaDoc
110     mangleIt( final String JavaDoc attributeName )
111     {
112         final char[] chars = attributeName.toCharArray();
113         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
114         
115         // capitalize the first letter
116
final char firstChar = chars[ 0 ];
117         if ( Character.isJavaIdentifierPart( firstChar ) )
118         {
119             buf.append( mCapitalizeFirstLetter ? toUpperCase( firstChar ) : "" + firstChar );
120         }
121         // else { omit it }
122

123         for( int i = 1; i < chars.length; ++i )
124         {
125             final char c = chars[ i ];
126             
127             if ( ! Character.isJavaIdentifierPart( c ) )
128             {
129                 ++i; // skip it
130

131                 final char nextChar = (i < chars.length) ? chars[ i ] : 0;
132                     
133                 if ( nextChar >= 'a' && nextChar <= 'z' )
134                 {
135                     buf.append( toUpperCase( chars[ i ] ) );
136                 }
137                 else if ( nextChar >= 'A' && nextChar <= 'Z' )
138                 {
139                     buf.append( "" + chars[ i ] );
140                 }
141                 else
142                 {
143                     // emit nothing and go onto next character
144
--i;
145                 }
146             }
147             else
148             {
149                 buf.append( c );
150             }
151         }
152         
153         final String JavaDoc before = buf.toString();
154         final String JavaDoc result = convertOverride( before );
155         
156         return( result );
157     }
158 }
159
Popular Tags