use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method wrapperAbsentIfEmpty.
/**
* How to achieve a wrapped list where no element for the wrapper will appear in the XML if the list is empty.
* This no longer works! Fortunately, looks like we don't use any classes similar to WrapperAbsentIfEmpty
* at the moment.
* For JUDASPRIEST/Java 7 setNumbers is called with 2 integers. For main/Java 8
* it is called with an empty list.
* WrapperAbsentIfEmpty.setNumbers(List<Integer>) line: 51
* WrapperAbsentIfEmpty$JaxbAccessorM_getNumbers_setNumbers_java_util_List.set(Object, Object) line: 60
* Lister$CollectionLister<BeanT,T>.startPacking(BeanT, Accessor<BeanT,T>) line: 298
* Lister$CollectionLister<BeanT,T>.startPacking(Object, Accessor) line: 269
* Scope<BeanT,PropT,ItemT,PackT>.start(Accessor<BeanT,PropT>, Lister<BeanT,PropT,ItemT,PackT>) line: 142
* ArrayERProperty$ItemsLoader.startElement(UnmarshallingContext$State, TagName) line: 119
* UnmarshallingContext._startElement(TagName) line: 501
* UnmarshallingContext.startElement(TagName) line: 480
* InterningXmlVisitor.startElement(TagName) line: 75
* SAXConnector.startElement(String, String, String, Attributes) line: 150
* DOMScanner.visit(Element) line: 244
* DOMScanner.visit(Node) line: 281
* DOMScanner.visit(Element) line: 250
* DOMScanner.scan(Element) line: 127
* UnmarshallerImpl.unmarshal0(Node, JaxBeanInfo) line: 369
* UnmarshallerImpl.unmarshal(Node, Class<T>) line: 348
* JaxbUtil.rawW3cDomDocToJaxb(Document, Class<?>, boolean) line: 1420
* JaxbUtil.w3cDomDocToJaxb(Document, Class<?>, boolean) line: 1392
* JaxbUtil.elementToJaxb(Element, Class<?>) line: 1438
* JaxbToJsonTest.wrapperAbsentIfEmpty() line: 1516
*/
@Ignore("Bug:95509 - this is hypothetical functionality that isn't currently used, so low priority")
@Test
public void wrapperAbsentIfEmpty() throws Exception {
WrapperAbsentIfEmpty jaxb = new WrapperAbsentIfEmpty();
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
WrapperAbsentIfEmpty roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertNull("original getNumbers (empty list)", jaxb.getNumbers());
Assert.assertNull("JSON roundtripped getNumbers (empty list)", roundtripped.getNumbers());
Assert.assertEquals("JSON for empty Numbers", "{\n \"_jsns\": \"urn:zimbraTest\"\n}", jsonJaxbElem.prettyPrint());
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
WrapperAbsentIfEmpty roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
Assert.assertNull("XML roundtripped getNumbers (empty list)", roundtrippedX.getNumbers());
Assert.assertEquals("XML for empty Numbers", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\"/>", xmlElem.prettyPrint());
jaxb.addNumber(314159);
jaxb.addNumber(42);
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("original size of Numbers (2 numbers)", 2, jaxb.getNumbers().size());
Assert.assertNotNull("XML roundtripped getNumbers (when 2 numbers in list)", roundtrippedX.getNumbers());
Assert.assertEquals("JSON roundtripped size of Numbers (2 numbers)", 2, roundtripped.getNumbers().size());
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
Assert.assertEquals("XML roundtripped size of Numbers (2 numbers)", 2, roundtrippedX.getNumbers().size());
Assert.assertEquals("XML when have 2 numbers", String.format("%s\n %s\n %s\n %s\n %s\n%s", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\">", "<numbers>", "<number>314159</number>", "<number>42</number>", "</numbers>", "</wrapper-absent-if-empty>"), xmlElem.prettyPrint());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method elementRefHandling.
/**
* XmlElementRef handling. Note that slightly counter-intuitively, any name specified is ignored (unless
* type=JAXBElement.class)
* <pre>
* @XmlElementRef(name="ignored-name-root-elem-name-used-instead", type=StringAttribIntValue.class)
* private StringAttribIntValue byRef;
* </pre>
* Desired JSON :
* {
* "string-attr-int-value": [{
* "attr1": "my string",
* "_content": 321
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void elementRefHandling() throws Exception {
String str = "my string";
int num = 321;
ElementRefTester jaxb = new ElementRefTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
jaxb.setByRef(inner);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
ElementRefTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefTester.class);
ElementRefTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefTester.class);
StringAttribIntValue rtByRef = roundtripped.getByRef();
StringAttribIntValue rtXmlByRef = roundtrippedX.getByRef();
Assert.assertEquals("roundtrippedX str", str, rtXmlByRef.getAttrib1());
Assert.assertEquals("roundtrippedX num", num, rtXmlByRef.getMyValue());
Assert.assertEquals("roundtripped str", str, rtByRef.getAttrib1());
Assert.assertEquals("roundtripped num", num, rtByRef.getMyValue());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method booleanMarshal.
/**
* By default JAXB renders true and false in XML as "true" and "false"
* For Zimbra SOAP the XML flavor uses "1" and "0"
* The JSON flavor uses "true" and "false"
* @throws Exception
*/
@Test
public void booleanMarshal() throws Exception {
Element legacy0Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
legacy0Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, false);
logDebug("NoOpResponse JSONElement ---> prettyPrint\n%1$s", legacy0Elem.prettyPrint());
Element legacy1Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
legacy1Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, true);
Element legacyFalseElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
legacyFalseElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "false");
Element legacyTrueElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
legacyTrueElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "true");
// NoOpResponse has:
// @XmlAttribute(name=MailConstants.A_WAIT_DISALLOWED, required=false)
// private ZmBoolean waitDisallowed;
NoOpResponse jaxb = NoOpResponse.create(false);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
logDebug("XMLElement from JAXB (false)---> prettyPrint\n%1$s", xmlElem.prettyPrint());
Assert.assertEquals("false Value of 'waitDisallowed'", "0", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
Element jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
logDebug("NoOpResponse JSONElement from JAXB (false)---> prettyPrint\n%1$s", jsonElem.prettyPrint());
Assert.assertEquals("false Value of 'waitDisallowed'", "false", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
// ensure that marshaling where Boolean has value true works
jaxb.setWaitDisallowed(true);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
logDebug("XMLElement from JAXB (true) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
Assert.assertEquals("true Value of 'waitDisallowed'", "1", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
logDebug("NoOpResponse JSONElement from JAXB (true) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
Assert.assertEquals("true Value of 'waitDisallowed'", "true", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
// ensure that unmarshaling where XML Boolean representation is "1" for true works
jaxb = JaxbUtil.elementToJaxb(legacy1Elem);
Assert.assertEquals("legacy1Elem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
// ensure that unmarshaling where XML Boolean representation is "0" for false works
jaxb = JaxbUtil.elementToJaxb(legacy0Elem);
Assert.assertEquals("legacy0Elem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
// ensure that unmarshaling where XML Boolean representation is "false" works
jaxb = JaxbUtil.elementToJaxb(legacyFalseElem);
Assert.assertEquals("legacyFalseElem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
// ensure that unmarshaling where XML Boolean representation is "true" works
jaxb = JaxbUtil.elementToJaxb(legacyTrueElem);
Assert.assertEquals("legacyTrueElem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method enumElemList.
/**
* Testing form:
* {@code @XmlElement(name="enum-entry", required=false)
* private List<ViewEnum> entries = Lists.newArrayList();}
* Desired JSON :
* {
* "enum-entry": [
* {
* "_content": "appointment"
* },
* {
* "_content": ""
* },
* {
* "_content": "document"
* }],
* "_jsns": "urn:zimbraTest"
*/
@Test
public void enumElemList() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("enum-elem-list", "urn:zimbraTest"));
jsonElem.addElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
jsonElem.addElement("enum-entry").addText(ViewEnum.UNKNOWN.toString());
jsonElem.addElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
EnumElemList tstr = new EnumElemList();
tstr.addEntry(ViewEnum.APPOINTMENT);
tstr.addEntry(ViewEnum.UNKNOWN);
tstr.addEntry(ViewEnum.DOCUMENT);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
EnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumElemList.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
List<Element> jsonElems = jsonJaxbElem.listElements();
List<ViewEnum> entries = roundtripped.getEntries();
Assert.assertEquals("jsonElems num", 3, jsonElems.size());
Assert.assertEquals("entries num", 3, entries.size());
Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
Assert.assertTrue("has UNKNOWN", entries.contains(ViewEnum.UNKNOWN));
Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class NameInfo method setWrappedInfo.
private void setWrappedInfo(AnnotationIntrospector ai, AnnotatedMember prop, String defaultWrappedName) {
ZimbraUniqueElement uniqueElemAnnot = prop.getAnnotation(ZimbraUniqueElement.class);
if (uniqueElemAnnot != null) {
treatAsUniqueElement = uniqueElemAnnot.value();
}
ZimbraJsonArrayForWrapper arrayForWrapperAnnot = prop.getAnnotation(ZimbraJsonArrayForWrapper.class);
if (arrayForWrapperAnnot != null) {
wrapperIsArray = arrayForWrapperAnnot.value();
}
ZimbraKeyValuePairs kvpAnnot = prop.getAnnotation(ZimbraKeyValuePairs.class);
if (kvpAnnot != null) {
keyValuePairs = kvpAnnot.value();
return;
}
ZimbraJsonAttribute jsonAttributeAnnot = prop.getAnnotation(ZimbraJsonAttribute.class);
if (jsonAttributeAnnot != null) {
treatAsAttribute = jsonAttributeAnnot.value();
}
mixedAllowed = (prop.getAnnotation(XmlMixed.class) != null);
anyElementAllowed = (prop.getAnnotation(XmlAnyElement.class) != null);
anyAttributeAllowed = (prop.getAnnotation(XmlAnyAttribute.class) != null);
XmlElement elemAnnot = prop.getAnnotation(XmlElement.class);
if (elemAnnot != null) {
wrappedName = getQName(prop.getName(), elemAnnot.namespace(), elemAnnot.name());
return;
}
XmlElementRef elemRefAnnot = prop.getAnnotation(XmlElementRef.class);
wrappedName = getElementRefName(elemRefAnnot);
if (wrappedName != null) {
return;
}
XmlElements elemsAnnot = prop.getAnnotation(XmlElements.class);
if (elemsAnnot != null) {
XmlElement[] elems = elemsAnnot.value();
wrappedNameMap = Maps.newHashMapWithExpectedSize(elems.length);
for (XmlElement elem : elems) {
QName qn = getQName(prop.getName(), elem.namespace(), elem.name());
Class<?> kls = elem.type();
getWrappedNameMap().put(kls, qn);
}
return;
}
XmlElementRefs elemRefsAnnot = prop.getAnnotation(XmlElementRefs.class);
if (elemRefsAnnot != null) {
XmlElementRef[] elems = elemRefsAnnot.value();
wrappedNameMap = Maps.newHashMapWithExpectedSize(elems.length);
for (XmlElementRef elem : elems) {
QName qn = getElementRefName(elem);
Class<?> kls = elem.type();
getWrappedNameMap().put(kls, qn);
}
return;
}
if (wrapperName != null) {
// We have a wrapper but nothing to tell us what the wrapped name should be, so use default
wrappedName = new QName("", defaultWrappedName);
}
}
Aggregations