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     */
017    package org.apache.camel.component.flatpack;
018    
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    import java.io.Reader;
022    
023    import net.sf.flatpack.Parser;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.InvalidPayloadException;
026    import org.apache.camel.util.ExchangeHelper;
027    import org.springframework.core.io.Resource;
028    
029    /**
030     * @version $Revision: 988 $
031     */
032    public class DelimitedEndpoint extends FixedLengthEndpoint {
033        private char delimiter = ',';
034        private char textQualifier = '"';
035        private boolean ignoreFirstRecord = true;
036    
037        public DelimitedEndpoint(String uri, Resource resource) {
038            super(uri, resource);
039        }
040    
041        public Parser createParser(Exchange exchange) throws InvalidPayloadException, IOException {
042            Reader bodyReader = ExchangeHelper.getMandatoryInBody(exchange, Reader.class);
043            Resource resource = getResource();
044            if (resource == null) {
045                return getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier);
046            } else {
047                return getParserFactory().newDelimitedParser(new InputStreamReader(resource.getInputStream()), bodyReader, delimiter, textQualifier, ignoreFirstRecord);
048            }
049        }
050    
051    
052        // Properties
053        //-------------------------------------------------------------------------
054    
055        public char getDelimiter() {
056            return delimiter;
057        }
058    
059        public void setDelimiter(char delimiter) {
060            this.delimiter = delimiter;
061        }
062    
063        public boolean isIgnoreFirstRecord() {
064            return ignoreFirstRecord;
065        }
066    
067        public void setIgnoreFirstRecord(boolean ignoreFirstRecord) {
068            this.ignoreFirstRecord = ignoreFirstRecord;
069        }
070    
071        public char getTextQualifier() {
072            return textQualifier;
073        }
074    
075        public void setTextQualifier(char textQualifier) {
076            this.textQualifier = textQualifier;
077        }
078    
079    }