KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > style > DefaultValueStyler


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.style;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.springframework.core.ReflectiveVisitorHelper;
27 import org.springframework.util.ClassUtils;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31  * Converts objects to string form, generally for debugging purposes,
32  * using Spring's <code>toString</code> styling conventions.
33  *
34  * <p>Uses the reflective visitor pattern underneath the hood to nicely
35  * encapsulate styling algorithms for each type of styled object.
36  *
37  * @author Keith Donald
38  * @author Juergen Hoeller
39  * @since 1.2.2
40  */

41 public class DefaultValueStyler implements ValueStyler {
42
43     private static final String JavaDoc EMPTY = "[empty]";
44     private static final String JavaDoc NULL = "[null]";
45     private static final String JavaDoc COLLECTION = "collection";
46     private static final String JavaDoc SET = "set";
47     private static final String JavaDoc LIST = "list";
48     private static final String JavaDoc MAP = "map";
49     private static final String JavaDoc ARRAY = "array";
50
51
52     private final ReflectiveVisitorHelper reflectiveVisitorHelper = new ReflectiveVisitorHelper();
53
54
55     public String JavaDoc style(Object JavaDoc value) {
56         return (String JavaDoc) reflectiveVisitorHelper.invokeVisit(this, value);
57     }
58
59     String JavaDoc visit(String JavaDoc value) {
60         return ('\'' + value + '\'');
61     }
62
63     String JavaDoc visit(Number JavaDoc value) {
64         return String.valueOf(value);
65     }
66
67     String JavaDoc visit(Class JavaDoc clazz) {
68         return ClassUtils.getShortName(clazz);
69     }
70
71     String JavaDoc visit(Method JavaDoc method) {
72         return method.getName() + "@" + ClassUtils.getShortName(method.getDeclaringClass());
73     }
74
75     String JavaDoc visit(Map JavaDoc value) {
76         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value.size() * 8 + 16);
77         buffer.append(MAP + "[");
78         for (Iterator JavaDoc i = value.entrySet().iterator(); i.hasNext();) {
79             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
80             buffer.append(style(entry));
81             if (i.hasNext()) {
82                 buffer.append(',').append(' ');
83             }
84         }
85         if (value.isEmpty()) {
86             buffer.append(EMPTY);
87         }
88         buffer.append("]");
89         return buffer.toString();
90     }
91
92     String JavaDoc visit(Map.Entry JavaDoc value) {
93         return style(value.getKey()) + " -> " + style(value.getValue());
94     }
95
96     String JavaDoc visit(Collection JavaDoc value) {
97         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value.size() * 8 + 16);
98         buffer.append(getCollectionTypeString(value) + "[");
99         for (Iterator JavaDoc i = value.iterator(); i.hasNext();) {
100             buffer.append(style(i.next()));
101             if (i.hasNext()) {
102                 buffer.append(',').append(' ');
103             }
104         }
105         if (value.isEmpty()) {
106             buffer.append(EMPTY);
107         }
108         buffer.append("]");
109         return buffer.toString();
110     }
111
112     private String JavaDoc getCollectionTypeString(Collection JavaDoc value) {
113         if (value instanceof List JavaDoc) {
114             return LIST;
115         }
116         else if (value instanceof Set JavaDoc) {
117             return SET;
118         }
119         else {
120             return COLLECTION;
121         }
122     }
123
124     String JavaDoc visit(Object JavaDoc value) {
125         if (value.getClass().isArray()) {
126             return styleArray(ObjectUtils.toObjectArray(value));
127         }
128         else {
129             return String.valueOf(value);
130         }
131     }
132
133     String JavaDoc visitNull() {
134         return NULL;
135     }
136
137     private String JavaDoc styleArray(Object JavaDoc[] array) {
138         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(array.length * 8 + 16);
139         buffer.append(ARRAY + "<" + ClassUtils.getShortName(array.getClass().getComponentType()) + ">[");
140         for (int i = 0; i < array.length - 1; i++) {
141             buffer.append(style(array[i]));
142             buffer.append(',').append(' ');
143         }
144         if (array.length > 0) {
145             buffer.append(style(array[array.length - 1]));
146         }
147         else {
148             buffer.append(EMPTY);
149         }
150         buffer.append("]");
151         return buffer.toString();
152     }
153
154 }
155
Popular Tags