Delimiter with more than 1 character

552 views
Skip to first unread message

jyoti kaur

unread,
Jan 24, 2014, 4:02:13 AM1/24/14
to bea...@googlegroups.com
Hi Kevin,
I wanted to write to a text with delimiter='^A' file using bean IO. Have created customized class for this. Its working for me but Just wanted to know if there is any other way of doing this
 
package gsis.beanio;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import org.beanio.stream.RecordIOException;
import org.beanio.stream.RecordReader;
import org.beanio.stream.RecordWriter;
import org.beanio.stream.delimited.DelimitedParserConfiguration;
import org.beanio.stream.delimited.DelimitedRecordParserFactory;
import org.beanio.stream.delimited.DelimitedWriter;
public class GMSDelimitedRecordParserFactory extends DelimitedRecordParserFactory {
 
 
 
 
    @Override
    public RecordWriter createWriter(Writer out) throws IllegalArgumentException {
     
     
       if (!(out instanceof BufferedWriter)) {
        out = new BufferedWriter(out);
          }
      
         
          final BufferedWriter writer = (BufferedWriter) out;
                   return new GMSRecordWriter(writer,this);
    }
}
class GMSRecordWriter extends DelimitedWriter
{
 boolean escapeEnabled;
 char delim ='|';
 String recordTerminator = "-";
 private Writer out;
 char escapeChar ='\\';
 String gmsdelimitor ;
 String gmsAppender = "^"; 
  
 public GMSRecordWriter(Writer out) {
   this(out,'\t');  
 }
 
 public GMSRecordWriter(Writer out, char delimiter) {
  this(out, new DelimitedParserConfiguration(delimiter));
  }
 
 public GMSRecordWriter(Writer out, DelimitedParserConfiguration config) {
  super(out,config);
  this.out = out;
  delim = config.getDelimiter();
  System.out.println("finally----"+escapeChar);
  if (config.getEscape() == null) {
       escapeEnabled = false;
        }
        else {
      escapeEnabled = true;
      escapeChar = config.getEscape();
              if (delim == escapeChar) {
                  throw new IllegalArgumentException("Delimiter cannot match the escape character");
               }
   }
           recordTerminator = config.getRecordTerminator();
           if (recordTerminator == null) {
               recordTerminator = System.getProperty("line.separator");
           }
 }
 
  
 
 @Override
 public void write(Object value) throws IOException {
   write((String[])value);
  
 }
 
 public void write(String[] record) throws IOException
 {
  gmsdelimitor = gmsAppender+delim;  
  if (escapeEnabled) {
      int pos = 0;
      for (String field : record) {
          if (pos++ > 0)
           out.write(gmsdelimitor);
          char[] cs = field.toCharArray();
          for (int i = 0, j = cs.length; i < j; i++) {
              if (cs[i] == delim || cs[i] == escapeChar) {
               out.write(escapeChar);
              }
           out.write(cs[i]);
          }
      }
  }
  else {
      int pos = 0;
      for (String field : record) {
          if (pos++ > 0) {
           out.write(gmsdelimitor);
           
          }
          out.write(field);
        
      }
  }
  out.write(recordTerminator);
  
 }
 @Override
 public void close() throws IOException {
  out.close();
  
 }
 @Override
 public void flush() throws IOException {
  out.flush();
  
 }
}

Kevin

unread,
Jan 25, 2014, 3:08:23 PM1/25/14
to bea...@googlegroups.com
BeanIO doesn't support reading multi-character delimiters, so until then, creating a custom writer as you've done is the best approach.

Thanks,
Kevin

jyoti kaur

unread,
Jan 28, 2014, 6:08:55 AM1/28/14
to bea...@googlegroups.com
Thanks Kevin for prompt reply. Just wanted to confirm whther i was using correct approach or not.

Subhash Katakdhond

unread,
May 8, 2015, 2:31:52 AM5/8/15
to bea...@googlegroups.com
I have the same kind of requirement for 3 char delimiter i.e. ~^~. I tried your approach but it is not working for me. Any help will be highly appreciated. Thanks...

My configuration is - 

<stream name="CurrencyExtract" format="delimited">
<parser class="com.barclays.paymentshub.ref.config.CMRDelimitedRecordParserFactory">
<property name="delimiter" value="~" />
</parser>
<record name="currencyExtract" minOccurs="0" maxOccurs="unbounded" class="com.barclays.paymentshub.ref.extract.dto.CurrencyExtract">
<field name="ccyCode" />
<field name="ccyGtslCutoffTime" />
<field name="ccyGtslCutoffDays" />
<field name="ccyBuyRate" />
<field name="ccyBuyReferInd" />
<field name="ccySellRate" />
<field name="ccySellReferInd" />
<field name="ccyReciprocalInd" />
<field name="ccySubUnitInd" />
<field name="ccyToBeRounded" />
<!-- <field name="ccyNumeric" /> -->
</record>
</stream>

Class is  -

public class CMRDelimitedRecordParserFactory extends DelimitedRecordParserFactory implements  RecordParserFactory {

private static final Logger logger = Logger
.getLogger(CMRDelimitedRecordParserFactory.class);
@Override
public RecordWriter createWriter(Writer out) throws IllegalArgumentException {
logger.debug("Inside CMR DelimitedRecordParserFactory...");
System.out.println("Inside CMR DelimitedRecordParserFactory...");
if (!(out instanceof BufferedWriter)) {
out = new BufferedWriter(out);
}

final BufferedWriter writer = (BufferedWriter) out;
return new CMRRecordWriter(writer, this);
}
}

public class CMRRecordWriter extends DelimitedWriter {

private static final Logger logger = Logger
.getLogger(CMRRecordWriter.class);
    private char escapeChar = '\\';
    private boolean escapeEnabled = true;
    private String recordTerminator;
    private Writer out;
    
//boolean escapeEnabled;
char delim = '~';
String cmrdelimitor;
String cmrAppender = "~^";

public CMRRecordWriter(Writer out) {
this(out, '\t');
}

public CMRRecordWriter(Writer out, char delimiter) {
this(out, new DelimitedParserConfiguration(delimiter));
}

public CMRRecordWriter(Writer out, DelimitedParserConfiguration config) {
super(out, config);
this.out = out;
delim = config.getDelimiter();
logger.debug("&&&& Config Delimiter is : " + delim);
System.out.println("finally----" + escapeChar);
if (config.getEscape() == null) {
escapeEnabled = false;
} else {
escapeEnabled = true;
escapeChar = config.getEscape();
if (delim == escapeChar) {
throw new IllegalArgumentException(
"Delimiter cannot match the escape character");
}
}
recordTerminator = config.getRecordTerminator();
if (recordTerminator == null) {
recordTerminator = System.getProperty("line.separator");
}
}

@Override
public void write(Object value) throws IOException {
write((String[]) value);

}

public void write(String[] record) throws IOException {
cmrdelimitor = cmrAppender + delim;
logger.debug("&&&&& CRM Delimiter is : " + cmrdelimitor);
if (escapeEnabled) {
int pos = 0;
for (String field : record) {
if (pos++ > 0)
out.write(cmrdelimitor);
char[] cs = field.toCharArray();
for (int i = 0, j = cs.length; i < j; i++) {
if (cs[i] == delim || cs[i] == escapeChar) {
out.write(escapeChar);
}
out.write(cs[i]);
}
}
} else {
int pos = 0;
for (String field : record) {
if (pos++ > 0) {
out.write(cmrdelimitor);
Reply all
Reply to author
Forward
0 new messages