/**
 * Copyright (C) 2013-2016 The Rythm Engine project
 * for LICENSE and other details see:
 * https://github.com/rythmengine/rythmengine
 */
package org.rythmengine.resource;

/*-
 * #%L
 * Rythm Template Engine
 * %%
 * Copyright (C) 2017 - 2021 OSGL (Open Source General Library)
 * %%
 * 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.
 * #L%
 */

import org.rythmengine.RythmEngine;

/**
 * A simple classpath loader
 */
public class ClasspathResourceLoader extends ResourceLoaderBase {

    private String root;

    public ClasspathResourceLoader(RythmEngine engine, String root) {
        setEngine(engine);
        while (root.startsWith("/") || root.startsWith("\\")) root = root.substring(1);
        if (!root.endsWith("/")) root = root + "/";
        this.root = root;
    }

    @Override
    public String getResourceLoaderRoot() {
        return root;
    }

    @Override
    public ITemplateResource load(String path) {
        while (path.startsWith("/") || path.startsWith("\\")) path = path.substring(1);
        if (!path.startsWith(root)) {
            path = root + path;
        }
        ClasspathTemplateResource ctr = new ClasspathTemplateResource(path, this);
        return ctr;
    }
}
