View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  
19  package org.apache.commons.betwixt.strategy;
20  
21  import org.apache.commons.betwixt.IntrospectionConfiguration;
22  
23  /**
24   * Strategy for binding simple types.
25   * Simple types (in xml) have no attributes or child elements.
26   * For Betwixt, these are converted to and from strings
27   * and these strings used to populate either attributes or element body's.
28   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
29   * @version $Revision: 561314 $
30   */
31  public abstract class SimpleTypeMapper {
32  
33      /**
34       * Enumerates binding options for simple types.
35       * Simple types (in xml) have no attributes or child elements.
36       * For Betwixt, these are converted to and from strings
37       * and these strings used to populate either attributes or element body's.
38       * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
39       * @version $Revision: 561314 $
40       */
41      public static class Binding {
42          public static final Binding ELEMENT = new Binding(1);
43          public static final Binding ATTRIBUTE = new Binding(2);
44          
45          private static final int ELEMENT_CODE = 1;
46          private static final int ATTRIBUTE_CODE = 2;
47          
48          private int code;
49          private Binding(int code) {
50              this.code = code;
51          }
52          
53          
54          /**
55           * Equals compatible with the enumeration.
56           */
57          public boolean equals( Object obj ) {
58              boolean result = false;
59              if ( obj == this ) {
60                  result = true; 
61              }
62              return result;
63          }
64  
65          /**
66           * Implementation compatible with equals
67           */
68          public int hashCode() {
69              return code;
70          }
71  
72          /**
73           * Generate something appropriate for logging.
74           */
75          public String toString() {
76              String result = "[Binding]";
77              switch (code) {
78                  case ELEMENT_CODE:
79                      result = "[Binding: ELEMENT]";
80                      break;
81                      
82                  case ATTRIBUTE_CODE:
83                      result = "[Binding: ATTRIBUTE]";
84                      break;
85              }   
86              return result;
87          }
88      }
89      
90      /**
91       * <p>Specifies the binding of a simple type.
92       * </p><p>
93       * <strong>Note:</strong> the xml name to which this property will be bound
94       * cannot be known at this stage (since it depends
95       * </p>
96       * @param propertyName the name of the property (to be bound)
97       * @param propertyType the type of the property (to be bound)
98       * @param configuration the current IntrospectionConfiguration
99       */
100     public abstract SimpleTypeMapper.Binding bind(
101                                                 String propertyName, 
102                                                 Class propertyType, 
103                                                 IntrospectionConfiguration configuration);
104 }