| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.osgi.dto; |
| 18 |
|
| 19 |
import java.lang.reflect.Array; |
| 20 |
import java.lang.reflect.Field; |
| 21 |
import java.lang.reflect.Modifier; |
| 22 |
import java.util.IdentityHashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Set; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public abstract class DTO { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
@Override |
| 57 |
public String toString() { |
| 58 |
return appendValue(new StringBuilder(), new IdentityHashMap<Object, String>(), "#", this).toString(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static StringBuilder appendDTO(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final DTO dto) { |
| 73 |
result.append("{"); |
| 74 |
String delim = ""; |
| 75 |
for (Field field : dto.getClass().getFields()) { |
| 76 |
if (Modifier.isStatic(field.getModifiers())) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
result.append(delim); |
| 80 |
final String name = field.getName(); |
| 81 |
appendString(result, name); |
| 82 |
result.append(":"); |
| 83 |
Object value = null; |
| 84 |
try { |
| 85 |
value = field.get(dto); |
| 86 |
} catch (IllegalAccessException e) { |
| 87 |
|
| 88 |
} |
| 89 |
appendValue(result, objectRefs, refpath + "/" + name, value); |
| 90 |
delim = ", "; |
| 91 |
} |
| 92 |
result.append("}"); |
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private static StringBuilder appendValue(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Object value) { |
| 113 |
if (value == null) { |
| 114 |
return result.append("null"); |
| 115 |
} |
| 116 |
|
| 117 |
if (value instanceof String || value instanceof Character) { |
| 118 |
return appendString(result, compress(value.toString())); |
| 119 |
} |
| 120 |
if (value instanceof Number || value instanceof Boolean) { |
| 121 |
return result.append(value.toString()); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
final String path = objectRefs.get(value); |
| 126 |
if (path != null) { |
| 127 |
result.append("{\"$ref\":"); |
| 128 |
appendString(result, path); |
| 129 |
result.append("}"); |
| 130 |
return result; |
| 131 |
} |
| 132 |
objectRefs.put(value, refpath); |
| 133 |
|
| 134 |
if (value instanceof DTO) { |
| 135 |
return appendDTO(result, objectRefs, refpath, (DTO) value); |
| 136 |
} |
| 137 |
if (value instanceof Map) { |
| 138 |
return appendMap(result, objectRefs, refpath, (Map<?, ?>) value); |
| 139 |
} |
| 140 |
if (value instanceof List || value instanceof Set) { |
| 141 |
return appendIterable(result, objectRefs, refpath, (Iterable<?>) value); |
| 142 |
} |
| 143 |
if (value.getClass().isArray()) { |
| 144 |
return appendArray(result, objectRefs, refpath, value); |
| 145 |
} |
| 146 |
return appendString(result, compress(value.toString())); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private static StringBuilder appendArray(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Object array) { |
| 161 |
result.append("["); |
| 162 |
final int length = Array.getLength(array); |
| 163 |
for (int i = 0; i < length; i++) { |
| 164 |
if (i > 0) { |
| 165 |
result.append(","); |
| 166 |
} |
| 167 |
appendValue(result, objectRefs, refpath + "/" + i, Array.get(array, i)); |
| 168 |
} |
| 169 |
result.append("]"); |
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private static StringBuilder appendIterable(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Iterable<?> iterable) { |
| 186 |
result.append("["); |
| 187 |
int i = 0; |
| 188 |
for (Object item : iterable) { |
| 189 |
if (i > 0) { |
| 190 |
result.append(","); |
| 191 |
} |
| 192 |
appendValue(result, objectRefs, refpath + "/" + i, item); |
| 193 |
i++; |
| 194 |
} |
| 195 |
result.append("]"); |
| 196 |
return result; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private static StringBuilder appendMap(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Map<?, ?> map) { |
| 211 |
result.append("{"); |
| 212 |
String delim = ""; |
| 213 |
for (Map.Entry<?, ?> entry : map.entrySet()) { |
| 214 |
result.append(delim); |
| 215 |
final String name = String.valueOf(entry.getKey()); |
| 216 |
appendString(result, name); |
| 217 |
result.append(":"); |
| 218 |
final Object value = entry.getValue(); |
| 219 |
appendValue(result, objectRefs, refpath + "/" + name, value); |
| 220 |
delim = ", "; |
| 221 |
} |
| 222 |
result.append("}"); |
| 223 |
return result; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
private static StringBuilder appendString(final StringBuilder result, final CharSequence string) { |
| 234 |
result.append("\""); |
| 235 |
int i = result.length(); |
| 236 |
result.append(string); |
| 237 |
while (i < result.length()) { |
| 238 |
char c = result.charAt(i); |
| 239 |
if ((c == '"') || (c == '\\')) { |
| 240 |
result.insert(i, '\\'); |
| 241 |
i = i + 2; |
| 242 |
continue; |
| 243 |
} |
| 244 |
if (c < 0x20) { |
| 245 |
result.insert(i + 1, Integer.toHexString(c | 0x10000)); |
| 246 |
result.replace(i, i + 2, "\\u"); |
| 247 |
i = i + 6; |
| 248 |
continue; |
| 249 |
} |
| 250 |
i++; |
| 251 |
} |
| 252 |
result.append("\""); |
| 253 |
return result; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private static CharSequence compress(final CharSequence in) { |
| 263 |
final int length = in.length(); |
| 264 |
if (length <= 21) { |
| 265 |
return in; |
| 266 |
} |
| 267 |
StringBuilder result = new StringBuilder(21); |
| 268 |
result.append(in, 0, 9); |
| 269 |
result.append("..."); |
| 270 |
result.append(in, length - 9, length); |
| 271 |
return result; |
| 272 |
} |
| 273 |
} |