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