View Javadoc

1   package org.codehaus.xfire.type;
2   
3   import java.util.Collection;
4   
5   import javax.xml.namespace.QName;
6   
7   import org.codehaus.xfire.type.basic.ArrayType;
8   import org.codehaus.xfire.type.basic.BeanType;
9   import org.codehaus.xfire.util.NamespaceHelper;
10  
11  /***
12   * A type mapping which automatically generates types
13   * for java classes which are not registered, allowing
14   * easy deployment of java services.
15   * 
16   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
17   * @since Feb 21, 2004
18   */
19  public class AutoTypeMapping
20      extends CustomTypeMapping
21  {
22      public AutoTypeMapping( TypeMapping defaultTM )
23      {
24          super( defaultTM );
25      }
26      
27      public AutoTypeMapping()
28      {
29          super();
30      }
31  	
32  	/***
33  	 * @see org.codehaus.xfire.type.TypeMapping#getSerializer(java.lang.Class, javax.xml.namespace.QName)
34  	 */
35  	public Type getType( Class javaType, QName xmlType )
36  	{
37          Type type = super.getType(javaType, xmlType);
38          
39          if ( type == null )
40          {
41              type = getType(javaType);
42              
43          	register( javaType, xmlType, type );
44              
45              type = super.getType(javaType, xmlType);
46          }
47          
48          return type;
49  	}
50  
51      /***
52       * Tries to determine a type class automatically from the type.
53       * 
54  	 * @param javaType
55  	 * @return
56  	 */
57  	protected Type findTypeForClass(Class javaType)
58  	{
59  		if ( javaType.isArray() ||
60               javaType.isAssignableFrom( Collection.class ) )
61          {
62  			return new ArrayType();
63          }
64          else
65          {
66          	return new BeanType();
67          }
68  	}
69  
70  	/***
71       * @see org.codehaus.xfire.type.TypeMapping#getType(java.lang.Class)
72       */
73      public Type getType(Class javaType)
74      {
75          Type type = super.getType(javaType);
76  
77          if ( type == null )
78          {
79              QName qname = createQName( javaType );
80              register( javaType, qname, findTypeForClass( javaType ) );
81  
82              type = super.getType(javaType, qname);
83          }
84          
85          return type;
86      }
87  
88  	/***
89  	 * @param javaType
90  	 * @return
91  	 * @throws ClassNotFoundException
92  	 */
93  	private QName createQName(Class javaType)
94  	{
95          String clsName = javaType.getName();
96          
97          if (clsName.startsWith("[L"))
98  		{
99  			clsName = clsName.substring(2, clsName.length() - 1);
100 		}
101 
102         String ns = NamespaceHelper.makeNamespaceFromClassName(clsName, "http");
103         
104         String localName = null;
105         
106         if (javaType.isArray() ||
107             javaType.isAssignableFrom( Collection.class ))
108         {
109             localName = "ArrayOf" + clsName.substring( clsName.lastIndexOf(".")+1 );
110             
111             // If this is an array of a primitive type, put the type
112             // we're creating in the default namespace.
113             if ( javaType.isArray() )
114             {
115                 Type type = getType( javaType.getComponentType() );
116                 
117                 if ( type.isComplex() )
118                 {
119                     ns = type.getSchemaType().getNamespaceURI();
120                 }
121                 else
122                 {
123                     ns = getEncodingStyleURI();
124                 }
125             }
126         }
127         else
128         {
129             localName = clsName.substring( clsName.lastIndexOf(".")+1 );
130         }
131 
132         return new QName( ns, localName );
133 	} 
134 }