- String printValue(Value value) throws Exception {
- Variant variant = value.value;
- Datatype dt = variant.getBinding().type();
- if(Datatypes.STRING.equals(dt)) {
- String s = (String)variant.getValue(Bindings.STRING);
- if(s.contains("\n")) {
- return "\"\"\"" + s + "\"\"\"";
- } else {
- return "\"" + s + "\"";
- }
- } else if(Datatypes.BOOLEAN.equals(dt)) {
- Boolean i = (Boolean)variant.getValue(Bindings.BOOLEAN);
- return i ? "true" : "false";
- } else if(Datatypes.INTEGER.equals(dt)) {
- Integer i = (Integer)variant.getValue(Bindings.INTEGER);
- return i.toString();
- } else if(Datatypes.LONG.equals(dt)) {
- Long i = (Long)variant.getValue(Bindings.LONG);
- return i.toString();
- } else {
- byte[] data = variant.getBinding().serializer().serialize(variant.getValue());
- m.reset();
- m.update(data, 0, data.length);
- return "\"" + new BigInteger(1, m.digest()).toString(16) + "\"";
- }
-
- }
+ DataValueRepository repo = new DataValueRepository();
+ DataValuePrinter printer = new DataValuePrinter(null, repo);
+
+ String printValue(Value value) throws Exception {
+ StringBuilder sb = new StringBuilder();
+ printer.setFormat(PrintFormat.SINGLE_LINE);
+ printer.setOutput(sb);
+
+ Variant variant = value.value;
+ printer.print(variant.getBinding(), variant.getValue());
+ String formattedOutput = sb.toString();
+ if (formattedOutput.length() > 100) {
+ // Ok, value too long, lets calculate a hash for it and store first 100 chars as comment
+ byte[] data = Bindings.getSerializerUnchecked(variant.getBinding()).serialize(variant.getValue());
+ m.reset();
+ m.update(data, 0, data.length);
+ String hash = "\"" + new BigInteger(1, m.digest()).toString(16) + "\"";
+ return hash + " // " + formattedOutput.substring(0, 100) + "..";
+ } else {
+ return formattedOutput;
+ }
+//
+// Variant variant = value.value;
+// Datatype dt = variant.getBinding().type();
+// if (Datatypes.STRING.equals(dt)) {
+// String s = (String) variant.getValue(Bindings.STRING);
+// if (s.contains("\n")) {
+// return "\"\"\"" + s + "\"\"\"";
+// } else {
+// return "\"" + s + "\"";
+// }
+// } else if (Datatypes.BOOLEAN.equals(dt)) {
+// Boolean i = (Boolean) variant.getValue(Bindings.BOOLEAN);
+// return i ? "true" : "false";
+// } else if (Datatypes.INTEGER.equals(dt)) {
+// Integer i = (Integer) variant.getValue(Bindings.INTEGER);
+// return i.toString();
+// } else if (Datatypes.LONG.equals(dt)) {
+// Long i = (Long) variant.getValue(Bindings.LONG);
+// return i.toString();
+// } else if (Datatypes.DOUBLE.equals(dt)) {
+// Double d = (Double) variant.getValue();
+// return d.toString();
+// } else if (Datatypes.FLOAT.equals(dt)) {
+// Float f = (Float) variant.getValue();
+// return f.toString();
+// } else if (Datatypes.STRING_ARRAY.equals(dt)) {
+// return Arrays.toString((String []) variant.getValue());
+// } else if (Datatypes.BOOLEAN_ARRAY.equals(dt)) {
+// return Arrays.toString((boolean []) variant.getValue());
+// } else if (Datatypes.INTEGER_ARRAY.equals(dt)) {
+// return Arrays.toString((int []) variant.getValue());
+// } else if (Datatypes.LONG_ARRAY.equals(dt)) {
+// return Arrays.toString((long []) variant.getValue());
+// } else if (Datatypes.DOUBLE_ARRAY.equals(dt)) {
+// return Arrays.toString((double []) variant.getValue());
+// } else if (Datatypes.FLOAT_ARRAY.equals(dt)) {
+// return Arrays.toString((float []) variant.getValue());
+// } else if (Datatypes.BYTE_ARRAY.equals(dt)) {
+// return Arrays.toString((byte []) variant.getValue());
+//// } else if (dt instanceof ArrayType) {
+//// return Arrays.toString((Object []) variant.getValue());
+// } else {
+// byte[] data = Bindings.getSerializerUnchecked(variant.getBinding()).serialize(variant.getValue());
+// m.reset();
+// m.update(data, 0, data.length);
+// return "\"" + new BigInteger(1, m.digest()).toString(16) + "\"";
+// }
+
+ }