xmlparser.XmlReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simplexml Show documentation
Show all versions of simplexml Show documentation
A clean and simple XML parser, serializer, and deserializer.
package xmlparser;
import xmlparser.annotations.*;
import xmlparser.error.InvalidAnnotation;
import xmlparser.error.InvalidXPath;
import xmlparser.model.XmlElement;
import xmlparser.parsing.DomBuilder;
import xmlparser.parsing.ObjectDeserializer;
import xmlparser.utils.Escaping.UnEscape;
import xmlparser.utils.Interfaces.AccessDeserializers;
import xmlparser.utils.Reflection;
import xmlparser.utils.Trimming.Trim;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.*;
import static org.objenesis.ObjenesisHelper.newInstance;
import static xmlparser.model.XmlElement.findChildForName;
import static xmlparser.utils.Reflection.*;
import static xmlparser.utils.Validator.multipleAreTrue;
import static xmlparser.xpath.XPathExpression.newXPath;
public interface XmlReader extends AccessDeserializers {
default T domToObject(final XmlElement node, final Class clazz) throws InvalidXPath {
if (node == null) return null;
final ObjectDeserializer c = getDeserializer(clazz);
if (c != null) return c.convert(node, clazz);
final T o = newInstance(clazz);
final String parentName = toName(clazz);
XmlElement selectedNode;
for (final Field f : listFields(clazz)) {
f.setAccessible(true);
if (Modifier.isStatic(f.getModifiers())) continue;
if (f.isAnnotationPresent(XmlNoImport.class)) continue;
selectedNode = node;
if (f.isAnnotationPresent(XmlPath.class)) {
selectedNode = newXPath(parentName + "/" + f.getAnnotation(XmlPath.class).value()).evaluateAny(node);
}
switch (toFieldType(f)) {
case FIELD_DESERIALIZER: setField(f, o, invokeFieldDeserializer(f, selectedNode)); break;
case TEXTNODE: setField(f, o, textNodeToValue(f.getType(), selectedNode)); break;
case ANNOTATED_ATTRIBUTE: setField(f, o, attributeToValue(f.getType(), toName(f), deWrap(selectedNode, f))); break;
case SET: setField(f, o, domToSet(f, toClassOfCollection(f), toName(f), deWrap(selectedNode, f))); break;
case LIST: setField(f, o, domToList(f, toClassOfCollection(f), toName(f), deWrap(selectedNode, f))); break;
case ARRAY: setField(f, o, domToArray(f.getType().getComponentType(), toName(f), deWrap(selectedNode, f))); break;
case MAP: setField(f, o, domToMap(f, (ParameterizedType) f.getGenericType(), toName(f), deWrap(selectedNode, f))); break;
case ENUM: setField(f, o, enumNodeToValue(Reflection.toEnumType(f), toName(f), deWrap(selectedNode, f))); break;
default:
final String name = toName(f);
final String value = selectedNode.attributes.get(name);
if (value != null) {
setField(f, o, stringToValue(f.getType(), value));
break;
}
if (isAbstract(f)) {
final XmlElement child = selectedNode.findChildForName(name, null);
setField(f, o, domToObject(child, findAbstractType(f.getAnnotation(XmlAbstractClass.class), child)));
break;
}
setField(f, o, domToObject(findChildForName(deWrap(selectedNode, f),name, null), f.getType()));
break;
}
}
return o;
}
default XmlElement deWrap(final XmlElement element, final Field field) {
if (!isWrapped(field)) return element;
return element.findChildForName(toWrappedName(field), null);
}
default Object textNodeToValue(final Class> type, final XmlElement node) {
final ObjectDeserializer conv = getDeserializer(type);
return (conv != null) ? conv.convert(node) : null;
}
default Object enumNodeToValue(final Class extends Enum> type, final String name, final XmlElement node) {
final XmlElement text = findChildForName(node, name, null);
if (text == null) return null;
final String value = text.getText();
if (value == null) return null;
final ObjectDeserializer conv = getDeserializer(type);
return (conv == null) ? Enum.valueOf(type, value) : conv.convert(node);
}
default Object attributeToValue(final Class> type, final String name, final XmlElement node) {
final ObjectDeserializer conv = getDeserializer(type);
if (conv == null) return null;
final String value = node.attributes.get(name);
if (value == null) return null;
return conv.convert(value);
}
default Object stringToValue(final Class> type, final String value) {
final ObjectDeserializer conv = getDeserializer(type);
return (conv != null) ? conv.convert(value) : null;
}
default Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy