// better
GenericXml genericXml = destination instanceof GenericXml ? (GenericXml) destination : null;
@SuppressWarnings("unchecked")
Map<String, Object> destinationMap =
genericXml == null && destination instanceof Map<?, ?> ? Map.class.cast(destination) : null;
ClassInfo classInfo =
destinationMap != null || destination == null ? null : ClassInfo.of(destination.getClass());
if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {
parser.next();
}
parseNamespacesForElement(parser, namespaceDictionary);
// generic XML
if (genericXml != null) {
genericXml.namespaceDictionary = namespaceDictionary;
String name = parser.getName();
String namespace = parser.getNamespace();
String alias = namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(namespace);
genericXml.name = alias.length() == 0 ? name : alias + ":" + name;
}
// attributes
if (destination != null) {
int attributeCount = parser.getAttributeCount();
for (int i = 0; i < attributeCount; i++) {
// TODO(yanivi): can have repeating attribute values, e.g. "@a=value1 @a=value2"?
String attributeName = parser.getAttributeName(i);
String attributeNamespace = parser.getAttributeNamespace(i);
String attributeAlias = attributeNamespace.length() == 0
? "" : namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(attributeNamespace);
String fieldName = getFieldName(true, attributeAlias, attributeNamespace, attributeName);
Field field = classInfo == null ? null : classInfo.getField(fieldName);
parseAttributeOrTextContent(parser.getAttributeValue(i),
field,
valueType,
context,
destination,
genericXml,
destinationMap,
fieldName);
}
}
Field field;
ArrayValueMap arrayValueMap = new ArrayValueMap(destination);
boolean isStopped = false;
main: while (true) {
int event = parser.next();
switch (event) {
case XmlPullParser.END_DOCUMENT:
isStopped = true;
break main;
case XmlPullParser.END_TAG:
isStopped = customizeParser != null
&& customizeParser.stopAfterEndTag(parser.getNamespace(), parser.getName());
break main;
case XmlPullParser.TEXT:
// parse text content
if (destination != null) {
field = classInfo == null ? null : classInfo.getField(TEXT_CONTENT);
parseAttributeOrTextContent(parser.getText(),
field,
valueType,
context,
destination,
genericXml,
destinationMap,
TEXT_CONTENT);
}
break;
case XmlPullParser.START_TAG:
if (customizeParser != null
&& customizeParser.stopBeforeStartTag(parser.getNamespace(), parser.getName())) {
isStopped = true;
break main;
}
if (destination == null) {
parseTextContentForElement(parser, context, true, null);
} else {
// element
parseNamespacesForElement(parser, namespaceDictionary);
String namespace = parser.getNamespace();
String alias = namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(namespace);
String fieldName = getFieldName(false, alias, namespace, parser.getName());
field = classInfo == null ? null : classInfo.getField(fieldName);
Type fieldType = field == null ? valueType : field.getGenericType();
fieldType = Data.resolveWildcardTypeOrTypeVariable(context, fieldType);
// field type is now class, parameterized type, or generic array type
// resolve a parameterized type to a class
Class<?> fieldClass = fieldType instanceof Class<?> ? (Class<?>) fieldType : null;