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