<commit_before>/*
 * Copyright 2014 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 asset.pipeline


import java.util.regex.Pattern
import java.security.MessageDigest
import java.nio.channels.FileChannel

/**
 * Helper class for resolving assets
 *
 * @author <NAME>
 * @author <NAME>
 */
class AssetHelper {
    static final Collection<Class<AssetFile>> assetSpecs = AssetSpecLoader.loadSpecifications()
    static final String QUOTED_FILE_SEPARATOR = Pattern.quote(File.separator)
    static final String DIRECTIVE_FILE_SEPARATOR = '/'

    /**
     * Resolve an {@link AssetFile} for the given URI
     *
     * @param uri The URI
     * @param contentType The content type
     * @param ext The extension
     * @param baseFile The base file
     * @return
     */
    static AssetFile fileForUri(String uri, String contentType=null, String ext=null, AssetFile baseFile=null) {
        AssetFile file
        for(resolver in AssetPipelineConfigHolder.resolvers) {
            file = resolver.getAsset(uri,contentType,ext,baseFile)
            if(file) {
                return file
            }
        }
        return null
    }

    /**
     * @return The classes that implement the {@link AssetFile} interface
     */
    static Collection<Class<AssetFile>> assetFileClasses() {
        return assetSpecs
    }

    static AssetFile assetForFile(file, String contentType, AssetFile baseFile=null) {
        if(contentType == null || file == null) {
            return file
        }

        def possibleFileSpecs = getPossibleFileSpecs(contentType)
        for(fileSpec in possibleFileSpecs) {
            for(extension in fileSpec.extensions) {
                def fileName = file.getAbsolutePath()
                if(fileName.endsWith("." + extension)) {
                    return fileSpec.newInstance(file: file, baseFile:baseFile)
                }
            }
        }

        return file
    }

    /**
    * Finds the AssetFile definition for the specified file name based on its extension
    * @param filename String filename representation
    */
    static Class<AssetFile> assetForFileName(filename) {
        return assetFileClasses().find{ fileClass ->
            fileClass.extensions.find { filename.endsWith(".${it}") }
        }
    }

    /**
     * Obtains an {@link AssetFile} instance for the given URI
     *
     * @param uri The given URI
     * @return The AssetFile instance or null if non exists
     */
    static AssetFile fileForFullName(String uri) {
        for(resolver in AssetPipelineConfigHolder.resolvers) {
            def file = resolver.getAsset(uri)
            if(file) {
                return file
            }
        }
        return null
    }

    /**
     * Obtains the extension for the given URI
     *
     * @param uri The URI
     * @return The extension or null
     */
    static String extensionFromURI(String uri) {
        def uriComponents = uri.split("/")
        def lastUriComponent = uriComponents[uriComponents.length - 1]
        String extension = null
        if(lastUriComponent.indexOf(".") >= 0) {
            extension = uri.substring(uri.indexOf(".") + 1)
        }
        return extension
    }

    /**
     * Obtains the name of the file sans the extension
     *
     * @param uri The URI
     * @return The name of the file without extension
     */
    static String nameWithoutExtension(String uri) {
        def uriComponents = uri.split("/")
        def lastUriComponent = uriComponents[uriComponents.length - 1]
        if(lastUriComponent.indexOf(".") >= 0) {
            return uri.substring(0,uri.indexOf("."))
        }
        return uri
    }

    static String fileNameWithoutExtensionFromArtefact(String filename, AssetFile assetFile) {<commit_msg>reverting change on lastIndexOf extensions<commit_after>/*
 * Copyright 2014 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 asset.pipeline


import java.util.regex.Pattern
import java.security.MessageDigest
import java.nio.channels.FileChannel

/**
 * Helper class for resolving assets
 *
 * @author <NAME>
 * @author <NAME>
 */
class AssetHelper {
    static final Collection<Class<AssetFile>> assetSpecs = AssetSpecLoader.loadSpecifications()
    static final String QUOTED_FILE_SEPARATOR = Pattern.quote(File.separator)
    static final String DIRECTIVE_FILE_SEPARATOR = '/'

    /**
     * Resolve an {@link AssetFile} for the given URI
     *
     * @param uri The URI
     * @param contentType The content type
     * @param ext The extension
     * @param baseFile The base file
     * @return
     */
    static AssetFile fileForUri(String uri, String contentType=null, String ext=null, AssetFile baseFile=null) {
        AssetFile file
        for(resolver in AssetPipelineConfigHolder.resolvers) {
            file = resolver.getAsset(uri,contentType,ext,baseFile)
            if(file) {
                return file
            }
        }
        return null
    }

    /**
     * @return The classes that implement the {@link AssetFile} interface
     */
    static Collection<Class<AssetFile>> assetFileClasses() {
        return assetSpecs
    }

    static AssetFile assetForFile(file, String contentType, AssetFile baseFile=null) {
        if(contentType == null || file == null) {
            return file
        }

        def possibleFileSpecs = getPossibleFileSpecs(contentType)
        for(fileSpec in possibleFileSpecs) {
            for(extension in fileSpec.extensions) {
                def fileName = file.getAbsolutePath()
                if(fileName.endsWith("." + extension)) {
                    return fileSpec.newInstance(file: file, baseFile:baseFile)
                }
            }
        }

        return file
    }

    /**
    * Finds the AssetFile definition for the specified file name based on its extension
    * @param filename String filename representation
    */
    static Class<AssetFile> assetForFileName(filename) {
        return assetFileClasses().find{ fileClass ->
            fileClass.extensions.find { filename.endsWith(".${it}") }
        }
    }

    /**
     * Obtains an {@link AssetFile} instance for the given URI
     *
     * @param uri The given URI
     * @return The AssetFile instance or null if non exists
     */
    static AssetFile fileForFullName(String uri) {
        for(resolver in AssetPipelineConfigHolder.resolvers) {
            def file = resolver.getAsset(uri)
            if(file) {
                return file
            }
        }
        return null
    }

    /**
     * Obtains the extension for the given URI
     *
     * @param uri The URI
     * @return The extension or null
     */
    static String extensionFromURI(String uri) {
        def uriComponents = uri.split("/")
        def lastUriComponent = uriComponents[uriComponents.length - 1]
        String extension = null
        if(lastUriComponent.lastIndexOf(".") >= 0) {
            extension = uri.substring(uri.lastIndexOf(".") + 1)
        }
        return extension
    }

    /**
     * Obtains the name of the file sans the extension
     *
     * @param uri The URI
     * @return The name of the file without extension
     */
    static String nameWithoutExtension(String uri) {
        def uriComponents = uri.split("/")
        def lastUriComponent = uriComponents[uriComponents.length - 1]
        if(lastUriComponent.lastIndexOf(".") >= 0) {
            return uri.substring(0,uri.lastIndexOf("."))
        }
        return uri
    }

    static String fileNameWithoutExtensionFromArtefact(String filename, AssetFile assetFile) {
{'fraction_non_alphanumeric': 0.0853813314872505, 'fraction_numerical': 0.0032306449751932616, 'mean_word_length': 2.379337231968811, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1464357', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2241, 'n_words': 724}