If tag is specified but nameAttribute is not, name attribute will not be added

This commit is contained in:
Vladimir Krivosheev
2014-12-17 16:33:29 +01:00
parent a4ba663e06
commit 581a3e890a
4 changed files with 36 additions and 9 deletions
@@ -42,14 +42,15 @@ class OptionTagBinding extends BasePrimitiveBinding {
myValueAttribute = Constants.VALUE;
}
else {
myNameAttribute = optionTag.nameAttribute();
myValueAttribute = optionTag.valueAttribute();
String tagName = optionTag.tag();
if (StringUtil.isEmpty(myNameAttribute) && Constants.OPTION.equals(tagName)) {
String nameAttribute = optionTag.nameAttribute();
if (StringUtil.isEmpty(nameAttribute) && Constants.OPTION.equals(tagName)) {
tagName = myAccessor.getName();
}
myTagName = tagName;
myNameAttribute = tagName.equals(Constants.OPTION) || !nameAttribute.equals(Constants.NAME) ? StringUtil.nullize(nameAttribute) : null;
}
}
@@ -59,7 +60,7 @@ class OptionTagBinding extends BasePrimitiveBinding {
Object value = myAccessor.read(o);
Element targetElement = new Element(myTagName);
if (!StringUtil.isEmpty(myNameAttribute)) {
if (myNameAttribute != null) {
targetElement.setAttribute(myNameAttribute, myName);
}
@@ -119,14 +120,22 @@ class OptionTagBinding extends BasePrimitiveBinding {
@Override
public boolean isBoundTo(Object node) {
if (!(node instanceof Element)) return false;
if (!(node instanceof Element)) {
return false;
}
Element e = (Element)node;
if (!e.getName().equals(myTagName)) return false;
if (!e.getName().equals(myTagName)) {
return false;
}
String name = e.getAttributeValue(myNameAttribute);
if (StringUtil.isEmpty(myNameAttribute)) {
if (myNameAttribute == null) {
return name == null || name.equals(myName);
}
return name != null && name.equals(myName);
else {
return myName.equals(name);
}
}
@NonNls
@@ -24,9 +24,13 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Please consider to use annotation parameters only to achieve backward compatibility. Otherwise feel free to file issues about serialization cosmetics.
*
* <p>Store value in tag like {@code <option name="optionName" value="optionValue"/>}</p>
* <p>nameAttribute can be empty, in which case it is skipped: {@code <option value="optionValue" />}</p>
*
* If {@link #tag} is specified but {@link #nameAttribute} is not, name attribute will not be added: {@code <customTagName value="optionValue" />}
*
* @author nik
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -1245,6 +1245,20 @@ public class XmlSerializerTest extends TestCase {
checkSmartSerialization(new Bean2(), "<Bean2 ac=\"2\" ab=\"32\" module=\"\" />");
}
public void testIgnoreNameAttributeIfTagSpecified() throws IOException, JDOMException {
Bean3 bean = new Bean3();
bean.MOVE_TO_FAILED_COMMIT_CHANGELIST = "foo";
doSerializerTest("<bean>\n" +
" <confirmMoveToFailedCommit value=\"foo\" />\n" +
"</bean>", bean, new SkipDefaultValuesSerializationFilters());
}
@Tag("bean")
static class Bean3 {
@OptionTag(tag = "confirmMoveToFailedCommit")
public String MOVE_TO_FAILED_COMMIT_CHANGELIST;
}
private static void checkSmartSerialization(@NotNull Bean2 bean, @NotNull String serialized) throws IOException, JDOMException {
SmartSerializer serializer = new SmartSerializer();
serializer.readExternal(bean, JDOMUtil.loadDocument(serialized).getRootElement());
@@ -63,9 +63,9 @@ public final class VcsConfiguration implements PersistentStateComponent<VcsConfi
public boolean PERFORM_ADD_REMOVE_IN_BACKGROUND = true;
public boolean PERFORM_ROLLBACK_IN_BACKGROUND = false;
public volatile boolean CHECK_LOCALLY_CHANGED_CONFLICTS_IN_BACKGROUND = false;
@OptionTag(tag = "confirmMoveToFailedCommit", nameAttribute = "")
@OptionTag(tag = "confirmMoveToFailedCommit")
public VcsShowConfirmationOption.Value MOVE_TO_FAILED_COMMIT_CHANGELIST = VcsShowConfirmationOption.Value.SHOW_CONFIRMATION;
@OptionTag(tag = "confirmRemoveEmptyChangelist", nameAttribute = "")
@OptionTag(tag = "confirmRemoveEmptyChangelist")
public VcsShowConfirmationOption.Value REMOVE_EMPTY_INACTIVE_CHANGELISTS = VcsShowConfirmationOption.Value.SHOW_CONFIRMATION;
public int CHANGED_ON_SERVER_INTERVAL = 60;
public boolean SHOW_ONLY_CHANGED_IN_SELECTION_DIFF = true;