All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.plugins.providers.jaxb.JaxbMap Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha4
Show newest version
package org.jboss.resteasy.plugins.providers.jaxb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.namespace.QName;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbMap {
    @XmlAnyElement
    List> value = new ArrayList>();

    @XmlTransient
    private String entryName;
    @XmlTransient
    private String keyAttributeName;
    @XmlTransient
    private String namespace;

    public JaxbMap() {
    }

    public JaxbMap(final String entryName, final String keyAttributeName, final String namespace) {
        this.entryName = entryName;
        this.namespace = namespace;
        this.keyAttributeName = keyAttributeName;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Entry {
        @XmlAnyElement
        Object value;

        @XmlAnyAttribute
        Map attribute = new HashMap();

        @XmlTransient
        private String key;

        @XmlTransient
        private String keyAttributeName;

        public Entry() {
        }

        public Entry(final String keyAttributeName, final String key, final Object value) {
            this.value = value;
            this.keyAttributeName = keyAttributeName;
            setKey(key);
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public String getKey() {
            if (key != null)
                return key;
            key = (String) attribute.values().iterator().next();
            return key;
        }

        public void setKey(String keyValue) {
            this.key = keyValue;
            attribute.clear();

            QName name = new QName(keyAttributeName);
            attribute.put(name, keyValue);
        }
    }

    public void addEntry(String key, Object val) {
        Entry entry = new Entry(keyAttributeName, key, val);
        //JAXBElement element = new JAXBElement(new QName(namespace, entryName, prefix), Entry.class, entry);
        JAXBElement element = new JAXBElement(new QName(namespace, entryName), Entry.class, entry);
        //JAXBElement element = new JAXBElement(new QName(entryName), Entry.class, entry);
        value.add(element);
    }

    public List> getValue() {
        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy