1 16 17 package org.springframework.core.style; 18 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.springframework.core.ReflectiveVisitorHelper; 27 import org.springframework.util.ClassUtils; 28 import org.springframework.util.ObjectUtils; 29 30 41 public class DefaultValueStyler implements ValueStyler { 42 43 private static final String EMPTY = "[empty]"; 44 private static final String NULL = "[null]"; 45 private static final String COLLECTION = "collection"; 46 private static final String SET = "set"; 47 private static final String LIST = "list"; 48 private static final String MAP = "map"; 49 private static final String ARRAY = "array"; 50 51 52 private final ReflectiveVisitorHelper reflectiveVisitorHelper = new ReflectiveVisitorHelper(); 53 54 55 public String style(Object value) { 56 return (String ) reflectiveVisitorHelper.invokeVisit(this, value); 57 } 58 59 String visit(String value) { 60 return ('\'' + value + '\''); 61 } 62 63 String visit(Number value) { 64 return String.valueOf(value); 65 } 66 67 String visit(Class clazz) { 68 return ClassUtils.getShortName(clazz); 69 } 70 71 String visit(Method method) { 72 return method.getName() + "@" + ClassUtils.getShortName(method.getDeclaringClass()); 73 } 74 75 String visit(Map value) { 76 StringBuffer buffer = new StringBuffer (value.size() * 8 + 16); 77 buffer.append(MAP + "["); 78 for (Iterator i = value.entrySet().iterator(); i.hasNext();) { 79 Map.Entry entry = (Map.Entry )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 visit(Map.Entry value) { 93 return style(value.getKey()) + " -> " + style(value.getValue()); 94 } 95 96 String visit(Collection value) { 97 StringBuffer buffer = new StringBuffer (value.size() * 8 + 16); 98 buffer.append(getCollectionTypeString(value) + "["); 99 for (Iterator 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 getCollectionTypeString(Collection value) { 113 if (value instanceof List ) { 114 return LIST; 115 } 116 else if (value instanceof Set ) { 117 return SET; 118 } 119 else { 120 return COLLECTION; 121 } 122 } 123 124 String visit(Object 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 visitNull() { 134 return NULL; 135 } 136 137 private String styleArray(Object [] array) { 138 StringBuffer buffer = new StringBuffer (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 |