C# nvelocity中是否有URLResourceLoader类的模拟

C# nvelocity中是否有URLResourceLoader类的模拟,c#,.net,nvelocity,C#,.net,Nvelocity,我想通过使用url路径向位于外部服务器上的VelocityEnEngine提供外部资源,在Apache velocity中有URLResourceLoader类,但我看到在NVelocity中只有FileResourceLoader 是否有办法在NVelocity中提供外部(url)资源ExtendedProperty 提前感谢。我自己实现了界面: public class UrlResourceLoader : ResourceLoader { protected ArrayList

我想通过使用url路径向位于外部服务器上的VelocityEnEngine提供外部资源,在Apache velocity中有URLResourceLoader类,但我看到在NVelocity中只有FileResourceLoader

是否有办法在NVelocity中提供外部(url)资源ExtendedProperty


提前感谢。

我自己实现了界面:

public class UrlResourceLoader : ResourceLoader
{
    protected ArrayList paths;
    protected Hashtable templatePaths;

    public UrlResourceLoader()
    {
        templatePaths = new Hashtable();
    }


    public override void Init(ExtendedProperties configuration)
    {
        paths = configuration.GetVector("path");
    }

    public override Stream GetResourceStream(string templateName)
    {
        lock (this)
        {
            int size = paths.Count;
            if (string.IsNullOrEmpty(templateName))
            {
                throw;
            }

            for (int i = 0; i < size; i++)
            {
                var path = (string) paths[i];
                var uri = new Uri(path + templateName);
                Stream inputStream = FindTemplate(uri);
                if (inputStream != null)
                {
                    SupportClass.PutElement(templatePaths, templateName, path);
                    return inputStream;
                }
            }
            throw;
        }
    }

    private Stream FindTemplate(Uri requestUri)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";

            var response = (HttpWebResponse) request.GetResponse();
            if (HttpStatusCode.OK != response.StatusCode)
            {
                throw;
            }

            return response.GetResponseStream();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public override bool IsSourceModified(Resource resource)
    {
        var path = (string)templatePaths[resource.Name];
        var uri = new Uri(path + resource.Name);

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            return response.LastModified.Ticks != resource.LastModified;
        }
    }

    public override long GetLastModified(Resource resource)
    {
        var path = (string)templatePaths[resource.Name];
        var uri = new Uri(path + resource.Name);

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            return response.LastModified.Ticks;
        }
    }
}
公共类UrlResourceLoader:ResourceLoader
{
受保护的数组列表路径;
受保护的哈希表模板路径;
公共UrlResourceLoader()
{
TemplatePath=新哈希表();
}
公共重写void Init(ExtendedProperties配置)
{
路径=配置.GetVector(“路径”);
}
公共重写流GetResourceStream(字符串templateName)
{
锁(这个)
{
int size=path.Count;
if(string.IsNullOrEmpty(templateName))
{
投掷;
}
对于(int i=0;i