[DEV] hirarchic tree

This commit is contained in:
Edouard DUPIN 2021-07-07 22:12:01 +02:00
parent bc83c265b8
commit a9b4d39fc0
7 changed files with 150 additions and 22 deletions

View File

@ -611,6 +611,7 @@ public class StringSerializer {
Converter conv = StringSerializer.VALUES_OF.get(clazz);
return conv.toString(data);
}
@Deprecated
public static String[] toStringList(final Object data) {
if (data == null) {
return null;

View File

@ -61,7 +61,8 @@ public class BuilderIntrospection implements Builder {
if (listTreeName == null) {
inferData = this.cacheModel.findOrCreate(ModelType.NORMAL, null, subTypeClass);
} else {
inferData = this.cacheModel.findOrCreate(ModelType.ARRAY, listTreeName, subTypeClass);
// when we request a list elements, we need to add the element in a list, then we need the return is LIST and not ARRAY
inferData = this.cacheModel.findOrCreate(ModelType.LIST, listTreeName, subTypeClass);
}
// Create the data when object is ended created...
return new IntrospectionObject(inferData);

View File

@ -28,22 +28,12 @@ public class IntrospectionModelArray extends IntrospectionModel {
List<Object> tmp = null;
if (this.nodeName == null) {
tmp = nodes.get(IntrospectionObject.STUPID_TOCKEN);
if (tmp == null) {
return null;
}
if (tmp.size() == 1) {
return tmp.get(0);
}
return null;
}
} else {
tmp = nodes.get(this.nodeName);
}
if (tmp == null) {
return null;
}
//return tmp;
// TODO This is not good at all this is bad ...
if (this.classType.isPrimitive()) {
return ArraysTools.listToPrimitiveAuto(this.classType, tmp);
}

View File

@ -84,7 +84,7 @@ public abstract class IntrospectionProperty {
} else {
name = name.toLowerCase();
for (final String elem : this.names) {
if (elem.toLowerCase().contentEquals(name)) {
if (elem.equalsIgnoreCase(name)) {
return true;
}
}

View File

@ -216,32 +216,49 @@ public class GeneratorIntrospection implements Generator {
}
} else if (model.isArray()) {
List<String> baseName = model.getNodeAvaillable();
IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType());
if (baseName == null || baseName.size() == 0) {
// mode render : <nodeName>aaa</nodeName><nodeName>bbb</nodeName>
IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType());
generateArrayNode(model.getClassType(), data, introspectionSub, nodeName, tmpp, indent);
} else {
// mode render : <nodeName><val>aaa</val><val>bbb</val></nodeName>
Tools.addIndent(tmpp, indent);
tmpp.append("<");
tmpp.append(nodeName);
tmpp.append(">");
generateArrayNode(model.getClassType(), data, introspectionSub, baseName.get(0), tmpp, indent+1);
Tools.addIndent(tmpp, indent);
tmpp.append("</");
tmpp.append(nodeName);
tmpp.append(">");
}
Log.error("lkjlk");
} else if (model.isList()) {
List<String> baseName = model.getNodeAvaillable();
if (baseName == null || baseName.size() == 0) {
// mode render : <nodeName>aaa</nodeName><nodeName>bbb</nodeName>
@SuppressWarnings("unchecked")
List<Object> datas = (List<Object>)data;
if (baseName == null || baseName.size() == 0) {
// mode render : <nodeName>aaa</nodeName><nodeName>bbb</nodeName>
for (Object elem : datas) {
// note the type can change in List ...
IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, elem.getClass());
generateNode(elem, introspectionSub, nodeName, tmpp, indent);
}
} else {
// mode render : <nodeName><val>aaa</val><val>bbb</val></nodeName>
Tools.addIndent(tmpp, indent);
tmpp.append("<");
tmpp.append(baseName.get(0));
tmpp.append(nodeName);
tmpp.append(">");
tmpp.append(model.toString(data));
for (Object elem : datas) {
// note the type can change in List ...
IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, elem.getClass());
generateNode(elem, introspectionSub, baseName.get(0), tmpp, indent+1);
}
//tmpp.append(model.toString(data));
Tools.addIndent(tmpp, indent);
tmpp.append("</");
tmpp.append(baseName.get(0));
tmpp.append(nodeName);
tmpp.append(">");
}
} else if (model.isEnum()) {

View File

@ -9,7 +9,7 @@ import java.util.List;
import org.atriasoft.exml.Exml;
import org.atriasoft.exml.annotation.XmlDefaultAttibute;
import org.atriasoft.exml.annotation.XmlList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -411,5 +411,81 @@ public class ExmlTestIntrospectionByte {
Assertions.assertEquals((byte)78, root.getValues().get(3));
Assertions.assertEquals((byte)-127, root.getValues().get(4));
}
public class TestListNodeByteStructuredFunc {
private List<Byte> values;
@XmlList(value="elem")
public List<Byte> getValues() {
return this.values;
}
public void setValues(final List<Byte> values) {
this.values = values;
}
}
@Test
public void testListNodeByteStructuredFunc() {
TestListNodeByteStructuredFunc elem = new TestListNodeByteStructuredFunc();
elem.setValues(List.of((byte)12, (byte)-13, (byte)33, (byte)78, (byte)-127));
StringBuilder builder = new StringBuilder();
Assertions.assertDoesNotThrow(() -> Exml.generate(elem, ExmlTestIntrospectionByte.NODE_NAME, builder));
String dataTest = builder.toString();
Log.warning("data generated: " + builder.toString());
Assertions.assertEquals("<elem>\n"
+ " <values>\n"
+ " <elem>12</elem>\n"
+ " <elem>-13</elem>\n"
+ " <elem>33</elem>\n"
+ " <elem>78</elem>\n"
+ " <elem>-127</elem>\n"
+ " </values>\n"
+ "</elem>", dataTest);
final TestListNodeByteStructuredFunc root = Assertions.assertDoesNotThrow(() -> Exml.parseOne(dataTest, TestListNodeByteStructuredFunc.class, ExmlTestIntrospectionByte.NODE_NAME));
Assertions.assertEquals(5, root.getValues().size());
Assertions.assertEquals((byte)12, root.getValues().get(0));
Assertions.assertEquals((byte)-13, root.getValues().get(1));
Assertions.assertEquals((byte)33, root.getValues().get(2));
Assertions.assertEquals((byte)78, root.getValues().get(3));
Assertions.assertEquals((byte)-127, root.getValues().get(4));
}
public class TestArrayNodeByteStructuredFunc {
private Byte[] values;
@XmlList(value="elem")
public Byte[] getValues() {
return this.values;
}
public void setValues(final Byte[] values) {
this.values = values;
}
}
@Test
public void testArrayNodeByteStructuredFunc() {
TestArrayNodeByteStructuredFunc elem = new TestArrayNodeByteStructuredFunc();
elem.setValues(new Byte[] {12, -13, 33, 78, -127});
StringBuilder builder = new StringBuilder();
Assertions.assertDoesNotThrow(() -> Exml.generate(elem, ExmlTestIntrospectionByte.NODE_NAME, builder));
String dataTest = builder.toString();
Log.warning("data generated: " + builder.toString());
Assertions.assertEquals("<elem>\n"
+ " <values>\n"
+ " <elem>12</elem>\n"
+ " <elem>-13</elem>\n"
+ " <elem>33</elem>\n"
+ " <elem>78</elem>\n"
+ " <elem>-127</elem>\n"
+ " </values>\n"
+ "</elem>", dataTest);
final TestArrayNodeByteStructuredFunc root = Assertions.assertDoesNotThrow(() -> Exml.parseOne(dataTest, TestArrayNodeByteStructuredFunc.class, ExmlTestIntrospectionByte.NODE_NAME));
Assertions.assertEquals(5, root.getValues().length);
Assertions.assertEquals((byte)12, root.getValues()[0]);
Assertions.assertEquals((byte)-13, root.getValues()[1]);
Assertions.assertEquals((byte)33, root.getValues()[2]);
Assertions.assertEquals((byte)78, root.getValues()[3]);
Assertions.assertEquals((byte)-127, root.getValues()[4]);
}
}

View File

@ -5,13 +5,18 @@
*/
package test.atriasoft.exml;
import java.util.List;
import org.atriasoft.exml.Exml;
import org.atriasoft.exml.annotation.XmlDefaultAttibute;
import org.atriasoft.exml.annotation.XmlList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import test.atriasoft.exml.ExmlTestIntrospectionByte.TestArrayNodeByteStructuredFunc;
import test.atriasoft.exml.ExmlTestIntrospectionByte.TestListNodeByteStructuredFunc;
public class ExmlTestIntrospectionByteNative {
static final String NODE_NAME = "elem";
@BeforeAll
@ -261,5 +266,43 @@ public class ExmlTestIntrospectionByteNative {
Assertions.assertEquals((byte)-127, root.getValues()[4]);
}
public class TestArrayNodeByteNativeStructuredFunc {
private byte[] values;
@XmlList(value="elem")
public byte[] getValues() {
return this.values;
}
public void setValues(final byte[] values) {
this.values = values;
}
}
@Test
public void testArrayNodeByteNativeStructuredFunc() {
TestArrayNodeByteNativeStructuredFunc elem = new TestArrayNodeByteNativeStructuredFunc();
elem.setValues(new byte[] {12, -13, 33, 78, -127});
StringBuilder builder = new StringBuilder();
Assertions.assertDoesNotThrow(() -> Exml.generate(elem, ExmlTestIntrospectionByte.NODE_NAME, builder));
String dataTest = builder.toString();
Log.warning("data generated: " + builder.toString());
Assertions.assertEquals("<elem>\n"
+ " <values>\n"
+ " <elem>12</elem>\n"
+ " <elem>-13</elem>\n"
+ " <elem>33</elem>\n"
+ " <elem>78</elem>\n"
+ " <elem>-127</elem>\n"
+ " </values>\n"
+ "</elem>", dataTest);
final TestArrayNodeByteNativeStructuredFunc root = Assertions.assertDoesNotThrow(() -> Exml.parseOne(dataTest, TestArrayNodeByteNativeStructuredFunc.class, ExmlTestIntrospectionByte.NODE_NAME));
Assertions.assertEquals(5, root.getValues().length);
Assertions.assertEquals((byte)12, root.getValues()[0]);
Assertions.assertEquals((byte)-13, root.getValues()[1]);
Assertions.assertEquals((byte)33, root.getValues()[2]);
Assertions.assertEquals((byte)78, root.getValues()[3]);
Assertions.assertEquals((byte)-127, root.getValues()[4]);
}
}