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

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

The newest version!
/*
 * Copyright 2011-2012 the original author or authors.
 *
 * 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.
 */
package com.gemstone.gemfire.management.internal.cli.util;

import com.gemstone.gemfire.management.internal.cli.util.spring.Assert;

/**
 * Utility based on code extracted from
 * {@link org.springframework.shell.core.AbstractShell#executeCommand(String)}
 * 
 * @author Abhishek Chaudhari
 * @since 7.0
 */
// @original-author Ben Alex
public class CommentSkipHelper {
  private boolean inBlockComment;
  
  public String skipComments(String line) {
    // We support simple block comments; ie a single pair per line
    if (!inBlockComment && line.contains("/*") && line.contains("*/")) {
      blockCommentBegin();
      String lhs = line.substring(0, line.lastIndexOf("/*"));
      if (line.contains("*/")) {
        line = lhs + line.substring(line.lastIndexOf("*/") + 2);
        blockCommentFinish();
      } else {
        line = lhs;
      }
    } else if (!inBlockComment && line.contains("/*")) {//GemStone Added
      blockCommentBegin();
    }
    if (inBlockComment) {
      if (!line.contains("*/")) {
        return null;
      }
      blockCommentFinish();
      line = line.substring(line.lastIndexOf("*/") + 2);
    }
    // We also support inline comments (but only at start of line, otherwise valid
    // command options like http://www.helloworld.com will fail as per ROO-517)
    if (!inBlockComment && (line.trim().startsWith("//") || line.trim().startsWith("#"))) { // # support in ROO-1116
      line = "";
    }
    // Convert any TAB characters to whitespace (ROO-527)
    line = line.replace('\t', ' ');
    return line;
  }

  public void blockCommentBegin() {
    /**asdsfsdf /*asdsdfsdsd */
    //why dis-allow this??? It's allowed in Java. It was probably because '/*' is considered as a command by Roo.
    Assert.isTrue(!inBlockComment, "Cannot open a new block comment when one already active");
    inBlockComment = true;
  }

  public void blockCommentFinish() {
    Assert.isTrue(inBlockComment, "Cannot close a block comment when it has not been opened");
    inBlockComment = false;
  }

  public void reset() {
    inBlockComment = false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy