All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.struts2.freemarker.BeangleClassTemplateLoader Maven / Gradle / Ivy

There is a newer version: 3.11.3
Show newest version
/*
 * Beangle, Agile Development Scaffold and Toolkits.
 *
 * Copyright © 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package org.beangle.struts2.freemarker;

import java.net.URL;

import org.beangle.commons.lang.ClassLoaders;
import org.beangle.commons.lang.Strings;

import freemarker.cache.URLTemplateLoader;

/**
 * 搜索带有固定前缀下的资源,排除/org/和/com/
 *
 * @author chaostone
 */
public class BeangleClassTemplateLoader extends URLTemplateLoader {

  /** Not starts with /,but end with / */
  String prefix = null;

  public BeangleClassTemplateLoader(String prefix) {
    super();
    setPrefix(prefix);
  }

  protected URL getURL(String name) {
    URL url = ClassLoaders.getResource(name, getClass());
    if (null != prefix && null == url) url = ClassLoaders.getResource(prefix + name, getClass());
    return url;
  }

  public String getPrefix() {
    return prefix;
  }

  public void setPrefix(String pre) {
    if (Strings.isBlank(pre)) this.prefix = null;
    else this.prefix = pre.trim();

    if (null != prefix) {
      if (prefix.equals("/")) {
        prefix = null;
      } else {
        if (!prefix.endsWith("/")) prefix += "/";
        if (prefix.startsWith("/")) prefix = prefix.substring(1);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy