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

com.github.sbaudoin.sonar.plugins.yaml.linecounter.LineCountParser Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
/**
 * Copyright (c) 2018-2021, Sylvain Baudoin
 *
 * 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.github.sbaudoin.sonar.plugins.yaml.linecounter;

import org.apache.commons.lang.StringUtils;
import com.github.sbaudoin.yamllint.Parser;

import java.util.*;

/**
 * Counting comment lines, blank lines in YAML files
 */
public final class LineCountParser {
    private int linesNumber;
    private Set commentLines;
    private Set linesOfCodeLines;
    private LineCountData data;


    /**
     * Constructor. The passed string is parsed and an instance of {@code LineCountData} is created and ready for
     * retrieval with the method {@linkplain #getLineCountData()}
     *
     * @param contents the YAML content to be parsed
     * @see #getLineCountData()
     */
    public LineCountParser(String contents) {
        this.commentLines = new HashSet<>();
        this.linesOfCodeLines = new HashSet<>();
        linesNumber = 0;

        List lines = Parser.getLines(contents);

        for (Parser.Line line : lines) {
            String lineContent = line.getContent();
            if (isCommentLine(lineContent)) {
                commentLines.add(line.getLineNo());
            } else if (!StringUtils.isBlank(lineContent)) {
                linesOfCodeLines.add(line.getLineNo());
            }

            // Is it useful? Lists are ordered
            if (line.getLineNo() > linesNumber) {
                linesNumber = line.getLineNo();
            }
        }

        this.data = new LineCountData(
                linesNumber,
                linesOfCodeLines,
                commentLines);
    }

    /**
     * Returns the {@code LineCountData} describing the passed YAML document
     *
     * @return the {@code LineCountData} describing the passed YAML document
     */
    public LineCountData getLineCountData() {
        return data;
    }


    /**
     * Tells if the passed line is a comment line, i.e. a line with only a non-empty comment
     *
     * @param lineContent
     * @return {@code true} if the passed string represents a line of comment. Inline comments return {@code false}.
     */
    private boolean isCommentLine(String lineContent) {
        assert lineContent != null;

        return lineContent.trim().matches("^\\h*#\\h*\\S.*");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy