diff --git a/src/module-info.java b/src/module-info.java index b6caaf5..4d8c7d6 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -5,7 +5,6 @@ */ open module org.atriasoft.exml { - exports org.atriasoft.eStringSerialize; exports org.atriasoft.exml; exports org.atriasoft.exml.model; exports org.atriasoft.exml.exception; diff --git a/src/org/atriasoft/eStringSerialize/Converter.java b/src/org/atriasoft/eStringSerialize/Converter.java deleted file mode 100644 index ec15772..0000000 --- a/src/org/atriasoft/eStringSerialize/Converter.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.atriasoft.eStringSerialize; - -public interface Converter { - /** - * Un-serialize a String in a specified Object - * @param value String to parse - * @return Data generated or Null - */ - Object valueOf(final String value); - /** - * Serialize in a string the require data - * @param data Object to serialize - * @return The new data... - */ - String toString(final Object data); -} \ No newline at end of file diff --git a/src/org/atriasoft/eStringSerialize/StringSerializer.java b/src/org/atriasoft/eStringSerialize/StringSerializer.java deleted file mode 100644 index 84e81ca..0000000 --- a/src/org/atriasoft/eStringSerialize/StringSerializer.java +++ /dev/null @@ -1,228 +0,0 @@ -package org.atriasoft.eStringSerialize; - -import java.util.HashMap; -import java.util.Map; - -public class StringSerializer { - private static final Map, Converter> VALUES_OF = new HashMap<>(); - static { - StringSerializer.VALUES_OF.put(byte.class, new Converter() { - @Override - public String toString(final Object data) { - return Byte.toString((Byte) data); - } - - @Override - public Object valueOf(final String value) { - return Byte.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Byte.class, new Converter() { - @Override - public String toString(final Object data) { - return Byte.toString((Byte) data); - } - - @Override - public Object valueOf(final String value) { - return Byte.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(int.class, new Converter() { - @Override - public String toString(final Object data) { - return Integer.toString((Integer) data); - } - - @Override - public Object valueOf(final String value) { - return Integer.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Integer.class, new Converter() { - @Override - public String toString(final Object data) { - return Integer.toString((Integer) data); - } - - @Override - public Object valueOf(final String value) { - return Integer.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(long.class, new Converter() { - @Override - public String toString(final Object data) { - return Long.toString((Long) data); - } - - @Override - public Object valueOf(final String value) { - return Long.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Long.class, new Converter() { - @Override - public String toString(final Object data) { - return Long.toString((Long) data); - } - - @Override - public Object valueOf(final String value) { - return Long.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(short.class, new Converter() { - @Override - public String toString(final Object data) { - return Short.toString((Short) data); - } - - @Override - public Object valueOf(final String value) { - return Short.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Short.class, new Converter() { - @Override - public String toString(final Object data) { - return Short.toString((Short) data); - } - - @Override - public Object valueOf(final String value) { - return Short.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(float.class, new Converter() { - @Override - public String toString(final Object data) { - return Float.toString((Float) data); - } - - @Override - public Object valueOf(final String value) { - return Float.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Float.class, new Converter() { - @Override - public String toString(final Object data) { - return Float.toString((Float) data); - } - - @Override - public Object valueOf(final String value) { - return Float.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(double.class, new Converter() { - @Override - public String toString(final Object data) { - return Double.toString((Double) data); - } - - @Override - public Object valueOf(final String value) { - return Double.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Double.class, new Converter() { - @Override - public String toString(final Object data) { - return Double.toString((Double) data); - } - - @Override - public Object valueOf(final String value) { - return Double.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(boolean.class, new Converter() { - @Override - public String toString(final Object data) { - return Boolean.toString((Boolean) data); - } - - @Override - public Object valueOf(final String value) { - return Boolean.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(Boolean.class, new Converter() { - @Override - public String toString(final Object data) { - return Boolean.toString((Boolean) data); - } - - @Override - public Object valueOf(final String value) { - return Boolean.valueOf(value.trim()); - } - }); - StringSerializer.VALUES_OF.put(String.class, new Converter() { - @Override - public String toString(final Object data) { - return (String) data; - } - - @Override - public Object valueOf(final String value) { - return value; - } - }); - } - - public static boolean contains(final Class clazz) { - return StringSerializer.VALUES_OF.containsKey(clazz); - } - - public static String toString(final boolean data) { - return Boolean.toString(data); - } - - public static String toString(final byte data) { - return Byte.toString(data); - } - - public static String toString(final int data) { - return Integer.toString(data); - } - - public static String toString(final long data) { - return Long.toString(data); - } - - /** - * Serialize in a string the require data - * @param data Object to serialize - * @return The new data... - */ - public static String toString(final Object data) { - if (data == null) { - return null; - } - final Class clazz = data.getClass(); - final Converter conv = StringSerializer.VALUES_OF.get(clazz); - return conv.toString(data); - } - - public static String toString(final short data) { - return Short.toString(data); - } - - /** - * Un-serialize a String in a specified Object - * @param value String to parse - * @return Data generated or Null - */ - public static Object valueOf(final Class clazz, final String value) { - if (value == null) { - return null; - } - final Converter conv = StringSerializer.VALUES_OF.get(clazz); - return conv.valueOf(value); - } - - private StringSerializer() {} -} diff --git a/src/org/atriasoft/exml/Exml.java b/src/org/atriasoft/exml/Exml.java index 7f0d116..9117375 100644 --- a/src/org/atriasoft/exml/Exml.java +++ b/src/org/atriasoft/exml/Exml.java @@ -12,13 +12,14 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import org.atriasoft.aknot.exception.AknotException; import org.atriasoft.aknot.model.InterfaceFactoryAccess; +import org.atriasoft.aknot.model.ModelType; +import org.atriasoft.aknot.pojo.IntrospectionObject; import org.atriasoft.etk.Uri; import org.atriasoft.exml.builder.Builder; import org.atriasoft.exml.builder.BuilderGeneric; import org.atriasoft.exml.builder.BuilderIntrospection; -import org.atriasoft.exml.builder.IntrospectionObject; -import org.atriasoft.exml.builder.ModelType; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlException; import org.atriasoft.exml.exception.ExmlParserErrorMulti; @@ -74,7 +75,7 @@ public class Exml { // return iGenerate(_data, 0); } - public static XmlElement parse(final Path data) throws ExmlException, ExmlParserErrorMulti { + public static XmlElement parse(final Path data) throws ExmlException, ExmlParserErrorMulti, AknotException { final Builder builder = new BuilderGeneric(); final ParseXml parser = new ParseXml(builder); final ParsingProperty property = new ParsingProperty(); @@ -94,7 +95,7 @@ public class Exml { return (XmlElement) parser.parse(dataToParse, property); } - public static T[] parse(final Path path, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti { + public static T[] parse(final Path path, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti, AknotException { String content = null; try { content = Exml.readFile(path, StandardCharsets.UTF_8); @@ -104,7 +105,7 @@ public class Exml { return Exml.parse(content, classType, rootNodeName); } - public static XmlElement parse(final String data) throws ExmlException, ExmlParserErrorMulti { + public static XmlElement parse(final String data) throws ExmlException, ExmlParserErrorMulti, AknotException { final Builder builder = new BuilderGeneric(); final ParseXml parser = new ParseXml(builder); final ParsingProperty property = new ParsingProperty(); @@ -113,7 +114,7 @@ public class Exml { } @SuppressWarnings("unchecked") - public static T[] parse(final String data, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti { + public static T[] parse(final String data, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti, AknotException { Builder builder; try { builder = new BuilderIntrospection(ModelType.ARRAY, classType, rootNodeName); @@ -134,7 +135,7 @@ public class Exml { } } - public static Object[] parse(final String data, final InterfaceFactoryAccess widgetXmlFactory) throws ExmlException, ExmlParserErrorMulti { + public static Object[] parse(final String data, final InterfaceFactoryAccess widgetXmlFactory) throws ExmlException, ExmlParserErrorMulti, AknotException { Builder builder; try { builder = new BuilderIntrospection(widgetXmlFactory); @@ -176,7 +177,7 @@ public class Exml { * parse(tmpDataUnicode); //Display(); return ret; } */ - public static XmlElement parse(final Uri data) throws ExmlException { + public static XmlElement parse(final Uri data) throws ExmlException, AknotException { final Builder builder = new BuilderGeneric(); final ParseXml parser = new ParseXml(builder); final ParsingProperty property = new ParsingProperty(); @@ -190,7 +191,7 @@ public class Exml { return (XmlElement) parser.parse(dataToParse, property); } - public static T parseOne(final Path path, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti { + public static T parseOne(final Path path, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti, AknotException { final T[] elements = Exml.parse(path, classType, rootNodeName); if (elements == null || elements.length == 0) { throw new ExmlBuilderException("Error in parsing the file, no node find ..."); @@ -201,7 +202,7 @@ public class Exml { return elements[0]; } - public static T parseOne(final String data, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti { + public static T parseOne(final String data, final Class classType, final String rootNodeName) throws ExmlException, ExmlParserErrorMulti, AknotException { final T[] elements = Exml.parse(data, classType, rootNodeName); if (elements == null || elements.length == 0) { throw new ExmlBuilderException("Error in parsing the file, no node find ..."); diff --git a/src/org/atriasoft/exml/builder/Builder.java b/src/org/atriasoft/exml/builder/Builder.java index 63eeec8..b8672d6 100644 --- a/src/org/atriasoft/exml/builder/Builder.java +++ b/src/org/atriasoft/exml/builder/Builder.java @@ -5,6 +5,7 @@ */ package org.atriasoft.exml.builder; +import org.atriasoft.aknot.exception.AknotException; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlException; @@ -15,7 +16,7 @@ public interface Builder { * @param element Element that is finished * @throws ExmlBuilderException */ - void endElement(Object element) throws ExmlException; + void endElement(Object element) throws ExmlException, AknotException; /** * This is called before a text adding, in some case the subData must not be parsed, then all the subData is pack as a single string without managing the XML properties. @@ -23,7 +24,7 @@ public interface Builder { * @return true if the data must be packed. * @throws ExmlException */ - boolean isPackText(Object parent) throws ExmlException; + boolean isPackText(Object parent) throws ExmlException, AknotException; /** * New comment added on this Element @@ -31,7 +32,7 @@ public interface Builder { * @param comment Comment value * @throws ExmlBuilderException Error with this node or element. */ - void newComment(Object element, String comment) throws ExmlException; + void newComment(Object element, String comment) throws ExmlException, AknotException; /** * New comment added on this Element @@ -40,7 +41,7 @@ public interface Builder { * @return Declaration object value * @throws ExmlBuilderException Error with this node or element. */ - Object newDeclaration(Object parent, String text) throws ExmlException; + Object newDeclaration(Object parent, String text) throws ExmlException, AknotException; /** * Add a new sub-element on the current parent element: {@code} @@ -49,7 +50,7 @@ public interface Builder { * @return the object representing the Element. * @throws ExmlBuilderException Error with this node or element. */ - Object newElement(Object parent, String nodeName) throws ExmlException; + Object newElement(Object parent, String nodeName) throws ExmlException, AknotException; /** * The sub-element is finish (creation done) @@ -57,7 +58,7 @@ public interface Builder { * @param tmpname Name of the node * @param element Element builder that has been created */ - void newElementFinished(Object parent, String tmpname, Object element) throws ExmlException; + void newElementFinished(Object parent, String tmpname, Object element) throws ExmlException, AknotException; /** * Add a property on the Element. @@ -66,14 +67,14 @@ public interface Builder { * @param propertyValue Value of the property * @throws ExmlBuilderException Error with this node or element. */ - void newProperty(Object element, String propertyName, String propertyValue) throws ExmlException; + void newProperty(Object element, String propertyName, String propertyValue) throws ExmlException, AknotException; /** * Create or get the root element of the document * @return An object that id a root element. * @throws ExmlBuilderException Error with this node or element. */ - Object newRoot() throws ExmlBuilderException; + Object newRoot() throws ExmlBuilderException, AknotException; /** * Add a text value on the current Element @@ -81,6 +82,6 @@ public interface Builder { * @param text Test to add. * @throws ExmlBuilderException Error with this node or element. */ - void newText(Object parent, String text) throws ExmlException; + void newText(Object parent, String text) throws ExmlException, AknotException; } diff --git a/src/org/atriasoft/exml/builder/BuilderIntrospection.java b/src/org/atriasoft/exml/builder/BuilderIntrospection.java index 56fd11f..5911bf3 100644 --- a/src/org/atriasoft/exml/builder/BuilderIntrospection.java +++ b/src/org/atriasoft/exml/builder/BuilderIntrospection.java @@ -3,8 +3,13 @@ package org.atriasoft.exml.builder; import java.util.ArrayList; import java.util.List; +import org.atriasoft.aknot.StringSerializer; +import org.atriasoft.aknot.exception.AknotException; import org.atriasoft.aknot.model.InterfaceFactoryAccess; -import org.atriasoft.eStringSerialize.StringSerializer; +import org.atriasoft.aknot.model.IntrospectionModel; +import org.atriasoft.aknot.model.ModelType; +import org.atriasoft.aknot.pojo.CacheIntrospectionModel; +import org.atriasoft.aknot.pojo.IntrospectionObject; import org.atriasoft.exml.exception.ExmlAttributeDoesNotExist; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlException; @@ -18,7 +23,7 @@ public class BuilderIntrospection implements Builder { final String rootNodeName; final InterfaceFactoryAccess factory; - public BuilderIntrospection(final InterfaceFactoryAccess factory) throws ExmlException { + public BuilderIntrospection(final InterfaceFactoryAccess factory) throws ExmlException, AknotException { this.factory = factory; this.rootNodeName = null; this.rootClassType = null; @@ -27,7 +32,7 @@ public class BuilderIntrospection implements Builder { } } - public BuilderIntrospection(final ModelType model, final Class classType, final String rootNodeName) throws ExmlException { + public BuilderIntrospection(final ModelType model, final Class classType, final String rootNodeName) throws ExmlException, AknotException { this.factory = null; this.rootNodeName = rootNodeName; this.rootClassType = classType; @@ -35,7 +40,7 @@ public class BuilderIntrospection implements Builder { } @Override - public void endElement(final Object element) throws ExmlBuilderException { + public void endElement(final Object element) throws ExmlBuilderException, AknotException { Log.verbose("End of Element: {}", element); if (element == null) { return; @@ -63,16 +68,16 @@ public class BuilderIntrospection implements Builder { } @Override - public void newComment(final Object element, final String comment) throws ExmlException {} + public void newComment(final Object element, final String comment) {} @Override - public Object newDeclaration(final Object parent, final String text) throws ExmlException { + public Object newDeclaration(final Object parent, final String text) { // we drop all declaration, no need of it too. return null; } @Override - public Object newElement(final Object parent, final String nodeName) throws ExmlException { + public Object newElement(final Object parent, final String nodeName) throws ExmlException, AknotException { if (parent == null) { return null; } @@ -136,7 +141,7 @@ public class BuilderIntrospection implements Builder { } @Override - public void newElementFinished(final Object parent, final String tmpName, final Object element) throws ExmlException { + public void newElementFinished(final Object parent, final String tmpName, final Object element) throws ExmlException, AknotException { Log.debug("new element fionished : ==> " + tmpName); if (parent == null || element == null) { return; @@ -163,7 +168,7 @@ public class BuilderIntrospection implements Builder { } @Override - public void newProperty(final Object element, final String propertyName, final String propertyValue) throws ExmlException { + public void newProperty(final Object element, final String propertyName, final String propertyValue) throws ExmlException, AknotException { if (element == null) { return; } @@ -217,13 +222,13 @@ public class BuilderIntrospection implements Builder { } @Override - public Object newRoot() throws ExmlBuilderException { + public Object newRoot() throws ExmlBuilderException, AknotException { final IntrospectionModel inferData = this.cacheModel.findOrCreate(ModelType.ARRAY, this.rootNodeName, this.rootClassType); return new IntrospectionObject(inferData); } @Override - public void newText(final Object parent, final String text) throws ExmlException { + public void newText(final Object parent, final String text) throws ExmlException, AknotException { if (parent == null) { return; } diff --git a/src/org/atriasoft/exml/builder/CacheIntrospectionModel.java b/src/org/atriasoft/exml/builder/CacheIntrospectionModel.java deleted file mode 100644 index 15178fc..0000000 --- a/src/org/atriasoft/exml/builder/CacheIntrospectionModel.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.util.HashMap; -import java.util.Map; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public class CacheIntrospectionModel { - final Map elements = new HashMap<>(); - - IntrospectionModel findOrCreate(final ModelType model, final String name, final Class classType) throws ExmlBuilderException { - MapKey key = new MapKey(model, name, classType); - IntrospectionModel out = this.elements.get(key); - if (out != null) { - return out; - } - if (model == ModelType.ARRAY) { - out = IntrospectionModelFactory.createModelArray(key.nodeName(), key); - } else if (model == ModelType.LIST) { - out = IntrospectionModelFactory.createModelList(key.nodeName(), key); - } else if (classType.isEnum()) { - out = IntrospectionModelFactory.createModelEnum(key); - } else { - out = IntrospectionModelFactory.createModel(key); - } - this.elements.put(key, out); - return out; - } - - - public CacheIntrospectionModel() { - - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModel.java b/src/org/atriasoft/exml/builder/IntrospectionModel.java deleted file mode 100644 index 64c8049..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModel.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.util.List; -import java.util.Map; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public abstract class IntrospectionModel { - protected static final Boolean DEFAULT_ATTRIBUTE = false; - protected static final Boolean DEFAULT_IGNORE_UNBKNOWN = false; - protected static final Boolean DEFAULT_DEFAULT_NULL_VALUE = false; - protected static final Boolean DEFAULT_CASE_SENSITIVE = true; - protected static final Boolean DEFAULT_MANAGED = true; - protected static final Boolean DEFAULT_OPTIONAL = false; - - protected boolean defaultNullValue = false; - protected boolean ignoreUnknown = false; - - protected final Class classType; - - public IntrospectionModel(final Class classType) { - this.classType = classType; - } - - public Object createObject(final Map properties, final Map> nodes) throws ExmlBuilderException { - return null; - } - - public List getAttributes() { - return null; - } - - public String getBeanName(final String nodeName) { - return nodeName; - } - - public String getBeanNameModel(final String nodeName) { - return getBeanName(nodeName); - } - - public Class getClassType() { - return this.classType; - } - - public List getNodeAvaillable() { - return null; - } - - public List getNodes() { - return null; - } - - public String getTextBeanName() { - // TODO Auto-generated method stub - return null; - } - - public String getTreeNameOfSubNode(final String nodeName) throws ExmlBuilderException { - return null; - } - - public Class getTypeOfProperty(final String nodeName) throws ExmlBuilderException { - return null; - } - - public Class getTypeOfSubNode(final String nodeName) throws ExmlBuilderException { - return null; - } - - public Class getTypeOfSubNodeList(final String nodeName) throws ExmlBuilderException { - return null; - } - - public Class getTypeOfSubProperty(final String nodeName) throws ExmlBuilderException { - return null; - } - - protected Class getTypeOfText() { - return null; - } - - public Object getValue(final String propertyName, final String propertyValue) throws ExmlBuilderException { - return null; - } - - public Object getValueFromText(final String text) throws ExmlBuilderException { - return null; - } - - /** - * This permit to know if an alement in the property is able to manage the Whole text under (this remove all parsing of xml inside the model...) Annotation @XmlText - * @return true if a parameter manage the text (otherwise the text is sended to the fromString() function. - */ - public boolean hasTextModel() { - return false; - } - - public boolean isArray() { - return false; - } - - protected boolean isDefaultNullValue() { - return this.defaultNullValue; - } - - public boolean isEnum() { - return this.classType.isEnum(); - } - - protected boolean isIgnoreUnknown() { - return this.ignoreUnknown; - } - - public boolean isList() { - return false; - } - - public boolean isNative() { - return false; - } - - public boolean isObject(final String propertyName) { - return Object.class.isAssignableFrom(this.classType); - } - - public boolean isRecord(final String propertyName) { - return Record.class.isAssignableFrom(this.classType); - } - - public abstract String toString(final Object data) throws ExmlBuilderException; - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelArray.java b/src/org/atriasoft/exml/builder/IntrospectionModelArray.java deleted file mode 100644 index 62800b4..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModelArray.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.atriasoft.etk.util.ArraysTools; -import org.atriasoft.exml.exception.ExmlBuilderException; - -public class IntrospectionModelArray extends IntrospectionModel { - @SuppressWarnings("unchecked") - public static T[] convertList(final Class classType, final List data) { - final List rootList = (List) data; - final T[] strarr = (T[]) Array.newInstance(classType, 0); - return rootList.toArray(strarr); - } - - final String nodeName; - - public IntrospectionModelArray(final String nodeName, final Class classType) { - super(classType); - this.nodeName = nodeName; - } - - @Override - public Object createObject(final Map properties, final Map> nodes) throws ExmlBuilderException { - List tmp = null; - if (this.nodeName == null) { - tmp = nodes.get(IntrospectionObject.STUPID_TOCKEN); - } else { - tmp = nodes.get(this.nodeName); - } - if (tmp == null) { - return null; - } - if (this.classType.isPrimitive()) { - return ArraysTools.listToPrimitiveAuto(this.classType, tmp); - } - return IntrospectionModelArray.convertList(this.classType, tmp); - } - - @Override - public List getNodeAvaillable() { - if (this.nodeName != null) { - return Arrays.asList(this.nodeName); - } - return null; - } - - @Override - public Object getValue(final String propertyName, final String propertyValue) throws ExmlBuilderException { - return null; - } - - @Override - public Object getValueFromText(final String text) throws ExmlBuilderException { - return new Object[0]; - } - - @Override - public boolean hasTextModel() { - return false; - } - - @Override - public boolean isArray() { - return true; - } - - @Override - public String toString(final Object data) { - return null; - } - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelBaseType.java b/src/org/atriasoft/exml/builder/IntrospectionModelBaseType.java deleted file mode 100644 index a478508..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModelBaseType.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.util.List; -import java.util.Map; - -import org.atriasoft.eStringSerialize.StringSerializer; -import org.atriasoft.exml.exception.ExmlBuilderException; - - -public class IntrospectionModelBaseType extends IntrospectionModel { - - public IntrospectionModelBaseType(final Class classType) { - super(classType); - } - - @Override - public Object createObject(final Map properties, final Map> nodes) throws ExmlBuilderException { - throw new ExmlBuilderException("Base type model can not have properties and nodes ... "); - } - - @Override - public List getNodeAvaillable() { - return null; - } - - @Override - public Object getValueFromText(final String text) throws ExmlBuilderException { - //il y a un bug ici ... - return StringSerializer.valueOf(this.classType, text); - } - - @Override - public Object getValue(final String propertyName, final String propertyValue) throws ExmlBuilderException { - return null; - } - - @Override - public boolean isNative() { - return true; - } - - @Override - public String toString(final Object data) { - return StringSerializer.toString(data); - } - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java b/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java deleted file mode 100644 index 6143cc7..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java +++ /dev/null @@ -1,1081 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Parameter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.atriasoft.aknot.reflect.ReflectClass; -import org.atriasoft.aknot.reflect.ReflectTools; -import org.atriasoft.eStringSerialize.StringSerializer; -import org.atriasoft.etk.util.ArraysTools; -import org.atriasoft.exml.exception.ExmlBuilderException; -import org.atriasoft.exml.internal.Log; -import org.atriasoft.exml.parser.Tools; - -record ConstructorModel( - String[] values, - Boolean[] isAttributes, - Constructor constructor) {} - -public class IntrospectionModelComplex extends IntrospectionModel { - private final boolean isRecord; - // TODO Optimize this with external object for basic types.... - private final Method valueof; // used for the set Text if the object is an end point... - private final Method tostring; // used for the set Text if the object is an end point... - private final boolean isSubClass; // if true, the constructor must be called with a null first object. - private final Constructor constructorEmpty; - private final List constructors = new ArrayList<>(); - private final List elements = new ArrayList<>(); - - public IntrospectionModelComplex(final Class classType) throws ExmlBuilderException { - super(classType); - try { - if (classType.getNestHost() == classType) { - this.isSubClass = false; - } else if (!Modifier.isStatic(classType.getModifiers())) { - this.isSubClass = true; - } else { - this.isSubClass = false; - } - this.isRecord = Record.class.isAssignableFrom(classType); - if (classType.isPrimitive()) { - Log.critical("Detect primitive ==> impossible case !!! "); - } - // ------------------------------------------------------------------------ - // -- Parse constructor - // ------------------------------------------------------------------------ - Log.verbose("Introspect class: '" + classType.getCanonicalName() + "'"); - final Constructor[] constructors = this.classType.getConstructors(); - Log.verbose(" Constructors: (" + constructors.length + ")"); - Constructor emptyConstructorTmp = null; - for (final Constructor elem : constructors) { - Log.verbose(" Constructor ??? : {}", elem.toGenericString()); - // we does not manage private field - if (!Modifier.isPublic(elem.getModifiers())) { - continue; - } - if (elem.getParameterCount() == 0) { - emptyConstructorTmp = elem; - Log.verbose(" >>> " + elem.toGenericString()); - } else { - int offsetSubClass = 0; - if (this.isSubClass) { - offsetSubClass = 1; - } - if (elem.getParameterCount() == 1 && offsetSubClass == 1) { - emptyConstructorTmp = elem; - Log.verbose(" >>> " + elem.toGenericString()); - } else { - // Retrieve full description in constructor properties... - String[] namesBeans = ReflectTools.getNames(elem, null); - if (namesBeans == null) { - namesBeans = new String[elem.getParameterCount() - offsetSubClass]; - } else if (elem.getParameterCount() != namesBeans.length + offsetSubClass) { - throw new ExmlBuilderException("Wrong number of parameter in constructor with ne number declared in the @XmlName (for bean name)"); - } - final Boolean[] isAttributes = new Boolean[elem.getParameterCount() - offsetSubClass]; - final Boolean[] isCaseSensitives = new Boolean[elem.getParameterCount() - offsetSubClass]; - final Boolean[] isOptionals = new Boolean[elem.getParameterCount() - offsetSubClass]; - final Boolean[] isManageds = new Boolean[elem.getParameterCount() - offsetSubClass]; - - final Class[][] clazz = new Class[elem.getParameterCount() - offsetSubClass][]; - final String[][] names = new String[elem.getParameterCount() - offsetSubClass][]; - - final Parameter[] params = elem.getParameters(); - for (int iii = offsetSubClass; iii < params.length; iii++) { - final Parameter paramElem = params[iii]; - isAttributes[iii - offsetSubClass] = ReflectTools.getIsAttribute(elem, paramElem, null); - isCaseSensitives[iii - offsetSubClass] = ReflectTools.getIsCaseSensitive(elem, paramElem, null); - isOptionals[iii - offsetSubClass] = ReflectTools.getIsOptional(elem, paramElem, null); - isManageds[iii - offsetSubClass] = ReflectTools.getIsManaged(elem, paramElem, null); - final String[] namesParam = ReflectTools.getNames(elem, paramElem, null); - final Class[] types = ReflectTools.getTypeParameterfunction(elem, iii); - clazz[iii - offsetSubClass] = types; - names[iii - offsetSubClass] = namesParam; - if (namesParam != null && namesParam.length != 0) { - // TODO maybe do something id name is already set ??? - namesBeans[iii - offsetSubClass] = namesParam[0]; - } - } - if (checkIfOneIsNull(namesBeans, 0)) { - Log.verbose(" - " + elem.toGenericString()); - Log.verbose(" ==> unmanaged (missing names description: " + Arrays.toString(namesBeans) + ")"); - } else { - // check default attributes in the global list ... (do it at the end to be sure the constructor is VALID ... - for (int iii = 0; iii < namesBeans.length; iii++) { - IntrospectionProperty prop = findElement(namesBeans[iii]); - if (prop == null) { - prop = new IntrospectionProperty(namesBeans[iii], clazz[iii], names[iii]); - this.elements.add(prop); - } else { - final Class curentType = prop.getType(); - final Class curentSubType = prop.getSubType(); - if (curentType != clazz[iii][0] && curentSubType != clazz[iii][1]) { - throw new ExmlBuilderException("Set 'return type' with a different value previous=" + curentType.getCanonicalName() + " / " + curentSubType.getCanonicalName() - + " ==> new=" + clazz[iii][0] + " / " + clazz[iii][1] + " in " + elem.toGenericString()); - } - final String[] names1 = names[iii]; - if (names1 != null) { - final String[] curentValue = prop.getNames(); - if (curentValue != null) { - // TODO maybe set the value permissive if no change !!! like the others... - throw new ExmlBuilderException("Set 'names' with a (already set!) " + elem.toGenericString()); - } - prop.setNames(names1); - } - } - // Set settable by the constructor - prop.setCanBeSetByConstructor(true); - final Boolean isAttribute = isAttributes[iii]; - if (isAttribute != null) { - final Boolean curentValue = prop.isAttribute(); - if (curentValue != null && curentValue != isAttribute) { - throw new ExmlBuilderException("Set 'attribute' with a different value previous=" + curentValue + " ==> new=" + isAttribute + " in " + elem.toGenericString()); - } - prop.setAttribute(isAttribute); - } - final Boolean isCaseSensitive = isCaseSensitives[iii]; - if (isCaseSensitive != null) { - final Boolean curentValue = prop.isCaseSensitive(); - if (curentValue != null && curentValue != isCaseSensitive) { - throw new ExmlBuilderException( - "Set 'caseSensitive' with a different value previous=" + curentValue + " ==> new=" + isCaseSensitive + " in " + elem.toGenericString()); - } - prop.setCaseSensitive(isCaseSensitive); - } - final Boolean isOptional = isOptionals[iii]; - if (isOptional != null) { - final Boolean curentValue = prop.isOptionnal(); - if (curentValue != null && curentValue != isOptional) { - throw new ExmlBuilderException("Set 'optionnal' with a different value previous=" + curentValue + " ==> new=" + isOptional + " in " + elem.toGenericString()); - } - prop.setOptionnal(isOptional); - } - final Boolean isManaged = isManageds[iii]; - if (isManaged != null) { - final Boolean curentValue = prop.isManaged(); - if (curentValue != null && curentValue != isManaged) { - throw new ExmlBuilderException("Set 'managed' with a different value previous=" + curentValue + " ==> new=" + isManaged + " in " + elem.toGenericString()); - } - prop.setManaged(isManaged); - } - } - this.constructors.add(new ConstructorModel(namesBeans, isAttributes, elem)); - } - } - } - } - this.constructorEmpty = emptyConstructorTmp; - Log.verbose(" ==> constructor = {}", this.constructorEmpty); - // Order the constructor from the bigger number of element to the lowest... - Collections.sort(this.constructors, (a, b) -> a.values().length - b.values().length); - for (final ConstructorModel elem : this.constructors) { - Log.verbose(" * " + elem.constructor().toGenericString()); - final StringBuilder tmpPrint = new StringBuilder(" ==> ("); - if (this.isSubClass) { - tmpPrint.append("null, "); - } - for (int iii = 0; iii < elem.values().length; iii++) { - if (iii != 0) { - tmpPrint.append(", "); - } - tmpPrint.append(elem.values()[iii]); - } - tmpPrint.append(")"); - Log.verbose(tmpPrint.toString()); - } - final List recordAllPossibleValues = new ArrayList<>(); - - if (this.isRecord) { - for (final ConstructorModel elem : this.constructors) { - for (int iii = 0; iii < elem.values().length; iii++) { - final String tmpp = elem.values()[iii]; - if (!recordAllPossibleValues.contains(tmpp)) { - recordAllPossibleValues.add(tmpp); - } - } - } - } - // ------------------------------------------------------------------------ - // -- Parse Field - // ------------------------------------------------------------------------ - final Field[] fields = this.classType.getFields(); - Log.verbose(" Fields: (" + fields.length + ")"); - for (final Field elem : fields) { - // we does not manage static field - if (Modifier.isStatic(elem.getModifiers())) { - continue; - } - // we does not manage private field - // NOTE: if the field is private I do not check the elements ==> the user want a private API !!! (maybe change ...) - if (!Modifier.isPublic(elem.getModifiers())) { - continue; - } - final String[] names = ReflectTools.getNames(elem, null); - final Class[] types = ReflectTools.getTypeField(elem); - - IntrospectionProperty prop = findElement(elem.getName()); - if (prop == null) { - prop = new IntrospectionProperty(elem.getName(), types, names); - this.elements.add(prop); - } else { - final Class curentType = prop.getType(); - final Class curentSubType = prop.getSubType(); - if (curentType != types[0] && curentSubType != types[1]) { - throw new ExmlBuilderException("Set 'return type' with a different value previous=" + curentType.getCanonicalName() + " / " + curentSubType.getCanonicalName() + " ==> new=" - + types[0] + " / " + types[1] + " in " + elem.toGenericString()); - } - final String[] names1 = names; - if (names1 != null) { - final String[] curentValue = prop.getNames(); - if (curentValue != null) { - // TODO maybe set the value permissive if no change !!! like the others... - throw new ExmlBuilderException("Set 'names' with a (already set!) " + elem.toGenericString()); - } - prop.setNames(names1); - } - } - - final String listName = ReflectTools.getListName(elem, null); - if (listName != null) { - prop.setListName(listName); - } - final Boolean isAttribute = ReflectTools.getIsAttribute(elem, null); - if (isAttribute != null) { - prop.setAttribute(isAttribute); - } - final Boolean isManaged = ReflectTools.getIsManaged(elem, null); - if (isManaged != null) { - prop.setManaged(isManaged); - } - final Boolean isOptionnal = ReflectTools.getIsOptional(elem, null); - if (isOptionnal != null) { - prop.setOptionnal(isOptionnal); - } - final Boolean isCaseSensitive = ReflectTools.getIsCaseSensitive(elem, null); - if (isCaseSensitive != null) { - prop.setCaseSensitive(isCaseSensitive); - } - final Boolean isText = ReflectTools.getIsText(elem, null); - if (isText != null) { - prop.setTextMode(isText); - } - final Class factory = ReflectTools.getFactory(elem); - if (factory != null) { - prop.setFactory(factory); - } - // generate getter and setter with field. - - final IntrospectionPropertyField modifier = new IntrospectionPropertyField(elem); - // if the field is final ==> we can not set the value... - if (Modifier.isFinal(elem.getModifiers())) { - prop.setGetter(modifier); - } else { - prop.setSetter(modifier); - prop.setGetter(modifier); - } - Log.verbose(" - " + elem.toGenericString()); - } - - final List methods = ReflectClass.getFilterGenericFucntion(this.classType, recordAllPossibleValues, true, true, true); - - Log.verbose(" Methods: (" + methods.size() + ")"); - for (final Method elem : methods) { - Log.verbose(" - " + elem.toGenericString()); - } - - // Separate the methods and filer as: - // - XXX GetXxx(); & XXX != boolean - // - void setXxx(XXX elem); - // - [bB]oolean isXxx(); - // for records: - // - xxx(); - - final List methodsGet = ReflectClass.extractGetMethod(classType, methods, recordAllPossibleValues); - final List methodsSet = ReflectClass.extractSetMethod(classType, methods); - final List methodsIs = ReflectClass.extractIsMethod(classType, methods); - this.valueof = ReflectClass.extractValueOf(methods); - this.tostring = ReflectClass.extractToString(methods); - // associate methods by pair. - for (final Method method : methodsGet) { - final String name = Tools.decapitalizeFirst(this.isRecord ? method.getName() : method.getName().substring(3)); - final IntrospectionProperty prop = updateForMethod(name, method, ReflectTools.getTypeReturnFunction(method)); - // generate getter and setter with field. - final IntrospectionPropertyMethodGetter modifier = new IntrospectionPropertyMethodGetter(method); - prop.setGetter(modifier); - } - if (!this.isRecord) { - for (final Method method : methodsIs) { - final String name = Tools.decapitalizeFirst(method.getName().substring(2)); - final IntrospectionProperty prop = updateForMethod(name, method, ReflectTools.getTypeReturnFunction(method)); - // generate getter and setter with field. - final IntrospectionPropertyMethodGetter modifier = new IntrospectionPropertyMethodGetter(method); - prop.setGetter(modifier); - } - for (final Method method : methodsSet) { - final String name = Tools.decapitalizeFirst(method.getName().substring(3)); - final IntrospectionProperty prop = updateForMethod(name, method, ReflectTools.getTypeParameterfunction(method)); - // generate getter and setter with field. - final IntrospectionPropertyMethodSetter modifier = new IntrospectionPropertyMethodSetter(method); - prop.setSetter(modifier); - } - } - this.ignoreUnknown = ReflectTools.getIsIgnoreUnknown(classType, IntrospectionModel.DEFAULT_IGNORE_UNBKNOWN); - - this.defaultNullValue = ReflectTools.getIsDefaultNullValue(classType, IntrospectionModel.DEFAULT_DEFAULT_NULL_VALUE); - - // Set only at the end ==> no need before... - final Boolean isDefaultAttribute = ReflectTools.getIsDefaultAttribute(classType, IntrospectionModel.DEFAULT_ATTRIBUTE); - for (final IntrospectionProperty prop : this.elements) { - if (prop.isAttribute() == null) { - prop.setAttribute(isDefaultAttribute); - } - } - final Boolean isDefaultManaged = ReflectTools.getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED); - for (final IntrospectionProperty prop : this.elements) { - if (prop.isManaged() == null) { - prop.setManaged(isDefaultManaged); - } - } - final Boolean isDefaultOptional = ReflectTools.getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL); - for (final IntrospectionProperty prop : this.elements) { - if (prop.isOptionnal() == null) { - prop.setOptionnal(isDefaultOptional); - } - } - final Boolean isDefaultCaseSensitive = ReflectTools.getIsDefaultCaseSensitive(classType, IntrospectionModel.DEFAULT_CASE_SENSITIVE); - for (final IntrospectionProperty prop : this.elements) { - if (prop.isCaseSensitive() == null) { - prop.setCaseSensitive(isDefaultCaseSensitive); - } - } - // set default name in the list: - for (final IntrospectionProperty prop : this.elements) { - if (prop.getNames() == null) { - prop.setNames(new String[] { prop.getBeanName() }); - } - } - - } catch (final Exception ex) { - ex.printStackTrace(); - throw new ExmlBuilderException("Error in creating introspection data ... " + ex.getMessage()); - } - - // Sort the parameters to generate all time the same XML.. - Collections.sort(this.elements, (a, b) -> a.getNames()[0].compareTo(b.getNames()[0])); - - for (final IntrospectionProperty prop : this.elements) { - Log.verbose("Property/node : " + prop.getBeanName()); - Log.verbose(" names: " + Arrays.toString(prop.getNames())); - Log.verbose(" list: " + prop.getListName()); - Log.verbose(" managed: " + prop.isManaged()); - Log.verbose(" attribute: " + prop.isAttribute()); - Log.verbose(" text: " + prop.isText()); - Log.verbose(" case-sensitive: " + prop.isCaseSensitive()); - Log.verbose(" optionnal: " + prop.isOptionnal()); - Log.verbose(" constructor: " + prop.isCanBeSetByConstructor()); - Log.verbose(" get/set: " + prop.canGetValue() + " / " + prop.canSetValue()); - Log.verbose(" type: " + prop.getType().getCanonicalName()); - if (prop.getSubType() != null) { - Log.verbose(" sub-type: " + prop.getSubType().getCanonicalName()); - } else { - Log.verbose(" sub-type: null"); - } - - } - } - - @SuppressWarnings("unchecked") - private T[] autoCast(final Class clazz, final List data) { - final T[] out = (T[]) java.lang.reflect.Array.newInstance(clazz, data.size());// T[data.size()]; - - for (int iii = 0; iii < data.size(); iii++) { - out[iii] = (T) data.get(iii); - } - return out; - //return data.stream().map(clazz::cast).toArray(new T[data.size()]);//java.lang.reflect.Array.newInstance((propMethode.getSubType(), tmpp.size())); - } - - private boolean checkIdenticalArray(final String[] valA, final String[] valB) { - if (valA == valB) { - return true; - } - if (valA.length != valB.length) { - return false; - } - for (int iii = 0; iii < valA.length; iii++) { - if (!valA[iii].equals(valB[iii])) { - return false; - } - } - return true; - } - - // private boolean checkIfOneIsNull(Boolean[] values, int offset) { - // for (int iii=offset; iii properties, final Map> nodes) throws ExmlBuilderException { - Object tmp = null; - // STEP 1: try to create the object with provided parameter (if a constructor exist....) - if (!this.constructors.isEmpty()) { - Object[] inputs = null; - ConstructorModel betterModel = null; - int lastEmpty = Integer.MAX_VALUE; - // try to find the constructor that fit with parameters ... - for (final ConstructorModel elem : this.constructors) { - final int offset = this.isSubClass ? 1 : 0; - final Object[] inputsTmp = new Object[elem.values().length + offset]; - inputsTmp[0] = null; - int empty = 0; - for (int iii = 0; iii < elem.values().length; iii++) { - Object valueToInject = properties.get(elem.values()[iii]); - if (valueToInject == null) { - final List tmppp = nodes.get(elem.values()[iii]); - if (tmppp != null) { - if (List.class == findBeanNodeDescription(elem.values()[iii]).getType()) { - valueToInject = tmppp; - } else if (tmppp.size() >= 1) { - valueToInject = tmppp.get(0); - } - } - } - if (valueToInject == null) { - empty++; - inputsTmp[iii + offset] = null; - continue; - } - inputsTmp[iii + offset] = valueToInject; - } - if (lastEmpty >= empty && (inputs == null || inputs.length < inputsTmp.length)) { - inputs = inputsTmp; - betterModel = elem; - lastEmpty = empty; - } - } - if (betterModel != null) { - if (isDefaultNullValue() || lastEmpty == 0) { - final ConstructorModel elem = betterModel; - if (inputs != null) { - // here we find our constructor... - try { - tmp = switch (inputs.length) { - case 0 -> elem.constructor().newInstance(); - case 1 -> elem.constructor().newInstance(inputs[0]); - case 2 -> elem.constructor().newInstance(inputs[0], inputs[1]); - case 3 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2]); - case 4 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3]); - case 5 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]); - case 6 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5]); - case 7 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6]); - case 8 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7]); - case 9 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8]); - case 10 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9]); - case 11 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10]); - case 12 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11]); - case 13 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12]); - case 14 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13]); - case 15 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14]); - case 16 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15]); - case 17 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15], inputs[16]); - case 18 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15], inputs[16], inputs[17]); - case 19 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15], inputs[16], inputs[17], inputs[18]); - case 20 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15], inputs[16], inputs[17], inputs[18], inputs[19]); - case 21 -> elem.constructor().newInstance(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], inputs[5], inputs[6], inputs[7], inputs[8], inputs[9], inputs[10], - inputs[11], inputs[12], inputs[13], inputs[14], inputs[15], inputs[16], inputs[17], inputs[18], inputs[19], inputs[20]); - default -> throw new ExmlBuilderException("to much parameter in the constructor... " + inputs.length); - }; - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new ExmlBuilderException("Error when creating the Object ..." + this.classType.getCanonicalName()); - } - // Remove all previously added parameters - for (int iii = 0; iii < elem.values().length; iii++) { - properties.remove(elem.values()[iii]); - nodes.remove(elem.values()[iii]); - } - } - } - } - } - // STEP 2: If we do not create the object ==> try with empty constructor... - if (tmp == null) { - if (this.constructorEmpty == null) { - throw new ExmlBuilderException("No constructor accessible for class: " + this.classType.getCanonicalName()); - } - try { - Log.verbose("create class : {} with subClass={}", this.classType.getCanonicalName(), this.isSubClass); - Log.verbose(" ==> constructor = {}", this.constructorEmpty); - if (this.isSubClass) { - final Object tmp2 = null; - tmp = this.constructorEmpty.newInstance(tmp2); - } else { - tmp = this.constructorEmpty.newInstance(); - } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { - e.printStackTrace(); - return null; - } - } - // STEP 3: set the rest if the parameters... - for (final Entry elem : properties.entrySet()) { - setValue(tmp, elem.getKey(), elem.getValue()); - } - for (final Entry> elem : nodes.entrySet()) { - setValue(tmp, elem.getKey(), elem.getValue()); - } - return tmp; - } - - protected IntrospectionProperty findBeanNodeDescription(final String propertyBeanName) throws ExmlBuilderException { - for (final IntrospectionProperty prop : this.elements) { - if (prop.isAttribute()) { - continue; - } - if (prop.getBeanName().equals(propertyBeanName)) { - return prop; - } - } - return null; - } - - protected IntrospectionProperty findBeanPropertyDescription(final String propertyBeanName) throws ExmlBuilderException { - for (final IntrospectionProperty prop : this.elements) { - if (!prop.isAttribute()) { - continue; - } - if (prop.getBeanName().equals(propertyBeanName)) { - return prop; - } - } - return null; - } - - protected IntrospectionProperty findElement(final String beanName) { - for (final IntrospectionProperty elem : this.elements) { - if (elem.getBeanName().equals(beanName)) { - return elem; - } - } - return null; - } - - protected IntrospectionProperty findNodeDescription(final String propertyName) throws ExmlBuilderException { - for (final IntrospectionProperty prop : this.elements) { - if (prop.isAttribute()) { - continue; - } - if (prop.isCompatible(propertyName)) { - return prop; - } - } - return null; - } - - protected IntrospectionProperty findPropertyDescription(final String propertyName) throws ExmlBuilderException { - for (final IntrospectionProperty prop : this.elements) { - if (!prop.isAttribute()) { - continue; - } - if (prop.isCompatible(propertyName)) { - return prop; - } - } - return null; - } - - @Override - public List getAttributes() { - final List out = new ArrayList<>(); - for (final IntrospectionProperty elem : this.elements) { - if (elem.isAttribute()) { - out.add(elem); - } - } - return out; - } - - @Override - public String getBeanName(final String nodeName) { - for (final IntrospectionProperty elem : this.elements) { - if (elem.isCompatible(nodeName)) { - return elem.getBeanName(); - } - } - return null; - } - - @Override - public String getBeanNameModel(final String nodeName) { - for (final IntrospectionProperty elem : this.elements) { - if (elem.isCompatible(nodeName)) { - if (elem.hasFactory()) { - return elem.getBeanName() + "#" + nodeName; - } - return elem.getBeanName(); - } - } - return null; - } - - @Override - public List getNodeAvaillable() { - final List out = new ArrayList<>(); - for (final IntrospectionProperty prop : this.elements) { - if (prop.isAttribute()) { - continue; - } - out.add(prop.getNames()[0]); - } - return out; - } - - @Override - public List getNodes() { - final List out = new ArrayList<>(); - for (final IntrospectionProperty elem : this.elements) { - if (!elem.isAttribute()) { - out.add(elem); - } - } - return out; - } - - @Override - public String getTextBeanName() { - for (final IntrospectionProperty prop : this.elements) { - final Boolean isText = prop.isText(); - if (isText != null && isText) { - return prop.getBeanName(); - } - } - return null; - } - - @Override - public String getTreeNameOfSubNode(final String nodeBeanName) throws ExmlBuilderException { - Log.debug(" nodeType='" + nodeBeanName + "'"); - final IntrospectionProperty propMethode = findBeanNodeDescription(nodeBeanName); - if (propMethode != null && propMethode.canSetValue()) { - Log.debug(" ==> find '" + propMethode.getNames()); - return propMethode.getListName(); - } - throw new ExmlBuilderException("can not find the field '" + nodeBeanName + "'"); - } - - @Override - public Class getTypeOfProperty(final String nodeName) throws ExmlBuilderException { - Log.debug("nodeType='" + nodeName + "'"); - final IntrospectionProperty propField = findPropertyDescription(nodeName); - if (propField != null && propField.canSetValue()) { - Log.debug(" ==> find '" + propField.getNames()); - return propField.getType(); - } - - throw new ExmlBuilderException("can not find the field '" + nodeName + "' available: " + getNodeAvaillable()); - } - - /** - * Detect a subNode, and ask the type of the node at the parent Class - * @param nodeBeanName Name of the node (bean name access ==> not the XML name) - * @return Class of the node to create - */ - @Override - public Class getTypeOfSubNode(final String nodeBeanNames) throws ExmlBuilderException { - final String[] elemstNames = nodeBeanNames.split("#"); - final String nodeBeanName = elemstNames[0]; - Log.debug(" nodeType='" + nodeBeanName + "'"); - final IntrospectionProperty propMethode = findBeanNodeDescription(nodeBeanName); - if (propMethode != null && propMethode.canSetValue()) { - Log.debug(" ==> find '" + propMethode.getNames()); - if (propMethode.hasFactory()) { - return propMethode.getCompatible(elemstNames[1]); - } else { - return propMethode.getType(); - } - } - throw new ExmlBuilderException("can not find the field '" + nodeBeanName + "' available: " + getNodeAvaillable()); - } - - @Override - public Class getTypeOfSubNodeList(final String nodeBeanNames) throws ExmlBuilderException { - final String[] elemstNames = nodeBeanNames.split("#"); - final String nodeBeanName = elemstNames[0]; - Log.debug(" nodeType='" + nodeBeanName + "'"); - final IntrospectionProperty propMethode = findBeanNodeDescription(nodeBeanName); - if (propMethode != null && propMethode.canSetValue()) { - Log.debug(" ==> find '" + propMethode.getNames()); - if (propMethode.hasFactory()) { - return propMethode.getCompatible(elemstNames[1]); - } else { - return propMethode.getSubType(); - } - } - throw new ExmlBuilderException("can not find the field '" + nodeBeanName + "' available: " + getNodeAvaillable()); - } - - @Override - public Class getTypeOfSubProperty(final String nodeName) throws ExmlBuilderException { - Log.debug(" nodeType='" + nodeName + "'"); - final IntrospectionProperty propField = findPropertyDescription(nodeName); - if (propField != null && propField.canSetValue()) { - Log.debug(" ==> find '" + propField.getNames()); - return propField.getSubType(); - } - throw new ExmlBuilderException("can not find the field '" + nodeName + "' available: " + getNodeAvaillable()); - } - - @Override - public Class getTypeOfText() { - for (final IntrospectionProperty prop : this.elements) { - final Boolean isText = prop.isText(); - if (isText != null && isText) { - return prop.getType(); - } - } - return null; - } - - @Override - public Object getValue(final String propertyName, final String propertyValue) throws ExmlBuilderException { - Log.debug(" propertyName='" + propertyName + "' propertyValue='" + propertyValue + "' "); - // by default use setter to set the property - final IntrospectionProperty propMethode = findNodeDescription(propertyName); - if (propMethode != null && propMethode.canSetValue()) { - Log.verbose(" ==> find '" + propMethode.getNames()); - return propMethode.createValue(propertyValue); - } - // try with direct field - final IntrospectionProperty propField = findPropertyDescription(propertyName); - if (propField != null && propField.canSetValue()) { - Log.verbose(" ==> find '" + propField.getNames()); - return propField.createValue(propertyValue); - } - throw new ExmlBuilderException("can not find the field '" + propertyName + "'"); - } - - @Override - public Object getValueFromText(final String text) throws ExmlBuilderException { - // Note if the type is an Array<>() or a List<>() ==> we parse element by element ... then we need to keep the undertype... - Class classTypeLocal = this.classType; - Log.debug("======>>>>>>> Get input type : " + this.classType.getCanonicalName()); - //Log.debug("======>>>>>>> Get input component type : " + this.classType.getComponentType().getCanonicalName()); - if (this.classType.isArray()) { - // generic array ... - classTypeLocal = this.classType.getComponentType(); - } else if (List.class == this.classType || Collection.class.isAssignableFrom(this.classType)) { - // a generic list .... - /*if (this.subClassType != null) { - classTypeLocal = this.subClassType; - }*/ - //Type[] tmpp = this.classType.getGenericInterfaces(); - //Class[] tmpp = this.classType.getNestMembers(); - //Class[] tmpp = this.classType.getClasses(); - //Class[] tmpp = this.classType.getDeclaredClasses(); - //Class[] tmpp = this.classType.getInterfaces(); - //Class tmpp = this.classType.getDeclaringClass(); - //TypeVariable[] tmpp = this.classType.getTypeParameters(); - /* - Class persistentClass =((ParameterizedType)classType.getGenericSuperclass()).getActualTypeArguments()[0]; - */ - //Type tmpp = classType.getGenericSuperclass(); - // Class tmpp = classType.getInterfaces(); - // Log.warning("======>>>>>>> Find List '" + tmpp + "'"); - // for (int iii = 0; iii < tmpp.length; iii++) { - // Log.warning(" - " + tmpp[iii]); - // } - } - Log.debug("======>>>>>>> subElement input type : " + classTypeLocal.getCanonicalName()); - - if (this.valueof == null) { - if (StringSerializer.contains(classTypeLocal)) { - throw new ExmlBuilderException("function 'valueOf' for '" + classTypeLocal.getCanonicalName() + "' is not defined and not registered for specific type"); - } - return StringSerializer.valueOf(classTypeLocal, text); - } - try { - return this.valueof.invoke(null, text); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - if (Enum.class.isAssignableFrom(this.classType)) { - throw new ExmlBuilderException("Error in call 'valueOf(String ...)' for enum '" + classTypeLocal.getCanonicalName() + "' ==> '" + text + "' ... availlable list: " - + Arrays.asList(this.classType.getEnumConstants())); - } - e.printStackTrace(); - throw new ExmlBuilderException("Error in call 'valueOf(String ...)' for '" + classTypeLocal.getCanonicalName() + "' " + e.getMessage()); - } - } - - @Override - public boolean hasTextModel() { - Log.verbose("in {}", this.classType.getCanonicalName()); - for (final IntrospectionProperty prop : this.elements) { - Log.warning(" check {}, manage={} cas setValue={} isText={}", prop.getBeanName(), prop.isManaged(), prop.canSetValue(), prop.isText()); - final Boolean isText = prop.isText(); - if (isText != null && isText) { - return true; - } - } - return false; - } - - @Override - public boolean isNative() { - return false; - } - - private void setValue(final Object data, final String beanName, final Object value) throws ExmlBuilderException { - Log.verbose(" Set value ='" + beanName + "' propertyValue='" + value + "' " + value.getClass().getCanonicalName()); - { - // by default use setter to set the property - final IntrospectionProperty propMethode = findBeanNodeDescription(beanName); - if (propMethode != null && propMethode.canSetValue()) { - Log.verbose(" ==> find '" + Arrays.toString(propMethode.getNames()) + " type=" + propMethode.getType() + " sub-type=" + propMethode.getSubType()); - if (propMethode.getType().isAssignableFrom(value.getClass())) { - propMethode.setExistingValue(data, value); - } else if (value instanceof List) { - @SuppressWarnings("unchecked") - final List tmpp = (List) value; - if (propMethode.getType().isArray()) { - if (propMethode.getType().componentType().isPrimitive()) { - final Object newData = ArraysTools.listToPrimitiveAuto(propMethode.getType().componentType(), tmpp); - propMethode.setExistingValue(data, newData); - } else { - Log.verbose(" datas type: " + autoCast(propMethode.getType().componentType(), tmpp).getClass().getCanonicalName()); - Log.verbose(" methode type: " + propMethode.getType().getCanonicalName()); - propMethode.setExistingValue(data, autoCast(propMethode.getType().componentType(), tmpp)); - } - } else if (tmpp.size() == 1) { - propMethode.setExistingValue(data, tmpp.get(0)); - } else { - // impossible case ... - } - } else if (propMethode.getType() == byte.class) { - final byte dataPrimitive = (Byte) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == short.class) { - final short dataPrimitive = (Short) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == int.class) { - final int dataPrimitive = (Integer) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == long.class) { - final long dataPrimitive = (Long) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == boolean.class) { - final boolean dataPrimitive = (Boolean) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == float.class) { - final float dataPrimitive = (Float) value; - propMethode.setExistingValue(data, dataPrimitive); - } else if (propMethode.getType() == double.class) { - final double dataPrimitive = (Double) value; - propMethode.setExistingValue(data, dataPrimitive); - } else { - - } - return; - } - } - // try with direct field - { - final IntrospectionProperty propField = findBeanPropertyDescription(beanName); - if (propField != null && propField.canSetValue()) { - Log.verbose(" ==> find '" + Arrays.toString(propField.getNames()) + " type=" + propField.getType() + " sub-type=" + propField.getSubType()); - if (propField.getType().isAssignableFrom(value.getClass())) { - propField.setExistingValue(data, value); - // Some specific case for primitives values - } else if (propField.getType() == byte.class) { - final byte dataPrimitive = (Byte) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == short.class) { - final short dataPrimitive = (Short) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == int.class) { - final int dataPrimitive = (Integer) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == long.class) { - final long dataPrimitive = (Long) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == float.class) { - final float dataPrimitive = (Float) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == double.class) { - final Double dataPrimitive = (Double) value; - propField.setExistingValue(data, dataPrimitive); - } else if (propField.getType() == boolean.class) { - final boolean dataPrimitive = (Boolean) value; - propField.setExistingValue(data, dataPrimitive); - } else { - @SuppressWarnings("unchecked") - final List tmpp = (List) value; - if (propField.getType().isArray()) { - if (propField.getType().componentType() == byte.class) { - final byte[] datas = ArraysTools.listByteToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == short.class) { - final short[] datas = ArraysTools.listShortToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == int.class) { - final int[] datas = ArraysTools.listIntegerToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == long.class) { - final long[] datas = ArraysTools.listLongToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == boolean.class) { - final boolean[] datas = ArraysTools.listBooleanToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == float.class) { - final float[] datas = ArraysTools.listFloatToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else if (propField.getType().componentType() == double.class) { - final double[] datas = ArraysTools.listDoubleToPrimitive(tmpp); - propField.setExistingValue(data, datas); - } else { - Log.verbose(" datas type: " + autoCast(propField.getType().componentType(), tmpp).getClass().getCanonicalName()); - Log.verbose(" methode type: " + propField.getType().getCanonicalName()); - propField.setExistingValue(data, autoCast(propField.getType().componentType(), tmpp)); - } - } else if (tmpp.size() == 1) { - propField.setExistingValue(data, tmpp.get(0)); - } else { - // impossible case ... - } - } - return; - } - } - throw new ExmlBuilderException("can not find the field '" + beanName + "'"); - } - - @Override - public String toString(final Object data) throws ExmlBuilderException { - if (this.tostring == null) { - if (StringSerializer.contains(this.classType)) { - throw new ExmlBuilderException("function 'toString' for '" + this.classType.getCanonicalName() + "' is not defined and not registered for specific type"); - } - return StringSerializer.toString(data); - } - try { - return (String) this.tostring.invoke(data); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - if (Enum.class.isAssignableFrom(this.classType)) { - throw new ExmlBuilderException( - "Error in call 'toString()' for '" + this.classType.getCanonicalName() + "' ==> '????' ... availlable list: " + Arrays.asList(this.classType.getEnumConstants())); - } - e.printStackTrace(); - throw new ExmlBuilderException("Error in call 'toString()' for '" + this.classType.getCanonicalName() + "' " + e.getMessage()); - } - } - - private IntrospectionProperty updateForMethod(final String name, final Method method, final Class[] types) throws Exception { - IntrospectionProperty prop = findElement(name); - if (prop == null) { - final String[] names = ReflectTools.getNames(method, null); - prop = new IntrospectionProperty(name, types, names); - this.elements.add(prop); - } else { - final Class curentType = prop.getType(); - final Class curentSubType = prop.getSubType(); - if (curentType != types[0] && curentSubType != types[1]) { - throw new ExmlBuilderException("Set 'return type' with a different value previous=" + curentType.getCanonicalName() + " / " + curentSubType.getCanonicalName() + " ==> new=" + types[0] - + " / " + types[1] + " in " + method.toGenericString()); - } - final String[] names = ReflectTools.getNames(method, null); - if (names != null) { - final String[] curentValue = prop.getNames(); - if (curentValue != null && !checkIdenticalArray(curentValue, names)) { - // TODO maybe set the value permissive if no change !!! like the others... - throw new ExmlBuilderException("Set 'names' with a (already set!) " + method.toGenericString()); - } - prop.setNames(names); - } - } - final String listName = ReflectTools.getListName(method, null); - if (listName != null) { - final String curentValue = prop.getListName(); - if (curentValue != null && curentValue != listName) { - throw new ExmlBuilderException("Set 'listNAme' with a different value previous=" + curentValue + " ==> new=" + listName + " in " + method.toGenericString()); - } - prop.setListName(listName); - } - final Boolean isAttribute = ReflectTools.getIsAttribute(method, null); - if (isAttribute != null) { - final Boolean curentValue = prop.isAttribute(); - if (curentValue != null && curentValue != isAttribute) { - throw new ExmlBuilderException("Set 'attribute' with a different value previous=" + curentValue + " ==> new=" + isAttribute + " in " + method.toGenericString()); - } - prop.setAttribute(isAttribute); - } - final Boolean isManaged = ReflectTools.getIsManaged(method, null); - if (isManaged != null) { - final Boolean curentValue = prop.isManaged(); - if (curentValue != null && curentValue != isManaged) { - throw new ExmlBuilderException("Set 'managed' with a different value previous=" + curentValue + " ==> new=" + isManaged + " in " + method.toGenericString()); - } - prop.setManaged(isManaged); - } - final Boolean isOptionnal = ReflectTools.getIsOptional(method, null); - if (isOptionnal != null) { - final Boolean curentValue = prop.isOptionnal(); - if (curentValue != null && curentValue != isOptionnal) { - throw new ExmlBuilderException("Set 'optionnal' with a different value previous=" + curentValue + " ==> new=" + isOptionnal + " in " + method.toGenericString()); - } - prop.setOptionnal(isOptionnal); - } - final Boolean isCaseSensitive = ReflectTools.getIsCaseSensitive(method, null); - if (isCaseSensitive != null) { - final Boolean curentValue = prop.isCaseSensitive(); - if (curentValue != null && curentValue != isCaseSensitive) { - throw new ExmlBuilderException("Set 'case sensitive' with a different value previous=" + curentValue + " ==> new=" + isCaseSensitive + " in " + method.toGenericString()); - } - prop.setCaseSensitive(isCaseSensitive); - } - final Boolean isText = ReflectTools.getIsText(method, null); - if (isText != null) { - final Boolean curentValue = prop.isText(); - if (curentValue != null && curentValue != isText) { - throw new ExmlBuilderException("Set 'case sensitive' with a different value previous=" + curentValue + " ==> new=" + isText + " in " + method.toGenericString()); - } - prop.setTextMode(isText); - } - final Class factory = ReflectTools.getFactory(method); - if (factory != null) { - prop.setFactory(factory); - } - return prop; - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelFactory.java b/src/org/atriasoft/exml/builder/IntrospectionModelFactory.java deleted file mode 100644 index 8ee0f0e..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModelFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.atriasoft.exml.builder; - -import org.atriasoft.eStringSerialize.StringSerializer; -import org.atriasoft.exml.exception.ExmlBuilderException; - - -public class IntrospectionModelFactory { - private IntrospectionModelFactory() {} - - public static IntrospectionModel createModelArray(final String nodeName, final MapKey modelType) throws ExmlBuilderException { - return new IntrospectionModelArray(nodeName, modelType.type()); - } - - public static IntrospectionModel createModelList(final String nodeName, final MapKey modelType) throws ExmlBuilderException { - return new IntrospectionModelList(nodeName, modelType.type()); - } - public static IntrospectionModel createModelEnum(final MapKey modelType) throws ExmlBuilderException { - return new IntrospectionModelComplex(modelType.type()); - } - - public static IntrospectionModel createModel(final MapKey modelType) throws ExmlBuilderException { - if (StringSerializer.contains(modelType.type())) { - return new IntrospectionModelBaseType(modelType.type()); - } - return new IntrospectionModelComplex(modelType.type()); - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelList.java b/src/org/atriasoft/exml/builder/IntrospectionModelList.java deleted file mode 100644 index 39a2d95..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionModelList.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.atriasoft.exml.exception.ExmlBuilderException; - - -public class IntrospectionModelList extends IntrospectionModel { - final String nodeName; - - public IntrospectionModelList(final String nodeName, final Class classType) { - super(classType); - this.nodeName = nodeName; - } - - @Override - public Object createObject(final Map properties, final Map> nodes) throws ExmlBuilderException { - if (this.nodeName == null) { - return nodes.get(IntrospectionObject.STUPID_TOCKEN); - } - return nodes.get(this.nodeName); - } - - @Override - public List getNodeAvaillable() { - if (this.nodeName != null) { - return Arrays.asList(this.nodeName); - } - return null; - } - - @Override - public Object getValueFromText(final String text) throws ExmlBuilderException { - return new ArrayList(); - } - - @Override - public Object getValue(final String propertyName, final String propertyValue) throws ExmlBuilderException { - return null; - } - - @Override - public boolean isList() { - return true; - } - @Override - public String toString(final Object data) { - return null; - } - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionObject.java b/src/org/atriasoft/exml/builder/IntrospectionObject.java deleted file mode 100644 index 9a069ae..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionObject.java +++ /dev/null @@ -1,168 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.atriasoft.exml.exception.ExmlBuilderException; -import org.atriasoft.exml.exception.ExmlException; -import org.atriasoft.exml.exception.ExmlNodeDoesNotExist; -import org.atriasoft.exml.internal.Log; - -public class IntrospectionObject { - public static final String PUBLIC_TEXT_NAME = "##<< ** TEXT-ZONE ** >>##"; - public static final String STUPID_TOCKEN = "___aé\"'__-è==**ù!^$:;,;AZEARTYUIOPMLKJHGFDSQW>XCVBN?"; // can not exist .... - private final IntrospectionModel modelInterface; - private Object data = null; - private final Map properties = new HashMap<>(); - private final Map> nodes = new HashMap<>(); - - public IntrospectionObject(final IntrospectionModel dataInterface) { - this.modelInterface = dataInterface; - } - - @SuppressWarnings("unchecked") - public void addObject(final String nodeName, final Object value) throws ExmlException { - if (value == null) { - // specific case when a List is empty but define for a specific list ==> need no action - return; - } - final String beanName = this.modelInterface.getBeanName(nodeName); - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - List node = this.nodes.get(beanName); - if (node == null) { - if (List.class.isAssignableFrom(value.getClass())) { - node = (List) value; - } else if (value.getClass().isArray()) { - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - } else { - node = new ArrayList<>(); - node.add(value); - } - this.nodes.put(beanName, node); - } else if (value.getClass().isArray()) { - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - Log.error("this is a big problem ..."); - } else if (List.class.isAssignableFrom(value.getClass())) { - final List nodeIn = (List) value; - node.addAll(nodeIn); - } else { - node.add(value); - } - } - - public void generateTheObject() throws ExmlBuilderException { - if (this.data != null) { - // nothing to do ... ==> element already created - return; - } - Log.debug("Create the element for the Specific node ... type = " + this.modelInterface.getClassType().getCanonicalName() + (this.modelInterface.isArray() ? "[array]" : "") - + (this.modelInterface.isList() ? "[List]" : "")); - Log.debug(" Properties : " + this.properties.keySet()); - Log.debug(" Nodes : " + this.nodes.keySet()); - this.data = this.modelInterface.createObject(this.properties, this.nodes); - } - - public Object getData() { - return this.data; - } - - public IntrospectionModel getModelIntrospection() { - return this.modelInterface; - } - - public String getTreeNameOfSubNode(final String nodeName) throws ExmlException { - final String beanName = this.modelInterface.getBeanName(nodeName); - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - return this.modelInterface.getTreeNameOfSubNode(beanName); - } - - public Class getTypeOfProperty(final String nodeName) throws ExmlException { - final String beanName = this.modelInterface.getBeanName(nodeName); - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - return this.modelInterface.getTypeOfProperty(beanName); - } - - /** - * Detect a subNode, and ask the type of the node at the parent Class - * @param nodeName Name of the node - * @return Class of the node to create - */ - public Class getTypeOfSubNode(final String nodeName) throws ExmlException { - final String beanName = this.modelInterface.getBeanNameModel(nodeName); - - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - return this.modelInterface.getTypeOfSubNode(beanName); - } - - public Class getTypeOfSubNodeSubType(final String nodeName) throws ExmlException { - final String beanName = this.modelInterface.getBeanNameModel(nodeName); - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - return this.modelInterface.getTypeOfSubNodeList(beanName); - } - - public Class getTypeOfSubProperty(final String nodeName) throws ExmlException { - final String beanName = this.modelInterface.getBeanName(nodeName); - if (beanName == null) { - throw new ExmlNodeDoesNotExist("The node '" + nodeName + "' Does not exist..."); - } - return this.modelInterface.getTypeOfSubProperty(beanName); - } - - public boolean isSubNodeOrPropertyExist(final String nodeName) { - final String beanName = this.modelInterface.getBeanName(nodeName); - if (beanName == null) { - return false; - } - return true; - } - - public void putProperty(final String propertyName, final Object propertyValue) throws ExmlException { - String beanName = null; - if (propertyName == PUBLIC_TEXT_NAME) { - beanName = this.modelInterface.getTextBeanName(); - } else { - beanName = this.modelInterface.getBeanName(propertyName); - } - if (this.properties.containsKey(beanName)) { - throw new ExmlBuilderException("Property have multiple values ==> impossible case; A Node must contain only 1 attibutes"); - } - this.properties.put(beanName, propertyValue); - } - - public void setText(final String text) throws ExmlBuilderException { - if (this.data != null) { - throw new ExmlBuilderException("Can not set multiple text value in a single NODE ..."); - } - this.data = this.modelInterface.getValueFromText(text); - } - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionProperty.java b/src/org/atriasoft/exml/builder/IntrospectionProperty.java deleted file mode 100644 index 9a75c87..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionProperty.java +++ /dev/null @@ -1,322 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map.Entry; - -import org.atriasoft.aknot.model.InterfaceFactoryAccess; -import org.atriasoft.eStringSerialize.StringSerializer; -import org.atriasoft.exml.exception.ExmlBuilderException; -import org.atriasoft.exml.internal.Log; - -public final class IntrospectionProperty { - // if the element is not managed by us (set at null while not define by a function attribute, parameter ...) - private Boolean managed = null; - // Case sensitive in parsing XML or not (set at null while not define by a function attribute, parameter ...) - private Boolean caseSensitive = null; - // Optional or not (set at null while not define by a function attribute, parameter ...) - private Boolean optionnal = null; - // Attribute or Node (set at null while not define by a function attribute, parameter ...) - private Boolean attribute = null; - private Boolean textMode = null; - // name of the field or the function before renaming... - private final String beanName; - // names that can take the Node or the aatibute - private String[] names = null; - // if organized in sublist (!= null) then the subNode have this value - private String listName = null; - private boolean canBeSetByConstructor = false; - private Class factory = null; - private InterfaceFactoryAccess factoryCreated = null; - private final Class type; - private final Class subType; - // can get the property, if null not gettable ... ==> TODO need to remove this property ??? - // First function call - // second field access - IntrospectionPropertyGetter getter = null; - // can get the property, if null not settable (otherwise use the constructor???) - // First constructor call - // second function call - // third field access - IntrospectionPropertySetter setter = null; - - public IntrospectionProperty(final String beanName, final Class[] type, final String[] names) { - this.beanName = beanName; - this.type = type[0]; - this.subType = type[1]; - this.names = names; - } - - public boolean canGetValue() { - return this.getter != null; - } - - public boolean canSetValue() { - return this.canBeSetByConstructor || this.setter != null; - } - - /** - * Create a value adapted to the property type. - * @apiNote generic type is transformed byte -> Byte, int -> Integer ... - * @param value Value to set in the Object - * @throws Exception An error occurred - * @return The object created - */ - public Object createValue(final String value) throws ExmlBuilderException { - try { - if (StringSerializer.contains(this.type)) { - // TODO This might be a deprecated code .... - return StringSerializer.valueOf(this.type, value); - } - if (this.type.isEnum()) { - - } else if ((this.type != List.class) || !StringSerializer.contains(this.subType)) { - throw new ExmlBuilderException("Can not parse the specific element ... need to introspect and find the 'xxx valueOf(String data);'"); - } - final ArrayList out = new ArrayList<>(); - for (final String elem : value.split(";")) { - out.add(StringSerializer.valueOf(this.subType, elem)); - } - return out; - } catch (final IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new ExmlBuilderException("Error in parsing the property value ... " + e.getMessage()); - } - } - - public String getBeanName() { - return this.beanName; - } - - public Class getCompatible(final String name) { - final InterfaceFactoryAccess factoryGenerator = getFactory(); - if (factoryGenerator != null) { - if (this.caseSensitive) { - for (final Entry> elem : factoryGenerator.getConversionMap().entrySet()) { - if (elem.getKey().contentEquals(name)) { - return elem.getValue(); - } - } - } else { - for (final Entry> elem : factoryGenerator.getConversionMap().entrySet()) { - if (elem.getKey().equalsIgnoreCase(name)) { - return elem.getValue(); - } - } - } - return null; - } - if (this.caseSensitive) { - for (final String elem : this.names) { - if (elem.contentEquals(name)) { - Log.verbose(" - '{}' ==> true", elem); - return getType(); - } - Log.verbose(" - '{}' == false", elem); - } - } else { - for (final String elem : this.names) { - if (elem.equalsIgnoreCase(name)) { - Log.verbose(" - '{}' ==> true", elem); - return getType(); - } - Log.verbose(" - '{}' == false", elem); - } - } - return null; - } - - public InterfaceFactoryAccess getFactory() { - if (this.factoryCreated == null && this.factory != null) { - try { - this.factoryCreated = (InterfaceFactoryAccess) this.factory.getConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - this.factoryCreated = null; - this.factory = null; - } - } - return this.factoryCreated; - } - - public String getListName() { - return this.listName; - } - - /** - * Get list of all names of the property/node (minimal 1 name) - * @return Array of names available - */ - public String[] getNames() { - return this.names; - } - - /** - * Get the type of the property (if list ==> need to be get on method otherwise it is an Object...) - * @return Type of the list element. - */ - public Class getSubType() { - return this.subType; - } - - /** - * Get basic type of the property - * @return property type detected. - */ - public Class getType() { - return this.type; - } - - /** - * Get the value in the object. - * @param object Object that is invoke to get the value. - * @return The generate value of the object - * @throws ExmlBuilderException in an error occured - */ - public Object getValue(final Object object) throws ExmlBuilderException { - if (this.getter != null) { - return this.getter.getValue(object); - } - throw new ExmlBuilderException("Property: " + this.names + " have no getter"); - } - - public boolean hasFactory() { - return this.factory != null; - } - - public Boolean isAttribute() { - return this.attribute; - } - - public boolean isCanBeSetByConstructor() { - return this.canBeSetByConstructor; - } - - public Boolean isCaseSensitive() { - return this.caseSensitive; - } - - /** - * Check if the input name is compatible win an element in the list availlable (respect case sensitive if needed) - * @param name Name to check - * @return true if the element is compatible, false otherwise - */ - public boolean isCompatible(final String name) { - Log.verbose("Check compatible : '{}' in {}", name, Arrays.toString(this.names)); - final InterfaceFactoryAccess factoryGenerator = getFactory(); - if (factoryGenerator != null) { - Log.verbose(" ===> Detect factory !!!!"); - if (this.caseSensitive) { - for (final Entry> elem : factoryGenerator.getConversionMap().entrySet()) { - if (elem.getKey().contentEquals(name)) { - Log.verbose(" + '{}' ==> true", elem.getKey()); - return true; - } - Log.verbose(" + '{}' == false", elem.getKey()); - } - } else { - for (final Entry> elem : factoryGenerator.getConversionMap().entrySet()) { - if (elem.getKey().equalsIgnoreCase(name)) { - Log.verbose(" + '{}' ==> true", elem.getKey()); - return true; - } - Log.verbose(" + '{}' == false", elem.getKey()); - } - } - return false; - } - if (this.caseSensitive) { - for (final String elem : this.names) { - if (elem.contentEquals(name)) { - Log.verbose(" - '{}' ==> true", elem); - return true; - } - Log.verbose(" - '{}' == false", elem); - } - } else { - for (final String elem : this.names) { - if (elem.equalsIgnoreCase(name)) { - Log.verbose(" - '{}' ==> true", elem); - return true; - } - Log.verbose(" - '{}' == false", elem); - } - } - return false; - } - - public Boolean isManaged() { - return this.managed; - } - - public Boolean isOptionnal() { - return this.optionnal; - } - - public Boolean isText() { - return this.textMode; - } - - public void setAttribute(final Boolean attribute) { - this.attribute = attribute; - } - - public void setCanBeSetByConstructor(final boolean canBeSetByConstructor) { - this.canBeSetByConstructor = canBeSetByConstructor; - } - - public void setCaseSensitive(final Boolean caseSensitive) { - this.caseSensitive = caseSensitive; - } - - /** - * set the specific string value in the specified object - * @param object Object to add the value - * @param value Value to set in the Object - * @throws Exception An error occurred - */ - public void setExistingValue(final Object object, final Object value) throws ExmlBuilderException { - if (this.setter != null) { - this.setter.setValue(object, value); - return; - } - throw new ExmlBuilderException("Property: " + this.names + " have no setter"); - } - - public void setFactory(final Class factory) { - this.factory = factory; - } - - public void setGetter(final IntrospectionPropertyGetter getter) { - this.getter = getter; - } - - public void setListName(final String listName) { - this.listName = listName; - } - - public void setManaged(final Boolean managed) { - this.managed = managed; - } - - public void setNames(final String[] names) { - this.names = names; - } - - public void setOptionnal(final Boolean optionnal) { - this.optionnal = optionnal; - } - - public void setSetter(final IntrospectionPropertySetter setter) { - this.setter = setter; - } - - public void setTextMode(final Boolean isText) { - this.textMode = isText; - } - -} \ No newline at end of file diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java deleted file mode 100644 index 017211f..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public class IntrospectionPropertyField implements IntrospectionPropertyGetter, IntrospectionPropertySetter { - private final Field fieldDescription; - private final boolean finalValue; - - - - public IntrospectionPropertyField(final Field fieldDescription) { - this.fieldDescription = fieldDescription; - this.finalValue = Modifier.isFinal(fieldDescription.getModifiers()); - } - - @Override - public Object getValue(final Object object) throws ExmlBuilderException { - try { - return this.fieldDescription.get(object); - } catch (IllegalArgumentException | IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); - } - } - @Override - public void setValue(final Object object, final Object value) throws ExmlBuilderException { - if (this.finalValue) { - throw new ExmlBuilderException("Can not set value The value is FINAL!!!"); - } - try { - this.fieldDescription.set(object, value); - } catch (IllegalArgumentException | IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); - } - } - -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java deleted file mode 100644 index 6f50f86..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.atriasoft.exml.builder; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public interface IntrospectionPropertyGetter { - Object getValue(Object object) throws ExmlBuilderException; -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java deleted file mode 100644 index af4aaf7..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public class IntrospectionPropertyMethodGetter implements IntrospectionPropertyGetter { -// private static Class[] getTypefunction(final Method setter, final Method getter) throws Exception { -// Class type = null; -// Class subType = null; -// if (setter == null && getter == null) { -// // impossible case... -// throw new Exception("kjhkhjkj"); -// } -// if (getter != null) { -// type = getter.getReturnType(); -// if (Enum.class.isAssignableFrom(type)) { -// Log.verbose("Find an enum ..."); -// } else { -// Type empppe = getter.getGenericReturnType(); -// if (empppe instanceof ParameterizedType plopppppp) { -// Type[] realType = plopppppp.getActualTypeArguments(); -// if (realType.length > 0) { -// subType = Class.forName(realType[0].getTypeName()); -// } -// } -// } -// } -// if (setter != null) { -// if (type != null && setter.getParameters()[0].getType() != type) { -// throw new Exception("The type of the setter ands the type return by the getter are not the same ..."); -// } -// type = setter.getParameters()[0].getType(); -// if (List.class.isAssignableFrom(type)) { -// Class internalModelClass = null; -// Type[] empppe = setter.getGenericParameterTypes(); -// if (empppe.length > 0) { -// if (empppe[0] instanceof ParameterizedType plopppppp) { -// Type[] realType = plopppppp.getActualTypeArguments(); -// if (realType.length > 0) { -// Log.warning(" -->> " + realType[0]); -// internalModelClass = Class.forName(realType[0].getTypeName()); -// } -// } -// } -// if (getter!=null && internalModelClass != subType) { -// throw new Exception("The type of the setter and the type return by the getter are not the same ..."); -// } -// subType = internalModelClass; -// } -// } -// return new Class[] {type, subType}; -// } -// - protected Method getter; - - public IntrospectionPropertyMethodGetter(final Method getter) throws Exception { - this.getter = getter; - } - - @Override - public Object getValue(final Object object) throws ExmlBuilderException { - if (this.getter == null) { - throw new ExmlBuilderException("no getter availlable"); - } - try { - return this.getter.invoke(object); - } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { - e.printStackTrace(); - throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); - } - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java deleted file mode 100644 index ae18dfe..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.atriasoft.exml.builder; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public class IntrospectionPropertyMethodSetter implements IntrospectionPropertySetter { - protected Method setter; - - public IntrospectionPropertyMethodSetter(final Method setter) throws Exception { - this.setter = setter; - } - - @Override - public void setValue(final Object object, final Object value) throws ExmlBuilderException { - if (this.setter == null) { - throw new ExmlBuilderException("no setter availlable"); - } - try { - this.setter.invoke(object, value); - } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); - } - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java deleted file mode 100644 index 8ae9eff..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.atriasoft.exml.builder; - -import org.atriasoft.exml.exception.ExmlBuilderException; - -public interface IntrospectionPropertySetter { - /** - * set the specific string value in the specified object - * @param object Object to add the value - * @param value Value to set in the Object - * @throws Exception An error occurred - */ - void setValue(Object object, Object value) throws ExmlBuilderException; -} diff --git a/src/org/atriasoft/exml/builder/MapKey.java b/src/org/atriasoft/exml/builder/MapKey.java deleted file mode 100644 index 4e0f6b4..0000000 --- a/src/org/atriasoft/exml/builder/MapKey.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.atriasoft.exml.builder; - -public record MapKey (ModelType model, - String nodeName, - Class type) { - public MapKey(final ModelType model, final String nodeName, final Class type) { - this.model = model; - this.nodeName = nodeName; - this.type = type; - } - public MapKey(final ModelType model, final Class type) { - this(model, null, type); - } -} \ No newline at end of file diff --git a/src/org/atriasoft/exml/builder/ModelType.java b/src/org/atriasoft/exml/builder/ModelType.java deleted file mode 100644 index 360df8d..0000000 --- a/src/org/atriasoft/exml/builder/ModelType.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.atriasoft.exml.builder; - -public enum ModelType { - NORMAL, - ARRAY, - LIST -} \ No newline at end of file diff --git a/src/org/atriasoft/exml/exception/ExmlParserError.java b/src/org/atriasoft/exml/exception/ExmlParserError.java index af6b97a..0a9315f 100644 --- a/src/org/atriasoft/exml/exception/ExmlParserError.java +++ b/src/org/atriasoft/exml/exception/ExmlParserError.java @@ -1,6 +1,6 @@ package org.atriasoft.exml.exception; -import org.atriasoft.exml.parser.FilePos; +import org.atriasoft.etk.util.FilePos; public class ExmlParserError extends ExmlBuilderException { private static final long serialVersionUID = 1L; diff --git a/src/org/atriasoft/exml/generator/GeneratorIntrospection.java b/src/org/atriasoft/exml/generator/GeneratorIntrospection.java index 4c33e9f..ab6f361 100644 --- a/src/org/atriasoft/exml/generator/GeneratorIntrospection.java +++ b/src/org/atriasoft/exml/generator/GeneratorIntrospection.java @@ -4,15 +4,16 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.atriasoft.aknot.exception.AknotException; +import org.atriasoft.aknot.model.IntrospectionModel; +import org.atriasoft.aknot.model.MapKey; +import org.atriasoft.aknot.model.ModelType; +import org.atriasoft.aknot.pojo.IntrospectionModelFactory; +import org.atriasoft.aknot.pojo.IntrospectionProperty; +import org.atriasoft.etk.Tools; import org.atriasoft.etk.util.ArraysTools; -import org.atriasoft.exml.builder.IntrospectionModel; -import org.atriasoft.exml.builder.IntrospectionModelFactory; -import org.atriasoft.exml.builder.IntrospectionProperty; -import org.atriasoft.exml.builder.MapKey; -import org.atriasoft.exml.builder.ModelType; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.internal.Log; -import org.atriasoft.exml.parser.Tools; public class GeneratorIntrospection implements Generator { // Keep in cache all the object already parsed ==> optimize CPU @@ -24,13 +25,39 @@ public class GeneratorIntrospection implements Generator { public GeneratorIntrospection(final ModelType model, final Class classType, final String rootNodeName) throws Exception { this.rootNodeName = rootNodeName; this.rootClassType = classType; - MapKey key = new MapKey(model, classType); - // TODO pb if it is a List or an Array ... + final MapKey key = new MapKey(model, classType); + // TODO pb if it is a List or an Array ... this.elements.put(key, IntrospectionModelFactory.createModel(key)); } - IntrospectionModel findOrCreate(final ModelType model, final String name, final Class classType) throws ExmlBuilderException { - MapKey key = new MapKey(model, name, classType); + private String autoArrayToString(final Class clazz, final Object inData, final IntrospectionModel model) throws ExmlBuilderException, AknotException { + @SuppressWarnings("unchecked") + final T[] datas = (T[]) inData; + final StringBuilder out = new StringBuilder(); + for (int iii = 0; iii < datas.length; iii++) { + if (!out.isEmpty()) { + out.append(";"); + } + out.append(model.toString(datas[iii])); + } + return out.toString(); + } + + private String autoListToString(final Class clazz, final Object inData, final IntrospectionModel model) throws ExmlBuilderException, AknotException { + @SuppressWarnings("unchecked") + final List elements1 = (List) inData; + final StringBuilder out = new StringBuilder(); + for (final Object elem1 : elements1) { + if (!out.isEmpty()) { + out.append(";"); + } + out.append(model.toString(elem1)); + } + return out.toString(); + } + + IntrospectionModel findOrCreate(final ModelType model, final String name, final Class classType) throws ExmlBuilderException, AknotException { + final MapKey key = new MapKey(model, name, classType); IntrospectionModel out = this.elements.get(key); if (out != null) { return out; @@ -45,152 +72,62 @@ public class GeneratorIntrospection implements Generator { this.elements.put(key, out); return out; } - - private String autoArrayToString(final Class clazz, final Object inData, final IntrospectionModel model) throws ExmlBuilderException { - @SuppressWarnings("unchecked") - T[] datas = (T[])inData; - StringBuilder out = new StringBuilder(); - for(int iii=0; iii String autoListToString(final Class clazz, final Object inData, final IntrospectionModel model) throws ExmlBuilderException { - @SuppressWarnings("unchecked") - List elements1 = (List)inData; - StringBuilder out = new StringBuilder(); - for (Object elem1: elements1) { - if (!out.isEmpty()) { - out.append(";"); - } - out.append(model.toString(elem1)); - } - return out.toString(); - } - - public void generateProperties(final Object data, final IntrospectionModel introspection, final StringBuilder tmpp) throws ExmlBuilderException { - List elements = introspection.getAttributes(); - if (elements == null) { - return; - } - for (IntrospectionProperty elem : elements) { - if (!elem.canGetValue()) { - continue; - } - Object dataObj = elem.getValue(data); - if (dataObj == null) { - continue; - } - String name = elem.getNames()[0]; - Class type = elem.getType(); - String dataString = null; - if (type.isArray()) { - Class typeClass = elem.getType().componentType(); - if (typeClass.isPrimitive()) { - dataString = ArraysTools.primitivaArrayToSting(dataObj); - } else { - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, typeClass); - dataString = autoArrayToString(typeClass, dataObj, introspectionSub); - } - } else if (List.class.isAssignableFrom(type)) { - Class typeClass = elem.getSubType(); - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, typeClass); - dataString = autoListToString(typeClass, dataObj, introspectionSub); - } else { - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, dataObj.getClass()); - dataString = introspectionSub.toString(dataObj); - } - if (dataString != null) { - tmpp.append(" "); - tmpp.append(name); - tmpp.append("=\""); - tmpp.append(dataString); - tmpp.append("\""); - } - } - } - public void generateSubNodes(final Object data, final IntrospectionModel introspection, final StringBuilder tmpp, final int indent) throws ExmlBuilderException { - List elements = introspection.getNodes(); - for (IntrospectionProperty elem : elements) { - if (!elem.canGetValue()) { - continue; - } - Object dataObj = elem.getValue(data); - if (dataObj == null) { - continue; - } - String name = elem.getNames()[0]; - Class type = elem.getType(); - IntrospectionModel introspectionSub = null; - if (type.isArray()) { - Class typeClass = elem.getType().componentType(); - String listTreeName = elem.getListName(); - introspectionSub = findOrCreate(ModelType.ARRAY, listTreeName, typeClass); - } else if (List.class.isAssignableFrom(type)) { - Class typeClass = elem.getSubType(); - String listTreeName = elem.getListName(); - introspectionSub = findOrCreate(ModelType.LIST, listTreeName, typeClass); - } else { - introspectionSub = findOrCreate(ModelType.NORMAL, null, dataObj.getClass()); - } - generateNode(dataObj, introspectionSub, name, tmpp, indent); - } - } - - public void generateArrayNode(final Class clazz, final Object data, final IntrospectionModel model, final String nodeName, final StringBuilder tmpp, final int indent) throws ExmlBuilderException { + public void generateArrayNode(final Class clazz, final Object data, final IntrospectionModel model, final String nodeName, final StringBuilder tmpp, final int indent) + throws ExmlBuilderException, AknotException { //Class clazzData = data.getClass(); if (clazz.isPrimitive()) { if (clazz == byte.class) { - byte[] datas = (byte[]) data; - for (int iii=0; iii"); } else if (model.isArray()) { - List baseName = model.getNodeAvaillable(); - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType()); + final List baseName = model.getNodeAvaillable(); + final IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, model.getClassType()); if (baseName == null || baseName.size() == 0) { // mode render : aaabbb generateArrayNode(model.getClassType(), data, introspectionSub, nodeName, tmpp, indent); @@ -212,7 +149,7 @@ public class GeneratorIntrospection implements Generator { tmpp.append("<"); tmpp.append(nodeName); tmpp.append(">"); - generateArrayNode(model.getClassType(), data, introspectionSub, baseName.get(0), tmpp, indent+1); + generateArrayNode(model.getClassType(), data, introspectionSub, baseName.get(0), tmpp, indent + 1); Tools.addIndent(tmpp, indent); tmpp.append(" baseName = model.getNodeAvaillable(); + final List baseName = model.getNodeAvaillable(); @SuppressWarnings("unchecked") - List datas = (List)data; + final List datas = (List) data; if (baseName == null || baseName.size() == 0) { // mode render : aaabbb - for (Object elem : datas) { + for (final Object elem : datas) { // note the type can change in List ... - IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, elem.getClass()); + final IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, elem.getClass()); generateNode(elem, introspectionSub, nodeName, tmpp, indent); } } else { @@ -236,10 +173,10 @@ public class GeneratorIntrospection implements Generator { tmpp.append("<"); tmpp.append(nodeName); tmpp.append(">"); - for (Object elem : datas) { + for (final 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); + final 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); @@ -272,10 +209,76 @@ public class GeneratorIntrospection implements Generator { tmpp.append("/>"); } } - + } - public void generate(final Object root, final StringBuilder tmpp) throws ExmlBuilderException { - IntrospectionModel introspection = findOrCreate(ModelType.NORMAL, null, root.getClass()); - generateNode(root, introspection, this.rootNodeName, tmpp, 0); + + public void generateProperties(final Object data, final IntrospectionModel introspection, final StringBuilder tmpp) throws ExmlBuilderException, AknotException { + final List elements = introspection.getAttributes(); + if (elements == null) { + return; + } + for (final IntrospectionProperty elem : elements) { + if (!elem.canGetValue()) { + continue; + } + final Object dataObj = elem.getValue(data); + if (dataObj == null) { + continue; + } + final String name = elem.getNames()[0]; + final Class type = elem.getType(); + String dataString = null; + if (type.isArray()) { + final Class typeClass = elem.getType().componentType(); + if (typeClass.isPrimitive()) { + dataString = ArraysTools.primitivaArrayToSting(dataObj); + } else { + final IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, typeClass); + dataString = autoArrayToString(typeClass, dataObj, introspectionSub); + } + } else if (List.class.isAssignableFrom(type)) { + final Class typeClass = elem.getSubType(); + final IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, typeClass); + dataString = autoListToString(typeClass, dataObj, introspectionSub); + } else { + final IntrospectionModel introspectionSub = findOrCreate(ModelType.NORMAL, null, dataObj.getClass()); + dataString = introspectionSub.toString(dataObj); + } + if (dataString != null) { + tmpp.append(" "); + tmpp.append(name); + tmpp.append("=\""); + tmpp.append(dataString); + tmpp.append("\""); + } + } + } + + public void generateSubNodes(final Object data, final IntrospectionModel introspection, final StringBuilder tmpp, final int indent) throws ExmlBuilderException, AknotException { + final List elements = introspection.getNodes(); + for (final IntrospectionProperty elem : elements) { + if (!elem.canGetValue()) { + continue; + } + final Object dataObj = elem.getValue(data); + if (dataObj == null) { + continue; + } + final String name = elem.getNames()[0]; + final Class type = elem.getType(); + IntrospectionModel introspectionSub = null; + if (type.isArray()) { + final Class typeClass = elem.getType().componentType(); + final String listTreeName = elem.getListName(); + introspectionSub = findOrCreate(ModelType.ARRAY, listTreeName, typeClass); + } else if (List.class.isAssignableFrom(type)) { + final Class typeClass = elem.getSubType(); + final String listTreeName = elem.getListName(); + introspectionSub = findOrCreate(ModelType.LIST, listTreeName, typeClass); + } else { + introspectionSub = findOrCreate(ModelType.NORMAL, null, dataObj.getClass()); + } + generateNode(dataObj, introspectionSub, name, tmpp, indent); + } } } diff --git a/src/org/atriasoft/exml/model/XmlAttribute.java b/src/org/atriasoft/exml/model/XmlAttribute.java index 27f1c5b..4da5840 100644 --- a/src/org/atriasoft/exml/model/XmlAttribute.java +++ b/src/org/atriasoft/exml/model/XmlAttribute.java @@ -5,7 +5,7 @@ */ package org.atriasoft.exml.model; -import org.atriasoft.exml.parser.FilePos; +import org.atriasoft.etk.util.FilePos; /** * Single attribute element diff --git a/src/org/atriasoft/exml/parser/FilePos.java b/src/org/atriasoft/exml/parser/FilePos.java deleted file mode 100644 index df5af36..0000000 --- a/src/org/atriasoft/exml/parser/FilePos.java +++ /dev/null @@ -1,185 +0,0 @@ -/** @file - * @author Edouard DUPIN - * @copyright 2021, Edouard DUPIN, all right reserved - * @license MPL v2.0 (see license file) - */ -package org.atriasoft.exml.parser; - -/** @file - * @author Edouard DUPIN - * @copyright 2011, Edouard DUPIN, all right reserved - * @license MPL v2.0 (see license file) - */ - -/** - * Position in the file of the original data. - */ -public class FilePos { - private int col; //!< source text colomn - private int line; //!< source Line colomn - - /** - * default contructor (set line and col at 0) - */ - public FilePos() { - this.col = 0; - this.line = 0; - } - - /** - * initialize constructor - * @param line Line in the file - * @param col Colomn in the file - */ - public FilePos(final int line, final int col) { - this.col = col; - this.line = line; - } - - /** - * Addition operator - * @param obj Addition object.. - * @return Reference on this - */ - public FilePos add(final FilePos obj) { - if (obj.line == 0) { - this.col += obj.col; - } else { - this.col = obj.col; - this.line += obj.line; - } - return this; - } - - /** - * Colomn addition operator - * @param col Number of colomn to add - * @return Reference on this - */ - public FilePos add(final int col) { - this.col += col; - return this; - } - - /** - * Check if the value is a new line and update internal property - * @param val Char value to check - * @return true We find a new line - * @return false We NOT find a new line - */ - public boolean check(final Character val) { - this.col++; - if (val == '\n') { - newLine(); - return true; - } - return false; - } - - /** - * Reset position at 0,0 - */ - public void clear() { - this.col = 0; - this.line = 0; - } - - @Override - public FilePos clone() { - final FilePos out = new FilePos(); - out.col = this.col; - out.line = this.line; - return out; - } - - /** - * Decrement the colomn position - * @return Reference on this - */ - public FilePos decrement() { - this.col--; - return this; - } - - @Override - public boolean equals(final Object obj) { - if (obj == null) { - return false; - } - if (!(obj instanceof FilePos)) { - return false; - } - final FilePos other = (FilePos) obj; - return this.col == other.col && this.line == other.line; - } - - /** - * Get the colomn position - * @return Colomn in number of utf8-char - */ - public int getCol() { - return this.col; - } - - /** - * Get the line number position - * @return line ID (start at 0) - */ - public int getLine() { - return this.line; - } - - @Override - public int hashCode() { - return super.hashCode() + this.line + this.col; - } - - /** - * Increment the colomn position - * @return Reference on this - */ - public FilePos increment() { - this.col++; - return this; - } - - /** - * Find a new line & reset colomn at 0 - */ - public void newLine() { - this.col = 0; - this.line++; - } - - /** - * Asignment operator - * @param obj Object to copy - * @return Reference on this - */ - public FilePos set(final FilePos obj) { - this.col = obj.col; - this.line = obj.line; - return this; - } - - /** - * Setter of specific data - * @param line Line in the file - * @param col Colomn in the file - */ - public void set(final int line, final int col) { - this.col = col; - this.line = line; - } - - @Override - public String toString() { - String out = "(l="; - out += this.line; - out += ",c="; - out += this.col; - out += ")"; - return out; - } - -} diff --git a/src/org/atriasoft/exml/parser/ParseXml.java b/src/org/atriasoft/exml/parser/ParseXml.java index 9cfb2a5..387bd25 100644 --- a/src/org/atriasoft/exml/parser/ParseXml.java +++ b/src/org/atriasoft/exml/parser/ParseXml.java @@ -1,5 +1,8 @@ package org.atriasoft.exml.parser; +import org.atriasoft.aknot.exception.AknotException; +import org.atriasoft.etk.Tools; +import org.atriasoft.etk.util.FilePos; import org.atriasoft.exml.builder.Builder; import org.atriasoft.exml.exception.ExmlException; import org.atriasoft.exml.exception.ExmlParserError; @@ -14,7 +17,8 @@ public class ParseXml { this.builder = builder; } - protected boolean iParseAttribute(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException { + protected boolean iParseAttribute(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) + throws ExmlException, AknotException { Log.verbose("start parse : 'attribute'"); // search end of the comment : int lastElementName = pos.value; @@ -105,7 +109,8 @@ public class ParseXml { return true; } - protected boolean iParseCDATA(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException { + protected boolean iParseCDATA(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) + throws ExmlException, AknotException { Log.verbose("start parse : 'text::CDATA'"); // search end of the comment : for (int iii = pos.value; iii + 2 < data.length(); iii++) { @@ -128,7 +133,8 @@ public class ParseXml { return false; } - protected boolean iParseComment(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException { + protected boolean iParseComment(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) + throws ExmlException, AknotException { Log.verbose("start parse : 'comment'"); final FilePos tmpPos = new FilePos(); final int white = Tools.countWhiteChar(data, pos.value, tmpPos); @@ -162,7 +168,8 @@ public class ParseXml { return false; } - protected boolean iParseDeclaration(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException { + protected boolean iParseDeclaration(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) + throws ExmlException, AknotException { // search end of the comment : for (int iii = pos.value; iii + 1 < data.length(); iii++) { Tools.drawElementParsed(data.charAt(iii), filePos); @@ -217,7 +224,7 @@ public class ParseXml { } protected boolean iParseElement(final Object parent, final String nameElement, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) - throws ExmlException { + throws ExmlException, AknotException { // note : When start parsing the upper element must have set the value of the element and set the position after this one // find a normal node ... for (int iii = pos.value; iii < data.length(); iii++) { @@ -335,7 +342,7 @@ public class ParseXml { return data.length(); } - protected boolean iParseText(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException { + protected boolean iParseText(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlException, AknotException { Log.verbose("start parse : 'text'"); if (this.builder.isPackText(parent)) { final int endOfText = iParseFindTextPackEnd(data, pos.value); @@ -375,7 +382,7 @@ public class ParseXml { return false; } - public Object parse(final String data, final ParsingProperty property) throws ExmlException { + public Object parse(final String data, final ParsingProperty property) throws ExmlException, AknotException { Log.verbose("Start parsing document (type: string) size=" + data.length()); // came from char == > force in utf8 ... final FilePos pos = new FilePos(1, 0); @@ -405,7 +412,7 @@ public class ParseXml { * @throws ExmlException */ protected boolean subParseElement(final Object parent, final String nameElement, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) - throws ExmlException { + throws ExmlException, AknotException { //EXMLPARSEELEMENT(" start subParse ... " << pos << " " << filePos); for (int iii = pos.value; iii < data.length(); iii++) { filePos.check(data.charAt(iii)); diff --git a/src/org/atriasoft/exml/parser/ParsingProperty.java b/src/org/atriasoft/exml/parser/ParsingProperty.java index e78c2a4..0ffb4ae 100644 --- a/src/org/atriasoft/exml/parser/ParsingProperty.java +++ b/src/org/atriasoft/exml/parser/ParsingProperty.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.atriasoft.etk.Tools; import org.atriasoft.exml.exception.ExmlParserError; import org.atriasoft.exml.internal.Log; diff --git a/src/org/atriasoft/exml/parser/Tools.java b/src/org/atriasoft/exml/parser/Tools.java deleted file mode 100644 index b96027b..0000000 --- a/src/org/atriasoft/exml/parser/Tools.java +++ /dev/null @@ -1,493 +0,0 @@ -package org.atriasoft.exml.parser; - -public class Tools { - /** - * add indentation of the string input. - * @param data String where the indentation is done. - * @param indent Number of tab to add at the string. - */ - public static void addIndent(final StringBuilder data, final int indent) { - if (!data.isEmpty()) { - data.append("\n"); - } - for (int iii = 0; iii < indent; iii++) { - data.append("\t"); - } - } - - /** - * check if an element or attribute is availlable (not : !"#$%&'()*+,/;<=>?@[\]^`{|}~ \\n\\t\\r and for first char : not -.0123456789). - * @param val Value to check the conformity. - * @param firstChar True if the element check is the first char. - * @return true The value can be a part of attribute name - * @return false The value can NOT be a part of attribute name - */ - public static boolean checkAvaillable(final Character val, final boolean firstChar) { - if (val == '!' || val == '"' || val == '#' || val == '$' || val == '%' || val == '&' || val == '\'' || val == '(' || val == ')' || val == '*' || val == '+' || val == ',' || val == '/' - || val == ';' || val == '<' || val == '=' || val == '>' || val == '?' || val == '@' || val == '[' || val == '\\' || val == ']' || val == '^' || val == '`' || val == '{' || val == '|' - || val == '}' || val == '~' || val == ' ' || val == '\n' || val == '\t' || val == '\r') { - return false; - } - if (firstChar) { - if (val == '-' || val == '.' || (val >= '0' && val <= '9')) { - return false; - } - } - return true; - } - - public static boolean checkNumber(final Character val, final boolean firstChar) { - if (val == '.' || (val >= '0' && val <= '9')) { - return true; - } - if (firstChar && val == '-') { - return true; - } - return false; - } - - public static String cleanNumberList(final String data) { - return data.replaceAll("[ \t\n\r]", "").replaceAll(",", ";"); - } - - /** - * count the number of white char in the string from the specify position (stop at the first element that is not a white char) - * @param data Data to parse. - * @param pos Start position in the string. - * @param filePos new poistion of te file to add. - * @return number of white element. - */ - public static int countWhiteChar(final String data, final int pos, final FilePos filePos) { - filePos.clear(); - int white = 0; - for (int iii = pos; iii < data.length(); iii++) { - filePos.check(data.charAt(iii)); - if (!Tools.isWhiteChar(data.charAt(iii))) { - break; - } - white++; - } - filePos.decrement(); - return white; - } - - public static String createPosPointer(final String line, final int pos) { - final StringBuilder out = new StringBuilder(); - int iii; - for (iii = 0; iii < pos && iii < line.length(); iii++) { - if (line.charAt(iii) == '\t') { - out.append("\t"); - } else { - out.append(" "); - } - } - for (; iii < pos; iii++) { - out.append(" "); - } - out.append("^"); - return out.toString(); - } - - // based on this: https://stackoverflow.com/questions/4052840/most-efficient-way-to-make-the-first-character-of-a-string-lower-case - public static String decapitalizeFirst(final String string) { - if (string == null || string.length() == 0) { - return string; - } - final char[] c = string.toCharArray(); - c[0] = Character.toLowerCase(c[0]); - return new String(c); - } - - /** - * Display the cuurent element that is curently parse. - * @param val Char that is parsed. - * @param filePos Position of the char in the file. - */ - public static void drawElementParsed(final Character val, final FilePos filePos) { - // if (val == '\n') { - // Log.error(filePos + " parse '\\n'"); - // } else if (val == '\t') { - // Log.error(filePos + " parse '\\t'"); - // } else { - // Log.error(filePos + " parse '" + val + "'"); - // } - } - - public static String extractLine(final String data, final int pos) { - // search back : '\n' - int startPos = data.lastIndexOf('\n', pos); - if (startPos == pos) { - startPos = 0; - } else { - startPos++; - } - // search forward : '\n' - int stopPos = pos; - if (data.length() == pos) { - stopPos = pos; - } else if (data.charAt(pos) != '\n') { - stopPos = data.indexOf('\n', pos); - if (stopPos == pos) { - stopPos = data.length(); - } - } - if (startPos == -1) { - startPos = 0; - } else if (startPos >= data.length()) { - return ""; - } - if (stopPos == -1) { - return ""; - } - if (stopPos >= data.length()) { - stopPos = data.length(); - } - return data.substring(startPos, stopPos); - } - - public static boolean isWhiteChar(final Character val) { - if (val == ' ' || val == '\t' || val == '\n' || val == '\r') { - return true; - } - return false; - } - - public static Boolean[] parseBooleanClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Boolean[] out = new Boolean[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Boolean.valueOf(str); - } - return out; - } - - public static boolean[] parseBooleanStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final boolean[] out = new boolean[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Boolean.parseBoolean(str); - } - return out; - } - - public static Byte[] parseByteClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Byte[] out = new Byte[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Byte.parseByte(str); - } - return out; - } - - public static byte[] parseByteStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final byte[] out = new byte[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Byte.parseByte(str); - } - return out; - } - - public static Double[] parseDoubleClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Double[] out = new Double[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Double.parseDouble(str); - } - return out; - } - - public static double[] parseDoubleStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final double[] out = new double[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Double.parseDouble(str); - } - return out; - } - - public static Float[] parseFloatClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Float[] out = new Float[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Float.parseFloat(str); - } - return out; - } - - public static float[] parseFloatStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final float[] out = new float[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Float.parseFloat(str); - } - return out; - } - - public static Integer[] parseIntegerClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Integer[] out = new Integer[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Integer.parseInt(str); - } - return out; - } - - public static int[] parseIntegerStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final int[] out = new int[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Integer.parseInt(str); - } - return out; - } - - public static Long[] parseLongClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Long[] out = new Long[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Long.parseLong(str); - } - return out; - } - - public static long[] parseLongStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final long[] out = new long[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Long.parseLong(str); - } - return out; - } - - public static Short[] parseShortClassStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final Short[] out = new Short[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Short.parseShort(str); - } - return out; - } - - public static short[] parseShortStringList(String data) { - data = Tools.cleanNumberList(data); - final String[] dataArray = data.split(";"); - final short[] out = new short[dataArray.length]; - int count = 0; - for (final String str : dataArray) { - out[count++] = Short.parseShort(str); - } - return out; - } - - // transform the Text with : - // "<" == "<" - // ">" == ">" - // "&" == "&" - // "'" == "'" - // """ == """ - public static String replaceSpecialChar(final String inval) { - String out = inval; - out = out.replace("<", "<"); - out = out.replace(">", ">"); - out = out.replace("'", "'"); - out = out.replace(""", "\""); - out = out.replace("&", "&"); - //EXMLERROR("INNN '"<< inval << "' => '" << out << "'"); - return out; - } - - public static String replaceSpecialCharOut(final String inval) { - String out = inval; - out = out.replace("<", "<"); - out = out.replace(">", ">"); - out = out.replace("'", "'"); - out = out.replace("\"", """); - out = out.replace("&", "&"); - //EXMLERROR("OUTTT '"<< inval << "' => '" << out << "'"); - return out; - } - - public static String toString(final boolean[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Boolean[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final byte[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Byte[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final double[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Double[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final float[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Float[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final int[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Integer[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final long[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Long[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final short[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - public static String toString(final Short[] data) { - final StringBuilder out = new StringBuilder(); - for (int iii = 0; iii < data.length; iii++) { - if (iii != 0) { - out.append(";"); - } - out.append(data[iii]); - } - return out.toString(); - } - - private Tools() {} - -} diff --git a/src/org/atriasoft/exml/serializer/SerializerXml.java b/src/org/atriasoft/exml/serializer/SerializerXml.java index e0133ce..1d438c3 100644 --- a/src/org/atriasoft/exml/serializer/SerializerXml.java +++ b/src/org/atriasoft/exml/serializer/SerializerXml.java @@ -2,6 +2,7 @@ package org.atriasoft.exml.serializer; import java.util.List; +import org.atriasoft.etk.Tools; import org.atriasoft.exml.internal.Log; import org.atriasoft.exml.model.XmlAttribute; import org.atriasoft.exml.model.XmlAttributeList; @@ -11,7 +12,6 @@ import org.atriasoft.exml.model.XmlElement; import org.atriasoft.exml.model.XmlNode; import org.atriasoft.exml.model.XmlNodeType; import org.atriasoft.exml.model.XmlText; -import org.atriasoft.exml.parser.Tools; public class SerializerXml { public static void serialize(final XmlNode node, final StringBuilder data, final int indent) { @@ -45,7 +45,7 @@ public class SerializerXml { private static void serializeComment(final XmlComment comment, final StringBuilder data, final int indent) { Tools.addIndent(data, indent); data.append("