All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.management.internal.cli.util.CommandStringBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.management.internal.cli.util;

import com.gemstone.gemfire.internal.lang.SystemUtils;
import com.gemstone.gemfire.management.internal.cli.GfshParser;
import com.gemstone.gemfire.management.internal.cli.parser.SyntaxConstants;

/**-
 * Helper class to build command strings, used in the Dunits for testing gfsh
 * commands
 * 
 * @author Saurabh Bansod
 * 
 * @since 7.0
 */
public class CommandStringBuilder {
  private final String OPTION_MARKER    = SyntaxConstants.LONG_OPTION_SPECIFIER;
  private final String EQUAL_TO         = SyntaxConstants.OPTION_VALUE_SPECIFIER;
  private final String ARG_SEPARATOR    = SyntaxConstants.OPTION_SEPARATOR;
  private final String OPTION_SEPARATOR = SyntaxConstants.OPTION_SEPARATOR;
  private final String SINGLE_SPACE     = " ";

  private final    StringBuffer buffer;
  private volatile boolean      hasOptions;

  public CommandStringBuilder(String command) {
    buffer = new StringBuffer(command);
  }

  public CommandStringBuilder addArgument(String argument) {
    if (hasOptions) {
      throw new IllegalStateException("Arguments can't be specified after options. Built String is: "+buffer.toString());
    }
    buffer.append(ARG_SEPARATOR);
    buffer.append(argument);
    return this;
  }

  public CommandStringBuilder addOption(String option, String value) {
    buffer.append(OPTION_SEPARATOR);
    buffer.append(OPTION_MARKER);
    buffer.append(option);
    buffer.append(EQUAL_TO);
    buffer.append(value);
    hasOptions = true;
    return this;
  }

  public CommandStringBuilder addOption(String option) {
    buffer.append(OPTION_SEPARATOR);
    buffer.append(OPTION_MARKER);
    buffer.append(option);
    hasOptions = true;
    return this;
  }
  
  public CommandStringBuilder addNewLine() {
    buffer.append(SINGLE_SPACE); // add a space before continuation char
    buffer.append(SyntaxConstants.CONTINUATION_CHARACTER);
    buffer.append(getLineSeparator());
    return this;
  }

  private static String getLineSeparator() {
    // Until TestableGfsh issue #46388 is resolved
    if (SystemUtils.isWindows()) {
      return "\r";
    } else {
      return GfshParser.LINE_SEPARATOR;
    }
  }

  public String getCommandString() {
    return buffer.toString();
  }

  @Override
  public String toString() {
    return getCommandString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy