package com.fasterxml.jackson.dataformat.protobuf;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage;
public class ProtobufWriteContext
extends JsonStreamContext
{
protected final ProtobufWriteContext _parent;
protected ProtobufMessage _message;
protected ProtobufField _field;
protected Object _currentValue;
protected ProtobufWriteContext _child = null;
protected ProtobufWriteContext(int type, ProtobufWriteContext parent,
ProtobufMessage msg)
{
super();
_type = type;
_parent = parent;
_message = msg;
}
private void reset(int type, ProtobufMessage msg, ProtobufField f) {
_type = type;
_message = msg;
_field = f;
_currentValue = null;
}
public static ProtobufWriteContext createRootContext(ProtobufMessage msg) {
return new ProtobufWriteContext(TYPE_ROOT, null, msg);
}
public static ProtobufWriteContext createNullContext() {
return null;
}
public ProtobufWriteContext createChildArrayContext() {
ProtobufWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new ProtobufWriteContext(TYPE_ARRAY, this, _message);
ctxt._field = _field;
return ctxt;
}
ctxt.reset(TYPE_ARRAY, _message, _field);
return ctxt;
}
public ProtobufWriteContext createChildObjectContext(ProtobufMessage type) {
ProtobufWriteContext ctxt = _child;
if (ctxt == null) {
_child = ctxt = new ProtobufWriteContext(TYPE_OBJECT, this, type);
return ctxt;
}
ctxt.reset(TYPE_OBJECT, type, null);
return ctxt;
}
@Override
public final ProtobufWriteContext getParent() { return _parent; }
@Override
public String getCurrentName() {
return ((_type == TYPE_OBJECT) && (_field != null)) ? _field.name : null;
}
@Override
public Object getCurrentValue() {
return _currentValue;
}
@Override
public void setCurrentValue(Object v) {
_currentValue = v;
}
public void setField(ProtobufField f) {
_field = f;
}
public ProtobufField getField() {
return _field;
}
public ProtobufMessage getMessageType() {
return _message;
}
public boolean notArray() { return _type != TYPE_ARRAY; }
public StringBuilder appendDesc(StringBuilder sb) {
if (_parent != null) {
sb = _parent.appendDesc(sb);
}
sb.append('/');
switch (_type) {
case TYPE_OBJECT:
if (_field != null) {
sb.append(_field.name);
}
break;
case TYPE_ARRAY:
sb.append(getCurrentIndex());
break;
case TYPE_ROOT:
}
return sb;
}
@Override
public final String toString() {
return appendDesc(new StringBuilder(64)).toString();
}
}