| 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.lang3.ArrayUtils; |
| 22 | |
|
| 23 | |
import net.sf.morph.Defaults; |
| 24 | |
import net.sf.morph.transform.DecoratedConverter; |
| 25 | |
import net.sf.morph.transform.transformers.BaseTransformer; |
| 26 | |
import net.sf.morph.util.ContainerUtils; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 12 | public class TextToByteConverter extends BaseTransformer implements |
| 33 | |
DecoratedConverter { |
| 34 | |
private DecoratedConverter textConverter; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
@Override |
| 40 | |
protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source, |
| 41 | |
Locale locale) throws Exception { |
| 42 | 114 | if (isByte(destinationClass)) { |
| 43 | 144 | byte[] b = (byte[]) getTextConverter().convert(byte[].class, |
| 44 | 36 | source, locale); |
| 45 | 108 | return b == null || b.length == 0 ? null : new Byte(b[0]); |
| 46 | |
} |
| 47 | 8 | byte[] b = source == null ? null : new byte[] { ((Byte) source) |
| 48 | 2 | .byteValue() }; |
| 49 | 6 | return getTextConverter().convert(destinationClass, b, locale); |
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
@Override |
| 56 | |
protected Class<?>[] getDestinationClassesImpl() throws Exception { |
| 57 | 16 | return ArrayUtils.addAll(getTextConverter().getDestinationClasses(), |
| 58 | 4 | byte.class, Byte.class); |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
@Override |
| 65 | |
protected Class<?>[] getSourceClassesImpl() throws Exception { |
| 66 | 16 | return ArrayUtils.addAll(getTextConverter().getSourceClasses(), |
| 67 | 4 | byte.class, Byte.class); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
@Override |
| 74 | |
protected boolean isTransformableImpl(@SuppressWarnings("rawtypes") Class destinationType, |
| 75 | |
@SuppressWarnings("rawtypes") Class sourceType) throws Exception { |
| 76 | 30 | DecoratedConverter cnv = getTextConverter(); |
| 77 | 38 | return (isByte(sourceType) && ContainerUtils.contains( |
| 78 | 0 | cnv.getDestinationClasses(), destinationType)) |
| 79 | 12 | || (isByte(destinationType) && ContainerUtils.contains( |
| 80 | 4 | cnv.getSourceClasses(), sourceType)); |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private boolean isByte(Class<?> c) { |
| 89 | 174 | return byte.class.equals(c) || Byte.class.equals(c); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public synchronized DecoratedConverter getTextConverter() { |
| 97 | 168 | if (textConverter == null) { |
| 98 | 12 | setTextConverter(Defaults.createTextConverter()); |
| 99 | |
} |
| 100 | 168 | return textConverter; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public synchronized void setTextConverter(DecoratedConverter textConverter) { |
| 108 | 12 | this.textConverter = textConverter; |
| 109 | 12 | } |
| 110 | |
|
| 111 | |
} |