View Javadoc

1   /*
2    * Copyright 2004-2005, 2007-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.copiers;
17  
18  import java.util.Locale;
19  
20  import net.sf.morph.transform.Copier;
21  import net.sf.morph.transform.DecoratedConverter;
22  import net.sf.morph.transform.DecoratedCopier;
23  import net.sf.morph.transform.ExplicitTransformer;
24  import net.sf.morph.transform.NodeCopier;
25  import net.sf.morph.transform.TransformationException;
26  import net.sf.morph.transform.Transformer;
27  import net.sf.morph.transform.transformers.BaseTransformer;
28  import net.sf.morph.util.TransformerUtils;
29  
30  /**
31   * Decorates any Copier so that it implements DecoratedCopier.
32   * 
33   * @author Matt Sgarlata
34   * @since Dec 5, 2004
35   */
36  public class CopierDecorator extends BaseTransformer implements DecoratedCopier,
37  		DecoratedConverter, ExplicitTransformer {
38  
39  	private Copier nestedCopier;
40  	private Locale defaultLocale;
41  
42  	/**
43  	 * Create a new CopierDecorator.
44  	 */
45  	public CopierDecorator() {
46  		super();
47  	}
48  
49  	/**
50  	 * Create a new CopierDecorator.
51  	 * @param copier
52  	 */
53  	public CopierDecorator(Copier copier) {
54  		this(copier, null);
55  	}
56  
57  	/**
58  	 * Create a new CopierDecorator.
59  	 * @param copier
60  	 * @param defaultLocale
61  	 */
62  	public CopierDecorator(Copier copier, Locale defaultLocale) {
63  		setNestedCopier(copier);
64  		setDefaultLocale(defaultLocale);
65  	}
66  
67  	/**
68  	 * {@inheritDoc}
69  	 */
70  	protected void copyImpl(Object destination, Object source, Locale locale,
71  			Integer preferredTransformationType) throws TransformationException {
72  		getNestedCopier().copy(destination, source, locale);
73  	}
74  
75  	/**
76  	 * @return Returns the nested copier.
77  	 */
78  	public Copier getNestedCopier() {
79  		return nestedCopier;
80  	}
81  
82  	/**
83  	 * @param copier The nested copier to set.
84  	 */
85  	public void setNestedCopier(Copier copier) {
86  		this.nestedCopier = copier;
87  		if (copier instanceof NodeCopier) {
88  			((NodeCopier) copier).setNestedTransformer(getNestedTransformer());
89  		}
90  	}
91  
92  	/**
93  	 * Get the defaultLocale.
94  	 * @return Locale
95  	 */
96  	public Locale getDefaultLocale() {
97  		return defaultLocale;
98  	}
99  
100 	/**
101 	 * Set the defaultLocale.
102 	 * @param defaultLocale the Locale to set
103 	 */
104 	public void setDefaultLocale(Locale defaultLocale) {
105 		this.defaultLocale = defaultLocale;
106 	}
107 
108 	/**
109 	 * {@inheritDoc}
110 	 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
111 	 */
112 	public synchronized void setSourceClasses(Class[] sourceClasses) {
113 		super.setSourceClasses(sourceClasses);
114 	}
115 
116 	/**
117 	 * {@inheritDoc}
118 	 */
119 	protected Class[] getSourceClassesImpl() {
120 		return nestedCopier.getSourceClasses();
121 	}
122 
123 	/**
124 	 * {@inheritDoc}
125 	 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
126 	 */
127 	public synchronized void setDestinationClasses(Class[] destinationClasses) {
128 		super.setDestinationClasses(destinationClasses);
129 	}
130 
131 	/**
132 	 * {@inheritDoc}
133 	 */
134 	protected Class[] getDestinationClassesImpl() {
135 		return nestedCopier.getDestinationClasses();
136 	}
137 
138 	/**
139 	 * {@inheritDoc}
140 	 * @see net.sf.morph.transform.transformers.BaseTransformer#isAutomaticallyHandlingNulls()
141 	 */
142 	protected boolean isAutomaticallyHandlingNulls() {
143 		return false;
144 	}
145 
146 	/**
147 	 * {@inheritDoc}
148 	 */
149 	protected boolean isWrappingRuntimeExceptions() {
150 		// the whole point of this copier is for decorating user defined
151 		// transformers, so we don't want to eat their exceptions ;)
152 		return false;
153 	}
154 
155 	/**
156 	 * {@inheritDoc}
157 	 */
158 	protected void setNestedTransformer(Transformer nestedTransformer) {
159 		super.setNestedTransformer(nestedTransformer);
160 		if (nestedCopier instanceof NodeCopier) {
161 			((NodeCopier) nestedCopier).setNestedTransformer(nestedTransformer);
162 		}
163 	}
164 
165 	/**
166 	 * {@inheritDoc}
167 	 */
168 	protected boolean isTransformableImpl(Class destinationType, Class sourceType)
169 			throws Exception {
170 		return TransformerUtils.isImplicitlyTransformable(this, destinationType,
171 				sourceType)
172 				&& TransformerUtils.isTransformable(getNestedTransformer(),
173 						destinationType, sourceType);
174 	}
175 
176 	/**
177 	 * {@inheritDoc}
178 	 */
179 	protected Locale getLocale() {
180 		Locale locale = getDefaultLocale();
181 		return locale == null ? super.getLocale() : locale;
182 	}
183 }