* Convert the given document mutation op into a mutation op message
* @param docOp
* @return DocumentMutation
*/
public static ProtocolDocumentOperation createMutationOp(DocOp docOp) {
final ProtocolDocumentOperation mutation =
MessageFactoryHelper.createDocumentOperation();
docOp.apply(new DocOpCursor() {
@Override
public void retain(int itemCount) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setRetainItemCount(itemCount);
mutation.addComponent(component);
}
@Override
public void characters(String characters) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setCharacters(characters);
mutation.addComponent(component);
}
@Override
public void elementStart(String type, Attributes attributes) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setElementStart(createElementStart(type, attributes));
mutation.addComponent(component);
}
@Override
public void elementEnd() {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setElementEnd(true);
mutation.addComponent(component);
}
@Override
public void deleteCharacters(String characters) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setDeleteCharacters(characters);
mutation.addComponent(component);
}
@Override
public void deleteElementStart(String type, Attributes attributes) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setDeleteElementStart(createElementStart(type, attributes));
mutation.addComponent(component);
}
@Override
public void deleteElementEnd() {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
component.setDeleteElementEnd(true);
mutation.addComponent(component);
}
@Override
public void replaceAttributes(Attributes oldAttributes, Attributes newAttributes) {
Component component =
MessageFactoryHelper.createDocumentOperationComponent();
ReplaceAttributes replace =
MessageFactoryHelper.createDocumentReplaceAttributes();
if (oldAttributes.isEmpty() && newAttributes.isEmpty()) {
replace.setEmpty(true);
} else {
for (Map.Entry<String, String> e : oldAttributes.entrySet()) {
replace.addOldAttribute(createKeyValuePair(e.getKey(), e.getValue()));
}
for (Map.Entry<String, String> e : newAttributes.entrySet()) {
replace.addNewAttribute(createKeyValuePair(e.getKey(), e.getValue()));
}
}
component.setReplaceAttributes(replace);
mutation.addComponent(component);
}
@Override
public void updateAttributes(AttributesUpdate update) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
UpdateAttributes updateAttributes =
MessageFactoryHelper.createDocumentUpdateAttributes();
int n = update.changeSize();
if (n == 0) {
updateAttributes.setEmpty(true);
} else {
for (int i = 0; i < n; i++) {
updateAttributes.addAttributeUpdate(createKeyValueUpdate(
update.getChangeKey(i), update.getOldValue(i), update.getNewValue(i)));
}
}
component.setUpdateAttributes(updateAttributes);
mutation.addComponent(component);
}
@Override
public void annotationBoundary(AnnotationBoundaryMap map) {
Component component = MessageFactoryHelper.createDocumentOperationComponent();
AnnotationBoundary boundary =
MessageFactoryHelper.createDocumentAnnotationBoundary();
int endSize = map.endSize();
int changeSize = map.changeSize();
if (endSize == 0 && changeSize == 0) {
boundary.setEmpty(true);
} else {
for (int i = 0; i < endSize; i++) {
boundary.addEnd(map.getEndKey(i));
}
for (int i = 0; i < changeSize; i++) {
boundary.addChange(createKeyValueUpdate(map.getChangeKey(i),
map.getOldValue(i), map.getNewValue(i)));
}
}
component.setAnnotationBoundary(boundary);
mutation.addComponent(component);
}
});
return mutation;
}