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  package org.apache.commons.betwixt;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.betwixt.strategy.DefaultObjectStringConverter;
22  import org.apache.commons.betwixt.strategy.IdStoringStrategy;
23  import org.apache.commons.betwixt.strategy.ObjectStringConverter;
24  import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
25  
26  /** <p>Stores mapping phase binding configuration.</p>
27    *
28    * <p>There are two phase in Betwixt's processing.
29    * The first phase is the introspection of the bean.
30    * Strutural configuration settings effect this phase.
31    * The second phase comes when Betwixt dynamically uses
32    * reflection to execute the mapping.
33    * This object stores configuration settings pertaining 
34    * to the second phase.</p>
35    *
36    * <p>These common settings have been collected into one class
37    * to make round tripping easier since the same <code>BindingConfiguration</code>
38    * can be shared.</p> 
39    *
40    * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
41    * @since 0.5
42    */
43  public class BindingConfiguration implements Serializable {
44  
45      /** Should <code>ID</code>'s and <code>IDREF</code> be used cross-reference matching objects? */
46      private boolean mapIDs = true;
47      /** Converts objects &lt-&gt; strings */
48      private ObjectStringConverter objectStringConverter;
49      /** The name of the classname attribute used when creating derived beans */
50      private String classNameAttribute = "className";
51      /** Strategy for suppressing attributes with certain values when writing */
52      private ValueSuppressionStrategy valueSuppressionStrategy  = ValueSuppressionStrategy.DEFAULT;
53      /** Strategy for storing and accessing ID values */
54      private IdStoringStrategy idStoringStrategy = IdStoringStrategy.createDefault();
55      
56      /**
57       * Constructs a BindingConfiguration with default properties.
58       */
59      public BindingConfiguration() {
60          this(new DefaultObjectStringConverter(), true);
61      }
62      
63      /** 
64       * Constructs a BindingConfiguration
65       * @param objectStringConverter the <code>ObjectStringConverter</code>
66       * to be used to convert Objects &lt;-&gt; Strings
67       * @param mapIDs should <code>ID</code>'s and <code>IDREF</code> be used to cross-reference
68       */ 
69      public BindingConfiguration(ObjectStringConverter objectStringConverter, boolean mapIDs) {
70          setObjectStringConverter(objectStringConverter);
71          setMapIDs(mapIDs);
72      }
73      
74      /**
75        * Gets the Object &lt;-&gt; String converter.
76        * @return the ObjectStringConverter to use, not null
77        */
78      public ObjectStringConverter getObjectStringConverter() {
79          return objectStringConverter;
80      }
81      
82      /**
83        * Sets the Object &lt;-&gt; String converter.
84        * @param objectStringConverter the ObjectStringConverter to be used, not null
85        */
86      public void setObjectStringConverter(ObjectStringConverter objectStringConverter) {
87          this.objectStringConverter = objectStringConverter;
88      }
89      
90      /** 
91       * Should <code>ID</code>'s and <code>IDREF</code> attributes 
92       * be used to cross-reference matching objects? 
93       *
94       * @return true if <code>ID</code> and <code>IDREF</code> 
95       * attributes should be used to cross-reference instances
96       */
97      public boolean getMapIDs() {
98          return mapIDs;
99      }
100     
101     /**
102      *Should <code>ID</code>'s and <code>IDREF</code> attributes 
103      * be used to cross-reference matching objects? 
104      *
105      * @param mapIDs pass true if <code>ID</code>'s should be used to cross-reference
106      */
107     public void setMapIDs(boolean mapIDs) {
108         this.mapIDs = mapIDs;
109     }        
110     
111     /**
112      * The name of the attribute which can be specified in the XML to override the
113      * type of a bean used at a certain point in the schema.
114      *
115      * <p>The default value is 'className'.</p>
116      * 
117      * @return The name of the attribute used to overload the class name of a bean
118      */
119     public String getClassNameAttribute() {
120         return classNameAttribute;
121     }
122 
123     /**
124      * Sets the name of the attribute which can be specified in 
125      * the XML to override the type of a bean used at a certain 
126      * point in the schema.
127      *
128      * <p>The default value is 'className'.</p>
129      * 
130      * @param classNameAttribute The name of the attribute used to overload the class name of a bean
131      */
132     public void setClassNameAttribute(String classNameAttribute) {
133         this.classNameAttribute = classNameAttribute;
134     }
135     
136     
137     /**
138      * Gets the <code>ValueSuppressionStrategy</code>.
139      * This is used to control the expression of attributes with certain values.
140      * @since 0.7
141      * @return <code>ValueSuppressionStrategy</code>, not null
142      */
143     public ValueSuppressionStrategy getValueSuppressionStrategy() {
144         return valueSuppressionStrategy;
145     }
146     
147     /**
148      * Sets the <code>ValueSuppressionStrategy</code>.
149      * This is used to control the expression of attributes with certain values.
150      * @since 0.7
151      * @param valueSuppressionStrategy <code>ValueSuppressionStrategy</code>, not null
152      */
153     public void setValueSuppressionStrategy(
154             ValueSuppressionStrategy valueSuppressionStrategy) {
155         this.valueSuppressionStrategy = valueSuppressionStrategy;
156     }
157     
158     /**
159      * Gets the strategy used to manage storage and retrieval of id's.
160      * 
161      * @since 0.7
162      * @return Returns the <code>IdStoringStrategy</code>, not null
163      */
164     public IdStoringStrategy getIdMappingStrategy() {
165         return idStoringStrategy;
166     }
167 
168     /**
169      * Sets the strategy used to manage storage and retrieval of id's.
170      * 
171      * @since 0.7
172      * @param idMappingStrategy
173      *            <code>IdStoringStrategy</code> to be set, not null
174      */
175     public void setIdMappingStrategy(IdStoringStrategy idMappingStrategy) {
176         this.idStoringStrategy = idMappingStrategy;
177     }
178 }