Search in sources :

Example 1 with RecordPropertySchema

use of jp.ossc.nimbus.beans.dataset.RecordPropertySchema 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);
        }
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XpathPropertySchema(jp.ossc.nimbus.beans.dataset.XpathPropertySchema) PropertySchema(jp.ossc.nimbus.beans.dataset.PropertySchema) RecordPropertySchema(jp.ossc.nimbus.beans.dataset.RecordPropertySchema) RecordListPropertySchema(jp.ossc.nimbus.beans.dataset.RecordListPropertySchema) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) Record(jp.ossc.nimbus.beans.dataset.Record) RecordPropertySchema(jp.ossc.nimbus.beans.dataset.RecordPropertySchema) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema) RecordListPropertySchema(jp.ossc.nimbus.beans.dataset.RecordListPropertySchema) XpathPropertySchema(jp.ossc.nimbus.beans.dataset.XpathPropertySchema)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 PropertySchema (jp.ossc.nimbus.beans.dataset.PropertySchema)1 Record (jp.ossc.nimbus.beans.dataset.Record)1 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)1 RecordListPropertySchema (jp.ossc.nimbus.beans.dataset.RecordListPropertySchema)1 RecordPropertySchema (jp.ossc.nimbus.beans.dataset.RecordPropertySchema)1 RecordSchema (jp.ossc.nimbus.beans.dataset.RecordSchema)1 XpathPropertySchema (jp.ossc.nimbus.beans.dataset.XpathPropertySchema)1 NodeList (org.w3c.dom.NodeList)1