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

javolution.xml.internal.XMLContextImpl Maven / Gradle / Ivy

The newest version!
/*
 * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
 * Copyright (C) 2012 - Javolution (http://javolution.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javolution.xml.internal;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javolution.util.FastMap;
import javolution.xml.DefaultXMLFormat;
import javolution.xml.XMLContext;
import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;

/**
 * Holds the default implementation of XMLContext.
 * 
 * @author  Jean-Marie Dautelle
 * @version 6.0, July 21, 2013
 */
@SuppressWarnings("rawtypes")
public final class XMLContextImpl extends XMLContext {

    private final FastMap, XMLFormat> formats = new FastMap, XMLFormat>();

    @Override
    protected XMLContext inner() {
        XMLContextImpl ctx = new XMLContextImpl();
        ctx.formats.putAll(formats);
        return ctx;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected  XMLFormat searchFormat(Class type) {
        XMLFormat xml = formats.get(type);
        if (xml != null)
            return xml;
        DefaultXMLFormat format = type.getAnnotation(DefaultXMLFormat.class);
        if (format != null) {
            Class formatClass = format.value();
            try {
                xml = formatClass.newInstance();
                synchronized (formats) { // Required since possible concurrent use 
                    // (getFormatInContext is not a configuration method).
                    formats.put(type, xml);
                }
                return xml;
            } catch (Throwable ex) {
                throw new RuntimeException(ex);
            }
        }
        // Check predefined format as last resource.
        if (Map.class.isAssignableFrom(type))
            return MAP_XML;
        if (Collection.class.isAssignableFrom(type))
            return COLLECTION_XML;
        return OBJECT_XML;
    }

    @Override
    public  void setFormat(Class type, XMLFormat format) {
        formats.put(type, format);
    }

    /////////////////////////
    // PREDEFINED FORMATS  //
    /////////////////////////
    /**
     * Holds the static XML format for java.lang.Object.class instances.
     * The XML representation consists of the text representation of the object
     * as a "value" attribute.
     */
    private static final XMLFormat OBJECT_XML = new XMLFormat.Default();

    /**
     * Holds the default XML representation for java.util.Collection
     * instances. This representation consists of nested XML elements one for
     * each element of the collection. The elements' order is defined by
     * the collection iterator order. Collections are deserialized using their
     * default constructor.
     */
    private static final XMLFormat COLLECTION_XML = new XMLFormat() {

        @SuppressWarnings("unchecked")
        public void read(XMLFormat.InputElement xml, Object obj)
                throws XMLStreamException {
            Collection collection = (Collection) obj;
            while (xml.hasNext()) {
                collection.add(xml.getNext());
            }
        }

        public void write(Object obj, XMLFormat.OutputElement xml)
                throws XMLStreamException {
            Collection collection = (Collection) obj;
            for (Iterator i = collection.iterator(); i.hasNext();) {
                xml.add(i.next());
            }
        }

    };

    /**
     * Holds the default XML representation for java.util.Map
     * instances. This representation consists of key/value pair as nested
     * XML elements. For example:[code]
     * 
     *     
     *     
     *     
     *     
     *     
     *     
     * [/code]
     *
     * The elements' order is defined by the map's entries iterator order.
     * Maps are deserialized using their default constructor.
     */
    private static final XMLFormat MAP_XML = new XMLFormat() {

        @SuppressWarnings("unchecked")
        public void read(XMLFormat.InputElement xml, Object obj)
                throws XMLStreamException {
            final Map map = (Map) obj;
            while (xml.hasNext()) {
                Object key = xml.get("Key");
                Object value = xml.get("Value");
                map.put(key, value);
            }
        }

        public void write(Object obj, XMLFormat.OutputElement xml)
                throws XMLStreamException {
            final Map map = (Map) obj;
            for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
                Map.Entry entry = (Map.Entry) it.next();
                xml.add(entry.getKey(), "Key");
                xml.add(entry.getValue(), "Value");
            }
        }

    };

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy