001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.betwixt;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.betwixt.strategy.DefaultObjectStringConverter;
022 import org.apache.commons.betwixt.strategy.IdStoringStrategy;
023 import org.apache.commons.betwixt.strategy.ObjectStringConverter;
024 import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
025
026 /** <p>Stores mapping phase binding configuration.</p>
027 *
028 * <p>There are two phase in Betwixt's processing.
029 * The first phase is the introspection of the bean.
030 * Strutural configuration settings effect this phase.
031 * The second phase comes when Betwixt dynamically uses
032 * reflection to execute the mapping.
033 * This object stores configuration settings pertaining
034 * to the second phase.</p>
035 *
036 * <p>These common settings have been collected into one class
037 * to make round tripping easier since the same <code>BindingConfiguration</code>
038 * can be shared.</p>
039 *
040 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
041 * @since 0.5
042 */
043 public class BindingConfiguration implements Serializable {
044
045 /** Should <code>ID</code>'s and <code>IDREF</code> be used cross-reference matching objects? */
046 private boolean mapIDs = true;
047 /** Converts objects <-> strings */
048 private ObjectStringConverter objectStringConverter;
049 /** The name of the classname attribute used when creating derived beans */
050 private String classNameAttribute = "className";
051 /** Strategy for suppressing attributes with certain values when writing */
052 private ValueSuppressionStrategy valueSuppressionStrategy = ValueSuppressionStrategy.DEFAULT;
053 /** Strategy for storing and accessing ID values */
054 private IdStoringStrategy idStoringStrategy = IdStoringStrategy.createDefault();
055
056 /**
057 * Constructs a BindingConfiguration with default properties.
058 */
059 public BindingConfiguration() {
060 this(new DefaultObjectStringConverter(), true);
061 }
062
063 /**
064 * Constructs a BindingConfiguration
065 * @param objectStringConverter the <code>ObjectStringConverter</code>
066 * to be used to convert Objects <-> Strings
067 * @param mapIDs should <code>ID</code>'s and <code>IDREF</code> be used to cross-reference
068 */
069 public BindingConfiguration(ObjectStringConverter objectStringConverter, boolean mapIDs) {
070 setObjectStringConverter(objectStringConverter);
071 setMapIDs(mapIDs);
072 }
073
074 /**
075 * Gets the Object <-> String converter.
076 * @return the ObjectStringConverter to use, not null
077 */
078 public ObjectStringConverter getObjectStringConverter() {
079 return objectStringConverter;
080 }
081
082 /**
083 * Sets the Object <-> String converter.
084 * @param objectStringConverter the ObjectStringConverter to be used, not null
085 */
086 public void setObjectStringConverter(ObjectStringConverter objectStringConverter) {
087 this.objectStringConverter = objectStringConverter;
088 }
089
090 /**
091 * Should <code>ID</code>'s and <code>IDREF</code> attributes
092 * be used to cross-reference matching objects?
093 *
094 * @return true if <code>ID</code> and <code>IDREF</code>
095 * attributes should be used to cross-reference instances
096 */
097 public boolean getMapIDs() {
098 return mapIDs;
099 }
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 }