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.reifier.dataformat; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.model.DataFormatDefinition; 021import org.apache.camel.model.dataformat.CsvDataFormat; 022import org.apache.camel.spi.DataFormat; 023import org.apache.camel.support.CamelContextHelper; 024import org.apache.camel.util.ObjectHelper; 025 026public class CsvDataFormatReifier extends DataFormatReifier<CsvDataFormat> { 027 028 public CsvDataFormatReifier(DataFormatDefinition definition) { 029 super((CsvDataFormat)definition); 030 } 031 032 @Override 033 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 034 // Format options 035 if (ObjectHelper.isNotEmpty(definition.getFormatRef())) { 036 Object format = CamelContextHelper.mandatoryLookup(camelContext, definition.getFormatRef()); 037 setProperty(camelContext, dataFormat, "format", format); 038 } else if (ObjectHelper.isNotEmpty(definition.getFormatName())) { 039 setProperty(camelContext, dataFormat, "formatName", definition.getFormatName()); 040 } 041 if (definition.getCommentMarkerDisabled() != null) { 042 setProperty(camelContext, dataFormat, "commentMarkerDisabled", definition.getCommentMarkerDisabled()); 043 } 044 if (definition.getCommentMarker() != null) { 045 setProperty(camelContext, dataFormat, "commentMarker", singleChar(definition.getCommentMarker(), "commentMarker")); 046 } 047 if (definition.getDelimiter() != null) { 048 setProperty(camelContext, dataFormat, "delimiter", singleChar(definition.getDelimiter(), "delimiter")); 049 } 050 if (definition.getEscapeDisabled() != null) { 051 setProperty(camelContext, dataFormat, "escapeDisabled", definition.getEscapeDisabled()); 052 } 053 if (definition.getEscape() != null) { 054 setProperty(camelContext, dataFormat, "escape", singleChar(definition.getEscape(), "escape")); 055 } 056 if (definition.getHeaderDisabled() != null) { 057 setProperty(camelContext, dataFormat, "headerDisabled", definition.getHeaderDisabled()); 058 } 059 if (definition.getHeader() != null && !definition.getHeader().isEmpty()) { 060 setProperty(camelContext, dataFormat, "header", definition.getHeader().toArray(new String[definition.getHeader().size()])); 061 } 062 if (definition.getAllowMissingColumnNames() != null) { 063 setProperty(camelContext, dataFormat, "allowMissingColumnNames", definition.getAllowMissingColumnNames()); 064 } 065 if (definition.getIgnoreEmptyLines() != null) { 066 setProperty(camelContext, dataFormat, "ignoreEmptyLines", definition.getIgnoreEmptyLines()); 067 } 068 if (definition.getIgnoreSurroundingSpaces() != null) { 069 setProperty(camelContext, dataFormat, "ignoreSurroundingSpaces", definition.getIgnoreSurroundingSpaces()); 070 } 071 if (definition.getNullStringDisabled() != null) { 072 setProperty(camelContext, dataFormat, "nullStringDisabled", definition.getNullStringDisabled()); 073 } 074 if (definition.getNullString() != null) { 075 setProperty(camelContext, dataFormat, "nullString", definition.getNullString()); 076 } 077 if (definition.getQuoteDisabled() != null) { 078 setProperty(camelContext, dataFormat, "quoteDisabled", definition.getQuoteDisabled()); 079 } 080 if (definition.getQuote() != null) { 081 setProperty(camelContext, dataFormat, "quote", singleChar(definition.getQuote(), "quote")); 082 } 083 if (definition.getRecordSeparatorDisabled() != null) { 084 setProperty(camelContext, dataFormat, "recordSeparatorDisabled", definition.getRecordSeparatorDisabled()); 085 } 086 if (definition.getRecordSeparator() != null) { 087 setProperty(camelContext, dataFormat, "recordSeparator", definition.getRecordSeparator()); 088 } 089 if (definition.getSkipHeaderRecord() != null) { 090 setProperty(camelContext, dataFormat, "skipHeaderRecord", definition.getSkipHeaderRecord()); 091 } 092 if (definition.getQuoteMode() != null) { 093 setProperty(camelContext, dataFormat, "quoteMode", definition.getQuoteMode()); 094 } 095 if (definition.getTrim() != null) { 096 setProperty(camelContext, dataFormat, "trim", definition.getTrim()); 097 } 098 if (definition.getIgnoreHeaderCase() != null) { 099 setProperty(camelContext, dataFormat, "ignoreHeaderCase", definition.getIgnoreHeaderCase()); 100 } 101 if (definition.getTrailingDelimiter() != null) { 102 setProperty(camelContext, dataFormat, "trailingDelimiter", definition.getTrailingDelimiter()); 103 } 104 105 // Unmarshall options 106 if (definition.getLazyLoad() != null) { 107 setProperty(camelContext, dataFormat, "lazyLoad", definition.getLazyLoad()); 108 } 109 if (definition.getUseMaps() != null) { 110 setProperty(camelContext, dataFormat, "useMaps", definition.getUseMaps()); 111 } 112 if (definition.getUseOrderedMaps() != null) { 113 setProperty(camelContext, dataFormat, "useOrderedMaps", definition.getUseOrderedMaps()); 114 } 115 if (ObjectHelper.isNotEmpty(definition.getRecordConverterRef())) { 116 Object recordConverter = CamelContextHelper.mandatoryLookup(camelContext, definition.getRecordConverterRef()); 117 setProperty(camelContext, dataFormat, "recordConverter", recordConverter); 118 } 119 if (ObjectHelper.isNotEmpty(definition.getMarshallerFactoryRef())) { 120 Object marshallerFactory = CamelContextHelper.mandatoryLookup(camelContext, definition.getMarshallerFactoryRef().trim()); 121 setProperty(camelContext, dataFormat, "marshallerFactory", marshallerFactory); 122 } 123 } 124 125 private static Character singleChar(String value, String attributeName) { 126 if (value.length() != 1) { 127 throw new IllegalArgumentException(String.format("The '%s' attribute must be exactly one character long.", attributeName)); 128 } 129 return value.charAt(0); 130 } 131 132}