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);
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        if (input == null) {
085            return null;
086        }
087
088        if (input.indexOf(separator) == -1) {
089            if (input.length() > 1) {
090                char ch = input.charAt(0);
091                char ch2 = input.charAt(input.length() - 1);
092                boolean singleQuoted = ch == '\'' && ch2 == '\'';
093                boolean doubleQuoted = ch == '"' && ch2 == '"';
094                if (singleQuoted || doubleQuoted) {
095                    input = input.substring(1, input.length() - 1);
096                }
097            }
098            // no separator in data, so return single string with input as is
099            return new String[] { trim ? input.trim() : input };
100        }
101
102        List<String> answer = new ArrayList<>();
103        StringBuilder sb = new StringBuilder();
104
105        boolean singleQuoted = false;
106        boolean doubleQuoted = false;
107        boolean skipLeadingWhitespace = true;
108
109        for (int i = 0; i < input.length(); i++) {
110            char ch = input.charAt(i);
111            char prev = i > 0 ? input.charAt(i - 1) : 0;
112            boolean isQuoting = singleQuoted || doubleQuoted;
113
114            if (!doubleQuoted && ch == '\'') {
115                if (singleQuoted && prev == ch && sb.length() == 0) {
116                    // its an empty quote so add empty text
117                    answer.add("");
118                }
119                // special logic needed if this quote is the end
120                if (i == input.length() - 1) {
121                    if (singleQuoted && sb.length() > 0) {
122                        String text = sb.toString();
123                        // do not trim a quoted string
124                        answer.add(text);
125                        sb.setLength(0);
126                    }
127                }
128                singleQuoted = !singleQuoted;
129                continue;
130            } else if (!singleQuoted && ch == '"') {
131                if (doubleQuoted && prev == ch && sb.length() == 0) {
132                    // its an empty quote so add empty text
133                    answer.add("");
134                }
135                // special logic needed if this quote is the end
136                if (i == input.length() - 1) {
137                    if (doubleQuoted && sb.length() > 0) {
138                        String text = sb.toString();
139                        // do not trim a quoted string
140                        answer.add(text);
141                        sb.setLength(0);
142                    }
143                }
144                doubleQuoted = !doubleQuoted;
145                continue;
146            } else if (!isQuoting && separator != ' ' && ch == ' ') {
147                if (skipLeadingWhitespace) {
148                    continue;
149                }
150            } else if (!isQuoting && ch == separator) {
151                // add as answer if we are not in a quote
152                if (sb.length() > 0) {
153                    String text = sb.toString();
154                    if (trim) {
155                        text = text.trim();
156                    }
157                    answer.add(text);
158                    sb.setLength(0);
159                }
160                // we should avoid adding the separator 
161                continue;
162            }
163
164            sb.append(ch);
165        }
166
167        // any leftover
168        if (sb.length() > 0) {
169            String text = sb.toString();
170            if (trim) {
171                text = text.trim();
172            }
173            answer.add(text);
174        }
175
176        return answer.toArray(new String[answer.size()]);
177    }
178
179}