KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > stringifier > SmartStringifier


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.stringifier;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.lang.reflect.Array JavaDoc;
29
30
31 import com.sun.appserv.management.util.misc.ClassUtil;
32 import com.sun.appserv.management.util.misc.ArrayConversion;
33
34
35 /**
36     Stringifies an Object in the "best" possible way, using the
37     StringifierRegistry.DEFAULT registry and/or internal logic.
38  */

39 public final class SmartStringifier implements Stringifier
40 {
41     public static final SmartStringifier DEFAULT = new SmartStringifier( "," );
42     private final String JavaDoc mMultiDelim;
43     private final boolean mEncloseArrays;
44     protected StringifierRegistry mRegistry;
45     
46         public
47     SmartStringifier()
48     {
49         this( "," );
50     }
51     
52         public
53     SmartStringifier( String JavaDoc multiDelim )
54     {
55         this ( multiDelim, true );
56     }
57     
58         public
59     SmartStringifier( String JavaDoc multiDelim, boolean encloseArrays )
60     {
61         this ( StringifierRegistryImpl.DEFAULT, multiDelim, encloseArrays );
62     }
63     
64         public
65     SmartStringifier( StringifierRegistry registry, String JavaDoc multiDelim, boolean encloseArrays)
66     {
67         mMultiDelim = multiDelim;
68         mEncloseArrays = encloseArrays;
69         mRegistry = registry;
70     }
71     
72         public void
73     setRegistry( StringifierRegistry registry )
74     {
75         mRegistry = registry;
76     }
77     
78     
79     private final static Class JavaDoc [] STRINGIFIER_REGISTRY_LOOKUPS =
80     {
81         Iterator JavaDoc.class,
82         Collection JavaDoc.class,
83         HashMap JavaDoc.class
84     };
85     
86         private Stringifier
87     getStringifier( final Object JavaDoc target )
88     {
89         if ( target == null )
90             return( null );
91             
92         final Class JavaDoc<?> targetClass = target.getClass();
93         
94         Stringifier stringifier = mRegistry.lookup( targetClass );
95         
96         if ( target instanceof javax.management.ObjectName JavaDoc )
97         {
98             assert( stringifier != null );
99         }
100         
101         if ( stringifier == null )
102         {
103             // exact match failed...look for match in defined order
104
final int numLookups = STRINGIFIER_REGISTRY_LOOKUPS.length;
105             for( int i = 0; i < numLookups; ++i )
106             {
107                 final Class JavaDoc<?> theClass = STRINGIFIER_REGISTRY_LOOKUPS[ i ];
108                 
109                 stringifier = mRegistry.lookup( theClass );
110                 if ( stringifier != null && theClass.isAssignableFrom( target.getClass() ) )
111                 {
112                     break;
113                 }
114             }
115         }
116         
117         if ( stringifier == null )
118         {
119             // see if there is a Stringifier for any superclass;
120
Class JavaDoc tempClass = targetClass;
121             while ( tempClass != Object JavaDoc.class )
122             {
123                 stringifier = mRegistry.lookup( tempClass );
124                 if ( stringifier != null )
125                 {
126                     break;
127                 }
128                 
129                 tempClass = tempClass.getSuperclass();
130             }
131             
132         }
133         
134         if ( stringifier == null )
135         {
136             final Class JavaDoc[] interfaces = targetClass.getInterfaces();
137             if ( interfaces.length != 0 )
138             {
139                 stringifier = new InterfacesStringifier( interfaces );
140             }
141         }
142     
143         return( stringifier );
144     }
145     
146         private String JavaDoc
147     smartStringify( Object JavaDoc target )
148     {
149         String JavaDoc result = null;
150         
151         if ( ClassUtil.objectIsArray( target ) )
152         {
153             Object JavaDoc [] theArray = null;
154             
155             final Class JavaDoc elementClass =
156                 ClassUtil.getArrayElementClass( target.getClass() );
157                 
158             if ( ClassUtil.IsPrimitiveClass( elementClass ) )
159             {
160                 theArray = ArrayConversion.toAppropriateType( target );
161             }
162             else
163             {
164                 theArray = (Object JavaDoc [])target;
165             }
166             
167             
168             result = ArrayStringifier.stringify( theArray, mMultiDelim, this);
169             if ( mEncloseArrays )
170             {
171                 result = "{" + result + "}";
172             }
173         }
174         else
175         {
176             Stringifier stringifier = getStringifier( target );
177             
178             if ( stringifier != null && stringifier.getClass() == this.getClass() )
179             {
180                 // avoid recursive call to self
181
stringifier = null;
182             }
183             
184             if ( stringifier != null )
185             {
186                 result = stringifier.stringify( target );
187             }
188         }
189         
190         if ( result == null )
191         {
192             result = target.toString();
193         }
194
195         return( result );
196     }
197
198         public static String JavaDoc
199     toString( Object JavaDoc target )
200     {
201         return( DEFAULT.stringify( target ) );
202     }
203     
204         public String JavaDoc
205     stringify( Object JavaDoc target )
206     {
207         if ( target == null )
208         {
209             return( "<null>" );
210         }
211         
212         return( smartStringify( target ) );
213     }
214 }
215
216
Popular Tags