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.model.DataFormatDefinition;
025 import org.apache.camel.spi.DataFormat;
026 import org.apache.camel.spi.RouteContext;
027 import org.apache.camel.util.CamelContextHelper;
028 import org.apache.camel.util.ObjectHelper;
029
030 /**
031 * Represents a CSV (Comma Separated Values) {@link org.apache.camel.spi.DataFormat}
032 *
033 * @version
034 */
035 @XmlRootElement(name = "csv")
036 @XmlAccessorType(XmlAccessType.FIELD)
037 public class CsvDataFormat extends DataFormatDefinition {
038 @XmlAttribute
039 private Boolean autogenColumns;
040 @XmlAttribute
041 private String delimiter;
042 @XmlAttribute
043 private String configRef;
044 @XmlAttribute
045 private String strategyRef;
046 @XmlAttribute
047 private Boolean skipFirstLine;
048
049 public CsvDataFormat() {
050 super("csv");
051 }
052
053 public CsvDataFormat(String delimiter) {
054 this();
055 setDelimiter(delimiter);
056 }
057
058 public Boolean isAutogenColumns() {
059 return autogenColumns;
060 }
061
062 public void setAutogenColumns(Boolean autogenColumns) {
063 this.autogenColumns = autogenColumns;
064 }
065
066 public String getDelimiter() {
067 return delimiter;
068 }
069
070 public void setDelimiter(String delimiter) {
071 this.delimiter = delimiter;
072 }
073
074 public String getConfigRef() {
075 return configRef;
076 }
077
078 public void setConfigRef(String configRef) {
079 this.configRef = configRef;
080 }
081
082 public String getStrategyRef() {
083 return strategyRef;
084 }
085
086 public void setStrategyRef(String strategyRef) {
087 this.strategyRef = strategyRef;
088 }
089
090 public Boolean isSkipFirstLine() {
091 return autogenColumns;
092 }
093
094 public void setSkipFirstLine(Boolean skipFirstLine) {
095 this.skipFirstLine = skipFirstLine;
096 }
097
098 @Override
099 protected DataFormat createDataFormat(RouteContext routeContext) {
100 DataFormat csvFormat = super.createDataFormat(routeContext);
101
102 if (ObjectHelper.isNotEmpty(configRef)) {
103 Object config = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), configRef);
104 setProperty(csvFormat, "config", config);
105 }
106 if (ObjectHelper.isNotEmpty(strategyRef)) {
107 Object strategy = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), strategyRef);
108 setProperty(csvFormat, "strategy", strategy);
109 }
110
111 return csvFormat;
112 }
113
114 @Override
115 protected void configureDataFormat(DataFormat dataFormat) {
116 if (autogenColumns != null) {
117 setProperty(dataFormat, "autogenColumns", autogenColumns);
118 }
119
120 if (delimiter != null) {
121 if (delimiter.length() > 1) {
122 throw new IllegalArgumentException("Delimiter must have a length of one!");
123 }
124 setProperty(dataFormat, "delimiter", delimiter);
125 } else {
126 // the default delimiter is ','
127 setProperty(dataFormat, "delimiter", ",");
128 }
129
130 if (skipFirstLine != null) {
131 setProperty(dataFormat, "skipFirstLine", skipFirstLine);
132 }
133 }
134 }