You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2019/06/10 23:25:39 UTC
svn commit: r1860993 - in /velocity/engine/branches/parser_experiments: ./
velocity-custom-parser-example/ velocity-custom-parser-example/src/
velocity-custom-parser-example/src/test/
velocity-custom-parser-example/src/test/java/ velocity-custom-parser...
Author: cbrisson
Date: Mon Jun 10 23:25:39 2019
New Revision: 1860993
URL: http://svn.apache.org/viewvc?rev=1860993&view=rev
Log:
[engine][VELOCITY-917] New velocity-custom-parser-example module is ready
Added:
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/pom.xml
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/test.md
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/
velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/test.md
Modified:
velocity/engine/branches/parser_experiments/pom.xml
velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml
Modified: velocity/engine/branches/parser_experiments/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/pom.xml?rev=1860993&r1=1860992&r2=1860993&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/pom.xml (original)
+++ velocity/engine/branches/parser_experiments/pom.xml Mon Jun 10 23:25:39 2019
@@ -187,6 +187,7 @@
<module>velocity-engine-core</module>
<module>velocity-engine-examples</module>
<module>velocity-engine-scripting</module>
+ <module>velocity-custom-parser-example</module>
</modules>
<!-- This project is an effort by many people. If you feel that your name
Added: velocity/engine/branches/parser_experiments/velocity-custom-parser-example/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-custom-parser-example/pom.xml?rev=1860993&view=auto
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-custom-parser-example/pom.xml (added)
+++ velocity/engine/branches/parser_experiments/velocity-custom-parser-example/pom.xml Mon Jun 10 23:25:39 2019
@@ -0,0 +1,267 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.velocity</groupId>
+ <artifactId>velocity-engine-parent</artifactId>
+ <version>2.2-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>velocity-custom-parser-example</artifactId>
+ <name>Apache Velocity Custom Parser - Example</name>
+ <description>Custom Parser Example for Apache Velocity</description>
+
+ <packaging>jar</packaging>
+
+ <!--
+ This plugin demonstrates how to build a custom Velocity parser.
+ The proposed custom parser replaces '#' with '@' and '@' with '%',
+ so that it's suitable to use with Markdown template files, for instance.
+
+ The generated parser class is org.apache.velocity.runtime.parser.${parser.name}Parser,
+ and must be specified at runtime using the Velocity property parser.class :
+ parser.class = org.apache.velocity.runtime.parser.MyCustomParser
+ -->
+
+ <properties>
+ <!-- whether to display debug logs while parsing -->
+ <parser.debug>false</parser.debug>
+ <!-- parser name -->
+ <parser.name>Custom</parser.name>
+ <!-- character to substitute to '*' -->
+ <parser.char.asterisk>*</parser.char.asterisk>
+ <!-- character to substitute to '@' -->
+ <parser.char.at>%</parser.char.at>
+ <!-- character to substitute to '$' -->
+ <parser.char.dollar>$</parser.char.dollar>
+ <!-- character to substitute to '#' -->
+ <parser.char.hash>@</parser.char.hash>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.velocity</groupId>
+ <artifactId>velocity-engine-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>${slf4j.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.6</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <!-- generate manifest file -->
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ </plugin>
+ <!-- extract raw parser grammar from velocity jar -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>3.1.1</version>
+ <executions>
+ <execution>
+ <id>fetch-grammar-file</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>unpack</goal>
+ </goals>
+ <configuration>
+ <artifact>org.apache.velocity:velocity-engine-core:${project.version}</artifact>
+ <includes>org/apache/velocity/runtime/parser/Parser.jjt</includes>
+ <outputDirectory>${project.build.directory}/grammar</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- generate custom grammar file -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>3.1.0</version>
+ <configuration>
+ <useDefaultDelimiters>false</useDefaultDelimiters>
+ <delimiters>
+ <delimiter>${*}</delimiter>
+ </delimiters>
+ </configuration>
+ <executions>
+ <execution>
+ <id>generate-parser-grammar</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>copy-resources</goal>
+ </goals>
+ <configuration>
+ <resources>
+ <resource>
+ <directory>${project.build.directory}/grammar</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ <outputDirectory>${project.build.directory}/parser</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- run javacc -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>javacc-maven-plugin</artifactId>
+ <version>2.6</version>
+ <configuration>
+ <isStatic>false</isStatic>
+ <buildParser>true</buildParser>
+ <buildNodeFiles>false</buildNodeFiles>
+ <multi>true</multi>
+ <debugParser>${parser.debug}</debugParser>
+ <debugLookAhead>${parser.debug}</debugLookAhead>
+ <debugTokenManager>${parser.debug}</debugTokenManager>
+ <jdkVersion>${maven.compiler.target}</jdkVersion>
+ <nodeUsesParser>true</nodeUsesParser>
+ <nodePackage>org.apache.velocity.runtime.parser.node</nodePackage>
+ <sourceDirectory>${project.build.directory}/parser/org/apache/velocity/runtime/parser</sourceDirectory>
+ <tokenManagerUsesParser>true</tokenManagerUsesParser>
+ </configuration>
+ <executions>
+ <execution>
+ <id>jjtree-javacc</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>jjtree-javacc</goal>
+ </goals>
+ <configuration>
+ <includes>
+ <include>Parser.jjt</include>
+ </includes>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- Remove extra generated files we don't want -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-clean-plugin</artifactId>
+ <version>3.1.0</version>
+ <executions>
+ <execution>
+ <id>clean-extra-javacc</id>
+ <phase>process-sources</phase>
+ <goals>
+ <goal>clean</goal>
+ </goals>
+ <configuration>
+ <excludeDefaultDirectories>true</excludeDefaultDirectories>
+ <filesets>
+ <fileset>
+ <directory>${project.build.directory}/generated-sources/javacc/org/apache/velocity/runtime/parser</directory>
+ <includes>
+ <include>*.java</include>
+ </includes>
+ <excludes>
+ <exclude>*${parser.name}*.java</exclude>
+ </excludes>
+ </fileset>
+ <fileset>
+ <directory>${project.build.directory}/generated-sources/jjtree/org/apache/velocity/runtime/parser/node</directory>
+ <includes>
+ <include>*.java</include>
+ </includes>
+ <excludes>
+ <exclude>*${parser.name}*.java</exclude>
+ </excludes>
+ </fileset>
+ </filesets>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- tests -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.plugin.version}</version>
+ <configuration>
+ <skip>${maven.test.skip}</skip>
+ <systemProperties>
+ <property>
+ <name>test</name>
+ <value>${test}</value>
+ </property>
+ <property>
+ <name>test.templates.dir</name>
+ <value>${project.build.testOutputDirectory}/templates</value>
+ </property>
+ <property>
+ <name>test.results.dir</name>
+ <value>${project.build.directory}/results</value>
+ </property>
+ <property>
+ <name>test.reference.dir</name>
+ <value>${project.build.testOutputDirectory}/reference</value>
+ </property>
+ <property>
+ <name>org.slf4j.simpleLogger.defaultLogLevel</name>
+ <value>warn</value>
+ </property>
+ <property>
+ <name>org.slf4j.simpleLogger.logFile</name>
+ <value>${project.build.directory}/velocity.log</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ <!--
+ <executions>
+ <execution>
+ <id>integration-test</id>
+ <phase>integration-test</phase>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </execution>
+ </executions>
+ -->
+ </plugin>
+ </plugins>
+ </build>
+</project>
Added: velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java?rev=1860993&view=auto
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java (added)
+++ velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java Mon Jun 10 23:25:39 2019
@@ -0,0 +1,55 @@
+package org.apache.velocity.runtime.parser;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+
+import static org.junit.Assert.*;
+
+public class CustomParserTestCase
+{
+ VelocityEngine engine;
+
+ static String TEMPLATES_DIR = System.getProperty("test.templates.dir");
+ static String RESULTS_DIR = System.getProperty("test.results.dir");
+ static String REFERENCE_DIR = System.getProperty("test.reference.dir");
+
+ @Before
+ public void setUp()
+ {
+ engine = new VelocityEngine();
+ engine.setProperty("resource.loaders", "file");
+ engine.setProperty("resource.loader.file.path", TEMPLATES_DIR);
+ engine.setProperty("parser.class", "org.apache.velocity.runtime.parser.CustomParser");
+ engine.init();
+ }
+
+ @Test
+ public void testMarkdownTemplate() throws Exception
+ {
+ VelocityContext ctx = new VelocityContext();
+ ctx.put("some", "value");
+ Template tmpl = engine.getTemplate("test.md", "UTF-8");
+
+ String resultFile = RESULTS_DIR + File.separator + "test.md";
+ String referenceFile = REFERENCE_DIR + File.separator + "test.md";
+
+ new File(resultFile).getParentFile().mkdirs();
+
+ FileWriter writer = new FileWriter(resultFile);
+ tmpl.merge(ctx, writer);
+ writer.flush();
+ writer.close();
+
+ String result = IOUtils.toString(new FileInputStream(resultFile), "UTF-8");
+ String reference = IOUtils.toString(new FileInputStream(referenceFile), "UTF-8");
+ assertEquals(reference, result);
+ }
+}
Added: velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/test.md
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/test.md?rev=1860993&view=auto
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/test.md (added)
+++ velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/reference/test.md Mon Jun 10 23:25:39 2019
@@ -0,0 +1,13 @@
+# Test markdown template for the custom parser
+
+
+
+
+## Custom parser is needed
+
+some value
+ all seems fine
+
+
+ block macro called with foo=value and bodyContent=here
+
Added: velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/test.md
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/test.md?rev=1860993&view=auto
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/test.md (added)
+++ velocity/engine/branches/parser_experiments/velocity-custom-parser-example/src/test/resources/templates/test.md Mon Jun 10 23:25:39 2019
@@ -0,0 +1,23 @@
+# Test markdown template for the custom parser
+
+@* this is a comment *@
+
+@set ($subtitle = 'Custom parser is needed')
+
+## $subtitle
+
+some $some @@ should print 'some value'
+
+@if ($some == 'value')
+ all seems fine
+@else
+ there is a problem
+@end
+
+@macro(block $foo)
+ block macro called with foo=$foo and bodyContent=$bodyContent
+@end
+
+@%block($some)
+here
+@end
Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml?rev=1860993&r1=1860992&r2=1860993&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml Mon Jun 10 23:25:39 2019
@@ -31,6 +31,17 @@
<name>Apache Velocity - Engine</name>
<properties>
+ <!-- You can modify those properties to build a custom parser.
+ The parser.name property will be used to generate the class:
+ org.apache.velocity.runtime.parser.${parser.name}Parser
+ which you must specify at runtime using the parser.class Velocity property.
+ You'll have to skip the tests which for now don't work for custom parsers:
+ mvn -DskipTests package
+ and extract the parser class from the jar if you wish this custom parser to
+ cohexist with the standard one.
+ You're strongly advised to change the groupId and/or artifactId if you plan
+ to use the generated jar directly.
+ -->
<parser.debug>false</parser.debug>
<parser.name>Standard</parser.name>
<parser.char.asterisk>*</parser.char.asterisk>