文件不存在时使用 #parse 的速度

发布于 2024-07-27 15:10:20 字数 309 浏览 8 评论 0原文

我们使用 Velocity 为几个不同的环境模板化我们的配置文件,这个过程运行得非常好,但我有一个关于解析不存在的文件的快速问题。

我的问题是: 在解析文件之前如何检查文件是否存在?

因此,在示例中,文件 default.user.properties 可能合法地不存在,如果不存在,则不会解析文件的其余部分。

#parse("./default.user.properties")

我知道一种解决方案是确保文件始终存在,但如果我不需要这样做那就太好了。

提前致谢。

we use Velocity to template our configuration files for several different environments and this process works really well but I have a quick question relating parsing a file that doesn't exist.

My question is:
How do you check if a file exists before parsing it?

So in the example the file default.user.properties might legitimately not exist and if it doesn't the rest of the file doesn't get parsed.

#parse("./default.user.properties")

I know one solution is to make sure the file is always there but it would be nice if I didn't have to.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-08-03 15:10:20

只是用弹簧和速度做到了这一点:

我在获取速度来拾取事件处理程序时遇到问题,最后在 servlet xml 文件中指定它:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="WEB-INF/templates"/>
        <property name="velocityPropertiesMap">
            <map>
                <entry key="eventhandler.include.class"><value>com.velocity.events.OptionalIncludeEventHandler</value></entry>
            </map>
        </property>
    </bean>

它根本不接受我将其放入属性文件中 - 它会实例化该类,但不将其注册为事件侦听器。 非常令人沮丧。

该类本身很简单,是对现有速度类“org.apache.velocity.app.event.implementIncludeNotFound”的明显抄袭。 现有的速度实现检查文件是否存在,如果不存在,则返回可配置的替代方案(默认值:notfound.vm)。

我的完全一样,只是如果文件不存在则返回 null,导致解析器跳过此包含/解析指令:

public class OptionalIncludeEventHandler implements IncludeEventHandler, RuntimeServicesAware {

    private RuntimeServices rs;

    @Override
    public void setRuntimeServices(RuntimeServices rs) {
        this.rs = rs;
    }

    @Override
    public String includeEvent(String includeResourcePath, String currentResourcePath, String directiveName) {
        return rs.getLoaderNameForResource(includeResourcePath) != null ? includeResourcePath : null;
    }

}

就像一个魅力一样。

希望它有用。

Just did this with spring and velocity:

I had problems getting velocity to pick up my event handler, in the end specifying it in the servlet xml file:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="WEB-INF/templates"/>
        <property name="velocityPropertiesMap">
            <map>
                <entry key="eventhandler.include.class"><value>com.velocity.events.OptionalIncludeEventHandler</value></entry>
            </map>
        </property>
    </bean>

It simply would not accept me putting it in the properties file - it would instantiate the class, but not register it as an event listener. very frustrating.

The class itself is simple, a quite blatant rip off from the existing velocity class "org.apache.velocity.app.event.implementIncludeNotFound". The existing velocity implementation checks for the existance of the file, and if not present returns a configurable alternative (default: notfound.vm).

Mine is exactly the same except it returns null if the file does not exist, causing the parser to skip this include / parse directive:

public class OptionalIncludeEventHandler implements IncludeEventHandler, RuntimeServicesAware {

    private RuntimeServices rs;

    @Override
    public void setRuntimeServices(RuntimeServices rs) {
        this.rs = rs;
    }

    @Override
    public String includeEvent(String includeResourcePath, String currentResourcePath, String directiveName) {
        return rs.getLoaderNameForResource(includeResourcePath) != null ? includeResourcePath : null;
    }

}

Works like a charm.

Hope it's useful.

潇烟暮雨 2024-08-03 15:10:20

一种新类型的事件处理程序“IncludeEventHandler”。 这允许您(开发人员)定义一个类(实现 IncludeEventHandler),每次评估 #parse 或 #include 时都会调用该类。 事件处理程序的目的是检查模板是否存在,如果不存在,则为调用代码设置错误标志。
尽管我自己没有测试过,但请查看文档以获取更多信息

A new type of event handler "IncludeEventHandler". This allows you, the developer, to define a class (implementing IncludeEventHandler) which will be called every time a #parse or #include is evaluated. The purpose of your event handler would be to check if the template exists and set an error flag for the calling code if not.
Check the documentation for more information though I haven't tested it myself

七色彩虹 2024-08-03 15:10:20

我使用的解决方案是创建一个实用方法来检查模板是否存在,即

public synchronized boolean templateExists(String templateFilename) {
    Boolean templateExists = this.templateExistsCache.get(templateFilename);
    if (templateExists != null) {
        return templateExists;
    }
    String absoluteFilename = this.request.getSession().getServletContext().getRealPath(
            "/WEB-INF/templates/" + templateFilename);
    File templateFile = new File(absoluteFilename);
    templateExists = templateFile.exists();
    this.templateExistsCache.put(templateFilename, templateExists);
    return templateExists;
}

private Map<String, Boolean> templateExistsCache = new HashMap<String, Boolean>();

来自

https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java

The solution I used is to create a utility method for checking template existence, i.e.

public synchronized boolean templateExists(String templateFilename) {
    Boolean templateExists = this.templateExistsCache.get(templateFilename);
    if (templateExists != null) {
        return templateExists;
    }
    String absoluteFilename = this.request.getSession().getServletContext().getRealPath(
            "/WEB-INF/templates/" + templateFilename);
    File templateFile = new File(absoluteFilename);
    templateExists = templateFile.exists();
    this.templateExistsCache.put(templateFilename, templateExists);
    return templateExists;
}

private Map<String, Boolean> templateExistsCache = new HashMap<String, Boolean>();

from

https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文