KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 org.springframework.util.Assert;
20 import org.springframework.util.ClassUtils;
21 import org.springframework.util.ObjectUtils;
22
23 /**
24  * Spring's default <code>toString()</code> styler.
25  *
26  * <p>This class is used by {@link ToStringCreator} to style <code>toString()</code>
27  * output in a consistent manner according to Spring conventions.
28  *
29  * @author Keith Donald
30  * @author Juergen Hoeller
31  * @since 1.2.2
32  */

33 public class DefaultToStringStyler implements ToStringStyler {
34
35     private final ValueStyler valueStyler;
36
37
38     /**
39      * Create a new DefaultToStringStyler.
40      * @param valueStyler the ValueStyler to use
41      */

42     public DefaultToStringStyler(ValueStyler valueStyler) {
43         Assert.notNull(valueStyler, "ValueStyler must not be null");
44         this.valueStyler = valueStyler;
45     }
46
47     /**
48      * Return the ValueStyler used by this ToStringStyler.
49      */

50     protected final ValueStyler getValueStyler() {
51         return this.valueStyler;
52     }
53
54
55     public void styleStart(StringBuffer JavaDoc buffer, Object JavaDoc obj) {
56         if (!obj.getClass().isArray()) {
57             buffer.append('[').append(ClassUtils.getShortName(obj.getClass()));
58             styleIdentityHashCode(buffer, obj);
59         }
60         else {
61             buffer.append('[');
62             styleIdentityHashCode(buffer, obj);
63             buffer.append(' ');
64             styleValue(buffer, obj);
65         }
66     }
67
68     private void styleIdentityHashCode(StringBuffer JavaDoc buffer, Object JavaDoc obj) {
69         buffer.append('@');
70         buffer.append(ObjectUtils.getIdentityHexString(obj));
71     }
72
73     public void styleEnd(StringBuffer JavaDoc buffer, Object JavaDoc o) {
74         buffer.append(']');
75     }
76
77     public void styleField(StringBuffer JavaDoc buffer, String JavaDoc fieldName, Object JavaDoc value) {
78         styleFieldStart(buffer, fieldName);
79         styleValue(buffer, value);
80         styleFieldEnd(buffer, fieldName);
81     }
82
83     protected void styleFieldStart(StringBuffer JavaDoc buffer, String JavaDoc fieldName) {
84         buffer.append(' ').append(fieldName).append(" = ");
85     }
86
87     protected void styleFieldEnd(StringBuffer JavaDoc buffer, String JavaDoc fieldName) {
88     }
89
90     public void styleValue(StringBuffer JavaDoc buffer, Object JavaDoc value) {
91         buffer.append(this.valueStyler.style(value));
92     }
93
94     public void styleFieldSeparator(StringBuffer JavaDoc buffer) {
95         buffer.append(',');
96     }
97
98 }
99
Popular Tags