i have written class override default parsor delimiter not working. appriciate help.
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 : " + 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 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 : " + cmrdelimitor); if (escapeenabled) { int pos = 0; (string field : record) { if (pos++ > 0) out.write(cmrdelimitor); char[] cs = field.tochararray(); (int = 0, j = cs.length; < j; i++) { if (cs[i] == delim || cs[i] == escapechar) { out.write(escapechar); } out.write(cs[i]); } } } else { int pos = 0; (string field : record) { if (pos++ > 0) { out.write(cmrdelimitor); } out.write(field); } } out.write(recordterminator); } @override public void close() throws ioexception { out.close(); } @override public void flush() throws ioexception { out.flush(); } }
the configuration file -
<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>
- you need define parser on mapping file (see below)
- then create stringdelimitedparserfactory , override createwriter return recordwriter
- finally need recordwriter implementation should use intended delimiter.
stringdelimitedparserfactory
@override public recordwriter createwriter(writer out) throws illegalargumentexception { return new stringdelimitedwriter(out, strdelimiter); } public void setdelimiter(string delimiter) { this.delimiter= delimiter; }
stringdelimitedwriter
public class stringdelimitedwriter implements recordwriter { private string delimeter = "\t"; public void write(string[] record) throws ioexception { if (escapeenabled) { int pos = 0; (string field : record) { if (pos++ > 0) out.write(delimeter); char[] cs = field.tochararray(); (int = 0, j = cs.length; < j; i++) { if (delimeter.equals(cs[i]) || cs[i] == escapechar) { out.write(escapechar); } out.write(cs[i]); } } } else { int pos = 0; (string field : record) { if (pos++ > 0) { out.write(delimeter); } out.write(field); } } out.write(recordterminator); }
mapping-file.xml
<parser class="xxx.xx.stringdelimitedparserfactory"> <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" /> </record>
Comments
Post a Comment