View Javadoc

    1   package org.codehaus.xfire.aegis.type;
    2
    3   import java.math.BigDecimal;
    4   import java.sql.Time;
    5   import java.sql.Timestamp;
    6   import java.util.Calendar;
    7   import java.util.Date;
    8   import java.util.Hashtable;
    9   import java.util.Iterator;
    10
    11  import javax.xml.namespace.QName;
    12
    13  import org.codehaus.xfire.aegis.type.basic.Base64Type;
    14  import org.codehaus.xfire.aegis.type.basic.BigDecimalType;
    15  import org.codehaus.xfire.aegis.type.basic.BooleanType;
    16  import org.codehaus.xfire.aegis.type.basic.CalendarType;
    17  import org.codehaus.xfire.aegis.type.basic.DateTimeType;
    18  import org.codehaus.xfire.aegis.type.basic.DocumentType;
    19  import org.codehaus.xfire.aegis.type.basic.DoubleType;
    20  import org.codehaus.xfire.aegis.type.basic.FloatType;
    21  import org.codehaus.xfire.aegis.type.basic.IntType;
    22  import org.codehaus.xfire.aegis.type.basic.LongType;
    23  import org.codehaus.xfire.aegis.type.basic.ShortType;
    24  import org.codehaus.xfire.aegis.type.basic.StringType;
    25  import org.codehaus.xfire.aegis.type.basic.TimeType;
    26  import org.codehaus.xfire.aegis.type.basic.TimestampType;
    27  import org.codehaus.xfire.soap.Soap11;
    28  import org.codehaus.xfire.soap.SoapConstants;
    29  import org.codehaus.xfire.util.ClassLoaderUtils;
    30  import org.w3c.dom.Document;
    31
    32  /***
    33   * The default implementation of TypeMappingRegistry.
    34   *
    35   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    36   * @since Feb 22, 2004
    37   */
    38  public class DefaultTypeMappingRegistry
    39          implements TypeMappingRegistry
    40  {
    41      private static final QName XSD_STRING = new QName(SoapConstants.XSD, "string");
    42      private static final QName XSD_LONG = new QName(SoapConstants.XSD, "long");
    43      private static final QName XSD_FLOAT = new QName(SoapConstants.XSD, "float");
    44      private static final QName XSD_DOUBLE = new QName(SoapConstants.XSD, "double");
    45      private static final QName XSD_INT = new QName(SoapConstants.XSD, "int");
    46      private static final QName XSD_SHORT = new QName(SoapConstants.XSD, "short");
    47      private static final QName XSD_BOOLEAN = new QName(SoapConstants.XSD, "boolean");
    48      private static final QName XSD_DATETIME = new QName(SoapConstants.XSD, "dateTime");
    49      private static final QName XSD_TIME = new QName(SoapConstants.XSD, "dateTime");
    50      private static final QName XSD_BASE64 = new QName(SoapConstants.XSD, "base64Binary");
    51      private static final QName XSD_DECIMAL = new QName(SoapConstants.XSD, "decimal");
    52
    53      private static final QName XSD_ANY = new QName(SoapConstants.XSD, "anyType");
    54
    55      private static final String ENCODED_NS = Soap11.getInstance().getSoapEncodingStyle();
    56      private static final QName ENCODED_STRING = new QName(ENCODED_NS, "string");
    57      private static final QName ENCODED_LONG = new QName(ENCODED_NS, "long");
    58      private static final QName ENCODED_FLOAT = new QName(ENCODED_NS, "float");
    59      private static final QName ENCODED_DOUBLE = new QName(ENCODED_NS, "double");
    60      private static final QName ENCODED_INT = new QName(ENCODED_NS, "int");
    61      private static final QName ENCODED_SHORT = new QName(ENCODED_NS, "short");
    62      private static final QName ENCODED_BOOLEAN = new QName(ENCODED_NS, "boolean");
    63      private static final QName ENCODED_DATETIME = new QName(ENCODED_NS, "dateTime");
    64      private static final QName ENCODED_BASE64 = new QName(ENCODED_NS, "base64Binary");
    65      private static final QName ENCODED_DECIMAL = new QName(ENCODED_NS, "decimal");
    66
    67      private Hashtable registry;
    68
    69      private TypeMapping defaultTM;
    70
    71      public DefaultTypeMappingRegistry()
    72      {
    73          registry = new Hashtable();
    74      }
    75
    76      public DefaultTypeMappingRegistry(boolean createDefault)
    77      {
    78          registry = new Hashtable();
    79
    80          if (createDefault)
    81          {
    82              createDefaultMappings();
    83          }
    84      }
    85
    86      public TypeMapping register(String encodingStyleURI, TypeMapping mapping)
    87      {
    88          TypeMapping previous = (TypeMapping) registry.get(encodingStyleURI);
    89
    90          mapping.setEncodingStyleURI(encodingStyleURI);
    91
    92          registry.put(encodingStyleURI, mapping);
    93
    94          return previous;
    95      }
    96
    97      public void registerDefault(TypeMapping mapping)
    98      {
    99          defaultTM = mapping;
    100     }
    101
    102     /***
    103      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#getDefaultTypeMapping()
    104      */
    105     public TypeMapping getDefaultTypeMapping()
    106     {
    107         return defaultTM;
    108     }
    109
    110     /***
    111      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#getRegisteredEncodingStyleURIs()
    112      */
    113     public String[] getRegisteredEncodingStyleURIs()
    114     {
    115         return (String[]) registry.keySet().toArray(new String[0]);
    116     }
    117
    118     /***
    119      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#getTypeMapping(java.lang.String)
    120      */
    121     public TypeMapping getTypeMapping(String encodingStyleURI)
    122     {
    123         return (TypeMapping) registry.get(encodingStyleURI);
    124     }
    125
    126     /***
    127      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#createTypeMapping(boolean)
    128      */
    129     public TypeMapping createTypeMapping(boolean autoTypes)
    130     {
    131         return createTypeMapping(getDefaultTypeMapping(), autoTypes);
    132     }
    133
    134     /***
    135      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#createTypeMapping(String, boolean)
    136      */
    137     public TypeMapping createTypeMapping(String parentNamespace, boolean autoTypes)
    138     {
    139         return createTypeMapping(getTypeMapping(parentNamespace), autoTypes);
    140     }
    141
    142     protected TypeMapping createTypeMapping(TypeMapping parent, boolean autoTypes)
    143     {
    144         CustomTypeMapping tm = new CustomTypeMapping(parent);
    145
    146         if (autoTypes) tm.setTypeCreator(createTypeCreator());
    147
    148         return tm;
    149     }
    150
    151     protected TypeCreator createTypeCreator()
    152     {
    153         AbstractTypeCreator xmlCreator = createRootTypeCreator();
    154         xmlCreator.setNextCreator(new DefaultTypeCreator());
    155         try
    156         {
    157             String j5TC = "org.codehaus.xfire.aegis.type.java5.Java5TypeCreator";
    158
    159             Class clazz = ClassLoaderUtils.loadClass(j5TC, getClass());
    160
    161             AbstractTypeCreator j5Creator = (AbstractTypeCreator) clazz.newInstance();
    162             j5Creator.setNextCreator(xmlCreator);
    163             return j5Creator;
    164         }
    165         catch (Throwable t)
    166         {
    167             return xmlCreator;
    168         }
    169     }
    170
    171     protected AbstractTypeCreator createRootTypeCreator()
    172     {
    173         return new XMLTypeCreator();
    174     }
    175
    176     /***
    177      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#unregisterTypeMapping(java.lang.String)
    178      */
    179     public TypeMapping unregisterTypeMapping(String encodingStyleURI)
    180     {
    181         TypeMapping tm = (TypeMapping) registry.get(encodingStyleURI);
    182         registry.remove(encodingStyleURI);
    183         return tm;
    184     }
    185
    186     public boolean removeTypeMapping(TypeMapping mapping)
    187     {
    188         int n = 0;
    189
    190         for (Iterator itr = registry.values().iterator(); itr.hasNext();)
    191         {
    192             if (itr.next().equals(mapping))
    193             {
    194                 itr.remove();
    195                 n++;
    196             }
    197         }
    198
    199         return (n > 0);
    200     }
    201
    202     /***
    203      * @see org.codehaus.xfire.aegis.type.TypeMappingRegistry#clear()
    204      */
    205     public void clear()
    206     {
    207         registry.clear();
    208     }
    209
    210     public TypeMapping createDefaultMappings()
    211     {
    212         TypeMapping tm = createTypeMapping(false);
    213
    214         tm.register(boolean.class, XSD_BOOLEAN, new BooleanType());
    215         tm.register(int.class, XSD_INT, new IntType());
    216         tm.register(short.class, XSD_SHORT, new ShortType());
    217         tm.register(double.class, XSD_DOUBLE, new DoubleType());
    218         tm.register(float.class, XSD_FLOAT, new FloatType());
    219         tm.register(long.class, XSD_LONG, new LongType());
    220         tm.register(String.class, XSD_STRING, new StringType());
    221         tm.register(Boolean.class, XSD_BOOLEAN, new BooleanType());
    222         tm.register(Integer.class, XSD_INT, new IntType());
    223         tm.register(Short.class, XSD_SHORT, new ShortType());
    224         tm.register(Double.class, XSD_DOUBLE, new DoubleType());
    225         tm.register(Float.class, XSD_FLOAT, new FloatType());
    226         tm.register(Long.class, XSD_LONG, new LongType());
    227         tm.register(Date.class, XSD_DATETIME, new DateTimeType());
    228         tm.register(Time.class, XSD_TIME, new TimeType());
    229         tm.register(Timestamp.class, XSD_DATETIME, new TimestampType());
    230         tm.register(Calendar.class, XSD_DATETIME, new CalendarType());
    231         tm.register(byte[].class, XSD_BASE64, new Base64Type());
    232         tm.register(BigDecimal.class, XSD_DECIMAL, new BigDecimalType());
    233         tm.register(Document.class, XSD_ANY, new DocumentType());
    234
    235         register(SoapConstants.XSD, tm);
    236         registerDefault(tm);
    237
    238         // Create a Type Mapping for SOAP 1.1 Encoding
    239         TypeMapping soapTM = createTypeMapping(tm, false);
    240
    241         soapTM.register(boolean.class, ENCODED_BOOLEAN, new BooleanType());
    242         soapTM.register(int.class, ENCODED_INT, new IntType());
    243         soapTM.register(short.class, ENCODED_SHORT, new ShortType());
    244         soapTM.register(double.class, ENCODED_DOUBLE, new DoubleType());
    245         soapTM.register(float.class, ENCODED_FLOAT, new FloatType());
    246         soapTM.register(long.class, ENCODED_LONG, new LongType());
    247         soapTM.register(String.class, ENCODED_STRING, new StringType());
    248         soapTM.register(Boolean.class, ENCODED_BOOLEAN, new BooleanType());
    249         soapTM.register(Integer.class, ENCODED_INT, new IntType());
    250         soapTM.register(Short.class, ENCODED_SHORT, new ShortType());
    251         soapTM.register(Double.class, ENCODED_DOUBLE, new DoubleType());
    252         soapTM.register(Float.class, ENCODED_FLOAT, new FloatType());
    253         soapTM.register(Long.class, ENCODED_LONG, new LongType());
    254         soapTM.register(Date.class, ENCODED_DATETIME, new DateTimeType());
    255         soapTM.register(Calendar.class, ENCODED_DATETIME, new CalendarType());
    256         soapTM.register(byte[].class, ENCODED_BASE64, new Base64Type());
    257         soapTM.register(BigDecimal.class, ENCODED_DECIMAL, new BigDecimalType());
    258
    259         soapTM.register(boolean.class, XSD_BOOLEAN, new BooleanType());
    260         soapTM.register(int.class, XSD_INT, new IntType());
    261         soapTM.register(short.class, XSD_SHORT, new ShortType());
    262         soapTM.register(double.class, XSD_DOUBLE, new DoubleType());
    263         soapTM.register(float.class, XSD_FLOAT, new FloatType());
    264         soapTM.register(long.class, XSD_LONG, new LongType());
    265         soapTM.register(String.class, XSD_STRING, new StringType());
    266         soapTM.register(Boolean.class, XSD_BOOLEAN, new BooleanType());
    267         soapTM.register(Integer.class, XSD_INT, new IntType());
    268         soapTM.register(Short.class, XSD_SHORT, new ShortType());
    269         soapTM.register(Double.class, XSD_DOUBLE, new DoubleType());
    270         soapTM.register(Float.class, XSD_FLOAT, new FloatType());
    271         soapTM.register(Long.class, XSD_LONG, new LongType());
    272         soapTM.register(Date.class, XSD_DATETIME, new DateTimeType());
    273         soapTM.register(Time.class, XSD_TIME, new TimeType());
    274         soapTM.register(Timestamp.class, XSD_DATETIME, new TimestampType());
    275         soapTM.register(Calendar.class, XSD_DATETIME, new CalendarType());
    276         soapTM.register(byte[].class, XSD_BASE64, new Base64Type());
    277         soapTM.register(BigDecimal.class, XSD_DECIMAL, new BigDecimalType());
    278         soapTM.register(Document.class, XSD_ANY, new DocumentType());
    279
    280         register(ENCODED_NS, tm);
    281
    282         return tm;
    283     }
    284 }