Added indentation support to the human readable serialisation
This commit is contained in:
parent
a05cbe7375
commit
47e7822177
|
@ -57,15 +57,25 @@ public class BdfArray implements IBdfType, Iterable<BdfObject>
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String serializeHumanReadable()
|
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||||
{
|
{
|
||||||
|
if(elements.size() == 0) {
|
||||||
|
return "[]";
|
||||||
|
}
|
||||||
|
|
||||||
String data = "[";
|
String data = "[";
|
||||||
|
|
||||||
for(int i=0;i<elements.size();i++)
|
for(int i=0;i<elements.size();i++)
|
||||||
{
|
{
|
||||||
BdfObject o = elements.get(i);
|
BdfObject o = elements.get(i);
|
||||||
|
|
||||||
data += o.serializeHumanReadable();
|
data += indent.breaker;
|
||||||
|
|
||||||
|
for(int n=0;n<=it;n++) {
|
||||||
|
data += indent.indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
data += o.serializeHumanReadable(indent, it + 1);
|
||||||
|
|
||||||
if(elements.size() > i+1)
|
if(elements.size() > i+1)
|
||||||
{
|
{
|
||||||
|
@ -73,6 +83,12 @@ public class BdfArray implements IBdfType, Iterable<BdfObject>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data += indent.breaker;
|
||||||
|
|
||||||
|
for(int n=0;n<it;n++) {
|
||||||
|
data += indent.indent;
|
||||||
|
}
|
||||||
|
|
||||||
return data + "]";
|
return data + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package bdf.types;
|
||||||
|
|
||||||
|
public class BdfIndent
|
||||||
|
{
|
||||||
|
String indent;
|
||||||
|
String breaker;
|
||||||
|
|
||||||
|
public BdfIndent(String indent, String breaker) {
|
||||||
|
this.indent = indent;
|
||||||
|
this.breaker = breaker;
|
||||||
|
}
|
||||||
|
}
|
|
@ -73,17 +73,27 @@ public class BdfNamedList implements IBdfType
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String serializeHumanReadable()
|
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||||
{
|
{
|
||||||
|
if(elements.size() == 0) {
|
||||||
|
return "{}";
|
||||||
|
}
|
||||||
|
|
||||||
String data = "{";
|
String data = "{";
|
||||||
|
|
||||||
for(int i=0;i<elements.size();i++)
|
for(int i=0;i<elements.size();i++)
|
||||||
{
|
{
|
||||||
Element e = elements.get(i);
|
Element e = elements.get(i);
|
||||||
|
|
||||||
|
data += indent.breaker;
|
||||||
|
|
||||||
|
for(int n=0;n<=it;n++) {
|
||||||
|
data += indent.indent;
|
||||||
|
}
|
||||||
|
|
||||||
data += DataHelpers.serializeString(new String(e.key, StandardCharsets.UTF_8));
|
data += DataHelpers.serializeString(new String(e.key, StandardCharsets.UTF_8));
|
||||||
data += ": ";
|
data += ": ";
|
||||||
data += e.object.serializeHumanReadable();
|
data += e.object.serializeHumanReadable(indent, it + 1);
|
||||||
|
|
||||||
if(elements.size() > i+1)
|
if(elements.size() > i+1)
|
||||||
{
|
{
|
||||||
|
@ -91,6 +101,12 @@ public class BdfNamedList implements IBdfType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data += indent.breaker;
|
||||||
|
|
||||||
|
for(int n=0;n<it;n++) {
|
||||||
|
data += indent.indent;
|
||||||
|
}
|
||||||
|
|
||||||
return data + "}";
|
return data + "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package bdf.types;
|
package bdf.types;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import bdf.data.BdfDatabase;
|
import bdf.data.BdfDatabase;
|
||||||
import bdf.util.DataHelpers;
|
import bdf.util.DataHelpers;
|
||||||
|
@ -48,7 +47,16 @@ public class BdfObject implements IBdfType
|
||||||
return BdfDatabase.add(new BdfDatabase(type), database);
|
return BdfDatabase.add(new BdfDatabase(type), database);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String serializeHumanReadable()
|
private String calcIndent(BdfIndent indent, int it) {
|
||||||
|
String t = "";
|
||||||
|
t += indent.breaker;
|
||||||
|
for(int i=0;i<=it;i++) {
|
||||||
|
t += indent.indent;
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||||
{
|
{
|
||||||
if(type == BdfTypes.BOOLEAN) {
|
if(type == BdfTypes.BOOLEAN) {
|
||||||
if(this.getBoolean()) return "true";
|
if(this.getBoolean()) return "true";
|
||||||
|
@ -56,8 +64,8 @@ public class BdfObject implements IBdfType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Objects
|
// Objects
|
||||||
if(type == BdfTypes.ARRAY) return ((IBdfType)object).serializeHumanReadable();
|
if(type == BdfTypes.ARRAY) return ((IBdfType)object).serializeHumanReadable(indent, it);
|
||||||
if(type == BdfTypes.NAMED_LIST) return ((IBdfType)object).serializeHumanReadable();
|
if(type == BdfTypes.NAMED_LIST) return ((IBdfType)object).serializeHumanReadable(indent, it);
|
||||||
if(type == BdfTypes.STRING) return DataHelpers.serializeString((String)object);
|
if(type == BdfTypes.STRING) return DataHelpers.serializeString((String)object);
|
||||||
|
|
||||||
// Primitives
|
// Primitives
|
||||||
|
@ -71,63 +79,63 @@ public class BdfObject implements IBdfType
|
||||||
|
|
||||||
// Arrays
|
// Arrays
|
||||||
if(type == BdfTypes.ARRAY_INTEGER) {
|
if(type == BdfTypes.ARRAY_INTEGER) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(int i : this.getIntegerArray()) {
|
for(int i : this.getIntegerArray()) {
|
||||||
str += Integer.toString(i) + "I, ";
|
str += Integer.toString(i) + "I, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_BOOLEAN) {
|
if(type == BdfTypes.ARRAY_BOOLEAN) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(boolean i : this.getBooleanArray()) {
|
for(boolean i : this.getBooleanArray()) {
|
||||||
str += (i ? "true" : "false") + ", ";
|
str += (i ? "true" : "false") + ", " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_BYTE) {
|
if(type == BdfTypes.ARRAY_BYTE) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(byte i : this.getByteArray()) {
|
for(byte i : this.getByteArray()) {
|
||||||
str += Byte.toString(i) + "B, ";
|
str += Byte.toString(i) + "B, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_LONG) {
|
if(type == BdfTypes.ARRAY_LONG) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(long i : this.getLongArray()) {
|
for(long i : this.getLongArray()) {
|
||||||
str += Long.toString(i) + "L, ";
|
str += Long.toString(i) + "L, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_SHORT) {
|
if(type == BdfTypes.ARRAY_SHORT) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(short i : this.getShortArray()) {
|
for(short i : this.getShortArray()) {
|
||||||
str += Short.toString(i) + "S, ";
|
str += Short.toString(i) + "S, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_DOUBLE) {
|
if(type == BdfTypes.ARRAY_DOUBLE) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(double i : this.getDoubleArray()) {
|
for(double i : this.getDoubleArray()) {
|
||||||
str += Double.toString(i) + "D, ";
|
str += Double.toString(i) + "D, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == BdfTypes.ARRAY_FLOAT) {
|
if(type == BdfTypes.ARRAY_FLOAT) {
|
||||||
String str = "(";
|
String str = "(" + calcIndent(indent, it);
|
||||||
for(float i : this.getFloatArray()) {
|
for(float i : this.getFloatArray()) {
|
||||||
str += Float.toString(i) + "F, ";
|
str += Float.toString(i) + "F, " + calcIndent(indent, it);
|
||||||
}
|
}
|
||||||
str = str.substring(0, str.length() - 2) + ")";
|
str = str.substring(0, str.length() - 2) + ")";
|
||||||
return str;
|
return str;
|
||||||
|
|
|
@ -5,5 +5,13 @@ import bdf.data.BdfDatabase;
|
||||||
public interface IBdfType
|
public interface IBdfType
|
||||||
{
|
{
|
||||||
public BdfDatabase serialize();
|
public BdfDatabase serialize();
|
||||||
public String serializeHumanReadable();
|
String serializeHumanReadable(BdfIndent indent, int it);
|
||||||
|
|
||||||
|
public default String serializeHumanReadable(BdfIndent indent) {
|
||||||
|
return this.serializeHumanReadable(indent, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public default String serializeHumanReadable() {
|
||||||
|
return this.serializeHumanReadable(new BdfIndent("", ""), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,6 @@ import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.zip.InflaterInputStream;
|
import java.util.zip.InflaterInputStream;
|
||||||
import java.util.zip.ZipException;
|
|
||||||
|
|
||||||
import com.sun.corba.se.impl.ior.ByteBuffer;
|
|
||||||
|
|
||||||
public class FileHelpers
|
public class FileHelpers
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package tests;
|
package tests;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
import bdf.data.BdfDatabase;
|
|
||||||
import bdf.file.BdfCompressedFileManager;
|
import bdf.file.BdfCompressedFileManager;
|
||||||
import bdf.file.BdfFileManager;
|
import bdf.types.BdfArray;
|
||||||
|
import bdf.types.BdfIndent;
|
||||||
import bdf.types.BdfNamedList;
|
import bdf.types.BdfNamedList;
|
||||||
import bdf.types.BdfObject;
|
import bdf.types.BdfObject;
|
||||||
|
|
||||||
|
@ -16,9 +14,23 @@ public class Tests {
|
||||||
|
|
||||||
BdfNamedList nl = bdf.getNamedList();
|
BdfNamedList nl = bdf.getNamedList();
|
||||||
|
|
||||||
nl.set("it", BdfObject.withInteger(nl.get("it").getInteger() + 1));
|
int array[] = {1,2,3,6,7,0};
|
||||||
|
int array2[] = {1,2,3,6,7,0};
|
||||||
|
|
||||||
System.out.println(bdf.serializeHumanReadable());
|
BdfArray array_bdf = new BdfArray();
|
||||||
|
|
||||||
|
array_bdf.add(BdfObject.withBoolean(true));
|
||||||
|
array_bdf.add(BdfObject.withBoolean(false));
|
||||||
|
array_bdf.add(BdfObject.withInteger(7));
|
||||||
|
array_bdf.add(BdfObject.withNamedList());
|
||||||
|
array_bdf.add(BdfObject.withArray());
|
||||||
|
array_bdf.add(BdfObject.withIntegerArray(array2));
|
||||||
|
|
||||||
|
nl.set("it", BdfObject.withInteger(nl.get("it").getInteger() + 1));
|
||||||
|
nl.set("int_array", BdfObject.withIntegerArray(array));
|
||||||
|
nl.set("array", BdfObject.withArray(array_bdf));
|
||||||
|
|
||||||
|
System.out.println(bdf.serializeHumanReadable(new BdfIndent(" ", "\n")));
|
||||||
|
|
||||||
bdf.saveDatabase();
|
bdf.saveDatabase();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue