this.modelType = modelType;
}
@SuppressWarnings({"unchecked", "rawtypes"})
public D read(Object loadConfig, Object data) {
Document doc = XMLParser.parse((String) data);
NodeList list = doc.getElementsByTagName(modelType.getRecordName());
ArrayList<ModelData> records = new ArrayList<ModelData>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
Element elem = (Element) node;
ModelData model = newModelInstance();
for (int j = 0; j < modelType.getFieldCount(); j++) {
DataField field = modelType.getField(j);
Class type = field.getType();
String name = field.getName();
String map = field.getMap() != null ? field.getMap() : field.getName();
String v = getValue(elem, map);
if (v == null) continue;
if (type != null) {
if (type.equals(Boolean.class)) {
model.set(name, Boolean.parseBoolean(v));
} else if (type.equals(Integer.class)) {
model.set(name, Integer.parseInt(v));
} else if (type.equals(Long.class)) {
model.set(name, Long.parseLong(v));
} else if (type.equals(Float.class)) {
model.set(name, Float.parseFloat(v));
} else if (type.equals(Double.class)) {
model.set(name, Double.parseDouble(v));
} else if (type.equals(Date.class)) {
if ("timestamp".equals(field.getFormat())) {
Date d = new Date(Long.parseLong(v) * 1000);
model.set(name, d);
} else {
DateTimeFormat format = DateTimeFormat.getFormat(field.getFormat());
Date d = format.parse(v);
model.set(name, d);
}
}
} else {
model.set(field.getName(), v);
}
}
records.add(model);
}
int totalCount = records.size();
Node root = doc.getElementsByTagName(modelType.getRoot()).item(0);
if (root != null && modelType.getTotalName() != null) {
String tot = getValue((Element) root, modelType.getTotalName());
if (tot != null) {
totalCount = Integer.parseInt(tot);
}