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