我编写了一个类来覆盖分隔符的默认parsor,但它不起作用。请帮助你。
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);
}
out.write(field);
}
}
out.write(recordTerminator);
}
@Override
public void close() throws IOException {
out.close();
}
@Override
public void flush() throws IOException {
out.flush();
}
}
配置文件是 -
<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>
答案 0 :(得分:0)
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;
for (String field : record) {
if (pos++ > 0)
out.write(delimeter);
char[] cs = field.toCharArray();
for (int i = 0, j = cs.length; i < j; i++) {
if (delimeter.equals(cs[i]) || cs[i] == escapeChar) {
out.write(escapeChar);
}
out.write(cs[i]);
}
}
}
else {
int pos = 0;
for (String field : record) {
if (pos++ > 0) {
out.write(delimeter);
}
out.write(field);
}
}
out.write(recordTerminator);
}
映射-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>