KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > 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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/util/stringifier/SmartStringifier.java,v 1.3 2005/12/25 03:46:08 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:46:08 $
28  */

29  
30 package com.sun.cli.util.stringifier;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.lang.reflect.Array JavaDoc;
36
37 import javax.management.*;
38
39 import com.sun.cli.util.*;
40
41
42 /*
43     Stringifies an Object in the "best" possible way, using the
44     StringifierRegistry.DEFAULT registry and/or internal logic.
45  */

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