<commit_before>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.uis.internal.io.reference;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import org.wso2.carbon.uis.internal.exception.FileOperationException;
import org.wso2.carbon.uis.internal.io.util.PathUtils;
import org.wso2.carbon.uis.internal.reference.I18nResourceReference;

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;

/**
 * A reference to an i18n resource inside a web app artifact.
 *
 * @since 0.8.0
 */
public class ArtifactI18nResourceReference implements I18nResourceReference {

    public static final String I18N_RESOURCE_FILE_EXTENSION = "json";
    private static final Gson GSON = new Gson();
    private static final Type GSON_TYPE = new TypeToken<Map<String, String>>() { }.getType();

    private final Path messagesFile;

    /**
     * Creates a reference to the i18n resource specified by the path.
     *
     * @param messagesFile path to the i18n resource
     */
    public ArtifactI18nResourceReference(Path messagesFile) {
        this.messagesFile = messagesFile;
    }

    @Override
    public Locale getLocale() throws FileOperationException {
        return Locale.forLanguageTag(PathUtils.getExtension(messagesFile));
    }

    @Override
    public Map<String, String> getMessages() throws FileOperationException {
        try {
            BufferedReader bufferedReader = Files.newBufferedReader(messagesFile, StandardCharsets.UTF_8);
            return GSON.fromJson(bufferedReader, GSON_TYPE);
        } catch (IOException e) {
            throw new FileOperationException("Cannot read content of I18n message file '" + messagesFile + "'.");
        } catch (JsonParseException e) {
            throw new FileOperationException("I18n message file '" + messagesFile + "' is not a valid JSON.");
        }
    }
}<commit_msg>fix typo in exception message in ArtifactI18nResourceReference class<commit_after>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.uis.internal.io.reference;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import org.wso2.carbon.uis.internal.exception.FileOperationException;
import org.wso2.carbon.uis.internal.io.util.PathUtils;
import org.wso2.carbon.uis.internal.reference.I18nResourceReference;

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;

/**
 * A reference to an i18n resource inside a web app artifact.
 *
 * @since 0.8.0
 */
public class ArtifactI18nResourceReference implements I18nResourceReference {

    public static final String I18N_RESOURCE_FILE_EXTENSION = "json";
    private static final Gson GSON = new Gson();
    private static final Type GSON_TYPE = new TypeToken<Map<String, String>>() { }.getType();

    private final Path messagesFile;

    /**
     * Creates a reference to the i18n resource specified by the path.
     *
     * @param messagesFile path to the i18n resource
     */
    public ArtifactI18nResourceReference(Path messagesFile) {
        this.messagesFile = messagesFile;
    }

    @Override
    public Locale getLocale() throws FileOperationException {
        return Locale.forLanguageTag(PathUtils.getExtension(messagesFile));
    }

    @Override
    public Map<String, String> getMessages() throws FileOperationException {
        try {
            BufferedReader bufferedReader = Files.newBufferedReader(messagesFile, StandardCharsets.UTF_8);
            return GSON.fromJson(bufferedReader, GSON_TYPE);
        } catch (IOException e) {
            throw new FileOperationException("Cannot read content of i18n message file '" + messagesFile + "'.");
        } catch (JsonParseException e) {
            throw new FileOperationException("I18n message file '" + messagesFile + "' is not a valid JSON.");
        }
    }
}
{'fraction_non_alphanumeric': 0.09049609304015992, 'fraction_numerical': 0.01453752498637107, 'mean_word_length': 3.6565143824027073, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1226097', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1527, 'n_words': 489}