Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ext;
  
Helper class used for isolating details of handling optional+external types (javax.xml classes) from standard factories that offer them.

Author(s):
tatu
 
 public class OptionalHandlerFactory
     implements java.io.Serializable
 {
     private static final long serialVersionUID = 1;
 
     /* 1.6.1+ To make 2 main "optional" handler groups (javax.xml.stream)
      * more dynamic, we better only figure out handlers completely dynamically, if and
      * when they are needed. To do this we need to assume package prefixes.
      */
     private final static String PACKAGE_PREFIX_JAVAX_XML = "javax.xml.";
 
     private final static String SERIALIZERS_FOR_JAVAX_XML = "com.fasterxml.jackson.databind.ext.CoreXMLSerializers";
     private final static String DESERIALIZERS_FOR_JAVAX_XML = "com.fasterxml.jackson.databind.ext.CoreXMLDeserializers";
 
     // Plus we also have a single serializer for DOM Node:
     private final static String CLASS_NAME_DOM_NODE = "org.w3c.dom.Node";
     private final static String CLASS_NAME_DOM_DOCUMENT = "org.w3c.dom.Node";
     private final static String SERIALIZER_FOR_DOM_NODE = "com.fasterxml.jackson.databind.ext.DOMSerializer";
     private final static String DESERIALIZER_FOR_DOM_DOCUMENT = "com.fasterxml.jackson.databind.ext.DOMDeserializer$DocumentDeserializer";
     private final static String DESERIALIZER_FOR_DOM_NODE = "com.fasterxml.jackson.databind.ext.DOMDeserializer$NodeDeserializer";
     
     public final static OptionalHandlerFactory instance = new OptionalHandlerFactory();
     
     protected OptionalHandlerFactory() { }
 
     /*
     /**********************************************************
     /* Public API
     /**********************************************************
      */
     
     public JsonSerializer<?> findSerializer(SerializationConfig configJavaType type,
             BeanDescription beanDesc)
     {
         Class<?> rawType = type.getRawClass();
         String className = rawType.getName();
         String factoryName;
         
         if (className.startsWith()
                 || hasSupertypeStartingWith(rawType)) {
             factoryName = ;
         } else if (doesImplement(rawType)) {
             return (JsonSerializer<?>) instantiate();
         } else {
             return null;
         }
 
         Object ob = instantiate(factoryName);
         if (ob == null) { // could warn, if we had logging system (j.u.l?)
             return null;
         }
         return ((Serializersob).findSerializer(configtypebeanDesc);
     }
 
     public JsonDeserializer<?> findDeserializer(JavaType typeDeserializationConfig config,
             BeanDescription beanDesc)
         throws JsonMappingException
     {
         Class<?> rawType = type.getRawClass();
         String className = rawType.getName();
         String factoryName;
         
         if (className.startsWith()
                 || hasSupertypeStartingWith(rawType)) {
             factoryName = ;
         } else if (doesImplement(rawType)) {
             return (JsonDeserializer<?>) instantiate();
         } else if (doesImplement(rawType)) {
             return (JsonDeserializer<?>) instantiate();
         } else {
             return null;
         }
         Object ob = instantiate(factoryName);
         if (ob == null) { // could warn, if we had logging system (j.u.l?)
             return null;
         }
         return ((Deserializersob).findBeanDeserializer(typeconfigbeanDesc);
     }
     
     /*
     /**********************************************************
     /* Internal helper methods
     /**********************************************************
      */
 
     private Object instantiate(String className)
     {
        try {
            return Class.forName(className).newInstance();
        }
        catch (LinkageError e) { }
        // too many different kinds to enumerate here:
        catch (Exception e) { }
        return null;
    }
    
    private boolean doesImplement(Class<?> actualTypeString classNameToImplement)
    {
        for (Class<?> type = actualTypetype != nulltype = type.getSuperclass()) {
            if (type.getName().equals(classNameToImplement)) {
                return true;
            }
            // or maybe one of super-interfaces
            if (hasInterface(typeclassNameToImplement)) {
                return true;
            }
        }
        return false;
    }
        
    private boolean hasInterface(Class<?> typeString interfaceToImplement)
    {
        Class<?>[] interfaces = type.getInterfaces();
        for (Class<?> iface : interfaces) {
            if (iface.getName().equals(interfaceToImplement)) {
                return true;
            }
        }
        // maybe super-interface?
        for (Class<?> iface : interfaces) {
            if (hasInterface(ifaceinterfaceToImplement)) {
                return true;
            }
        }
        return false;
    }
    private boolean hasSupertypeStartingWith(Class<?> rawTypeString prefix)
    {
        // first, superclasses
        for (Class<?> supertype = rawType.getSuperclass(); supertype != nullsupertype = supertype.getSuperclass()) {
            if (supertype.getName().startsWith(prefix)) {
                return true;
            }
        }
        // then interfaces
        for (Class<?> cls = rawTypecls != nullcls = cls.getSuperclass()) {
            if (hasInterfaceStartingWith(clsprefix)) {
                return true;
            }
        }
        return false;
    }
    private boolean hasInterfaceStartingWith(Class<?> typeString prefix)
    {
        Class<?>[] interfaces = type.getInterfaces();
        for (Class<?> iface : interfaces) {
            if (iface.getName().startsWith(prefix)) {
                return true;
            }
        }
        // maybe super-interface?
        for (Class<?> iface : interfaces) {
            if (hasInterfaceStartingWith(ifaceprefix)) {
                return true;
            }
        }
        return false;
    }
    
New to GrepCode? Check out our FAQ X