use of com.yahoo.vespa.objects.FieldBase in project vespa by vespa-engine.
the class VespaXMLFieldReader method read.
public void read(FieldBase field, Document document) {
try {
// workaround for documents inside array <item>
if (reader.getEventType() != XMLStreamReader.START_ELEMENT || !"document".equals(reader.getName().toString())) {
while (reader.hasNext()) {
if (reader.getEventType() == XMLStreamReader.START_ELEMENT && "document".equals(reader.getName().toString())) {
break;
}
reader.next();
}
}
// First fetch attributes.
String typeName = null;
for (int i = 0; i < reader.getAttributeCount(); i++) {
final String attributeName = reader.getAttributeName(i).toString();
if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
document.setId(new DocumentId(reader.getAttributeValue(i)));
} else if ("documenttype".equals(attributeName) || "type".equals(attributeName)) {
typeName = reader.getAttributeValue(i);
} else if ("condition".equals(attributeName)) {
condition = Optional.of(reader.getAttributeValue(i));
}
}
if (document.getId() != null) {
if (field == null) {
field = new FieldBase(document.getId().toString());
}
}
DocumentType doctype = docTypeManager.getDocumentType(typeName);
if (doctype == null) {
throw newDeserializeException(field, "Must specify an existing document type, not '" + typeName + "'");
} else {
document.setDataType(doctype);
}
// Then fetch fields
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamReader.START_ELEMENT) {
Field f = doctype.getField(reader.getName().toString());
if (f == null) {
throw newDeserializeException(field, "Field " + reader.getName() + " not found.");
}
FieldValue fv = f.getDataType().createFieldValue();
fv.deserialize(f, this);
document.setFieldValue(f, fv);
skipToEnd(f.getName());
} else if (type == XMLStreamReader.END_ELEMENT) {
return;
}
}
} catch (XMLStreamException e) {
throw newException(field, e);
}
}
Aggregations