Search in sources :

Example 1 with Parser

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Parser(net.sf.flatpack.Parser)

Example 2 with 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;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Parser(net.sf.flatpack.Parser)

Example 3 with 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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataSet(net.sf.flatpack.DataSet) DataSetList(org.apache.camel.component.flatpack.DataSetList) Parser(net.sf.flatpack.Parser)

Example 4 with Parser

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());
    }
}
Also used : DataSet(net.sf.flatpack.DataSet) Parser(net.sf.flatpack.Parser)

Example 5 with Parser

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;
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Parser(net.sf.flatpack.Parser)

Aggregations

Parser (net.sf.flatpack.Parser)5 InputStreamReader (java.io.InputStreamReader)4 InputStream (java.io.InputStream)3 DataSet (net.sf.flatpack.DataSet)2 Reader (java.io.Reader)1 DataSetList (org.apache.camel.component.flatpack.DataSetList)1