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;
021import java.util.Map;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.model.DataFormatDefinition;
025import org.apache.camel.model.dataformat.UniVocityAbstractDataFormat;
026import org.apache.camel.model.dataformat.UniVocityHeader;
027
028public class UniVocityAbstractDataFormatReifier<T extends UniVocityAbstractDataFormat> extends DataFormatReifier<T> {
029
030    public UniVocityAbstractDataFormatReifier(CamelContext camelContext, DataFormatDefinition definition) {
031        super(camelContext, (T)definition);
032    }
033
034    @Override
035    protected void prepareDataFormatConfig(Map<String, Object> properties) {
036        properties.put("nullValue", definition.getNullValue());
037        properties.put("skipEmptyLines", definition.getSkipEmptyLines());
038        properties.put("ignoreTrailingWhitespaces", definition.getIgnoreTrailingWhitespaces());
039        properties.put("ignoreLeadingWhitespaces", definition.getIgnoreLeadingWhitespaces());
040        properties.put("headersDisabled", definition.getHeadersDisabled());
041        properties.put("headers", getValidHeaderNames());
042        properties.put("headerExtractionEnabled", definition.getHeaderExtractionEnabled());
043        properties.put("numberOfRecordsToRead", definition.getNumberOfRecordsToRead());
044        properties.put("emptyValue", definition.getEmptyValue());
045        properties.put("lineSeparator", definition.getLineSeparator());
046        properties.put("normalizedLineSeparator", definition.getNormalizedLineSeparator());
047        properties.put("comment", definition.getComment());
048        properties.put("lazyLoad", definition.getLazyLoad());
049        properties.put("asMap", definition.getAsMap());
050    }
051
052    /**
053     * Gets only the headers with non-null and non-empty names. It returns
054     * {@code null} if there's no such headers.
055     *
056     * @return The headers with non-null and non-empty names
057     */
058    private String[] getValidHeaderNames() {
059        if (definition.getHeaders() == null) {
060            return null;
061        }
062        List<String> names = new ArrayList<>(definition.getHeaders().size());
063        for (UniVocityHeader header : definition.getHeaders()) {
064            if (header.getName() != null && !header.getName().isEmpty()) {
065                names.add(header.getName());
066            }
067        }
068        return names.isEmpty() ? null : names.toArray(new String[0]);
069    }
070}