use of net.sf.flatpack.Parser in project camel by apache.
the class FlatpackEndpoint method createDelimitedParser.
public Parser createDelimitedParser(Exchange exchange) throws InvalidPayloadException, IOException {
Reader bodyReader = exchange.getIn().getMandatoryBody(Reader.class);
Parser parser;
if (ObjectHelper.isEmpty(getResourceUri())) {
parser = getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier);
} else {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
InputStreamReader reader = new InputStreamReader(is, IOHelper.getCharsetName(exchange));
parser = getParserFactory().newDelimitedParser(reader, bodyReader, delimiter, textQualifier, ignoreFirstRecord);
}
if (isAllowShortLines()) {
parser.setHandlingShortLines(true);
parser.setIgnoreParseWarnings(true);
}
if (isIgnoreExtraColumns()) {
parser.setIgnoreExtraColumns(true);
parser.setIgnoreParseWarnings(true);
}
return parser;
}
use of net.sf.flatpack.Parser in project camel by apache.
the class FlatpackEndpoint method createFixedParser.
protected Parser createFixedParser(String resourceUri, Reader bodyReader) throws IOException {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
InputStreamReader reader = new InputStreamReader(is);
Parser parser = getParserFactory().newFixedLengthParser(reader, bodyReader);
if (isAllowShortLines()) {
parser.setHandlingShortLines(true);
parser.setIgnoreParseWarnings(true);
}
if (isIgnoreExtraColumns()) {
parser.setIgnoreExtraColumns(true);
parser.setIgnoreParseWarnings(true);
}
return parser;
}
use of net.sf.flatpack.Parser in project camel by apache.
the class FlatpackDataFormat method unmarshal.
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
InputStreamReader reader = new InputStreamReader(stream, IOHelper.getCharsetName(exchange));
try {
Parser parser = createParser(exchange, reader);
DataSet dataSet = parser.parse();
return new DataSetList(dataSet);
} finally {
reader.close();
}
}
use of net.sf.flatpack.Parser in project camel by apache.
the class FlatpackProducer method process.
@SuppressWarnings("unchecked")
public void process(Exchange exchange) throws Exception {
Parser parser = endpoint.createParser(exchange);
DataSet dataSet = parser.parse();
if (dataSet.getErrorCount() > 0) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("Flatpack has found %s errors while parsing", dataSet.getErrorCount()));
throw new FlatpackException(sb.toString(), exchange, dataSet.getErrors());
}
if (endpoint.isSplitRows()) {
int counter = 0;
while (dataSet.next()) {
endpoint.processDataSet(exchange, dataSet, counter++);
}
} else {
endpoint.processDataSet(exchange, dataSet, dataSet.getRowCount());
}
}
use of net.sf.flatpack.Parser in project camel by apache.
the class FlatpackDataFormat method createParser.
// Implementation methods
//-------------------------------------------------------------------------
protected Parser createParser(Exchange exchange, Reader bodyReader) throws IOException {
if (isFixed()) {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(exchange.getContext(), getDefinition());
InputStreamReader reader = new InputStreamReader(is, IOHelper.getCharsetName(exchange));
Parser parser = getParserFactory().newFixedLengthParser(reader, bodyReader);
if (allowShortLines) {
parser.setHandlingShortLines(true);
parser.setIgnoreParseWarnings(true);
}
if (ignoreExtraColumns) {
parser.setIgnoreExtraColumns(true);
parser.setIgnoreParseWarnings(true);
}
return parser;
} else {
if (ObjectHelper.isEmpty(getDefinition())) {
return getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier);
} else {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(exchange.getContext(), getDefinition());
InputStreamReader reader = new InputStreamReader(is, IOHelper.getCharsetName(exchange));
Parser parser = getParserFactory().newDelimitedParser(reader, bodyReader, delimiter, textQualifier, ignoreFirstRecord);
if (allowShortLines) {
parser.setHandlingShortLines(true);
parser.setIgnoreParseWarnings(true);
}
if (ignoreExtraColumns) {
parser.setIgnoreExtraColumns(true);
parser.setIgnoreParseWarnings(true);
}
return parser;
}
}
}
Aggregations