org.carrot2.util.simplexml.SimpleXmlWrappers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of carrot2-mini Show documentation
Show all versions of carrot2-mini Show documentation
Carrot2 search results clustering framework. Minimal functional subset
(core algorithms and infrastructure, no document sources).
/*
* Carrot2 project.
*
* Copyright (C) 2002-2016, Dawid Weiss, Stanisław Osiński.
* All rights reserved.
*
* Refer to the full license file "carrot2.LICENSE"
* in the root folder of the repository checkout or at:
* http://www.carrot2.org/carrot2.LICENSE
*/
package org.carrot2.util.simplexml;
import java.util.*;
import org.simpleframework.xml.Root;
import org.carrot2.shaded.guava.common.collect.Maps;
/**
* Enables SimpleXML-based serialization of collections of arbitrary types, also those
* contained in libraries. Depending on the actual type, 3 wrapping scenarios are
* possible:
*
* - Primitive types. Primitive types, and also {@link Class},
* {@link String} and {@link Enum}, are handled directly by {@link SimpleXmlWrapperValue},
* no extra code is required for serialization / deserialization.
* - SimpleXML-annotated types. Types annotated with SimpleXML's
* {@link Root} annotation will not be wrapped and serialized / deserialized directly by
* SimpleXML
* - Other types. For any other types, a {@link ISimpleXmlWrapper}
* implementation must be registered using the {@link #addWrapper(Class, Class)} method.
*
*/
public class SimpleXmlWrappers
{
/**
* The supported {@link ISimpleXmlWrapper}s.
*/
static Map, Class extends ISimpleXmlWrapper>>> wrappers = Maps.newHashMap();
/**
* Indicates whether the wrapper mapping is strict.
*/
static Map, Boolean> strict = Maps.newHashMap();
/**
* Registers a new {@link ISimpleXmlWrapper}. If a wrapper for the provide
* wrappedClass
already exists, it will be replaced with the new value.
* The wrapper mapping added using this method will be strict, it will apply only to
* objects of wrappedClass
, but not its subclasses.
*
* @param wrappedClass type to be wrapped
* @param wrapperClass class name of the {@link ISimpleXmlWrapper} implementation to
* wrap wrappedClass
* @see SimpleXmlWrappers#addWrapper(Class, Class, boolean)
*/
public static synchronized void addWrapper(Class wrappedClass,
Class extends ISimpleXmlWrapper super T>> wrapperClass)
{
addWrapper(wrappedClass, wrapperClass, true);
}
/**
* Registers a new {@link ISimpleXmlWrapper}. If a wrapper for the provide
* wrappedClass
already exists, it will be replaced with the new value.
*
* @param wrappedClass type to be wrapped
* @param wrapperClass class name of the {@link ISimpleXmlWrapper} implementation to
* wrap wrappedClass
* @param strict if true
, the mapping will apply only to objects of
* wrappedClass
, but not to its subclasses. If
* false
, if no exact mapping is found for
* wrappedClass
, the first available superclass mapping will
* be sought.
*/
public static synchronized void addWrapper(Class wrappedClass,
Class extends ISimpleXmlWrapper super T>> wrapperClass, boolean strict)
{
wrappers.put(wrappedClass, wrapperClass);
SimpleXmlWrappers.strict.put(wrappedClass, strict);
}
/**
* Wraps the provided map for serialization.
*
* @return map for SimpleXML serialization
*/
public static Map wrap(Map toWrap)
{
final HashMap wrapped = Maps.newHashMap();
for (Map.Entry entry : toWrap.entrySet())
{
wrapped.put(entry.getKey(), SimpleXmlWrapperValue.wrap(entry.getValue()));
}
return wrapped;
}
/**
* Unwraps the provided map after deserialization.
*
* @return map with original values
*/
public static Map unwrap(Map wrapped)
{
final HashMap result = Maps.newHashMap();
for (Map.Entry entry : wrapped.entrySet())
{
result.put(entry.getKey(), unwrap(entry.getValue()));
}
return result;
}
/**
* Wraps the provided list for serialization.
*
* @return list for SimpleXML serialization
*/
public static List wrap(List> toWrap)
{
return (List) wrap(toWrap,
new ArrayList());
}
/**
* Unwraps the provided list after deserialization.
*
* @return list with original values
*/
public static List