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

org.xwiki.extension.repository.internal.CollectionExtensionPropertySerializer Maven / Gradle / Ivy

There is a newer version: 16.10.2
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.repository.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Serialize and unserialize {@link Collection} properties.
 *
 * @param  the type of the property value
 * @version $Id: d52366c498f59123a0b0eaeb2c20a4a3c23d91e8 $
 */
public class CollectionExtensionPropertySerializer extends AbstractExtensionPropertySerializer
{
    /**
     * The serializers by type id.
     */
    protected Map serializerById;

    /**
     * The serializers by class.
     */
    protected Map, ExtensionPropertySerializer> serializerByClass;

    /**
     * @param type the type of the property
     * @param serializerById the serializers by type id
     * @param serializerByClass the serializers by class
     */
    protected CollectionExtensionPropertySerializer(String type,
        Map serializerById,
        Map, ExtensionPropertySerializer> serializerByClass)
    {
        super(type);

        this.serializerById = serializerById;
        this.serializerByClass = serializerByClass;
    }

    /**
     * @param serializerById the serializers by type id
     * @param serializerByClass the serializers by class
     */
    public CollectionExtensionPropertySerializer(Map serializerById,
        Map, ExtensionPropertySerializer> serializerByClass)
    {
        this("collection", serializerById, serializerByClass);
    }

    /**
     * @return a new collection
     */
    protected C createCollection()
    {
        return (C) new ArrayList();
    }

    /**
     * @param  the type of the expected value
     * @param element the element to unserialize
     * @param serializerById the serializers by type id
     * @return the unserialized property value
     */
    public static  T toValue(Element element, Map serializerById)
    {
        if (element != null) {
            String type = element.getAttribute("type");

            ExtensionPropertySerializer serializer = serializerById.get(type);

            if (serializer != null) {
                return (T) serializer.toValue(element);
            }
        }

        return null;
    }

    /**
     * @param valueClass the class of the value to serialize
     * @param serializerByClass the serializers by class
     * @return the serializer for the provided class
     */
    public static ExtensionPropertySerializer getSerializerByClass(Class valueClass,
        Map, ExtensionPropertySerializer> serializerByClass)
    {
        for (Map.Entry, ExtensionPropertySerializer> entry : serializerByClass.entrySet()) {
            if (entry.getKey().isAssignableFrom(valueClass)) {
                return entry.getValue();
            }
        }

        return null;
    }

    /**
     * @param value the value to serialize
     * @param document the document used to create new {@link Element}
     * @param elementName the name of the element to create
     * @param serializerByClass the serializers by class
     * @return the serialized property {@link Element}
     */
    public static Element toElement(Object value, Document document, String elementName,
        Map, ExtensionPropertySerializer> serializerByClass)
    {
        if (value != null) {
            ExtensionPropertySerializer serializer = getSerializerByClass(value.getClass(), serializerByClass);

            if (serializer != null) {
                return serializer.toElement(document, elementName, value);
            }
        }

        return null;
    }

    @Override
    public C toValue(Element element)
    {
        C collection = createCollection();

        NodeList featuresNodes = element.getChildNodes();

        for (int i = 0; i < featuresNodes.getLength(); ++i) {
            Node node = featuresNodes.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Object value = toValue((Element) node, this.serializerById);

                if (value == null) {
                    return null;
                } else {
                    collection.add(value);
                }
            }
        }

        return collection;
    }

    @Override
    public Element toElement(Document document, String elementName, C elementValue)
    {
        Element element = createRootElement(document, elementName);

        for (Object subValue : elementValue) {
            Element subElement = toElement(subValue, document, elementName, this.serializerByClass);

            if (subElement == null) {
                return null;
            }

            element.appendChild(subElement);
        }

        return element;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy