set to null if empty tag specified

This commit is contained in:
Vladimir Krivosheev
2014-11-10 12:21:19 +01:00
parent 7934a58e81
commit 483f9c0e98
4 changed files with 21 additions and 44 deletions
@@ -38,6 +38,10 @@ abstract class Binding {
@Nullable
public abstract Object deserialize(Object context, @NotNull Object node);
public Object deserializeEmpty(Object context) {
return null;
}
public abstract boolean isBoundTo(Object node);
public abstract Class getBoundNodeType();
@@ -52,8 +56,15 @@ abstract class Binding {
return ((MultiNodeBinding)binding).deserializeList(context, nodes);
}
else {
assert nodes.size() == 1;
return binding.deserialize(context, nodes.get(0));
if (nodes.size() == 1) {
return binding.deserialize(context, nodes.get(0));
}
else if (nodes.isEmpty()) {
return binding.deserializeEmpty(context);
}
else {
throw new AssertionError("Duplicate data for " + binding + " will be ignored");
}
}
}
}
@@ -37,16 +37,10 @@ public class DateBinding extends PrimitiveValueBinding {
@Override
protected Object convertString(String value) {
try {
long l = Long.parseLong(value);
return new Date(l);
return new Date(Long.parseLong(value));
}
catch (NumberFormatException e) {
return new Date(0);
}
}
@Override
public boolean isMulti() {
return false;
}
}
@@ -71,15 +71,7 @@ class JDOMElementBinding extends Binding implements MultiNodeBinding {
myAccessor.write(context, nodes.toArray(new Element[nodes.size()]));
}
else {
Element element = null;
for (Object aNode : nodes) {
if (!XmlSerializerImpl.isIgnoredNode(aNode)) {
element = (Element)aNode;
break;
}
}
assert element != null;
myAccessor.write(context, element);
myAccessor.write(context, nodes.get(0));
}
return context;
}
@@ -21,9 +21,7 @@ import org.jdom.Text;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
class PrimitiveValueBinding extends Binding implements MultiNodeBinding {
class PrimitiveValueBinding extends Binding {
private final Class<?> myType;
public PrimitiveValueBinding(@NotNull Class<?> myType, @Nullable Accessor accessor) {
@@ -37,35 +35,17 @@ class PrimitiveValueBinding extends Binding implements MultiNodeBinding {
return new Text(String.valueOf(o));
}
@Nullable
@Override
public Object deserializeList(Object context, @NotNull List<?> nodes) {
if (nodes.isEmpty()) {
return convertString("");
}
else if (nodes.size() > 1) {
StringBuilder result = new StringBuilder();
for (Object node : nodes) {
result.append(node instanceof Attribute ? ((Attribute)node).getValue() : ((Content)node).getValue());
}
return convertString(result.toString());
}
else {
return deserialize(context, nodes.get(0));
}
}
@Override
public boolean isMulti() {
return true;
}
@Override
@Nullable
public Object deserialize(Object o, @NotNull Object node) {
return convertString(node instanceof Attribute ? ((Attribute)node).getValue() : ((Content)node).getValue());
}
@Override
public Object deserializeEmpty(Object context) {
return convertString("");
}
@Nullable
protected Object convertString(String value) {
return XmlSerializerImpl.convert(value, myType);