use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbInfo method getKeyValuePairElementInfo.
/**
* If this object has keyvaluepairs for children, this returns information about them, otherwise returns null.
* Note implicit assumption that there can only be one set of keyvaluepairs - this is because the JSON
* representation would be unable to differentiate between them.
*/
public KeyValuePairXmlRepresentationInfo getKeyValuePairElementInfo() {
if (haveKvpXmlInfo) {
return kvpXmlInfo;
}
String elemName = null;
String attrName = null;
Field[] fields = jaxbClass.getDeclaredFields();
for (Field field : fields) {
ZimbraKeyValuePairs annot = field.getAnnotation(ZimbraKeyValuePairs.class);
if (annot == null) {
continue;
}
XmlElement xmlElemAnnot = field.getAnnotation(XmlElement.class);
if (xmlElemAnnot != null) {
elemName = xmlElemAnnot.name();
} else {
elemName = field.getName();
}
}
if (elemName != null) {
Method[] methods = jaxbClass.getDeclaredMethods();
for (Method method : methods) {
ZimbraKeyValuePairs annot = method.getAnnotation(ZimbraKeyValuePairs.class);
if (annot == null) {
continue;
}
XmlElement xmlElemAnnot = method.getAnnotation(XmlElement.class);
if (xmlElemAnnot != null) {
elemName = xmlElemAnnot.name();
} else {
elemName = method.getName();
}
}
}
if (elemName != null) {
Class<?> kvpElemClass = this.getClassForElement(elemName);
if (kvpElemClass != null) {
JaxbInfo kvpJaxbInfo = JaxbInfo.getFromCache(kvpElemClass);
if (kvpJaxbInfo != null) {
Iterable<String> attribNames = kvpJaxbInfo.getAttributeNames();
if (attribNames != null) {
for (String attribName : attribNames) {
// Should only be one...
attrName = attribName;
break;
}
}
}
}
}
if ((elemName != null) && (attrName != null)) {
kvpXmlInfo = new KeyValuePairXmlRepresentationInfo(elemName, attrName);
} else {
kvpXmlInfo = null;
}
haveKvpXmlInfo = true;
return kvpXmlInfo;
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbInfo method processFieldRelatedAnnotations.
private void processFieldRelatedAnnotations(Annotation[] annots, String fieldName, Type defaultGenericType) {
WrappedElementInfo wrappedInfo = null;
for (Annotation annot : annots) {
if (annot instanceof XmlElementWrapper) {
XmlElementWrapper wrapper = (XmlElementWrapper) annot;
wrappedInfo = new WrappedElementInfo(wrapper, fieldName);
jaxbElemNodeInfo.add(wrappedInfo);
break;
}
}
for (Annotation annot : annots) {
if (annot instanceof XmlValue) {
elementValue = new JaxbValueInfo((XmlValue) annot, fieldName, defaultGenericType);
} else if (annot instanceof XmlAttribute) {
XmlAttribute attr = (XmlAttribute) annot;
String attrName = attr.name();
if ((attrName == null) || DEFAULT_MARKER.equals(attrName)) {
attrName = fieldName;
}
this.setXmlAttributeInfo(attr, fieldName, defaultGenericType);
this.attributeNames.add(attrName);
} else if (annot instanceof XmlElement) {
XmlElement xmlElem = (XmlElement) annot;
if (wrappedInfo == null) {
setXmlElementInfo(xmlElem, fieldName, defaultGenericType);
} else {
wrappedInfo.add(xmlElem, fieldName, defaultGenericType);
}
} else if (annot instanceof XmlElementRef) {
XmlElementRef xmlElemR = (XmlElementRef) annot;
if (wrappedInfo == null) {
setXmlElementInfo(xmlElemR, null, null);
} else {
wrappedInfo.add(xmlElemR, null, null);
}
} else if (annot instanceof XmlElements) {
JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
if (wrappedInfo == null) {
jaxbElemNodeInfo.add(choiceNode);
} else {
wrappedInfo.add(choiceNode);
}
XmlElements xmlElemsAnnot = (XmlElements) annot;
for (XmlElement xmlE : xmlElemsAnnot.value()) {
choiceNode.add(xmlE);
}
} else if (annot instanceof XmlElementRefs) {
JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
if (wrappedInfo == null) {
jaxbElemNodeInfo.add(choiceNode);
} else {
wrappedInfo.add(choiceNode);
}
XmlElementRefs elemRefs = (XmlElementRefs) annot;
for (XmlElementRef xmlE : elemRefs.value()) {
choiceNode.add(xmlE);
}
}
}
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.
/**
* Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
* (i.e. An "_attrs" array).
* In this case, the target objects for each keyvalue pair do NOT use the defaults of "a" for the element name and
* "n" for the attribute name in the XML form.
* Desired XML :<br />
* <key-value-pairs-tester xmlns="urn:zimbraTest">
* <oddElemName name="key1">value1</oddElemName>
* <oddElemName name="key2">value2-a</oddElemName>
* <oddElemName name="key2">value2-b</oddElemName>
* </key-value-pairs-tester>
*<br />
* Desired JSON :<br />
* {
* "_attrs": {
* "key1": "value1",
* "key2": [
* "value2-a",
* "value2-b"]
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
List<Attr> attrs = Lists.newArrayList();
attrs.add(new Attr("key1", "value1"));
attrs.add(new Attr("key2", "value2-a"));
attrs.add(new Attr("key2", "value2-b"));
OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(attrs);
logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
String origJson = jsonJaxbElem.prettyPrint();
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
List<Attr> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
String finalJson = jsonJaxbElem.prettyPrint();
if (!origJson.equals(finalJson)) {
logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
}
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method anyElementHandling.
/**
* {@link XmlAnyElement} handling
* <pre>
* @XmlAnyElement
* private List<org.w3c.dom.Element> elems = Lists.newArrayList();
* </pre>
*/
@Test
public void anyElementHandling() throws Exception {
String given = "Given information";
List<Object> elems = Lists.newArrayList();
AnyTester jaxb = new AnyTester();
DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
elem.setAttribute("myAttr", "myValue");
org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
child.setTextContent("Purple beans");
elem.appendChild(child);
org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
child2.setAttribute("name", "Kate");
child2.setAttribute("age", "23");
elem.appendChild(child2);
elems.add(elem);
org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
elem2.setAttribute("fourth", "be with you");
jaxb.setGiven(given);
jaxb.setElems(Lists.newArrayList(elem, elem2));
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method multipleDispositionCONTENTAttributes.
/**
* <p>Demonstrates that CANNOT have more than one attribute with same name, even if using
* {@link Element.Disposition.CONTENT} to force treating the attribute as an element in XML.</p>
* Desired serialization to XML:
* <pre>
* <multi-content-attrs xmlns="urn:zimbraTest">
* <soapURL>https://soap.example.test</soapURL>
* </multi-content-attrs>
* </pre>
* Desired serialization to JSON:
* <pre>
* {
* "soapURL": "https://soap.example.test",
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void multipleDispositionCONTENTAttributes() throws Exception {
final String httpSoap = "http://soap.example.test";
final String httpsSoap = "https://soap.example.test";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
logDebug("MultiContentAttrs XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("MultiContentAttrs JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
Assert.assertEquals("XMLElement soapURL as attribute", httpsSoap, xmlElem.getAttribute(AccountConstants.E_SOAP_URL));
Assert.assertEquals("XMLElement num soapURL elements", 1, xmlElem.listElements(AccountConstants.E_SOAP_URL).size());
Assert.assertEquals("XMLElement soapURL as element", httpsSoap, xmlElem.getElement(AccountConstants.E_SOAP_URL).getText());
Assert.assertEquals("JSONElement soapURL as attribute", httpsSoap, jsonElem.getAttribute(AccountConstants.E_SOAP_URL));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num soapURL elements", 0, jsonElem.listElements(AccountConstants.E_SOAP_URL).size());
}
Aggregations