Here you can find the source of quoteArgument(final String argument)
Parameter | Description |
---|---|
argument | the argument to be quoted |
Parameter | Description |
---|---|
IllegalArgumentException | If argument contains both types of quotes |
public static String quoteArgument(final String argument)
//package com.java2s; /*/*from ww w . j av a 2s. c o m*/ * Copyright (c) 2011-2015 The original author or authors * ------------------------------------------------------ * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Apache License v2.0 which accompanies this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * * The Apache License v2.0 is available at * http://www.opensource.org/licenses/apache2.0.php * * You may elect to redistribute this code under either of these licenses. */ public class Main { private static final String SINGLE_QUOTE = "\'"; private static final String DOUBLE_QUOTE = "\""; /** * Puts quotes around the given String if necessary. * <p> * If the argument doesn't include spaces or quotes, return it as is. If it * contains double quotes, use single quotes - else surround the argument by * double quotes. * </p> * * @param argument the argument to be quoted * @return the quoted argument * @throws IllegalArgumentException If argument contains both types of quotes */ public static String quoteArgument(final String argument) { String cleanedArgument = argument.trim(); // strip the quotes from both ends while (cleanedArgument.startsWith(SINGLE_QUOTE) && cleanedArgument.endsWith(SINGLE_QUOTE) || cleanedArgument.startsWith(DOUBLE_QUOTE) && cleanedArgument.endsWith(DOUBLE_QUOTE)) { cleanedArgument = cleanedArgument.substring(1, cleanedArgument.length() - 1); } final StringBuilder buf = new StringBuilder(); if (cleanedArgument.contains(DOUBLE_QUOTE)) { if (cleanedArgument.contains(SINGLE_QUOTE)) { throw new IllegalArgumentException("Can't handle single and double quotes in same argument"); } return buf.append(SINGLE_QUOTE).append(cleanedArgument).append(SINGLE_QUOTE).toString(); } else if (cleanedArgument.contains(SINGLE_QUOTE) || cleanedArgument.contains(" ")) { return buf.append(DOUBLE_QUOTE).append(cleanedArgument).append(DOUBLE_QUOTE).toString(); } else { return cleanedArgument; } } }