    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 *     https://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.commons.configuration2.convert;
    018
    019import java.lang.reflect.Array;
    020import java.nio.file.Path;
    021import java.util.ArrayList;
    022import java.util.Collection;
    023import java.util.Iterator;
    024import java.util.LinkedList;
    025import java.util.Set;
    026
    027/**
    028 * <p>
    029 * An abstract base class for concrete {@code ListDelimiterHandler} implementations.
    030 * </p>
    031 * <p>
    032 * This base class provides a fully functional implementation for parsing a value object which can deal with different
    033 * cases like collections, arrays, iterators, etc. This logic is typically needed by every concrete subclass. Other
    034 * methods are partly implemented handling special corner cases like <strong>null</strong> values; concrete subclasses do not have
    035 * do implement the corresponding checks.
    036 * </p>
    037 *
    038 * @since 2.0
    039 */
    040public abstract class AbstractListDelimiterHandler implements ListDelimiterHandler {
    041
    042    static Collection<?> flatten(final ListDelimiterHandler handler, final Object value, final int limit, final Set<Object> dejaVu) {
    043        if (value instanceof String) {
    044            return handler.split((String) value, true);
    045        }
    046        dejaVu.add(value);
    047        final Collection<Object> result = new LinkedList<>();
    048        if (value instanceof Path) {
    049            // Don't handle as an Iterable.
    050            result.add(value);
    051        } else if (value instanceof Iterable) {
    052            flattenIterator(handler, result, ((Iterable<?>) value).iterator(), limit, dejaVu);
    053        } else if (value instanceof Iterator) {
    054            flattenIterator(handler, result, (Iterator<?>) value, limit, dejaVu);
    055        } else if (value != null) {
    056            if (value.getClass().isArray()) {
    057                for (int len = Array.getLength(value), idx = 0, size = 0; idx < len && size < limit; idx++, size = result.size()) {
    058                    result.addAll(handler.flatten(Array.get(value, idx), limit - size));
    059                }
    060            } else {
    061                result.add(value);
    062            }
    063        }
    064        return result;
    065    }
    066
    067    /**
    068     * Flattens the given iterator. For each element in the iteration {@code flatten()} is called recursively.
    069     *
    070     * @param handler the working handler
    071     * @param target the target collection
    072     * @param iterator the iterator to process
    073     * @param limit a limit for the number of elements to extract
    074     * @param dejaVue Previously visited objects.
    075     */
    076    static void flattenIterator(final ListDelimiterHandler handler, final Collection<Object> target, final Iterator<?> iterator, final int limit,
    077            final Set<Object> dejaVue) {
    078        int size = target.size();
    079        while (size < limit && iterator.hasNext()) {
    080            final Object next = iterator.next();
    081            if (!dejaVue.contains(next)) {
    082                target.addAll(flatten(handler, next, limit - size, dejaVue));
    083                size = target.size();
    084            }
    085        }
    086    }
    087
    088    /**
    089     * Constructs a new instance.
    090     */
    091    public AbstractListDelimiterHandler() {
    092        // empty
    093    }
    094
    095    /**
    096     * {@inheritDoc} This implementation checks whether the object to be escaped is a string. If yes, it delegates to
    097     * {@link #escapeString(String)}, otherwise no escaping is performed. Eventually, the passed in transformer is invoked
    098     * so that additional encoding can be performed.
    099     */
    100    @Override
    101    public Object escape(final Object value, final ValueTransformer transformer) {
    102        return transformer.transformValue(value instanceof String ? escapeString((String) value) : value);
    103    }
    104
    105    /**
    106     * Escapes the specified string. This method is called by {@code escape()} if the passed in object is a string. Concrete
    107     * subclasses have to implement their specific escaping logic here, so that the list delimiters they support are
    108     * properly escaped.
    109     *
    110     * @param s the string to be escaped (not <strong>null</strong>)
    111     * @return the escaped string
    112     */
    113    protected abstract String escapeString(String s);
    114
    115    /**
    116     * Performs the actual work as advertised by the {@code parse()} method. This method delegates to
    117     * {@link #flatten(Object, int)} without specifying a limit.
    118     *
    119     * @param value the value to be processed
    120     * @return a &quot;flat&quot; collection containing all primitive values of the passed in object
    121     */
    122    private Collection<?> flatten(final Object value) {
    123        return flatten(value, Integer.MAX_VALUE);
    124    }
    125
    126    /**
    127     * {@inheritDoc} Depending on the type of the passed in object the following things happen:
    128     * <ul>
    129     * <li>Strings are checked for delimiter characters and split if necessary. This is done by calling the {@code split()}
    130     * method.</li>
    131     * <li>For objects implementing the {@code Iterable} interface, the corresponding {@code Iterator} is obtained, and
    132     * contained elements are added to the resulting iteration.</li>
    133     * <li>Arrays are treated as {@code Iterable} objects.</li>
    134     * <li>All other types are directly inserted.</li>
    135     * <li>Recursive combinations are supported, for example a collection containing an array that contains strings: The resulting
    136     * collection will only contain primitive objects.</li>
    137     * </ul>
    138     */
    139    @Override
    140    public Iterable<?> parse(final Object value) {
    141        return flatten(value);
    142    }
    143
    144    /**
    145     * {@inheritDoc} This implementation handles the case that the passed in string is <strong>null</strong>. In this case, an empty
    146     * collection is returned. Otherwise, this method delegates to {@link #splitString(String, boolean)}.
    147     */
    148    @Override
    149    public Collection<String> split(final String s, final boolean trim) {
    150        return s == null ? new ArrayList<>(0) : splitString(s, trim);
    151    }
    152
    153    /**
    154     * Actually splits the passed in string which is guaranteed to be not <strong>null</strong>. This method is called by the base
    155     * implementation of the {@code split()} method. Here the actual splitting logic has to be implemented.
    156     *
    157     * @param s the string to be split (not <strong>null</strong>)
    158     * @param trim a flag whether the single components have to be trimmed
    159     * @return a collection with the extracted components of the passed in string
    160     */
    161    protected abstract Collection<String> splitString(String s, boolean trim);
    162}