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

com.ebay.jetstream.xmlser.XMLSerializationManager Maven / Gradle / Ivy

/*******************************************************************************
 *  Copyright © 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/
package com.ebay.jetstream.xmlser;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ebay.jetstream.xmlser.simple.SimpleXMLSerializer;
import com.ebay.jetstream.xmlser.spring.SpringBeanDeserializer;
import com.ebay.jetstream.xmlser.spring.SpringXMLSerializer;

public class XMLSerializationManager {
  private static final Map s_serializers = new HashMap();
  private static final Map s_deserializers = new HashMap();
  private static final List> s_xserializable = new ArrayList>();

  static {
    registerSerializer("xml", new SimpleXMLSerializer());
    registerSerializer("spring", new SpringXMLSerializer());
    registerDeserializer("spring", new SpringBeanDeserializer());
  }

  public static IXmlDeserializer getDeserializer(String format) {
    return s_deserializers.get(format);
  }

  public static XMLSerializer getSerializer(String format) {
    return s_serializers.get(format);
  }

  public static boolean isHidden(Method getter) {
    return getter == null || getter.isAnnotationPresent(Hidden.class) || getter.getParameterTypes().length != 0
        || getter.getName().equals("getClass");
  }

  /**
   * Returns true if classes of this object should be considered to be XML serializable.
   * 
   * @param clazz
   *            the class to check
   * @return true if the class is XSerializable, or is explicitly listed as serializable.
   */
  public static boolean isXSerializable(Class clazz) {
    if (XSerializable.class.isAssignableFrom(clazz))
      return true;
    for (Class explicitx : s_xserializable)
      if (explicitx.isAssignableFrom(clazz))
        return true;
    return false;
  }

  public static IXmlDeserializer registerDeserializer(String format, IXmlDeserializer deserializer) {
    IXmlDeserializer old = s_deserializers.get(format);
    s_deserializers.put(format, deserializer);
    return old;
  }

  public static XMLSerializer registerSerializer(String format, XMLSerializer serializer) {
    XMLSerializer old = s_serializers.get(format);
    s_serializers.put(format, serializer);
    return old;
  }

  /**
   * Adds a class to the explicit list of serializable classes. If the class implements XSerializable, it is already
   * implicitly serializable. This mechanism exists for classes that should be serialized but the source is unavailable
   * and extending isn't an appropriate option.
   * 
   * @param clazz
   *            the class to add.
   * @return true iff the class is added.
   */
  public static boolean registerXSerializable(Class clazz) {
    boolean add = !s_xserializable.contains(clazz);
    if (add)
      s_xserializable.add(clazz);
    return add;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy