Retarget java 8
This commit is contained in:
parent
db0a0eef97
commit
daf65a6059
8 changed files with 687 additions and 749 deletions
|
@ -1,3 +1,3 @@
|
|||
apiVer=1.4
|
||||
Bus_API=1.0
|
||||
Bus_Patch=29
|
||||
Bus_Patch=30
|
|
@ -2,11 +2,10 @@ package dev.zontreck.ariaslib.file;
|
|||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class AriaIO
|
||||
{
|
||||
public static void write(Path fileName, Entry folder)
|
||||
{
|
||||
public class AriaIO {
|
||||
public static void write(Path fileName, Entry folder) {
|
||||
try {
|
||||
DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName.toFile()));
|
||||
folder.write(dos);
|
||||
|
@ -18,8 +17,7 @@ public class AriaIO
|
|||
}
|
||||
}
|
||||
|
||||
public static Entry read(Path fileName)
|
||||
{
|
||||
public static Entry read(Path fileName) {
|
||||
try {
|
||||
DataInputStream dis = new DataInputStream(new FileInputStream(fileName.toFile()));
|
||||
return Entry.read(dis);
|
||||
|
@ -31,6 +29,6 @@ public class AriaIO
|
|||
}
|
||||
|
||||
public static Path resolveDataFile(String main) {
|
||||
return Path.of(main+".aria");
|
||||
return Paths.get(main + ".aria");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
package dev.zontreck.ariaslib.file;
|
||||
|
||||
import dev.zontreck.ariaslib.terminal.Terminal;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An entry in the serialized file
|
||||
*/
|
||||
public class Entry<K>
|
||||
{
|
||||
public class Entry<K> {
|
||||
public static final byte YES = 1;
|
||||
public static final byte NO = 0;
|
||||
|
||||
|
@ -21,297 +17,156 @@ public class Entry<K>
|
|||
public String name;
|
||||
public K value;
|
||||
|
||||
public Entry(K v, String name)
|
||||
{
|
||||
value=v;
|
||||
this.name=name;
|
||||
if(v instanceof String) {
|
||||
public Entry(K v, String name) {
|
||||
value = v;
|
||||
this.name = name;
|
||||
if (v instanceof String) {
|
||||
type = EntryType.STRING;
|
||||
}else if(v instanceof Integer)
|
||||
{
|
||||
type=EntryType.INT;
|
||||
} else if(v instanceof List<?>)
|
||||
{
|
||||
} else if (v instanceof Integer) {
|
||||
type = EntryType.INT;
|
||||
} else if (v instanceof List<?>) {
|
||||
type = EntryType.FOLDER;
|
||||
} else if(v instanceof Boolean)
|
||||
{
|
||||
} else if (v instanceof Boolean) {
|
||||
type = EntryType.BOOL;
|
||||
} else if(v instanceof Long)
|
||||
{
|
||||
} else if (v instanceof Long) {
|
||||
type = EntryType.LONG;
|
||||
}else if(v instanceof Short)
|
||||
{
|
||||
} else if (v instanceof Short) {
|
||||
type = EntryType.SHORT;
|
||||
}else if(v instanceof Byte)
|
||||
{
|
||||
} else if (v instanceof Byte) {
|
||||
type = EntryType.BYTE;
|
||||
}else if(v instanceof Double)
|
||||
{
|
||||
} else if (v instanceof Double) {
|
||||
type = EntryType.DOUBLE;
|
||||
}else if(v instanceof Float)
|
||||
{
|
||||
} else if (v instanceof Float) {
|
||||
type = EntryType.FLOAT;
|
||||
}else if(v instanceof int[]){
|
||||
} else if (v instanceof int[]) {
|
||||
type = EntryType.INT_ARRAY;
|
||||
}else if(v instanceof String[])
|
||||
{
|
||||
} else if (v instanceof String[]) {
|
||||
type = EntryType.STRING_ARRAY;
|
||||
}else if(v instanceof byte[])
|
||||
{
|
||||
} else if (v instanceof byte[]) {
|
||||
type = EntryType.BYTE_ARRAY;
|
||||
}else if(v instanceof long[])
|
||||
{
|
||||
} else if (v instanceof long[]) {
|
||||
type = EntryType.LONG_ARRAY;
|
||||
}
|
||||
|
||||
else{
|
||||
} else {
|
||||
type = EntryType.INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
private Entry(){}
|
||||
|
||||
|
||||
public void write(DataOutputStream dos) throws IOException {
|
||||
|
||||
dos.writeByte((int)type.value);
|
||||
byte[] nameBytes = name.getBytes();
|
||||
dos.writeInt(nameBytes.length);
|
||||
dos.write(nameBytes);
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case FOLDER:
|
||||
{
|
||||
List<Entry<?>> entries = (List<Entry<?>>) value;
|
||||
dos.writeInt(entries.size());
|
||||
for (Entry<?> x:
|
||||
entries) {
|
||||
x.write(dos);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case STRING:
|
||||
{
|
||||
String s = (String)value;
|
||||
byte[] bS = s.getBytes();
|
||||
dos.writeInt(bS.length);
|
||||
dos.write(bS);
|
||||
|
||||
break;
|
||||
}
|
||||
case INT:
|
||||
{
|
||||
dos.writeInt((int)value);
|
||||
|
||||
break;
|
||||
}
|
||||
case BOOL:
|
||||
{
|
||||
boolean X = (boolean)value;
|
||||
if(X) dos.writeByte(YES);
|
||||
else dos.writeByte(NO);
|
||||
|
||||
break;
|
||||
}
|
||||
case LONG:
|
||||
{
|
||||
long lng = (long)value;
|
||||
dos.writeLong(lng);
|
||||
|
||||
break;
|
||||
}
|
||||
case SHORT:
|
||||
{
|
||||
short s = (short)value;
|
||||
dos.writeShort(s);
|
||||
|
||||
break;
|
||||
}
|
||||
case BYTE:
|
||||
{
|
||||
dos.write((byte)value);
|
||||
break;
|
||||
}
|
||||
case DOUBLE:
|
||||
{
|
||||
dos.writeDouble((double)value);
|
||||
break;
|
||||
}
|
||||
case FLOAT:
|
||||
{
|
||||
dos.writeFloat((float)value);
|
||||
break;
|
||||
}
|
||||
case INT_ARRAY:
|
||||
{
|
||||
int[] arr = (int[])value;
|
||||
dos.writeInt(arr.length);
|
||||
for (int x: arr
|
||||
) {
|
||||
dos.writeInt(x);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STRING_ARRAY:
|
||||
{
|
||||
String[] arr = (String[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for(String s : arr)
|
||||
{
|
||||
byte[] bArr = s.getBytes();
|
||||
dos.writeInt(bArr.length);
|
||||
dos.write(bArr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BYTE_ARRAY:
|
||||
{
|
||||
byte[] arr = (byte[])value;
|
||||
dos.writeInt(arr.length);
|
||||
for(byte b : arr)
|
||||
{
|
||||
dos.write(b);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LONG_ARRAY:
|
||||
{
|
||||
long[] arr = (long[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for(long L : arr)
|
||||
{
|
||||
dos.writeLong(L);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
private Entry() {
|
||||
}
|
||||
|
||||
public static Entry read(DataInputStream dis) throws IOException {
|
||||
EntryType et = EntryType.of(dis.readByte());
|
||||
int nameLen = dis.readInt();
|
||||
byte[] nm = dis.readNBytes(nameLen);
|
||||
byte[] nm = new byte[nameLen];
|
||||
for (int i = 0; i < nameLen; i++) {
|
||||
nm[i] = dis.readByte();
|
||||
}
|
||||
Entry work = new Entry<>();
|
||||
work.type = et;
|
||||
work.name = new String(nm);
|
||||
//System.out.println("Read start: " + work.name + " [ " + work.type.toString() + " ]");
|
||||
|
||||
switch(et)
|
||||
{
|
||||
case FOLDER:
|
||||
{
|
||||
switch (et) {
|
||||
case FOLDER: {
|
||||
Entry<List<Entry>> entries = (Entry<List<Entry>>) work;
|
||||
entries.value = new ArrayList<>();
|
||||
|
||||
int numEntries = dis.readInt();
|
||||
for(int i = 0; i<numEntries; i++)
|
||||
{
|
||||
for (int i = 0; i < numEntries; i++) {
|
||||
entries.value.add(Entry.read(dis));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case STRING:
|
||||
{
|
||||
case STRING: {
|
||||
Entry<String> w = (Entry<String>) work;
|
||||
int vLen = dis.readInt();
|
||||
w.value = new String(dis.readNBytes(vLen));
|
||||
byte[] x = new byte[vLen];
|
||||
for (int i = 0; i < vLen; i++) {
|
||||
x[i] = dis.readByte();
|
||||
}
|
||||
w.value = new String(x);
|
||||
break;
|
||||
}
|
||||
case INT:
|
||||
{
|
||||
case INT: {
|
||||
Entry<Integer> w = (Entry<Integer>) work;
|
||||
w.value = dis.readInt();
|
||||
break;
|
||||
}
|
||||
case BOOL:
|
||||
{
|
||||
case BOOL: {
|
||||
Entry<Boolean> w = (Entry<Boolean>) work;
|
||||
byte b = dis.readByte();
|
||||
if(b == YES)w.value = true;
|
||||
else w.value=false;
|
||||
if (b == YES) w.value = true;
|
||||
else w.value = false;
|
||||
|
||||
break;
|
||||
}
|
||||
case LONG:
|
||||
{
|
||||
case LONG: {
|
||||
Entry<Long> w = (Entry<Long>) work;
|
||||
w.value = dis.readLong();
|
||||
|
||||
break;
|
||||
}
|
||||
case SHORT:
|
||||
{
|
||||
case SHORT: {
|
||||
Entry<Short> w = (Entry<Short>) work;
|
||||
w.value = dis.readShort();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case BYTE:
|
||||
{
|
||||
case BYTE: {
|
||||
Entry<Byte> w = (Entry<Byte>) work;
|
||||
w.value = dis.readByte();
|
||||
break;
|
||||
}
|
||||
case DOUBLE:
|
||||
{
|
||||
case DOUBLE: {
|
||||
Entry<Double> w = (Entry<Double>) work;
|
||||
w.value = dis.readDouble();
|
||||
break;
|
||||
}
|
||||
case FLOAT:
|
||||
{
|
||||
case FLOAT: {
|
||||
Entry<Float> w = (Entry<Float>) work;
|
||||
w.value = dis.readFloat();
|
||||
break;
|
||||
}
|
||||
case INT_ARRAY:
|
||||
{
|
||||
case INT_ARRAY: {
|
||||
Entry<int[]> w = (Entry<int[]>) work;
|
||||
int num = dis.readInt();
|
||||
w.value = new int[num];
|
||||
|
||||
for(int i=0;i<num;i++)
|
||||
{
|
||||
for (int i = 0; i < num; i++) {
|
||||
w.value[i] = dis.readInt();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STRING_ARRAY:
|
||||
{
|
||||
case STRING_ARRAY: {
|
||||
Entry<String[]> w = (Entry<String[]>) work;
|
||||
int num = dis.readInt();
|
||||
w.value = new String[num];
|
||||
for(int i=0;i<num;i++)
|
||||
{
|
||||
for (int i = 0; i < num; i++) {
|
||||
int len = dis.readInt();
|
||||
byte[] bStr = dis.readNBytes(len);
|
||||
byte[] bStr = new byte[len];
|
||||
for (int j = 0; j < len; j++) {
|
||||
bStr[j] = dis.readByte();
|
||||
}
|
||||
w.value[i] = new String(bStr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BYTE_ARRAY:
|
||||
{
|
||||
case BYTE_ARRAY: {
|
||||
Entry<byte[]> w = (Entry<byte[]>) work;
|
||||
int num = dis.readInt();
|
||||
w.value = new byte[num];
|
||||
for(int i=0;i<num;i++)
|
||||
{
|
||||
for (int i = 0; i < num; i++) {
|
||||
w.value[i] = dis.readByte();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LONG_ARRAY:
|
||||
{
|
||||
case LONG_ARRAY: {
|
||||
Entry<long[]> w = (Entry<long[]>) work;
|
||||
int num = dis.readInt();
|
||||
w.value = new long[num];
|
||||
for(int i=0;i<num;i++)
|
||||
{
|
||||
for (int i = 0; i < num; i++) {
|
||||
w.value[i] = dis.readLong();
|
||||
}
|
||||
break;
|
||||
|
@ -323,4 +178,104 @@ public class Entry<K>
|
|||
return work;
|
||||
}
|
||||
|
||||
public void write(DataOutputStream dos) throws IOException {
|
||||
|
||||
dos.writeByte((int) type.value);
|
||||
byte[] nameBytes = name.getBytes();
|
||||
dos.writeInt(nameBytes.length);
|
||||
dos.write(nameBytes);
|
||||
|
||||
switch (type) {
|
||||
case FOLDER: {
|
||||
List<Entry<?>> entries = (List<Entry<?>>) value;
|
||||
dos.writeInt(entries.size());
|
||||
for (Entry<?> x :
|
||||
entries) {
|
||||
x.write(dos);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case STRING: {
|
||||
String s = (String) value;
|
||||
byte[] bS = s.getBytes();
|
||||
dos.writeInt(bS.length);
|
||||
dos.write(bS);
|
||||
|
||||
break;
|
||||
}
|
||||
case INT: {
|
||||
dos.writeInt((Integer) value);
|
||||
|
||||
break;
|
||||
}
|
||||
case BOOL: {
|
||||
boolean x = (Boolean) value;
|
||||
|
||||
if (x) dos.writeByte(YES);
|
||||
else dos.writeByte(NO);
|
||||
|
||||
break;
|
||||
}
|
||||
case LONG: {
|
||||
dos.writeLong((Long) value);
|
||||
|
||||
break;
|
||||
}
|
||||
case SHORT: {
|
||||
dos.writeShort((Short) value);
|
||||
|
||||
break;
|
||||
}
|
||||
case BYTE: {
|
||||
dos.write((Byte) value);
|
||||
break;
|
||||
}
|
||||
case DOUBLE: {
|
||||
dos.writeDouble((Double) value);
|
||||
break;
|
||||
}
|
||||
case FLOAT: {
|
||||
dos.writeFloat((Float) value);
|
||||
break;
|
||||
}
|
||||
case INT_ARRAY: {
|
||||
int[] arr = (int[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for (int x : arr
|
||||
) {
|
||||
dos.writeInt(x);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STRING_ARRAY: {
|
||||
String[] arr = (String[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for (String s : arr) {
|
||||
byte[] bArr = s.getBytes();
|
||||
dos.writeInt(bArr.length);
|
||||
dos.write(bArr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BYTE_ARRAY: {
|
||||
byte[] arr = (byte[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for (byte b : arr) {
|
||||
dos.write(b);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LONG_ARRAY: {
|
||||
long[] arr = (long[]) value;
|
||||
dos.writeInt(arr.length);
|
||||
for (long L : arr) {
|
||||
dos.writeLong(L);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -6,285 +6,278 @@ import dev.zontreck.ariaslib.html.bootstrap.Size;
|
|||
import dev.zontreck.ariaslib.util.Percent;
|
||||
|
||||
public class Bootstrap {
|
||||
public static class Border {
|
||||
public static Border make ( ) {
|
||||
return new Border ( );
|
||||
}
|
||||
public static class Border {
|
||||
public Side side;
|
||||
public int width = 1;
|
||||
public boolean usesSides = false;
|
||||
public Colors colors;
|
||||
public boolean invert;
|
||||
|
||||
public enum Side {
|
||||
Start,
|
||||
End,
|
||||
Top,
|
||||
Bottom
|
||||
}
|
||||
public static Border make() {
|
||||
return new Border();
|
||||
}
|
||||
|
||||
public Side side;
|
||||
public int width = 1;
|
||||
public boolean usesSides = false;
|
||||
public Colors colors;
|
||||
public Border withColor(Colors color) {
|
||||
this.colors = color.withPrefix("border");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Border withColor ( Colors color ) {
|
||||
this.colors = color.withPrefix ( "border" );
|
||||
return this;
|
||||
}
|
||||
public Border widthSide(Side side) {
|
||||
this.side = side;
|
||||
usesSides = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Border widthSide ( Side side ) {
|
||||
this.side = side;
|
||||
usesSides = true;
|
||||
return this;
|
||||
}
|
||||
public Border withWidth(int width) {
|
||||
this.width = width;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Border withWidth ( int width ) {
|
||||
this.width = width;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Removes borders instead of adding
|
||||
*/
|
||||
public Border setInverted() {
|
||||
invert = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean invert;
|
||||
public void apply(HTMLElementBuilder elem) {
|
||||
elem.addClass("border");
|
||||
|
||||
/**
|
||||
* Removes borders instead of adding
|
||||
*/
|
||||
public Border setInverted ( ) {
|
||||
invert = true;
|
||||
return this;
|
||||
}
|
||||
colors.apply(elem);
|
||||
|
||||
if (usesSides) {
|
||||
elem.addClass("border-" + side.name().toLowerCase() + (invert ? "-0" : ""));
|
||||
} else {
|
||||
if (invert) elem.addClass("border-0");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void apply ( HTMLElementBuilder elem ) {
|
||||
elem.addClass ( "border" );
|
||||
public enum Side {
|
||||
Start,
|
||||
End,
|
||||
Top,
|
||||
Bottom
|
||||
}
|
||||
}
|
||||
|
||||
colors.apply ( elem );
|
||||
public static class Opacity {
|
||||
public Percent value;
|
||||
public String prefix;
|
||||
|
||||
if ( usesSides ) {
|
||||
elem.addClass ( "border-" + side.name ( ).toLowerCase ( ) + ( invert ? "-0" : "" ) );
|
||||
}
|
||||
else {
|
||||
if ( invert ) elem.addClass ( "border-0" );
|
||||
}
|
||||
}
|
||||
}
|
||||
public static Opacity make() {
|
||||
return new Opacity();
|
||||
}
|
||||
|
||||
public static class Opacity {
|
||||
public Percent value;
|
||||
public String prefix;
|
||||
public Opacity withPercent(Percent val) {
|
||||
value = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Opacity make ( ) {
|
||||
return new Opacity ( );
|
||||
}
|
||||
public Opacity withPrefix(String pref) {
|
||||
this.prefix = pref;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Opacity withPercent ( Percent val ) {
|
||||
value = val;
|
||||
return this;
|
||||
}
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
builder.addClass((prefix != "" ? prefix + "-" : "") + "opacity-" + value.get());
|
||||
}
|
||||
}
|
||||
|
||||
public Opacity withPrefix ( String pref ) {
|
||||
this.prefix = pref;
|
||||
return this;
|
||||
}
|
||||
public static class Colors {
|
||||
public Color color;
|
||||
public boolean emphasis;
|
||||
public boolean subtle;
|
||||
public String prefix;
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
builder.addClass ( ( prefix != "" ? prefix + "-" : "" ) + "opacity-" + value.get ( ) );
|
||||
}
|
||||
}
|
||||
public static Colors make() {
|
||||
return new Colors();
|
||||
}
|
||||
|
||||
public static class Colors {
|
||||
public static Colors make ( ) {
|
||||
return new Colors ( );
|
||||
}
|
||||
public Colors withColor(Color color) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Color color;
|
||||
public boolean emphasis;
|
||||
public boolean subtle;
|
||||
public String prefix;
|
||||
public Colors setEmphasis() {
|
||||
emphasis = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Colors setSubtle() {
|
||||
subtle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Colors withPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
builder.addClass(((prefix != "") ? prefix + "-" : "") + color.name().toLowerCase() + (emphasis ? "-emphasis" : "") + (subtle ? "-subtle" : ""));
|
||||
}
|
||||
}
|
||||
|
||||
public static class Background {
|
||||
public Colors color;
|
||||
public Opacity opacity;
|
||||
public boolean gradient;
|
||||
|
||||
public static Background make() {
|
||||
return new Background();
|
||||
}
|
||||
|
||||
public Background withColor(Colors color) {
|
||||
this.color = color.withPrefix("bg");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Background withOpacity(Opacity op) {
|
||||
this.opacity = op.withPrefix("bg");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Background setGradient() {
|
||||
gradient = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Colors withColor ( Color color ) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
color.apply(builder);
|
||||
opacity.apply(builder);
|
||||
if (gradient)
|
||||
builder.addClass(".bg-gradient");
|
||||
}
|
||||
}
|
||||
|
||||
public Colors setEmphasis ( ) {
|
||||
emphasis = true;
|
||||
return this;
|
||||
}
|
||||
public static class Shadow {
|
||||
public Size size;
|
||||
|
||||
public Colors setSubtle ( ) {
|
||||
subtle = true;
|
||||
return this;
|
||||
}
|
||||
public static Shadow make() {
|
||||
return new Shadow();
|
||||
}
|
||||
|
||||
public Colors withPrefix ( String prefix ) {
|
||||
this.prefix = prefix;
|
||||
return this;
|
||||
}
|
||||
public Shadow withSize(Size size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
builder.addClass ( ( ( prefix != "" ) ? prefix + "-" : "" ) + color.name ( ).toLowerCase ( ) + ( emphasis ? "-emphasis" : "" ) + ( subtle ? "-subtle" : "" ) );
|
||||
}
|
||||
}
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
builder.addClass("shadow" + size.sizeText());
|
||||
}
|
||||
}
|
||||
|
||||
public static class Background {
|
||||
public static Background make ( ) {
|
||||
return new Background ( );
|
||||
}
|
||||
public static class FocusRing {
|
||||
public Colors color;
|
||||
|
||||
public Colors color;
|
||||
public Opacity opacity;
|
||||
public boolean gradient;
|
||||
public static FocusRing make() {
|
||||
return new FocusRing();
|
||||
}
|
||||
|
||||
public Background withColor ( Colors color ) {
|
||||
this.color = color.withPrefix ( "bg" );
|
||||
return this;
|
||||
}
|
||||
public FocusRing withColor(Colors color) {
|
||||
this.color = color.withPrefix("focus-ring");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Background withOpacity ( Opacity op ) {
|
||||
this.opacity = op.withPrefix ( "bg" );
|
||||
return this;
|
||||
}
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
builder.addClass("focus-ring");
|
||||
color.apply(builder);
|
||||
}
|
||||
}
|
||||
|
||||
public Background setGradient ( ) {
|
||||
gradient = true;
|
||||
return this;
|
||||
}
|
||||
public static class Link {
|
||||
public Colors color;
|
||||
|
||||
public static Link make() {
|
||||
return new Link();
|
||||
}
|
||||
|
||||
public Link withColor(Colors color) {
|
||||
this.color = color.withPrefix("link");
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
color.apply(builder);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Toast {
|
||||
public Icons icon;
|
||||
public HTMLElementBuilder toastHeader;
|
||||
public HTMLElementBuilder toastBody;
|
||||
|
||||
public Toast() {
|
||||
toastHeader = new HTMLElementBuilder("div").addClass("toast-header");
|
||||
toastHeader.addChild("svg").addClass("bi").addClass(icon.getClassName()).addClass("rounded");
|
||||
toastHeader.addChild("strong").addClass("me-auto");
|
||||
toastHeader.addChild("small").withText("Text?");
|
||||
toastHeader.addChild("button").withAttribute("type", "button").addClass("btn-close").withAttribute("data-bs-dismiss", "toast").withAttribute("aria-label", "Close");
|
||||
|
||||
toastBody = new HTMLElementBuilder("div").addClass("toast-body");
|
||||
}
|
||||
|
||||
public static Toast make() {
|
||||
return new Toast();
|
||||
}
|
||||
|
||||
public Toast withIcon(Icons icon) {
|
||||
this.icon = icon;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
HTMLElementBuilder toast = builder.addChild("div").addClass("toast").withAttribute("role", "alert").withAttribute("aria-live", "assertive").withAttribute("aria-atomic", "true");
|
||||
toast.addChild(toastHeader);
|
||||
toast.addChild(toastBody);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Button {
|
||||
public Colors color;
|
||||
public boolean outline;
|
||||
public Size size;
|
||||
|
||||
public static Button make() {
|
||||
return new Button();
|
||||
}
|
||||
|
||||
public Button withColor(Colors color) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Button setOutline() {
|
||||
outline = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Button withSize(Size size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
color.apply ( builder );
|
||||
opacity.apply ( builder );
|
||||
if ( gradient )
|
||||
builder.addClass ( ".bg-gradient" );
|
||||
}
|
||||
}
|
||||
public void apply(HTMLElementBuilder builder) {
|
||||
builder.addClass("btn");
|
||||
|
||||
public static class Shadow {
|
||||
public static Shadow make ( ) {
|
||||
return new Shadow ( );
|
||||
}
|
||||
if (outline) {
|
||||
color.withPrefix("btn-outline");
|
||||
} else color.withPrefix("btn");
|
||||
|
||||
public Size size;
|
||||
color.apply(builder);
|
||||
if (size != Size.Regular)
|
||||
builder.addClass("btn" + size.sizeText());
|
||||
}
|
||||
|
||||
public Shadow withSize ( Size size ) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
builder.addClass ( "shadow" + size.sizeText ( ) );
|
||||
}
|
||||
}
|
||||
|
||||
public static class FocusRing {
|
||||
public static FocusRing make ( ) {
|
||||
return new FocusRing ( );
|
||||
}
|
||||
|
||||
public Colors color;
|
||||
|
||||
public FocusRing withColor ( Colors color ) {
|
||||
this.color = color.withPrefix ( "focus-ring" );
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
builder.addClass ( "focus-ring" );
|
||||
color.apply ( builder );
|
||||
}
|
||||
}
|
||||
|
||||
public static class Link {
|
||||
public static Link make ( ) {
|
||||
return new Link ( );
|
||||
}
|
||||
|
||||
public Colors color;
|
||||
|
||||
public Link withColor ( Colors color ) {
|
||||
this.color = color.withPrefix ( "link" );
|
||||
return this;
|
||||
}
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
color.apply ( builder );
|
||||
}
|
||||
}
|
||||
|
||||
public static class Toast {
|
||||
public static Toast make ( ) {
|
||||
return new Toast ( );
|
||||
}
|
||||
|
||||
public Icons icon;
|
||||
|
||||
public Toast withIcon ( Icons icon ) {
|
||||
this.icon = icon;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Toast ( ) {
|
||||
toastHeader = new HTMLElementBuilder ( "div" ).addClass ( "toast-header" );
|
||||
toastHeader.addChild ( "svg" ).addClass ( "bi" ).addClass ( icon.getClassName ( ) ).addClass ( "rounded" );
|
||||
toastHeader.addChild ( "strong" ).addClass ( "me-auto" );
|
||||
toastHeader.addChild ( "small" ).withText ( "Text?" );
|
||||
toastHeader.addChild ( "button" ).withAttribute ( "type" , "button" ).addClass ( "btn-close" ).withAttribute ( "data-bs-dismiss" , "toast" ).withAttribute ( "aria-label" , "Close" );
|
||||
|
||||
toastBody = new HTMLElementBuilder ( "div" ).addClass ( "toast-body" );
|
||||
}
|
||||
|
||||
public HTMLElementBuilder toastHeader;
|
||||
public HTMLElementBuilder toastBody;
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
var toast = builder.addChild ( "div" ).addClass ( "toast" ).withAttribute ( "role" , "alert" ).withAttribute ( "aria-live" , "assertive" ).withAttribute ( "aria-atomic" , "true" );
|
||||
toast.addChild ( toastHeader );
|
||||
toast.addChild ( toastBody );
|
||||
}
|
||||
}
|
||||
|
||||
public static class Button {
|
||||
public static Button make ( ) {
|
||||
return new Button ( );
|
||||
}
|
||||
|
||||
|
||||
public Colors color;
|
||||
public boolean outline;
|
||||
|
||||
public Button withColor ( Colors color ) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Button setOutline ( ) {
|
||||
outline = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Size size;
|
||||
|
||||
public Button withSize ( Size size ) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public void apply ( HTMLElementBuilder builder ) {
|
||||
builder.addClass ( "btn" );
|
||||
|
||||
if ( outline ) {
|
||||
color.withPrefix ( "btn-outline" );
|
||||
}
|
||||
else color.withPrefix ( "btn" );
|
||||
|
||||
color.apply ( builder );
|
||||
if ( size != Size.Regular )
|
||||
builder.addClass ( "btn" + size.sizeText ( ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Disabled {
|
||||
public static void setDisabled ( HTMLElementBuilder builder ) {
|
||||
builder.withAttribute ( "disabled" );
|
||||
}
|
||||
}
|
||||
public static class Disabled {
|
||||
public static void setDisabled(HTMLElementBuilder builder) {
|
||||
builder.withAttribute("disabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,53 +3,53 @@ package dev.zontreck.ariaslib.html;
|
|||
public class DOM {
|
||||
|
||||
|
||||
public static HTMLElementBuilder beginBootstrapDOM ( String pageTitle ) {
|
||||
var builder = new HTMLElementBuilder ( "!doctype" ).withText ( "html" );
|
||||
public static HTMLElementBuilder beginBootstrapDOM(String pageTitle) {
|
||||
HTMLElementBuilder builder = new HTMLElementBuilder("!doctype").withText("html");
|
||||
|
||||
var html = builder.getOrCreate ( "html" );
|
||||
HTMLElementBuilder html = builder.getOrCreate("html");
|
||||
|
||||
var head = html.getOrCreate ( "head" );
|
||||
HTMLElementBuilder head = html.getOrCreate("head");
|
||||
|
||||
head.addChild ( "meta" ).withAttribute ( "charset" , "utf-8" );
|
||||
head.addChild("meta").withAttribute("charset", "utf-8");
|
||||
|
||||
head.addChild ( "meta" ).withAttribute ( "name" , "viewport" ).withAttribute ( "content" , "width=device-width, initial-scale=1" );
|
||||
head.addChild("meta").withAttribute("name", "viewport").withAttribute("content", "width=device-width, initial-scale=1");
|
||||
|
||||
head.getOrCreate ( "link" ).withAttribute ( "href" , "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" ).withAttribute ( "integrity" , "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" ).withAttribute ( "crossorigin" , "anonymous" ).withAttribute ( "rel" , "stylesheet" );
|
||||
head.getOrCreate("link").withAttribute("href", "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css").withAttribute("integrity", "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM").withAttribute("crossorigin", "anonymous").withAttribute("rel", "stylesheet");
|
||||
|
||||
head.addClass ( "link" ).withAttribute ( "rel" , "stylesheet" ).withAttribute ( "href" , "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" );
|
||||
head.addClass("link").withAttribute("rel", "stylesheet").withAttribute("href", "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css");
|
||||
|
||||
head.getOrCreate ( "title" ).withText ( pageTitle );
|
||||
head.getOrCreate("title").withText(pageTitle);
|
||||
|
||||
var body = html.getOrCreate ( "body" );
|
||||
body.addChild ( "script" ).withAttribute ( "src" , "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" ).withAttribute ( "integrity" , "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" ).withAttribute ( "crossorigin" , "anonymous" ).withText ( " " );
|
||||
HTMLElementBuilder body = html.getOrCreate("body");
|
||||
body.addChild("script").withAttribute("src", "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js").withAttribute("integrity", "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz").withAttribute("crossorigin", "anonymous").withText(" ");
|
||||
|
||||
body.addChild ( "script" ).withAttribute ( "src" , "https://code.jquery.com/jquery-3.7.0.min.js" ).withAttribute ( "crossorigin" , "anonymous" ).withText ( " " );
|
||||
body.addChild("script").withAttribute("src", "https://code.jquery.com/jquery-3.7.0.min.js").withAttribute("crossorigin", "anonymous").withText(" ");
|
||||
|
||||
body.addChild ( "style" ).withText ( "\n" +
|
||||
" .command-popover{\n" +
|
||||
" --bs-popover-header-bg: var(--bs-info);\n" +
|
||||
" --bs-popover-header-color: var(--bs-dark);\n" +
|
||||
" --bs-popover-bg: var(--bs-dark);\n" +
|
||||
" --bs-popover-body-color: var(--bs-light);\n" +
|
||||
" }\n" );
|
||||
body.addChild("style").withText("\n" +
|
||||
" .command-popover{\n" +
|
||||
" --bs-popover-header-bg: var(--bs-info);\n" +
|
||||
" --bs-popover-header-color: var(--bs-dark);\n" +
|
||||
" --bs-popover-bg: var(--bs-dark);\n" +
|
||||
" --bs-popover-body-color: var(--bs-light);\n" +
|
||||
" }\n");
|
||||
|
||||
|
||||
return builder;
|
||||
return builder;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void addPopOverScan ( HTMLElementBuilder builder ) {
|
||||
builder.getChildByTagName ( "html" ).getChildByTagName ( "body" ).addChild ( "script" ).withText ( "" +
|
||||
"function scanPopOver()" +
|
||||
"{" +
|
||||
"var popoverTriggerList = document.querySelectorAll('[data-bs-toggle=\"popover\"]');\n" +
|
||||
"var popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));" +
|
||||
"" +
|
||||
"}" );
|
||||
}
|
||||
public static void addPopOverScan(HTMLElementBuilder builder) {
|
||||
builder.getChildByTagName("html").getChildByTagName("body").addChild("script").withText("" +
|
||||
"function scanPopOver()" +
|
||||
"{" +
|
||||
"var popoverTriggerList = document.querySelectorAll('[data-bs-toggle=\"popover\"]');\n" +
|
||||
"var popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));" +
|
||||
"" +
|
||||
"}");
|
||||
}
|
||||
|
||||
|
||||
public static String closeHTML ( ) {
|
||||
return "</body></html>";
|
||||
}
|
||||
public static String closeHTML() {
|
||||
return "</body></html>";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,25 @@
|
|||
package dev.zontreck.ariaslib.html.bootstrap;
|
||||
|
||||
public enum Size
|
||||
{
|
||||
Small,
|
||||
Regular,
|
||||
Large,
|
||||
None;
|
||||
public enum Size {
|
||||
Small,
|
||||
Regular,
|
||||
Large,
|
||||
None;
|
||||
|
||||
public String sizeText()
|
||||
{
|
||||
switch(this)
|
||||
{
|
||||
case Small -> {
|
||||
return "-sm";
|
||||
}
|
||||
case None -> {
|
||||
return "-none";
|
||||
}
|
||||
case Large -> {
|
||||
return "-lg";
|
||||
}
|
||||
default -> {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
public String sizeText() {
|
||||
switch (this) {
|
||||
case Small:
|
||||
return "-sm";
|
||||
|
||||
case None:
|
||||
return "-none";
|
||||
|
||||
case Large:
|
||||
return "-lg";
|
||||
|
||||
default:
|
||||
return "";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,108 +9,107 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
public class DynamicSerializer {
|
||||
/**
|
||||
* Serializes the object instance
|
||||
*
|
||||
* @param inst The class object to serialize
|
||||
* @return A byte array of serialized data
|
||||
*/
|
||||
public static byte[] doSerialize ( Object inst ) throws InvocationTargetException, IllegalAccessException {
|
||||
Map<String, Object> ret = serialize ( inst );
|
||||
/**
|
||||
* Serializes the object instance
|
||||
*
|
||||
* @param inst The class object to serialize
|
||||
* @return A byte array of serialized data
|
||||
*/
|
||||
public static byte[] doSerialize(Object inst) throws InvocationTargetException, IllegalAccessException {
|
||||
Map<String, Object> ret = serialize(inst);
|
||||
|
||||
JsonObject js = new JsonObject ( ret );
|
||||
JsonObject js = new JsonObject(ret);
|
||||
|
||||
return js.toJSONString ( ).getBytes ( );
|
||||
}
|
||||
return js.toJSONString().getBytes();
|
||||
}
|
||||
|
||||
private static Map<String, Object> serialize ( Object inst ) throws InvocationTargetException, IllegalAccessException {
|
||||
Class<?> clazz = inst.getClass ( );
|
||||
if ( ! clazz.isAnnotationPresent ( DynSerial.class ) )
|
||||
return null;
|
||||
Method[] mth = clazz.getDeclaredMethods ( );
|
||||
Method onComplete = null;
|
||||
for (
|
||||
Method mt :
|
||||
mth
|
||||
) {
|
||||
if ( mt.isAnnotationPresent ( PreSerialize.class ) ) {
|
||||
mt.invoke ( inst );
|
||||
}
|
||||
private static Map<String, Object> serialize(Object inst) throws InvocationTargetException, IllegalAccessException {
|
||||
Class<?> clazz = inst.getClass();
|
||||
if (!clazz.isAnnotationPresent(DynSerial.class))
|
||||
return null;
|
||||
Method[] mth = clazz.getDeclaredMethods();
|
||||
Method onComplete = null;
|
||||
for (
|
||||
Method mt :
|
||||
mth
|
||||
) {
|
||||
if (mt.isAnnotationPresent(PreSerialize.class)) {
|
||||
mt.invoke(inst);
|
||||
}
|
||||
|
||||
if ( mt.isAnnotationPresent ( Completed.class ) )
|
||||
onComplete = mt;
|
||||
}
|
||||
if (mt.isAnnotationPresent(Completed.class))
|
||||
onComplete = mt;
|
||||
}
|
||||
|
||||
Field[] fields = clazz.getDeclaredFields ( );
|
||||
Map<String, Object> ret = new HashMap<> ( );
|
||||
for (
|
||||
Field field :
|
||||
fields
|
||||
) {
|
||||
field.setAccessible ( true );
|
||||
if ( field.isAnnotationPresent ( IgnoreSerialization.class ) )
|
||||
continue;
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
for (
|
||||
Field field :
|
||||
fields
|
||||
) {
|
||||
field.setAccessible(true);
|
||||
if (field.isAnnotationPresent(IgnoreSerialization.class))
|
||||
continue;
|
||||
|
||||
Object fieldVal = field.get ( inst );
|
||||
if ( fieldVal == null ) continue;
|
||||
Object fieldVal = field.get(inst);
|
||||
if (fieldVal == null) continue;
|
||||
|
||||
String fieldName = field.getName ( );
|
||||
String fieldName = field.getName();
|
||||
|
||||
if (field.isAnnotationPresent ( ListOrMap.class )) {
|
||||
// Special handling for List and Map types
|
||||
ret.put(fieldName, serializeCollectionOrMap(fieldVal));
|
||||
continue;
|
||||
}
|
||||
if ( ! ( fieldVal.getClass ( ).isAnnotationPresent ( DynSerial.class ) ) ) {
|
||||
// Special handler for List and Map is needed right here.
|
||||
if(fieldVal instanceof List || fieldVal instanceof Map) continue;
|
||||
if (field.isAnnotationPresent(ListOrMap.class)) {
|
||||
// Special handling for List and Map types
|
||||
ret.put(fieldName, serializeCollectionOrMap(fieldVal));
|
||||
continue;
|
||||
}
|
||||
if (!(fieldVal.getClass().isAnnotationPresent(DynSerial.class))) {
|
||||
// Special handler for List and Map is needed right here.
|
||||
if (fieldVal instanceof List || fieldVal instanceof Map) continue;
|
||||
|
||||
ret.put ( fieldName , fieldVal );
|
||||
}
|
||||
else {
|
||||
Map<String, Object> TMP = serialize ( fieldVal );
|
||||
ret.put ( fieldName , TMP );
|
||||
}
|
||||
}
|
||||
ret.put(fieldName, fieldVal);
|
||||
} else {
|
||||
Map<String, Object> TMP = serialize(fieldVal);
|
||||
ret.put(fieldName, TMP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( onComplete != null )
|
||||
onComplete.invoke ( inst , false );
|
||||
if (onComplete != null)
|
||||
onComplete.invoke(inst, false);
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
private static Object serializeCollectionOrMap ( Object collectionOrMap ) throws InvocationTargetException, IllegalAccessException {
|
||||
if ( collectionOrMap instanceof List<?> list ) {
|
||||
List<Object> serializedList = new ArrayList<> ( );
|
||||
for ( Object item : list ) {
|
||||
if ( item.getClass ( ).isAnnotationPresent ( DynSerial.class ) ) {
|
||||
serializedList.add ( serialize ( item ) );
|
||||
}
|
||||
else {
|
||||
serializedList.add ( item );
|
||||
}
|
||||
}
|
||||
return serializedList;
|
||||
}
|
||||
else if ( collectionOrMap instanceof Map<?,?> mp ) {
|
||||
Map<String,Object> map = (Map<String, Object> ) mp;
|
||||
Map<String, Object> serializedMap = new HashMap<> ( );
|
||||
for ( Map.Entry<String, Object> entry : map.entrySet ( ) ) {
|
||||
String key = entry.getKey ( );
|
||||
Object value = entry.getValue ( );
|
||||
if ( value.getClass ( ).isAnnotationPresent ( DynSerial.class ) ) {
|
||||
value = serialize ( value );
|
||||
}
|
||||
serializedMap.put ( key , value );
|
||||
}
|
||||
return serializedMap;
|
||||
}
|
||||
return collectionOrMap;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Object serializeCollectionOrMap(Object collectionOrMap) throws InvocationTargetException, IllegalAccessException {
|
||||
if (collectionOrMap instanceof List<?>) {
|
||||
List<?> list = (List<?>) collectionOrMap;
|
||||
List<Object> serializedList = new ArrayList<>();
|
||||
for (Object item : list) {
|
||||
if (item.getClass().isAnnotationPresent(DynSerial.class)) {
|
||||
serializedList.add(serialize(item));
|
||||
} else {
|
||||
serializedList.add(item);
|
||||
}
|
||||
}
|
||||
return serializedList;
|
||||
} else if (collectionOrMap instanceof Map<?, ?>) {
|
||||
Map<?, ?> mp = (Map<?, ?>) collectionOrMap;
|
||||
Map<String, Object> map = (Map<String, Object>) mp;
|
||||
Map<String, Object> serializedMap = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (value.getClass().isAnnotationPresent(DynSerial.class)) {
|
||||
value = serialize(value);
|
||||
}
|
||||
serializedMap.put(key, value);
|
||||
}
|
||||
return serializedMap;
|
||||
}
|
||||
return collectionOrMap;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -10,178 +10,174 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
public class JsonObject {
|
||||
private Map<String, Object> data;
|
||||
private Map<String, Object> data;
|
||||
|
||||
public JsonObject() {
|
||||
data = new HashMap<>();
|
||||
}
|
||||
public JsonObject() {
|
||||
data = new HashMap<>();
|
||||
}
|
||||
|
||||
public JsonObject(Map<String, Object> dat)
|
||||
{
|
||||
data = new HashMap<> ( dat );
|
||||
}
|
||||
public JsonObject(Map<String, Object> dat) {
|
||||
data = new HashMap<>(dat);
|
||||
}
|
||||
|
||||
public void put(String key, Object value) {
|
||||
data.put(key, value);
|
||||
}
|
||||
public static JsonObject parseJSON(InputStream inputStream) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
StringBuilder jsonString = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
jsonString.append(line);
|
||||
}
|
||||
return parseJsonObject(jsonString.toString());
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return data.get(key);
|
||||
}
|
||||
private static JsonObject parseJsonObject(String jsonString) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonString = jsonString.trim();
|
||||
if (jsonString.startsWith("{") && jsonString.endsWith("}")) {
|
||||
jsonString = jsonString.substring(1, jsonString.length() - 1);
|
||||
String[] keyValuePairs = jsonString.split(",");
|
||||
for (String pair : keyValuePairs) {
|
||||
String[] keyValue = pair.split(":");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0].trim().replace("\"", "");
|
||||
String value = keyValue[1].trim();
|
||||
jsonObject.put(key, parseValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public void merge(Map<String,Object> ret)
|
||||
{
|
||||
data.putAll ( ret );
|
||||
}
|
||||
private static Object parseValue(String value) {
|
||||
if (value.startsWith("{") && value.endsWith("}")) {
|
||||
return parseJsonObject(value);
|
||||
} else if (value.startsWith("[") && value.endsWith("]")) {
|
||||
return parseJSONArray(value);
|
||||
} else if (value.startsWith("\"") && value.endsWith("\"")) {
|
||||
return value.substring(1, value.length() - 1);
|
||||
} else if (value.equalsIgnoreCase("true")) {
|
||||
return true;
|
||||
} else if (value.equalsIgnoreCase("false")) {
|
||||
return false;
|
||||
} else if (value.equalsIgnoreCase("null")) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
try {
|
||||
return Double.parseDouble(value);
|
||||
} catch (NumberFormatException ex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> getMap()
|
||||
{
|
||||
return new HashMap<> ( data );
|
||||
}
|
||||
private static List<Object> parseJSONArray(String jsonArray) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
jsonArray = jsonArray.trim();
|
||||
if (jsonArray.startsWith("[") && jsonArray.endsWith("]")) {
|
||||
jsonArray = jsonArray.substring(1, jsonArray.length() - 1);
|
||||
String[] elements = jsonArray.split(",");
|
||||
for (String element : elements) {
|
||||
list.add(parseValue(element.trim()));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public void add(String key, Object value) {
|
||||
if (data.containsKey(key)) {
|
||||
Object existingValue = data.get(key);
|
||||
if (existingValue instanceof List) {
|
||||
((List<Object>) existingValue).add(value);
|
||||
} else {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(existingValue);
|
||||
list.add(value);
|
||||
data.put(key, list);
|
||||
}
|
||||
} else {
|
||||
data.put(key, value);
|
||||
}
|
||||
}
|
||||
public void put(String key, Object value) {
|
||||
data.put(key, value);
|
||||
}
|
||||
|
||||
public String toJSONString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
public Object get(String key) {
|
||||
return data.get(key);
|
||||
}
|
||||
|
||||
boolean first = true;
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
if (!first) {
|
||||
sb.append(",");
|
||||
}
|
||||
first = false;
|
||||
public void merge(Map<String, Object> ret) {
|
||||
data.putAll(ret);
|
||||
}
|
||||
|
||||
sb.append("\"");
|
||||
sb.append(escape(entry.getKey()));
|
||||
sb.append("\":");
|
||||
sb.append(toJSONValue(entry.getValue()));
|
||||
}
|
||||
public Map<String, Object> getMap() {
|
||||
return new HashMap<>(data);
|
||||
}
|
||||
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
public void add(String key, Object value) {
|
||||
if (data.containsKey(key)) {
|
||||
Object existingValue = data.get(key);
|
||||
if (existingValue instanceof List) {
|
||||
((List<Object>) existingValue).add(value);
|
||||
} else {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(existingValue);
|
||||
list.add(value);
|
||||
data.put(key, list);
|
||||
}
|
||||
} else {
|
||||
data.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private String escape(String str) {
|
||||
if(str == null)return "";
|
||||
// Add necessary escape characters (e.g., double quotes, backslashes)
|
||||
// You can implement this method based on your specific requirements.
|
||||
// This is a simplified version for demonstration purposes.
|
||||
return str.replace("\"", "\\\"");
|
||||
}
|
||||
public String toJSONString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
|
||||
private String toJSONValue(Object value) {
|
||||
if (value instanceof String) {
|
||||
return "\"" + escape(value.toString()) + "\"";
|
||||
} else if (value instanceof JsonObject js) {
|
||||
return js.toJSONString();
|
||||
} else if (value instanceof List) {
|
||||
return toJSONList ( ( List<Object> ) value );
|
||||
} else if(value instanceof Map<?,?> mp )
|
||||
{
|
||||
return new JsonObject ( (Map<String, Object> ) mp ).toJSONString ();
|
||||
} else {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
boolean first = true;
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
if (!first) {
|
||||
sb.append(",");
|
||||
}
|
||||
first = false;
|
||||
|
||||
private String toJSONList(List<Object> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[");
|
||||
sb.append("\"");
|
||||
sb.append(escape(entry.getKey()));
|
||||
sb.append("\":");
|
||||
sb.append(toJSONValue(entry.getValue()));
|
||||
}
|
||||
|
||||
boolean first = true;
|
||||
for (Object item : list) {
|
||||
if (!first) {
|
||||
sb.append(",");
|
||||
}
|
||||
first = false;
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
sb.append(toJSONValue(item));
|
||||
}
|
||||
private String escape(String str) {
|
||||
if (str == null) return "";
|
||||
// Add necessary escape characters (e.g., double quotes, backslashes)
|
||||
// You can implement this method based on your specific requirements.
|
||||
// This is a simplified version for demonstration purposes.
|
||||
return str.replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
private String toJSONValue(Object value) {
|
||||
if (value instanceof String) {
|
||||
return "\"" + escape(value.toString()) + "\"";
|
||||
} else if (value instanceof JsonObject) {
|
||||
return ((JsonObject) value).toJSONString();
|
||||
} else if (value instanceof List) {
|
||||
return toJSONList((List<Object>) value);
|
||||
} else if (value instanceof Map<?, ?>) {
|
||||
return new JsonObject((Map<String, Object>) ((Map<?, ?>) value)).toJSONString();
|
||||
} else {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonObject parseJSON( InputStream inputStream) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader (inputStream));
|
||||
StringBuilder jsonString = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
jsonString.append(line);
|
||||
}
|
||||
return parseJsonObject(jsonString.toString());
|
||||
}
|
||||
private String toJSONList(List<Object> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[");
|
||||
|
||||
private static JsonObject parseJsonObject(String jsonString) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonString = jsonString.trim();
|
||||
if (jsonString.startsWith("{") && jsonString.endsWith("}")) {
|
||||
jsonString = jsonString.substring(1, jsonString.length() - 1);
|
||||
String[] keyValuePairs = jsonString.split(",");
|
||||
for (String pair : keyValuePairs) {
|
||||
String[] keyValue = pair.split(":");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0].trim().replace("\"", "");
|
||||
String value = keyValue[1].trim();
|
||||
jsonObject.put(key, parseValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
boolean first = true;
|
||||
for (Object item : list) {
|
||||
if (!first) {
|
||||
sb.append(",");
|
||||
}
|
||||
first = false;
|
||||
|
||||
private static Object parseValue(String value) {
|
||||
if (value.startsWith("{") && value.endsWith("}")) {
|
||||
return parseJsonObject(value);
|
||||
} else if (value.startsWith("[") && value.endsWith("]")) {
|
||||
return parseJSONArray(value);
|
||||
} else if (value.startsWith("\"") && value.endsWith("\"")) {
|
||||
return value.substring(1, value.length() - 1);
|
||||
} else if (value.equalsIgnoreCase("true")) {
|
||||
return true;
|
||||
} else if (value.equalsIgnoreCase("false")) {
|
||||
return false;
|
||||
} else if (value.equalsIgnoreCase("null")) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
try {
|
||||
return Double.parseDouble(value);
|
||||
} catch (NumberFormatException ex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append(toJSONValue(item));
|
||||
}
|
||||
|
||||
private static List<Object> parseJSONArray(String jsonArray) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
jsonArray = jsonArray.trim();
|
||||
if (jsonArray.startsWith("[") && jsonArray.endsWith("]")) {
|
||||
jsonArray = jsonArray.substring(1, jsonArray.length() - 1);
|
||||
String[] elements = jsonArray.split(",");
|
||||
for (String element : elements) {
|
||||
list.add(parseValue(element.trim()));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue