View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import net.sf.morph.Defaults;
19  import net.sf.morph.transform.Converter;
20  import net.sf.morph.transform.DecoratedConverter;
21  import net.sf.morph.transform.ImpreciseTransformer;
22  import net.sf.morph.transform.transformers.BaseReflectorTransformer;
23  import net.sf.morph.util.TransformerUtils;
24  
25  /**
26   * Base class for converts that convert objects to a pretty programmer-friendly
27   * representation using information retrieved using a reflector.
28   * 
29   * @author Matt Sgarlata
30   * @since Feb 15, 2005
31   */
32  public abstract class BaseToPrettyTextConverter extends BaseReflectorTransformer
33  		implements DecoratedConverter, ImpreciseTransformer {
34  
35  	private String prefix;
36  	private String suffix;
37  	private String separator;
38  	private Converter textConverter;
39  	private Converter toTextConverter;
40  	private boolean showNullValues = false;
41  
42  	/**
43  	 * {@inheritDoc}
44  	 */
45  	protected Class[] getDestinationClassesImpl() throws Exception {
46  		return getTextConverter().getDestinationClasses();
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	// don't do any logging, because it will cause an infinite loop.  you can't
53  	// log an object's string representation as one of the steps of constructing
54  	// that representation
55  	protected boolean isPerformingLogging() {
56  		return false;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	protected boolean isWrappingRuntimeExceptions() {
63  	    return true;
64      }
65  
66  	/**
67  	 * Get the separator.
68  	 * @return String
69  	 */
70  	public String getSeparator() {
71  		return separator;
72  	}
73  
74  	/**
75  	 * Set the separator.
76  	 * @param separator
77  	 */
78  	public void setSeparator(String separator) {
79  		this.separator = separator;
80  	}
81  
82  	/**
83  	 * Get the prefix.
84  	 * @return String
85  	 */
86  	public String getPrefix() {
87  		return prefix;
88  	}
89  
90  	/**
91  	 * Set the prefix.
92  	 * @param prefix
93  	 */
94  	public void setPrefix(String prefix) {
95  		this.prefix = prefix;
96  	}
97  
98  	/**
99  	 * Get the suffix.
100 	 * @return String
101 	 */
102 	public String getSuffix() {
103 		return suffix;
104 	}
105 
106 	/**
107 	 * Set the suffix.
108 	 * @param suffix
109 	 */
110 	public void setSuffix(String suffix) {
111 		this.suffix = suffix;
112 	}
113 
114 	/**
115 	 * Get the text converter used by this BaseToPrettyTextConverter.
116 	 * @return Converter
117 	 */
118 	public Converter getTextConverter() {
119 		if (textConverter == null) {
120 			setTextConverter(Defaults.createTextConverter());
121 		}
122 		return textConverter;
123 	}
124 
125 	/**
126 	 * Set the text converter used by this BaseToPrettyTextConverter.
127 	 * @param textConverter
128 	 */
129 	public void setTextConverter(Converter textConverter) {
130 		this.textConverter = textConverter;
131 	}
132 
133 	/**
134 	 * Get the "to text" converter used by this BaseToPrettyTextConverter.
135 	 * @return Converter
136 	 */
137 	public Converter getToTextConverter() {
138 		if (toTextConverter == null) {
139 			setToTextConverter(Defaults.createToTextConverter());
140 		}
141 		return toTextConverter;
142 	}
143 
144 	/**
145 	 * Set the "to text" converter used by this BaseToPrettyTextConverter.
146 	 * @param objectToTextConverter
147 	 */
148 	public void setToTextConverter(Converter objectToTextConverter) {
149 		this.toTextConverter = objectToTextConverter;
150 	}
151 
152 	/**
153 	 * Learn whether this BaseToPrettyTextConverter is configured to show null values.
154 	 * @return boolean
155 	 */
156 	public boolean isShowNullValues() {
157 		return showNullValues;
158 	}
159 
160 	/**
161 	 * Set whether this BaseToPrettyTextConverter should show null values. Default <code>false</code>.
162 	 * @param showNullValues
163 	 */
164 	public void setShowNullValues(boolean showNullValues) {
165 		this.showNullValues = showNullValues;
166 	}
167 
168 	/**
169 	 * {@inheritDoc}
170 	 */
171 	protected boolean isImpreciseTransformationImpl(Class destinationClass, Class sourceClass) {
172 		return TransformerUtils.isImpreciseTransformation(getTextConverter(), destinationClass, sourceClass);
173 	}
174 
175 	/**
176 	 * Get the intermediate class passed to the text converter.
177 	 * @return
178 	 */
179 	protected Class getIntermediateClass() {
180 		return StringBuffer.class;
181 	}
182 }