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
060     * must start with the same start and ending quote, which is either a single
061     * quote or double quote value.
062     * <p/>
063     * Will <i>trim</i> each splitted value by default.
064     *
065     * @param input    the input
066     * @param separator the separator char to split the input, for example a comma.
067     * @return the input splitted, or <tt>null</tt> if the input is null.
068     */
069    public static String[] splitSafeQuote(String input, char separator) {
070        return splitSafeQuote(input, separator, true);
071    }
072
073    /**
074     * Splits the input safely honoring if values is enclosed in quotes.
075     * <p/>
076     * Though this method does not support double quoting values. A quoted value
077     * must start with the same start and ending quote, which is either a single
078     * quote or double quote value.
079     * \
080     * @param input    the input
081     * @param separator the separator char to split the input, for example a comma.
082     * @param trim      whether to trim each splitted value
083     * @return the input splitted, or <tt>null</tt> if the input is null.
084     */
085    public static String[] splitSafeQuote(String input, char separator, boolean trim) {
086        if (input == null) {
087            return null;
088        }
089
090        if (input.indexOf(separator) == -1) {
091            // no separator in data, so return single string with input as is
092            return new String[]{trim ? input.trim() : input};
093        }
094
095        List<String> answer = new ArrayList<>();
096        StringBuilder sb = new StringBuilder();
097
098        boolean singleQuoted = false;
099        boolean doubleQuoted = false;
100        boolean skipLeadingWhitespace = true;
101
102        for (int i = 0; i < input.length(); i++) {
103            char ch = input.charAt(i);
104            char prev = i > 0 ? input.charAt(i - 1) : 0;
105            boolean isQuoting = singleQuoted || doubleQuoted;
106
107            if (!doubleQuoted &&  ch == '\'') {
108                if (singleQuoted && prev == ch && sb.length() == 0) {
109                    // its an empty quote so add empty text
110                    answer.add("");
111                }
112                // special logic needed if this quote is the end
113                if (i == input.length() - 1) {
114                    if (singleQuoted && sb.length() > 0) {
115                        String text = sb.toString();
116                        // do not trim a quoted string
117                        answer.add(text);
118                        sb.setLength(0);
119                    }
120                }
121                singleQuoted = !singleQuoted;
122                continue;
123            } else if (!singleQuoted && ch == '"') {
124                if (doubleQuoted && prev == ch && sb.length() == 0) {
125                    // its an empty quote so add empty text
126                    answer.add("");
127                }
128                // special logic needed if this quote is the end
129                if (i == input.length() - 1) {
130                    if (doubleQuoted && sb.length() > 0) {
131                        String text = sb.toString();
132                        // do not trim a quoted string
133                        answer.add(text);
134                        sb.setLength(0);
135                    }
136                }
137                doubleQuoted = !doubleQuoted;
138                continue;
139            } else if (!isQuoting && ch == ' ') {
140                if (skipLeadingWhitespace) {
141                    continue;
142                }
143            } else if (!isQuoting && ch == separator) {
144                // add as answer if we are not in a quote
145                if (sb.length() > 0) {
146                    String text = sb.toString();
147                    if (trim) {
148                        text = text.trim();
149                    }
150                    answer.add(text);
151                    sb.setLength(0);
152                }
153                // we should avoid adding the separator 
154                continue;
155            }
156
157            sb.append(ch);
158        }
159
160        // any leftover
161        if (sb.length() > 0) {
162            String text = sb.toString();
163            if (trim) {
164                text = text.trim();
165            }
166            answer.add(text);
167        }
168
169        return answer.toArray(new String[answer.size()]);
170    }
171
172}