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 */ 017package org.apache.commons.flatfile.morph; 018 019import java.util.Locale; 020 021import org.apache.commons.flatfile.Entity; 022 023import net.sf.morph.Defaults; 024import net.sf.morph.transform.DecoratedCopier; 025import net.sf.morph.transform.converters.TextConverter; 026import net.sf.morph.transform.transformers.BaseTransformer; 027 028/** 029 * Copy any text type directly onto an Entity. 030 * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $ 031 */ 032public class TextToEntityCopier extends BaseTransformer implements 033 DecoratedCopier { 034 private TextConverter textConverter; 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 protected void copyImpl(Object destination, Object source, Locale locale, 041 Integer preferredTransformationType) throws Exception { 042 byte[] b = 043 (byte[]) getTextConverter().convert(byte[].class, source, locale); 044 ((Entity) destination).setValue(b); 045 } 046 047 /** 048 * {@inheritDoc} 049 */ 050 @Override 051 public synchronized void setSourceClasses(@SuppressWarnings("rawtypes") Class[] sourceClasses) { 052 super.setSourceClasses(sourceClasses); 053 } 054 055 /** 056 * {@inheritDoc} 057 */ 058 @Override 059 public synchronized void setDestinationClasses(@SuppressWarnings("rawtypes") Class[] destinationClasses) { 060 super.setDestinationClasses(destinationClasses); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 @Override 067 protected Class<?>[] getDestinationClassesImpl() throws Exception { 068 return new Class[] { Entity.class }; 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 protected Class<?>[] getSourceClassesImpl() throws Exception { 076 return getTextConverter().getSourceClasses(); 077 } 078 079 /** 080 * Get the TextConverter textConverter. 081 * @return TextConverter 082 */ 083 public synchronized TextConverter getTextConverter() { 084 if (textConverter == null) { 085 setTextConverter(Defaults.createTextConverter()); 086 } 087 return textConverter; 088 } 089 090 /** 091 * Set the TextConverter textConverter. 092 * @param textConverter TextConverter 093 */ 094 public synchronized void setTextConverter(TextConverter textConverter) { 095 this.textConverter = textConverter; 096 } 097 098}