<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF 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 httl.spi.loaders;

import httl.Engine;
import httl.Resource;
import httl.spi.Loader;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


/**
 * AbstractLoader. (SPI, Singleton, ThreadSafe)
 * 
 * @see httl.Engine#setLoader(Loader)
 * 
 * @author <NAME> (liangfei0201 AT gmail DOT com)
 */
public abstract class AbstractLoader implements Loader {
	
	private Engine engine;

	private String encoding;
    
    private String directory;
    
    private String[] suffixes;

	public Engine getEngine() {
		return engine;
	}

    public void setEngine(Engine engine) {
		this.engine = engine;
	}

    protected String getEncoding() {
        return encoding;
    }
    
    protected String getDirectory() {
        return directory;
    }
    
    public String[] getSuffixes() {
        return suffixes;
    }
    
    public void setInputEncoding(String encoding) {
    	if (encoding != null && encoding.trim().length() > 0) {
            encoding = encoding.trim();
            Charset.forName(encoding);
            this.encoding = encoding;
        }
    }

    public void setTemplateDirectory(String directory) {
    	if (directory != null && directory.trim().length() > 0) {
            if (directory.endsWith("/") || directory.endsWith("\\")) {
                directory = directory.substring(0, directory.length() - 1);
            }
            this.directory = directory.trim();
        }
    }

    public void setTemplateSuffix(String[] suffix) {
    	this.suffixes = suffix;
    }

    public List<String> list() throws IOException {
        String directory = getDirectory();
        if (directory == null || directory.length() == 0) {
            return new ArrayList<String>(0);
        }
        String[] suffixes = getSuffixes();
        if (suffixes == null || suffixes.length == 0) {
            return new ArrayList<String>(0);
        }
        List<String> list = doList(directory, suffixes);
        if (list == null) {
            list = new ArrayList<String>(0);
        }
        return list;
    }
    
    protected abstract List<String> doList(String directory, String[] suffixes) throws IOException;
    
    public Resource load(String name, String encoding) throws IOException {
        if (encoding == null || encoding.length() == 0) {
            encoding = this.encoding;
        }
        String dir = directory;
        return doLoad(name, encoding, dir == null ? name : dir + name);
    }
    
    protected abstract Resource doLoad(String name, String encoding, String path) throws IOException;
    
}<commit_msg>AbstractLoad path<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF 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 httl.spi.loaders;

import httl.Engine;
import httl.Resource;
import httl.spi.Loader;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


/**
 * AbstractLoader. (SPI, Singleton, ThreadSafe)
 * 
 * @see httl.Engine#setLoader(Loader)
 * 
 * @author <NAME> (<EMAIL> AT gmail DOT com)
 */
public abstract class AbstractLoader implements Loader {
	
	private Engine engine;

	private String encoding;
    
    private String directory;
    
    private String[] suffixes;

	public Engine getEngine() {
		return engine;
	}

    public void setEngine(Engine engine) {
		this.engine = engine;
	}

    protected String getEncoding() {
        return encoding;
    }
    
    protected String getDirectory() {
        return directory;
    }
    
    public String[] getSuffixes() {
        return suffixes;
    }
    
    public void setInputEncoding(String encoding) {
    	if (encoding != null && encoding.trim().length() > 0) {
            encoding = encoding.trim();
            Charset.forName(encoding);
            this.encoding = encoding;
        }
    }

    public void setTemplateDirectory(String directory) {
    	if (directory != null && directory.trim().length() > 0) {
            if (directory.endsWith("/") || directory.endsWith("\\")) {
                directory = directory.substring(0, directory.length() - 1);
            }
            this.directory = directory.trim();
        }
    }

    public void setTemplateSuffix(String[] suffix) {
    	this.suffixes = suffix;
    }

    public List<String> list() throws IOException {
        String directory = getDirectory();
        if (directory == null || directory.length() == 0) {
            return new ArrayList<String>(0);
        }
        String[] suffixes = getSuffixes();
        if (suffixes == null || suffixes.length == 0) {
            return new ArrayList<String>(0);
        }
        List<String> list = doList(directory, suffixes);
        if (list == null) {
            list = new ArrayList<String>(0);
        }
        return list;
    }
    
    protected abstract List<String> doList(String directory, String[] suffixes) throws IOException;
    
    public Resource load(String name, String encoding) throws IOException {
        if (encoding == null || encoding.length() == 0) {
            encoding = this.encoding;
        }
        String dir = directory;
        if (! name.startsWith("/") && ! name.startsWith("\\")) {
        	name = "/" + name;
        }
        return doLoad(name, encoding, dir == null ? name : dir + name);
    }
    
    protected abstract Resource doLoad(String name, String encoding, String path) throws IOException;
    
}
{'fraction_non_alphanumeric': 0.10237007424328955, 'fraction_numerical': 0.004568817818389492, 'mean_word_length': 2.588627049180328, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 22, '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': '6608072', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1841, 'n_words': 624}