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.util;
018
019import java.util.ArrayList;
020import java.util.List;
021
022/**
023 * Utility class for parsing quoted string which is intended for parameters, separated by comma.
024 */
025public final class StringQuoteHelper {
026
027    private StringQuoteHelper() {
028    }
029
030    /**
031     * Returns the text wrapped double quotes
032     */
033    public static String doubleQuote(String text) {
034        return quote(text, "\"");
035    }
036
037    /**
038     * Returns the text wrapped single quotes
039     */
040    public static String singleQuote(String text) {
041        return quote(text, "'");
042    }
043
044    /**
045     * Wraps the text in the given quote text
046     *
047     * @param  text  the text to wrap in quotes
048     * @param  quote the quote text added to the prefix and postfix of the text
049     *
050     * @return       the text wrapped in the given quotes
051     */
052    public static String quote(String text, String quote) {
053        return quote + text + quote;
054    }
055
056    /**
057     * Splits the input safely honoring if values is enclosed in quotes.
058     * <p/>
059     * Though this method does not support double quoting values. A quoted value must start with the same start and
060     * ending quote, which is either a single quote or double quote value.
061     * <p/>
062     * Will <i>trim</i> each split value by default.
063     *
064     * @param  input     the input
065     * @param  separator the separator char to split the input, for example a comma.
066     * @return           the input split, or <tt>null</tt> if the input is null.
067     */
068    public static String[] splitSafeQuote(String input, char separator) {
069        return splitSafeQuote(input, separator, true, false);
070    }
071
072    /**
073     * Splits the input safely honoring if values is enclosed in quotes.
074     * <p/>
075     * Though this method does not support double quoting values. A quoted value must start with the same start and
076     * ending quote, which is either a single quote or double quote value.
077     *
078     * @param  input     the input
079     * @param  separator the separator char to split the input, for example a comma.
080     * @param  trim      whether to trim each split value
081     * @return           the input split, or <tt>null</tt> if the input is null.
082     */
083    public static String[] splitSafeQuote(String input, char separator, boolean trim) {
084        return splitSafeQuote(input, separator, trim, false);
085    }
086
087    /**
088     * Splits the input safely honoring if values is enclosed in quotes.
089     * <p/>
090     * Though this method does not support double quoting values. A quoted value must start with the same start and
091     * ending quote, which is either a single quote or double quote value.
092     *
093     * @param  input      the input
094     * @param  separator  the separator char to split the input, for example a comma.
095     * @param  trim       whether to trim each split value
096     * @param  keepQuotes whether to keep quotes
097     * @return            the input split, or <tt>null</tt> if the input is null.
098     */
099    public static String[] splitSafeQuote(String input, char separator, boolean trim, boolean keepQuotes) {
100        if (input == null) {
101            return null;
102        }
103
104        if (input.indexOf(separator) == -1) {
105            if (input.length() > 1) {
106                char ch = input.charAt(0);
107                char ch2 = input.charAt(input.length() - 1);
108                boolean singleQuoted = ch == '\'' && ch2 == '\'';
109                boolean doubleQuoted = ch == '"' && ch2 == '"';
110                if (!keepQuotes && (singleQuoted || doubleQuoted)) {
111                    input = input.substring(1, input.length() - 1);
112                }
113            }
114            // no separator in data, so return single string with input as is
115            return new String[] { trim ? input.trim() : input };
116        }
117
118        List<String> answer = new ArrayList<>();
119        StringBuilder sb = new StringBuilder();
120
121        boolean singleQuoted = false;
122        boolean doubleQuoted = false;
123        boolean skipLeadingWhitespace = true;
124
125        for (int i = 0; i < input.length(); i++) {
126            char ch = input.charAt(i);
127            char prev = i > 0 ? input.charAt(i - 1) : 0;
128            boolean isQuoting = singleQuoted || doubleQuoted;
129            boolean last = i == input.length() - 1;
130
131            if (!doubleQuoted && ch == '\'') {
132                if (singleQuoted && prev == ch && sb.length() == 0) {
133                    // its an empty quote so add empty text
134                    if (keepQuotes) {
135                        answer.add("''");
136                    } else {
137                        answer.add("");
138                    }
139                }
140                // special logic needed if this quote is the end
141                if (last) {
142                    if (singleQuoted && sb.length() > 0) {
143                        String text = sb.toString();
144                        // do not trim a quoted string
145                        if (keepQuotes) {
146                            answer.add(text + "'"); // append ending quote
147                        } else {
148                            answer.add(text);
149                        }
150                        sb.setLength(0);
151                    }
152                    break; // break out as we are finished
153                }
154                singleQuoted = !singleQuoted;
155                if (keepQuotes) {
156                    sb.append(ch);
157                }
158                continue;
159            } else if (!singleQuoted && ch == '"') {
160                if (doubleQuoted && prev == ch && sb.length() == 0) {
161                    // its an empty quote so add empty text
162                    if (keepQuotes) {
163                        answer.add("\""); // append ending quote
164                    } else {
165                        answer.add("");
166                    }
167                }
168                // special logic needed if this quote is the end
169                if (last) {
170                    if (doubleQuoted && sb.length() > 0) {
171                        String text = sb.toString();
172                        // do not trim a quoted string
173                        if (keepQuotes) {
174                            answer.add(text + "\"");
175                        } else {
176                            answer.add(text);
177                        }
178                        sb.setLength(0);
179                    }
180                    break; // break out as we are finished
181                }
182                doubleQuoted = !doubleQuoted;
183                if (keepQuotes) {
184                    sb.append(ch);
185                }
186                continue;
187            } else if (!isQuoting && separator != ' ' && ch == ' ') {
188                if (skipLeadingWhitespace) {
189                    continue;
190                }
191            } else if (!isQuoting && ch == separator) {
192                // add as answer if we are not in a quote
193                if (sb.length() > 0) {
194                    String text = sb.toString();
195                    if (trim) {
196                        text = text.trim();
197                    }
198                    answer.add(text);
199                    sb.setLength(0);
200                }
201                // we should avoid adding the separator
202                continue;
203            }
204
205            // append char
206            sb.append(ch);
207        }
208
209        // any leftover
210        if (sb.length() > 0) {
211            String text = sb.toString();
212            if (trim) {
213                text = text.trim();
214            }
215            answer.add(text);
216        }
217
218        return answer.toArray(new String[0]);
219    }
220
221}