001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.dataset;
018
019import java.io.BufferedReader;
020import java.io.File;
021import java.io.FileNotFoundException;
022import java.io.FileReader;
023import java.io.IOException;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.Scanner;
027
028/**
029 * A DataSet that reads payloads from a file that are used to create each message exchange
030 * along with using a pluggable transformer to customize the messages.  The file contents may optionally
031 * be split using a supplied token.
032 *
033 * @version
034 */
035public class FileDataSet extends ListDataSet {
036    private File sourceFile;
037    private String delimiter = "\\z";
038
039    private List<Object> defaultBodies;
040
041    public FileDataSet(String sourceFileName) throws IOException {
042        this(new File(sourceFileName));
043    }
044
045    public FileDataSet(File sourceFile) throws IOException {
046        this(sourceFile, "\\z");
047    }
048
049    public FileDataSet(String sourceFileName, String delimiter) throws IOException {
050        this(new File(sourceFileName), delimiter);
051    }
052
053    public FileDataSet(File sourceFile, String delimiter) throws IOException {
054        setSourceFile(sourceFile, delimiter);
055    }
056
057    // Properties
058    //-------------------------------------------------------------------------
059
060    public File getSourceFile() {
061        return sourceFile;
062    }
063
064    public void setSourceFile(File sourceFile) throws IOException {
065        this.sourceFile = sourceFile;
066        readSourceFile();
067    }
068
069    public void setSourceFile(File sourceFile, String delimiter) throws IOException {
070        this.sourceFile = sourceFile;
071        this.delimiter = delimiter;
072        readSourceFile();
073    }
074
075    public String getDelimiter() {
076        return delimiter;
077    }
078
079    // Implementation methods
080    //-------------------------------------------------------------------------
081
082    private void readSourceFile() throws IOException {
083        List<Object> bodies = new LinkedList<>();
084        try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
085            Scanner scanner = new Scanner(br);
086            scanner.useDelimiter(delimiter);
087            while (scanner.hasNext()) {
088                String nextPayload = scanner.next();
089                if ((nextPayload != null)  &&  (nextPayload.length() > 0)) {
090                    bodies.add(nextPayload);
091                }
092            }
093            setDefaultBodies(bodies);
094        }
095    }
096}