Search in sources :

Example 66 with XmlElement

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;
}
Also used : Field(java.lang.reflect.Field) ZimbraKeyValuePairs(com.zimbra.soap.json.jackson.annotate.ZimbraKeyValuePairs) XmlElement(javax.xml.bind.annotation.XmlElement) Method(java.lang.reflect.Method)

Example 67 with XmlElement

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);
            }
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElements(javax.xml.bind.annotation.XmlElements) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlValue(javax.xml.bind.annotation.XmlValue) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementRefs(javax.xml.bind.annotation.XmlElementRefs) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 68 with XmlElement

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 />
 * &lt;key-value-pairs-tester xmlns="urn:zimbraTest">
 *   &lt;oddElemName name="key1">value1&lt;/oddElemName>
 *   &lt;oddElemName name="key2">value2-a&lt;/oddElemName>
 *   &lt;oddElemName name="key2">value2-b&lt;/oddElemName>
 * &lt;/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);
    }
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElemJsonAttr(com.zimbra.soap.jaxb.XmlElemJsonAttr) Attr(com.zimbra.soap.account.type.Attr) OddKeyValuePairsTester(com.zimbra.soap.jaxb.OddKeyValuePairsTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 69 with XmlElement

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());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) AnyTester(com.zimbra.soap.jaxb.AnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 70 with XmlElement

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>
 *     &lt;multi-content-attrs xmlns="urn:zimbraTest">
 *       &lt;soapURL>https://soap.example.test&lt;/soapURL>
 *     &lt;/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());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)85 Test (org.junit.Test)31 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)29 Element (com.zimbra.common.soap.Element)28 JSONElement (com.zimbra.common.soap.Element.JSONElement)28 XMLElement (com.zimbra.common.soap.Element.XMLElement)28 FilterTest (com.zimbra.soap.mail.type.FilterTest)26 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)14 Method (java.lang.reflect.Method)13 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)13 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)13 XmlElements (javax.xml.bind.annotation.XmlElements)10 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)8 KeyValuePair (com.zimbra.soap.type.KeyValuePair)7 Annotation (java.lang.annotation.Annotation)6 Type (java.lang.reflect.Type)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 XmlList (javax.xml.bind.annotation.XmlList)4