From a9b4d39fc00193d3b92e1cfffd047409a4cb7a05 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 7 Jul 2021 22:12:01 +0200 Subject: [PATCH] [DEV] hirarchic tree --- .../eStringSerialize/StringSerializer.java | 1 + .../exml/builder/BuilderIntrospection.java | 3 +- .../exml/builder/IntrospectionModelArray.java | 14 +--- .../exml/builder/IntrospectionProperty.java | 2 +- .../generator/GeneratorIntrospection.java | 29 +++++-- .../exml/ExmlTestIntrospectionByte.java | 78 ++++++++++++++++++- .../exml/ExmlTestIntrospectionByteNative.java | 45 ++++++++++- 7 files changed, 150 insertions(+), 22 deletions(-) diff --git a/src/org/atriasoft/eStringSerialize/StringSerializer.java b/src/org/atriasoft/eStringSerialize/StringSerializer.java index 68c3cf5..f08a15d 100644 --- a/src/org/atriasoft/eStringSerialize/StringSerializer.java +++ b/src/org/atriasoft/eStringSerialize/StringSerializer.java @@ -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; diff --git a/src/org/atriasoft/exml/builder/BuilderIntrospection.java b/src/org/atriasoft/exml/builder/BuilderIntrospection.java index 10afcd6..d2e8b82 100644 --- a/src/org/atriasoft/exml/builder/BuilderIntrospection.java +++ b/src/org/atriasoft/exml/builder/BuilderIntrospection.java @@ -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); diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelArray.java b/src/org/atriasoft/exml/builder/IntrospectionModelArray.java index 5088323..1b80541 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionModelArray.java +++ b/src/org/atriasoft/exml/builder/IntrospectionModelArray.java @@ -28,22 +28,12 @@ public class IntrospectionModelArray extends IntrospectionModel { List 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); } - 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); } diff --git a/src/org/atriasoft/exml/builder/IntrospectionProperty.java b/src/org/atriasoft/exml/builder/IntrospectionProperty.java index 5a24eb6..4cd082a 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionProperty.java +++ b/src/org/atriasoft/exml/builder/IntrospectionProperty.java @@ -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; } } diff --git a/src/org/atriasoft/exml/generator/GeneratorIntrospection.java b/src/org/atriasoft/exml/generator/GeneratorIntrospection.java index 3a18076..2fac920 100644 --- a/src/org/atriasoft/exml/generator/GeneratorIntrospection.java +++ b/src/org/atriasoft/exml/generator/GeneratorIntrospection.java @@ -216,32 +216,49 @@ public class GeneratorIntrospection implements Generator { } } else if (model.isArray()) { List baseName = model.getNodeAvaillable(); + IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType()); if (baseName == null || baseName.size() == 0) { // mode render : aaabbb - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType()); generateArrayNode(model.getClassType(), data, introspectionSub, nodeName, tmpp, indent); } else { // mode render : aaabbb + 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(""); } Log.error("lkjlk"); } else if (model.isList()) { List baseName = model.getNodeAvaillable(); + @SuppressWarnings("unchecked") + List datas = (List)data; if (baseName == null || baseName.size() == 0) { // mode render : aaabbb - @SuppressWarnings("unchecked") - List datas = (List)data; 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 : aaabbb + 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(""); } } else if (model.isEnum()) { diff --git a/test/src/test/atriasoft/exml/ExmlTestIntrospectionByte.java b/test/src/test/atriasoft/exml/ExmlTestIntrospectionByte.java index f800bbe..1e8e17b 100644 --- a/test/src/test/atriasoft/exml/ExmlTestIntrospectionByte.java +++ b/test/src/test/atriasoft/exml/ExmlTestIntrospectionByte.java @@ -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 values; + @XmlList(value="elem") + public List getValues() { + return this.values; + } + public void setValues(final List 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("\n" + + " \n" + + " 12\n" + + " -13\n" + + " 33\n" + + " 78\n" + + " -127\n" + + " \n" + + "", 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("\n" + + " \n" + + " 12\n" + + " -13\n" + + " 33\n" + + " 78\n" + + " -127\n" + + " \n" + + "", 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]); + } + } diff --git a/test/src/test/atriasoft/exml/ExmlTestIntrospectionByteNative.java b/test/src/test/atriasoft/exml/ExmlTestIntrospectionByteNative.java index 23a8252..5e2b59e 100644 --- a/test/src/test/atriasoft/exml/ExmlTestIntrospectionByteNative.java +++ b/test/src/test/atriasoft/exml/ExmlTestIntrospectionByteNative.java @@ -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 @@ -260,6 +265,44 @@ public class ExmlTestIntrospectionByteNative { Assertions.assertEquals((byte)78, root.getValues()[3]); 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("\n" + + " \n" + + " 12\n" + + " -13\n" + + " 33\n" + + " 78\n" + + " -127\n" + + " \n" + + "", 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]); + } }