use of jp.ossc.nimbus.beans.dataset.RecordListPropertySchema in project nimbus by nimbus-org.
the class DataSetXpathConverter method createRecord.
private void createRecord(Document document, DataSet dataSet, Object target, RecordSchema recordSchema) {
PropertySchema[] propertySchemata = recordSchema.getPropertySchemata();
for (int i = 0; i < propertySchemata.length; i++) {
if (propertySchemata[i] instanceof XpathPropertySchema) {
// PropertySchemaからXPath取得
XpathPropertySchema xmlBindingPropertySchema = (XpathPropertySchema) propertySchemata[i];
XPathExpression expression = xmlBindingPropertySchema.getXpathExpression();
// XPathによりXML要素を抽出
NodeList nodeList = null;
try {
nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
throw new ConvertException("The converter failed to evaluate a XML. ", e);
}
// DataSetへ変換
int length = nodeList.getLength();
if (target instanceof Record) {
if (length > 0) {
Object nodeValue = nodeList.item(0).getNodeValue();
((Record) target).setParseProperty(xmlBindingPropertySchema.getName(), nodeValue);
}
} else if (target instanceof RecordList) {
RecordList targetRecordList = (RecordList) target;
int offset = length - targetRecordList.size();
if (offset > 0) {
for (int j = 0; j < offset; j++) {
Record record = targetRecordList.createRecord();
targetRecordList.addRecord(record);
}
}
for (int j = 0; j < length; j++) {
Object nodeValue = nodeList.item(j).getNodeValue();
Record record = targetRecordList.getRecord(j);
record.setParseProperty(xmlBindingPropertySchema.getName(), nodeValue);
}
}
} else if (propertySchemata[i] instanceof RecordPropertySchema) {
RecordPropertySchema recordPropertySchema = (RecordPropertySchema) propertySchemata[i];
RecordSchema nestedRecordSchema = dataSet.getNestedRecordSchema(recordPropertySchema.getName());
Record nestedRecord = dataSet.createNestedRecord(recordPropertySchema.getRecordName());
createRecord(document, dataSet, target, nestedRecordSchema);
((Record) target).setProperty(recordPropertySchema.getName(), nestedRecord);
} else if (propertySchemata[i] instanceof RecordListPropertySchema) {
RecordListPropertySchema recordListPropertySchema = (RecordListPropertySchema) propertySchemata[i];
RecordSchema nestedRecordSchema = dataSet.getNestedRecordListSchema(recordListPropertySchema.getRecordListName());
RecordList nestedRecordList = dataSet.createNestedRecordList(recordListPropertySchema.getRecordListName());
createRecord(document, dataSet, nestedRecordList, nestedRecordSchema);
((Record) target).setProperty(recordListPropertySchema.getName(), nestedRecordList);
}
}
}
Aggregations