*
* @param op protocol buffer document operation to deserialize
* @return deserialized DocOp
*/
public static DocOp deserialize(ProtocolDocumentOperation op) {
DocOpBuilder output = new DocOpBuilder();
for (ProtocolDocumentOperation.Component c : op.getComponentList()) {
if (c.hasAnnotationBoundary()) {
if (c.getAnnotationBoundary().getEmpty()) {
output.annotationBoundary(AnnotationBoundaryMapImpl.EMPTY_MAP);
} else {
String[] ends = new String[c.getAnnotationBoundary().getEndCount()];
String[] changeKeys = new String[c.getAnnotationBoundary().getChangeCount()];
String[] oldValues = new String[c.getAnnotationBoundary().getChangeCount()];
String[] newValues = new String[c.getAnnotationBoundary().getChangeCount()];
if (c.getAnnotationBoundary().getEndCount() > 0) {
c.getAnnotationBoundary().getEndList().toArray(ends);
}
for (int i = 0; i < changeKeys.length; i++) {
ProtocolDocumentOperation.Component.KeyValueUpdate kvu =
c.getAnnotationBoundary().getChange(i);
changeKeys[i] = kvu.getKey();
oldValues[i] = kvu.hasOldValue() ? kvu.getOldValue() : null;
newValues[i] = kvu.hasNewValue() ? kvu.getNewValue() : null;
}
output.annotationBoundary(
new AnnotationBoundaryMapImpl(ends, changeKeys, oldValues, newValues));
}
} else if (c.hasCharacters()) {
output.characters(c.getCharacters());
} else if (c.hasElementStart()) {
Map<String, String> attributesMap = Maps.newHashMap();
for (ProtocolDocumentOperation.Component.KeyValuePair pair :
c.getElementStart().getAttributeList()) {
attributesMap.put(pair.getKey(), pair.getValue());
}
output.elementStart(c.getElementStart().getType(), new AttributesImpl(attributesMap));
} else if (c.hasElementEnd()) {
output.elementEnd();
} else if (c.hasRetainItemCount()) {
output.retain(c.getRetainItemCount());
} else if (c.hasDeleteCharacters()) {
output.deleteCharacters(c.getDeleteCharacters());
} else if (c.hasDeleteElementStart()) {
Map<String, String> attributesMap = Maps.newHashMap();
for (ProtocolDocumentOperation.Component.KeyValuePair pair :
c.getDeleteElementStart().getAttributeList()) {
attributesMap.put(pair.getKey(), pair.getValue());
}
output.deleteElementStart(c.getDeleteElementStart().getType(),
new AttributesImpl(attributesMap));
} else if (c.hasDeleteElementEnd()) {
output.deleteElementEnd();
} else if (c.hasReplaceAttributes()) {
if (c.getReplaceAttributes().getEmpty()) {
output.replaceAttributes(AttributesImpl.EMPTY_MAP, AttributesImpl.EMPTY_MAP);
} else {
Map<String, String> oldAttributesMap = Maps.newHashMap();
Map<String, String> newAttributesMap = Maps.newHashMap();
for (ProtocolDocumentOperation.Component.KeyValuePair pair :
c.getReplaceAttributes().getOldAttributeList()) {
oldAttributesMap.put(pair.getKey(), pair.getValue());
}
for (ProtocolDocumentOperation.Component.KeyValuePair pair :
c.getReplaceAttributes().getNewAttributeList()) {
newAttributesMap.put(pair.getKey(), pair.getValue());
}
output.replaceAttributes(new AttributesImpl(oldAttributesMap),
new AttributesImpl(newAttributesMap));
}
} else if (c.hasUpdateAttributes()) {
if (c.getUpdateAttributes().getEmpty()) {
output.updateAttributes(AttributesUpdateImpl.EMPTY_MAP);
} else {
String[] triplets = new String[c.getUpdateAttributes().getAttributeUpdateCount()*3];
for (int i = 0, j = 0; i < c.getUpdateAttributes().getAttributeUpdateCount(); i++) {
ProtocolDocumentOperation.Component.KeyValueUpdate kvu =
c.getUpdateAttributes().getAttributeUpdate(i);
triplets[j++] = kvu.getKey();
triplets[j++] = kvu.hasOldValue() ? kvu.getOldValue() : null;
triplets[j++] = kvu.hasNewValue() ? kvu.getNewValue() : null;
}
output.updateAttributes(new AttributesUpdateImpl(triplets));
}
} else {
//throw new IllegalArgumentException("Unsupported operation component: " + c);
}
}
return output.build();
}