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 java.util.ArrayList; 020import java.util.List; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.model.DataFormatDefinition; 024import org.apache.camel.model.dataformat.UniVocityAbstractDataFormat; 025import org.apache.camel.model.dataformat.UniVocityHeader; 026import org.apache.camel.spi.DataFormat; 027 028public class UniVocityAbstractDataFormatReifier<T extends UniVocityAbstractDataFormat> extends DataFormatReifier<T> { 029 030 public UniVocityAbstractDataFormatReifier(DataFormatDefinition definition) { 031 super((T)definition); 032 } 033 034 @Override 035 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 036 super.configureDataFormat(dataFormat, camelContext); 037 038 if (definition.getNullValue() != null) { 039 setProperty(camelContext, dataFormat, "nullValue", definition.getNullValue()); 040 } 041 if (definition.getSkipEmptyLines() != null) { 042 setProperty(camelContext, dataFormat, "skipEmptyLines", definition.getSkipEmptyLines()); 043 } 044 if (definition.getIgnoreTrailingWhitespaces() != null) { 045 setProperty(camelContext, dataFormat, "ignoreTrailingWhitespaces", definition.getIgnoreTrailingWhitespaces()); 046 } 047 if (definition.getIgnoreLeadingWhitespaces() != null) { 048 setProperty(camelContext, dataFormat, "ignoreLeadingWhitespaces", definition.getIgnoreLeadingWhitespaces()); 049 } 050 if (definition.getHeadersDisabled() != null) { 051 setProperty(camelContext, dataFormat, "headersDisabled", definition.getHeadersDisabled()); 052 } 053 String[] validHeaderNames = getValidHeaderNames(); 054 if (validHeaderNames != null) { 055 setProperty(camelContext, dataFormat, "headers", validHeaderNames); 056 } 057 if (definition.getHeaderExtractionEnabled() != null) { 058 setProperty(camelContext, dataFormat, "headerExtractionEnabled", definition.getHeaderExtractionEnabled()); 059 } 060 if (definition.getNumberOfRecordsToRead() != null) { 061 setProperty(camelContext, dataFormat, "numberOfRecordsToRead", definition.getNumberOfRecordsToRead()); 062 } 063 if (definition.getEmptyValue() != null) { 064 setProperty(camelContext, dataFormat, "emptyValue", definition.getEmptyValue()); 065 } 066 if (definition.getLineSeparator() != null) { 067 setProperty(camelContext, dataFormat, "lineSeparator", definition.getLineSeparator()); 068 } 069 if (definition.getNormalizedLineSeparator() != null) { 070 setProperty(camelContext, dataFormat, "normalizedLineSeparator", singleCharOf("normalizedLineSeparator", definition.getNormalizedLineSeparator())); 071 } 072 if (definition.getComment() != null) { 073 setProperty(camelContext, dataFormat, "comment", singleCharOf("comment", definition.getComment())); 074 } 075 if (definition.getLazyLoad() != null) { 076 setProperty(camelContext, dataFormat, "lazyLoad", definition.getLazyLoad()); 077 } 078 if (definition.getAsMap() != null) { 079 setProperty(camelContext, dataFormat, "asMap", definition.getAsMap()); 080 } 081 } 082 083 protected static Character singleCharOf(String attributeName, String string) { 084 if (string.length() != 1) { 085 throw new IllegalArgumentException("Only one character must be defined for " + attributeName); 086 } 087 return string.charAt(0); 088 } 089 090 /** 091 * Gets only the headers with non-null and non-empty names. It returns 092 * {@code null} if there's no such headers. 093 * 094 * @return The headers with non-null and non-empty names 095 */ 096 private String[] getValidHeaderNames() { 097 if (definition.getHeaders() == null) { 098 return null; 099 } 100 List<String> names = new ArrayList<>(definition.getHeaders().size()); 101 for (UniVocityHeader header : definition.getHeaders()) { 102 if (header.getName() != null && !header.getName().isEmpty()) { 103 names.add(header.getName()); 104 } 105 } 106 return names.isEmpty() ? null : names.toArray(new String[names.size()]); 107 } 108}