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.Collections;
022import java.util.Iterator;
023import java.util.List;
024
025import org.apache.commons.lang3.StringUtils;
026
027/**
028 * <p>
029 * A specialized implementation of {@code ListDelimiterHandler} which simulates
030 * the list delimiter handling as it was used by {@code PropertiesConfiguration}
031 * in Commons Configuration 1.x.
032 * </p>
033 * <p>
034 * This class mainly exists for compatibility reasons. It is intended to be used
035 * by applications which have to deal with properties files created by an older
036 * version of this library.
037 * </p>
038 * <p>
039 * In the 1.x series of Commons Configuration list handling was not fully
040 * consistent. The escaping of property values was done in a different way if
041 * they contained a list delimiter or not. From version 2.0 on, escaping is more
042 * stringent which might cause slightly different results when parsing
043 * properties files created by or for Configuration 1.x. If you encounter such
044 * problems, you can switch to this {@code ListDelimiterHandler} implementation
045 * rather than the default one. In other cases, this class should not be used!
046 * </p>
047 * <p>
048 * Implementation note: An instance of this class can safely be shared between
049 * multiple {@code Configuration} instances.
050 * </p>
051 *
052 * @version $Id: LegacyListDelimiterHandler.java 1790899 2017-04-10 21:56:46Z ggregory $
053 * @since 2.0
054 */
055public class LegacyListDelimiterHandler extends AbstractListDelimiterHandler
056{
057    /** Constant for the escaping character. */
058    private static final String ESCAPE = "\\";
059
060    /** Constant for the escaped escaping character. */
061    private static final String DOUBLE_ESC = ESCAPE + ESCAPE;
062
063    /** Constant for a duplicated sequence of escaping characters. */
064    private static final String QUAD_ESC = DOUBLE_ESC + DOUBLE_ESC;
065
066    /** The list delimiter character. */
067    private final char delimiter;
068
069    /**
070     * Creates a new instance of {@code LegacyListDelimiterHandler} and sets the
071     * list delimiter character.
072     *
073     * @param listDelimiter the list delimiter character
074     */
075    public LegacyListDelimiterHandler(char listDelimiter)
076    {
077        delimiter = listDelimiter;
078    }
079
080    /**
081     * Returns the list delimiter character.
082     *
083     * @return the list delimiter character
084     */
085    public char getDelimiter()
086    {
087        return delimiter;
088    }
089
090    /**
091     * {@inheritDoc} This implementation performs delimiter escaping for a
092     * single value (which is not part of a list).
093     */
094    @Override
095    public Object escape(Object value, ValueTransformer transformer)
096    {
097        return escapeValue(value, false, transformer);
098    }
099
100    /**
101     * {@inheritDoc} This implementation performs a special encoding of
102     * backslashes at the end of a string so that they are not interpreted as
103     * escape character for a following list delimiter.
104     */
105    @Override
106    public Object escapeList(List<?> values, ValueTransformer transformer)
107    {
108        if (!values.isEmpty())
109        {
110            Iterator<?> it = values.iterator();
111            String lastValue = escapeValue(it.next(), true, transformer);
112            StringBuilder buf = new StringBuilder(lastValue);
113            while (it.hasNext())
114            {
115                // if the last value ended with an escape character, it has
116                // to be escaped itself; otherwise the list delimiter will
117                // be escaped
118                if (lastValue.endsWith(ESCAPE)
119                        && (countTrailingBS(lastValue) / 2) % 2 != 0)
120                {
121                    buf.append(ESCAPE).append(ESCAPE);
122                }
123                buf.append(getDelimiter());
124                lastValue = escapeValue(it.next(), true, transformer);
125                buf.append(lastValue);
126            }
127            return buf.toString();
128        }
129        else
130        {
131            return null;
132        }
133    }
134
135    /**
136     * {@inheritDoc} This implementation simulates the old splitting algorithm.
137     * The string is split at the delimiter character if it is not escaped. If
138     * the delimiter character is not found, the input is returned unchanged.
139     */
140    @Override
141    protected Collection<String> splitString(String s, boolean trim)
142    {
143        if (s.indexOf(getDelimiter()) < 0)
144        {
145            return Collections.singleton(s);
146        }
147
148        List<String> list = new ArrayList<>();
149
150        StringBuilder token = new StringBuilder();
151        int begin = 0;
152        boolean inEscape = false;
153        char esc = ESCAPE.charAt(0);
154
155        while (begin < s.length())
156        {
157            char c = s.charAt(begin);
158            if (inEscape)
159            {
160                // last character was the escape marker
161                // can current character be escaped?
162                if (c != getDelimiter() && c != esc)
163                {
164                    // no, also add escape character
165                    token.append(esc);
166                }
167                token.append(c);
168                inEscape = false;
169            }
170
171            else
172            {
173                if (c == getDelimiter())
174                {
175                    // found a list delimiter -> add token and
176                    // resetDefaultFileSystem buffer
177                    String t = token.toString();
178                    if (trim)
179                    {
180                        t = t.trim();
181                    }
182                    list.add(t);
183                    token = new StringBuilder();
184                }
185                else if (c == esc)
186                {
187                    // eventually escape next character
188                    inEscape = true;
189                }
190                else
191                {
192                    token.append(c);
193                }
194            }
195
196            begin++;
197        }
198
199        // Trailing delimiter?
200        if (inEscape)
201        {
202            token.append(esc);
203        }
204        // Add last token
205        String t = token.toString();
206        if (trim)
207        {
208            t = t.trim();
209        }
210        list.add(t);
211
212        return list;
213    }
214
215    /**
216     * {@inheritDoc} This is just a dummy implementation. It is never called.
217     */
218    @Override
219    protected String escapeString(String s)
220    {
221        return null;
222    }
223
224    /**
225     * Performs the escaping of backslashes in the specified properties value.
226     * Because a double backslash is used to escape the escape character of a
227     * list delimiter, double backslashes also have to be escaped if the
228     * property is part of a (single line) list. In addition, because the output
229     * is written into a properties file, each occurrence of a backslash again
230     * has to be doubled. This method is called by {@code escapeValue()}.
231     *
232     * @param value the value to be escaped
233     * @param inList a flag whether the value is part of a list
234     * @return the value with escaped backslashes as string
235     */
236    protected String escapeBackslashs(Object value, boolean inList)
237    {
238        String strValue = String.valueOf(value);
239
240        if (inList && strValue.indexOf(DOUBLE_ESC) >= 0)
241        {
242            strValue = StringUtils.replace(strValue, DOUBLE_ESC, QUAD_ESC);
243        }
244
245        return strValue;
246    }
247
248    /**
249     * Escapes the given property value. This method is called on saving the
250     * configuration for each property value. It ensures a correct handling of
251     * backslash characters and also takes care that list delimiter characters
252     * in the value are escaped.
253     *
254     * @param value the property value
255     * @param inList a flag whether the value is part of a list
256     * @param transformer the {@code ValueTransformer}
257     * @return the escaped property value
258     */
259    protected String escapeValue(Object value, boolean inList,
260            ValueTransformer transformer)
261    {
262        String escapedValue =
263                String.valueOf(transformer.transformValue(escapeBackslashs(
264                        value, inList)));
265        if (getDelimiter() != 0)
266        {
267            escapedValue =
268                    StringUtils.replace(escapedValue,
269                            String.valueOf(getDelimiter()), ESCAPE
270                                    + getDelimiter());
271        }
272        return escapedValue;
273    }
274
275    /**
276     * Returns the number of trailing backslashes. This is sometimes needed for
277     * the correct handling of escape characters.
278     *
279     * @param line the string to investigate
280     * @return the number of trailing backslashes
281     */
282    private static int countTrailingBS(String line)
283    {
284        int bsCount = 0;
285        for (int idx = line.length() - 1; idx >= 0 && line.charAt(idx) == '\\'; idx--)
286        {
287            bsCount++;
288        }
289
290        return bsCount;
291    }
292}