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.model.dataformat;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.model.DataFormatDefinition;
026    import org.apache.camel.spi.DataFormat;
027    import org.apache.camel.spi.RouteContext;
028    import org.apache.camel.util.CamelContextHelper;
029    import org.apache.camel.util.ObjectHelper;
030    
031    /**
032     * Represents a CSV (Comma Separated Values) {@link org.apache.camel.spi.DataFormat}
033     *
034     * @version
035     */
036    @XmlRootElement(name = "csv")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class CsvDataFormat extends DataFormatDefinition {
039        @XmlAttribute
040        private Boolean autogenColumns;
041        @XmlAttribute
042        private String delimiter;
043        @XmlAttribute
044        private String configRef;
045        @XmlAttribute
046        private String strategyRef;
047        @XmlAttribute
048        private Boolean skipFirstLine;
049        @XmlAttribute
050        private Boolean lazyLoad;
051        @XmlAttribute
052        private Boolean useMaps;
053    
054        public CsvDataFormat() {
055            super("csv");
056        }
057    
058        public CsvDataFormat(String delimiter) {
059            this();
060            setDelimiter(delimiter);
061        }
062    
063        public CsvDataFormat(boolean lazyLoad) {
064            this();
065            setLazyLoad(lazyLoad);
066        }
067    
068        public Boolean isAutogenColumns() {
069            return autogenColumns;
070        }
071    
072        public void setAutogenColumns(Boolean autogenColumns) {
073            this.autogenColumns = autogenColumns;
074        }
075    
076        public String getDelimiter() {
077            return delimiter;
078        }
079    
080        public void setDelimiter(String delimiter) {
081            this.delimiter = delimiter;
082        }
083    
084        public String getConfigRef() {
085            return configRef;
086        }
087    
088        public void setConfigRef(String configRef) {
089            this.configRef = configRef;
090        }
091    
092        public String getStrategyRef() {
093            return strategyRef;
094        }
095    
096        public void setStrategyRef(String strategyRef) {
097            this.strategyRef = strategyRef;
098        }
099    
100        public Boolean isSkipFirstLine() {
101            return autogenColumns;
102        }
103    
104        public void setSkipFirstLine(Boolean skipFirstLine) {
105            this.skipFirstLine = skipFirstLine;
106        }
107    
108        public Boolean getLazyLoad() {
109            return lazyLoad;
110        }
111    
112        public void setLazyLoad(Boolean lazyLoad) {
113            this.lazyLoad = lazyLoad;
114        }
115    
116        public Boolean getUseMaps() {
117            return useMaps;
118        }
119    
120        public void setUseMaps(Boolean useMaps) {
121            this.useMaps = useMaps;
122        }
123    
124        @Override
125        protected DataFormat createDataFormat(RouteContext routeContext) {
126            DataFormat csvFormat = super.createDataFormat(routeContext);
127    
128            if (ObjectHelper.isNotEmpty(configRef)) {
129                Object config = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), configRef);
130                setProperty(routeContext.getCamelContext(), csvFormat, "config", config);
131            }
132            if (ObjectHelper.isNotEmpty(strategyRef)) {
133                Object strategy = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), strategyRef);
134                setProperty(routeContext.getCamelContext(), csvFormat, "strategy", strategy);
135            }
136    
137            return csvFormat;
138        }
139    
140        @Override
141        protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
142            if (autogenColumns != null) {
143                setProperty(camelContext, dataFormat, "autogenColumns", autogenColumns);
144            }
145    
146            if (delimiter != null) {
147                if (delimiter.length() > 1) {
148                    throw new IllegalArgumentException("Delimiter must have a length of one!");
149                }
150                setProperty(camelContext, dataFormat, "delimiter", delimiter);
151            } else {
152                // the default delimiter is ','
153                setProperty(camelContext, dataFormat, "delimiter", ",");
154            }
155    
156            if (skipFirstLine != null) {
157                setProperty(camelContext, dataFormat, "skipFirstLine", skipFirstLine);
158            }
159    
160            if (lazyLoad != null) {
161                setProperty(camelContext, dataFormat, "lazyLoad", lazyLoad);
162            }
163    
164            if (useMaps != null) {
165                setProperty(camelContext, dataFormat, "useMaps", useMaps);
166            }
167        }
168    }