1 package org.cyclopsgroup.jmxterm.io;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.commons.lang3.Validate;
5
6 import javax.management.openmbean.CompositeData;
7 import java.lang.reflect.Array;
8 import java.util.Collection;
9 import java.util.Map;
10
11
12
13
14
15
16 public class ValueOutputFormat {
17 private final int indentSize;
18
19 private final boolean showDescription;
20
21 private final boolean showQuotationMarks;
22
23
24
25
26 public ValueOutputFormat() {
27 this(2, true, true);
28 }
29
30
31
32
33
34
35 public ValueOutputFormat(int indentSize, boolean showDescription, boolean showQuotationMarks) {
36 Validate.isTrue(indentSize >= 0, "Invalid indent size value " + indentSize);
37 this.indentSize = indentSize;
38 this.showDescription = showDescription;
39 this.showQuotationMarks = showQuotationMarks;
40 }
41
42
43
44
45
46
47
48
49
50 public void printExpression(CommandOutput output, Object name, Object value, String description) {
51 printExpression(output, name, value, description, 0);
52 }
53
54
55
56
57
58
59
60
61
62
63 private void printExpression(CommandOutput output, Object name, Object value, String description,
64 int indent) {
65 output.print(StringUtils.repeat(" ", indent));
66 printValue(output, name, indent);
67 output.print(" = ");
68 printValue(output, value, indent);
69 output.print(";");
70 if (showDescription && description != null) {
71 output.print(" (" + description + ")");
72 }
73 output.println("");
74 }
75
76
77
78
79
80 public void printValue(CommandOutput output, Object value) {
81 printValue(output, value, 0);
82 }
83
84
85
86
87
88
89
90
91 private void printValue(CommandOutput output, Object value, int indent) {
92 if (value == null) {
93 output.print("null");
94 } else if (value.getClass().isArray()) {
95 int length = Array.getLength(value);
96 output.print("[ ");
97 for (int i = 0; i < length; i++) {
98 if (i != 0) {
99 output.print(", ");
100 }
101 printValue(output, Array.get(value, i), indent);
102 }
103 output.print(" ]");
104 } else if (Collection.class.isAssignableFrom(value.getClass())) {
105 boolean start = true;
106 output.print("( ");
107 for (Object obj : ((Collection<?>) value)) {
108 if (!start) {
109 output.print(", ");
110 }
111 start = false;
112 printValue(output, obj, indent);
113 }
114 output.print(" )");
115 } else if (Map.class.isAssignableFrom(value.getClass())) {
116 output.println("{ ");
117 for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
118 printExpression(output, entry.getKey(), entry.getValue(), null, indent + indentSize);
119 }
120 output.print(StringUtils.repeat(" ", indent) + " }");
121 } else if (CompositeData.class.isAssignableFrom(value.getClass())) {
122 output.println("{ ");
123 CompositeData data = (CompositeData) value;
124 for (Object key : data.getCompositeType().keySet()) {
125 Object v = data.get((String) key);
126 printExpression(output, key, v, data.getCompositeType().getDescription((String) key),
127 indent + indentSize);
128 }
129 output.print(StringUtils.repeat(" ", indent) + " }");
130 } else if (value instanceof String && showQuotationMarks) {
131 output.print("\"" + value + "\"");
132 } else {
133 output.print(value.toString());
134 }
135 }
136 }