org.apache.struts.maven.snippetextractor.parser.java.CommentParser.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.struts.maven.snippetextractor.parser.java.CommentParser.java

Source

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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 org.apache.struts.maven.snippetextractor.parser.java;

import org.apache.commons.lang.StringUtils;

public class CommentParser {

    private static final String JAVA_COMMENT_START_STRING = "/*";
    private static final String JAVA_COMMENT_END_STRING = "*/";

    /**
     * Check if the provided string starts with the pattern {@value #JAVA_COMMENT_START_STRING} thus
     * is a java comment.
     *
     * @param string {@link String} to be checked for a java comment start
     * @return {@code true} if the string starts with a java comment start, otherwise {@code false}.
     */
    public static boolean isStart(String string) {
        return string.trim().startsWith(JAVA_COMMENT_START_STRING);
    }

    /**
     * Find the position of the end of the java comment by using the pattern {@value #JAVA_COMMENT_END_STRING}.
     *
     * @param string {@link String} to be checked for a java comment end
     * @return position of the java comment end as int, if none is found {@code -1} is returned
     */
    public static int indexOfEnd(String string) {
        return string.indexOf(JAVA_COMMENT_END_STRING);
    }

    /**
     * Check whether the provided {@link String} contains a java comment end by searching for the pattern
     * {@value #JAVA_COMMENT_END_STRING}.
     *
     * @param string {@link String} to check for a java comment end
     * @return {@code true} if a java comment end is found, otherwise {@code false}
     */
    public static boolean isEnd(String string) {
        return indexOfEnd(string) > -1;
    }

    /**
     * Remove the java comment start pattern {@value #JAVA_COMMENT_START_STRING} from the start of
     * provided string. Whitespaces at the start are removed. If the java comment is a javadoc start, the second
     * asterisk is also removed. If no pattern is found the string is returned unchanged.
     *
     * @param string {@link String} which is to be stripped from the java comment start.
     * @return {@link String} without java comment or javadoc start or the unchanged string
     */
    public static String removeCommentStart(String string) {
        String result = StringUtils.stripStart(string, " \t");
        if (!result.startsWith(CommentParser.JAVA_COMMENT_START_STRING)) {
            return string;
        }
        result = StringUtils.removeStart(result, CommentParser.JAVA_COMMENT_START_STRING);
        if (result.startsWith("*")) {
            result = result.substring(1);
        }
        return result;
    }

    /**
     * Remove the asterisk every javadoc line starts with. All whitespaces before the asterisk will be removed. If no
     * asterisk is found the string is returned unchanged.
     *
     * @param string {@link String} to be stripped from javadoc asterisks
     * @return {@link String} without the javadoc start asterisk or the unchanged provided string
     */
    public static String removeJavadocAsterisk(String string) {
        String stripped = StringUtils.stripStart(string, " \t");
        if (stripped.startsWith("*")) {
            return stripped.substring(1, stripped.length());
        }
        return string;
    }
}