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

de.pfabulist.loracle.attribution.Header Maven / Gradle / Ivy

Go to download

maven plugin to check the licenses of all dependencies and possible incompatibilities

There is a newer version: 2.0.1
Show newest version
package de.pfabulist.loracle.attribution;

import de.pfabulist.frex.Frex;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static de.pfabulist.roast.NonnullCheck._nn;

/**
 * Copyright (c) 2006 - 2016, Stephan Pfab
 * SPDX-License-Identifier: BSD-2-Clause
 */

public class Header {

    static Pattern start =
            Frex.txt( "//" ).zeroOrOnce().
                    then( Frex.or( Frex.fullWord( "public" ),
                                   Frex.fullWord( "private" ),
                                   Frex.fullWord( "protected" ),
                                   Frex.fullWord( "abstract" ),
                                   Frex.fullWord( "final" ),
                                   Frex.fullWord( "static" ),
                                   Frex.whitespace() ).zeroOrMore() ).
                    then( Frex.or( Frex.fullWord( "class" ),
                                   Frex.fullWord( "interface" ),
                                   Frex.fullWord( "@interface" ),
                                   Frex.fullWord( "enum" ) ) ).
                    then( Frex.any().oneOrMore() ).
                    then( Frex.or( Frex.txt( '\r' ), Frex.txt( '\n' ) ).zeroOrMore()).
                    buildCaseInsensitivePattern();


    public static String getSrcHeader( String in ) {
        String[] lines = in.split( "\n" );

        AtomicReference afterClass = new AtomicReference<>( false );
        AtomicReference count = new AtomicReference<>( 0 );

        return Arrays.stream( lines ).
                filter( l -> !l.startsWith( "import" ) && !l.startsWith( "package" ) ).
                peek( l -> count.set( _nn( count.get() ) + l.length() ) ).
                map( l -> {
                    if( _nn( afterClass.get() ) ) {
                        return "";
                    }

                    if( start.matcher( l ).matches() || _nn( count.get() ) > 3000 ) {
                        afterClass.set( true );
                        return "";
                    }

                    return l;
                } ).
                filter( l -> !l.trim().isEmpty() ).
                collect( Collectors.joining( "\n" ) );

    }

    public static String getPomHeader( String in ) {
        int start = in.indexOf( "" ) ) {
            ret = ret.substring( "".length() ).trim();
        }

        return ret;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy