Java中的Velocity EscapeTool

wnavrhmk  于 2023-04-04  发布在  Java
关注(0)|答案(3)|浏览(71)

我在我的Annotation处理器上使用Velocity Escape工具时遇到了问题。首先,我让Annotation处理器单独使用Velocity时工作正常,所以我知道设置是正确的。当使用带有scape字符的字符串时,我开始对我创建的源代码产生问题。所以我在互联网上找到了关于Velocity的EscapeTool。让我们只说文档在配置方面不是很好。
我能找到一些代码,不记得在哪里,让我开始:

ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("velocity-tools.xml");
VelocityContext vc =
           new VelocityContext(velocityToolManager.createContext());

但我在配置部分得到以下错误:

java.lang.RuntimeException: There was an error while parsing the InputStream
    at org.apache.velocity.tools.config.XmlFactoryConfiguration.read(XmlFactoryConfiguration.java:132)
    at org.apache.velocity.tools.config.FileFactoryConfiguration.read(FileFactoryConfiguration.java:140)
    at org.apache.velocity.tools.config.FileFactoryConfiguration.read(FileFactoryConfiguration.java:116)
    at org.apache.velocity.tools.config.FileFactoryConfiguration.read(FileFactoryConfiguration.java:91)
    at org.apache.velocity.tools.config.FileFactoryConfiguration.read(FileFactoryConfiguration.java:69)
    at org.apache.velocity.tools.config.FileFactoryConfiguration.read(FileFactoryConfiguration.java:59)
    at org.apache.velocity.tools.config.ConfigurationUtils.getDefaultTools(ConfigurationUtils.java:79)
    at org.apache.velocity.tools.config.ConfigurationUtils.getAutoLoaded(ConfigurationUtils.java:176)
    at org.apache.velocity.tools.ToolManager.autoConfigure(ToolManager.java:74)
    at org.apache.velocity.tools.ToolManager.<init>(ToolManager.java:68)
    at org.apache.velocity.tools.ToolManager.<init>(ToolManager.java:54)
    at games.jwrestling.core.annotation.processor.CardAnnotationProcessor.process(CardAnnotationProcessor.java:116)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:793)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:722)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1700(JavacProcessingEnvironment.java:97)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1029)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1163)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1108)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
    at com.sun.tools.javac.main.Main.compile(Main.java:439)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.executeWithExceptionsHandled(AbstractAnnotationProcessorMojo.java:424)
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:250)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 58; Error at line 23 char 58: org.apache.velocity.tools.config.Data
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3181)
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3207)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1456)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1303)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2717)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.apache.commons.digester.Digester.parse(Digester.java:1765)
    at org.apache.velocity.tools.config.XmlFactoryConfiguration.read(XmlFactoryConfiguration.java:128)
    ... 43 more
Caused by: java.lang.ClassNotFoundException: org.apache.velocity.tools.config.Data
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
    at org.apache.commons.digester.ObjectCreateRule.begin(ObjectCreateRule.java:205)
    at org.apache.commons.digester.Rule.begin(Rule.java:175)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1453)
    ... 56 more

我认为这是一个依赖关系问题,但依赖关系如下:

<dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>

数据包含在velocity-tools jar中。
你可以找到代码here(在repo上注解)。
如果感兴趣,可以从https://javydreamercsw@bitbucket.org/javydreamercsw/jwrestling中检出源代码。
任何帮助都比欢迎!

**更新:**以下是velocity-tools.xml的内容,是velocity-tool jar中的副本:

<tools> 
    <data type="number" key="TOOLS_VERSION" value="2.0"/>
    <data type="boolean" key="GENERIC_TOOLS_AVAILABLE" value="true"/>
    <toolbox scope="application">
        <tool class="org.apache.velocity.tools.generic.AlternatorTool"/>
        <tool class="org.apache.velocity.tools.generic.DisplayTool"/>
        <tool class="org.apache.velocity.tools.generic.MathTool"/>
        <tool class="org.apache.velocity.tools.generic.NumberTool"/>
        <tool class="org.apache.velocity.tools.generic.ComparisonDateTool"/>
        <tool class="org.apache.velocity.tools.generic.ClassTool"/>
        <tool class="org.apache.velocity.tools.generic.ConversionTool"/>
        <tool class="org.apache.velocity.tools.generic.EscapeTool"/>
        <tool class="org.apache.velocity.tools.generic.FieldTool"/>
        <tool class="org.apache.velocity.tools.generic.ListTool"/>
        <tool class="org.apache.velocity.tools.generic.ResourceTool"/>
        <tool class="org.apache.velocity.tools.generic.SortTool"/>
    </toolbox>
    <toolbox scope="request">
        <tool class="org.apache.velocity.tools.generic.LoopTool"/>
        <tool class="org.apache.velocity.tools.generic.ContextTool"/>
        <tool class="org.apache.velocity.tools.generic.LinkTool"/>
        <tool class="org.apache.velocity.tools.generic.RenderTool"/>
    </toolbox>
</tools>
5ssjco0h

5ssjco0h1#

XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration("Default Tools");
        try {
            factoryConfiguration.read(new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("/").getFile(), TOOL_BOX_CONF_FILE)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ToolboxFactory factory = factoryConfiguration.createFactory();
        factory.configure(factoryConfiguration);
        toolContext = new ToolContext();
        for (String scope : Scope.values()) {
            toolContext.addToolbox(factory.createToolbox(scope));
        }
aemubtdh

aemubtdh2#

尝试

new ToolManager(false,false)
0md85ypi

0md85ypi3#

我在Sping Boot 中使用Velocity,并试图弄清楚上面的问题。我没有尝试上面的方法,因为我不需要每个工具,但只是想分享你可以从Velocity工具中添加一个特定的工具。
我的速度引擎配置

@Configuration
public class VelocityConfiguration {
    
    @Bean
    public VelocityEngine velocityEngine() throws IOException{
        VelocityEngineFactoryBean bean = new VelocityEngineFactoryBean();
        Properties velocityProperties = new Properties();
        velocityProperties.setProperty("runtime.log.invalid.references", "true");
        velocityProperties.setProperty("input.encoding", "UTF-8");
        velocityProperties.setProperty("runtime.log.invalid.references", "true");
        
        //for CLASS = template must be in /resources ONLY (under the <default package>
        velocityProperties.setProperty("resource.loader", "class");
        velocityProperties.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
        velocityProperties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        
        //enable inclusion of relative templates
        velocityProperties.setProperty("eventhandler.include.class", "org.apache.velocity.app.event.implement.IncludeRelativePath");
        
        bean.setVelocityProperties(velocityProperties);
                        
        return bean.createVelocityEngine();
    }

}

我只需要日期工具,所以我只添加了XML文件velocityContext.put(“date”,newComparisonDateTool())中的类;

@Component
public class VelocityTemplateWriter {

    private final VelocityEngine velocityEngine;

    @Value("${vm.template.path}")
    private String apacheVMTemplatePath;

    @Autowired
    public VelocityTemplateWriter(@Qualifier("velocityEngine") VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    public String write(Map<String, Object> data){
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("output", data);
        velocityContext.put("date", new ComparisonDateTool());

        StringWriter stringWriter = new StringWriter();
        velocityEngine.mergeTemplate(apacheVMTemplatePath + "ParentResponse.vm", "UTF-8", velocityContext, stringWriter);

        String output = stringWriter.toString();

        return output;
    }

相关问题