<commit_before>package org.aim42.htmlsanitycheck.check

import org.aim42.htmlsanitycheck.html.URLUtil
import org.aim42.htmlsanitycheck.collect.SingleCheckResults
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class MissingLocalResourcesChecker extends Checker {

    public static final String MLRC_MESSAGE_PREFIX  = "local resource "
    public static final String MLRC_MESSAGE_MISSING = "missing"
    public static final String MLRC_REFCOUNT        = " (reference count "

    // members are initialized in implicit constructor
    private List<String> localResources

    // we need to know the baseDir of the html file, so we can check
    // for local resources either with relative or absolute paths
    private String baseDirPath



    // logging stuff
    private static Logger logger = LoggerFactory.getLogger(MissingLocalResourcesChecker.class);

    @Override
    protected void initCheckingResultsDescription() {
        checkingResults.whatIsChecked  = "Missing Local Resources Check"
        checkingResults.sourceItemName = "anchor tag href attribute"
        checkingResults.targetItemName = "missing local resources"
    }

    @Override
    protected SingleCheckResults check() {
        //get list of all anchor-tags containing href="xyz" in html file
        List<String> allHrefs = pageToCheck.getAllHrefStrings()

        // now filter out all local resources
        localResources = allHrefs.findAll {
            URLUtil.isLocalResource( it )
        }

        logger.info """local resources: $localResources"""

        // make sure we have a non-null baseDir
        // (for html pages given as "string", this should be "")
        if (baseDirPath == null) {
            baseDirPath = ""
        }

        // perform the actual checks
        checkAllLocalResources()

        return checkingResults

    }

    private void checkAllLocalResources() {
        localResources.each { localResource ->
            checkSingleLocalResource( localResource )
        }
    }


    /*
    @param localResource can be either:
    - file.ext
    - dir/file.ext
    - file:/dir/file.ext
    - file.ext#anchor
     */
    private void checkSingleLocalResource( String localResource )  {
        // the localResource is either path+filename  or filename or directory

        //logger.info( "resource to be checked: ", localResource )

        // bookkeeping:
        checkingResults.incNrOfChecks()

        // we need to strip the localResource of #anchor-parts
        String localResourcePath = new URI( localResource ).getPath()

        // we need the baseDir for robust checking of local resources...
        File localFile = new File( baseDirPath, localResourcePath );

        if (!localFile.exists() ) {
            String findingText = """$MLRC_MESSAGE_PREFIX + \"$localResource\" + $MLRC_MESSAGE_MISSING"""
            checkingResults.newFinding(findingText)
        }
    }
}


/************************************************************************<commit_msg>clarified and corrected minor issues with space-characters in string constants<commit_after>package org.aim42.htmlsanitycheck.check

import org.aim42.htmlsanitycheck.html.URLUtil
import org.aim42.htmlsanitycheck.collect.SingleCheckResults
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class MissingLocalResourcesChecker extends Checker {

    public static final String MLRC_MESSAGE_PREFIX  = "local resource"
    public static final String MLRC_MESSAGE_MISSING = "missing"
    public static final String MLRC_REFCOUNT        = ", reference count:"

    // members are initialized in implicit constructor
    private List<String> localResources

    // we need to know the baseDir of the html file, so we can check
    // for local resources either with relative or absolute paths
    private String baseDirPath



    // logging stuff
    private static Logger logger = LoggerFactory.getLogger(MissingLocalResourcesChecker.class);

    @Override
    protected void initCheckingResultsDescription() {
        checkingResults.whatIsChecked  = "Missing Local Resources Check"
        checkingResults.sourceItemName = "anchor tag href attribute"
        checkingResults.targetItemName = "missing local resources"
    }

    @Override
    protected SingleCheckResults check() {
        //get list of all anchor-tags containing href="xyz" in html file
        List<String> allHrefs = pageToCheck.getAllHrefStrings()

        // now filter out all local resources
        localResources = allHrefs.findAll {
            URLUtil.isLocalResource( it )
        }

        logger.debug """local resources: ${localResources}"""

        // make sure we have a non-null baseDir
        // (for html pages given as "string", this should be "")
        if (baseDirPath == null) {
            baseDirPath = ""
        }

        // perform the actual checks
        checkAllLocalResources()

        return checkingResults

    }

    private void checkAllLocalResources() {
        localResources.each { localResource ->
            checkSingleLocalResource( localResource )
        }
    }


    /*
    @param localResource can be either:
    - file.ext
    - dir/file.ext
    - file:/dir/file.ext
    - file.ext#anchor
     */
    private void checkSingleLocalResource( String localResource )  {
        // the localResource is either path+filename  or filename or directory

        logger.debug( "single resource to be checked: + $localResource" )

        // bookkeeping:
        checkingResults.incNrOfChecks()

        // we need to strip the localResource of #anchor-parts
        String localResourcePath = new URI( localResource ).getPath()

        // we need the baseDir for robust checking of local resources...
        File localFile = new File( baseDirPath, localResourcePath );

        if (!localFile.exists() ) {
            String findingText = """$MLRC_MESSAGE_PREFIX \"${localResource}\" $MLRC_MESSAGE_MISSING"""
            checkingResults.newFinding(findingText)
        }
    }
}


/************************************************************************
{'fraction_non_alphanumeric': 0.09789404409345179, 'fraction_numerical': 0.0026324448831852583, 'mean_word_length': 2.7664188351920695, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501975', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1489, 'n_words': 466}