| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.flatfile.morph; |
| 18 | |
|
| 19 | |
import java.util.Locale; |
| 20 | |
|
| 21 | |
import org.apache.commons.flatfile.FieldOption; |
| 22 | |
|
| 23 | |
import net.sf.morph.Defaults; |
| 24 | |
import net.sf.morph.transform.DecoratedConverter; |
| 25 | |
import net.sf.morph.transform.transformers.BaseTransformer; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
@SuppressWarnings("unchecked") |
| 32 | 6 | public class FieldOptionConstantConverter extends BaseTransformer implements |
| 33 | |
DecoratedConverter { |
| 34 | |
private DecoratedConverter textConverter; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
@SuppressWarnings("rawtypes") |
| 40 | |
@Override |
| 41 | |
protected Class[] getDestinationClassesImpl() throws Exception { |
| 42 | 6 | return new Class[] { FieldOption.class }; |
| 43 | |
} |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
@SuppressWarnings("rawtypes") |
| 49 | |
@Override |
| 50 | |
protected Class[] getSourceClassesImpl() throws Exception { |
| 51 | 6 | return getTextConverter().getSourceClasses(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
@Override |
| 58 | |
protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source, |
| 59 | |
Locale locale) throws Exception { |
| 60 | 272 | String constant = (String) getTextConverter().convert(String.class, |
| 61 | 68 | source, locale); |
| 62 | 204 | if (destinationClass.isEnum()) { |
| 63 | 204 | return Enum.valueOf(destinationClass, constant); |
| 64 | |
} |
| 65 | |
|
| 66 | 0 | return destinationClass.getField(constant).get(null); |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public synchronized DecoratedConverter getTextConverter() { |
| 74 | 210 | if (textConverter == null) { |
| 75 | 6 | setTextConverter(Defaults.createTextConverter()); |
| 76 | |
} |
| 77 | 210 | return textConverter; |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public synchronized void setTextConverter(DecoratedConverter textConverter) { |
| 85 | 6 | this.textConverter = textConverter; |
| 86 | 6 | } |
| 87 | |
|
| 88 | |
} |