View Javadoc
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   * A utility to print out object values in particular format.
13   * 
14   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
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     * Default constructor with indentiation = 2, showDescription and showQuotationMarks are both true
25     */
26    public ValueOutputFormat() {
27      this(2, true, true);
28    }
29  
30    /**
31     * @param indentSize Size of indentation
32     * @param showDescription True if value description is printed
33     * @param showQuotationMarks True if quotation mark is printed
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     * Print out equal expression of an variable with description
44     * 
45     * @param output Output to print to
46     * @param name Name of variable
47     * @param value Value of variable
48     * @param description Description of variable
49     */
50    public void printExpression(CommandOutput output, Object name, Object value, String description) {
51      printExpression(output, name, value, description, 0);
52    }
53  
54    /**
55     * Print out equal expression of an variable with description
56     * 
57     * @param output Output to print to
58     * @param name Name of variable
59     * @param value Value of variable
60     * @param description Description of variable
61     * @param indent Starting indent position
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     * @param output Output writer where value is printed to
78     * @param value Value to print
79     */
80    public void printValue(CommandOutput output, Object value) {
81      printValue(output, value, 0);
82    }
83  
84    /**
85     * Print out expression of given value considering various possible types of value
86     * 
87     * @param output Output writer where value is printed
88     * @param value Object value which can be anything
89     * @param indent Starting indentation length
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 }