-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first version (quick and dirty implementation) of pippo-velocity (#144)
- Loading branch information
Showing
11 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
pippo-demo/pippo-demo-template/src/main/java/ro/pippo/demo/template/VelocityDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package ro.pippo.demo.template; | ||
|
||
import ro.pippo.core.Pippo; | ||
import ro.pippo.velocity.VelocityTemplateEngine; | ||
|
||
/** | ||
* @author Decebal Suiu | ||
*/ | ||
public class VelocityDemo { | ||
|
||
public static void main(String[] args) { | ||
// .vm is the default file extension | ||
Pippo pippo = new Pippo(new TemplateApplication(new VelocityTemplateEngine(), "velocity/hello")); | ||
pippo.start(); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
pippo-demo/pippo-demo-template/src/main/resources/templates/velocity/hello.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Velocity Demo</title> | ||
</head> | ||
|
||
<body> | ||
<span>$welcome</span> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/maven-v4_0_0.xsd"> | ||
|
||
<parent> | ||
<groupId>ro.pippo</groupId> | ||
<artifactId>pippo-parent</artifactId> | ||
<version>0.6.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>jar</packaging> | ||
<artifactId>pippo-velocity</artifactId> | ||
<version>0.6.0-SNAPSHOT</version> | ||
<name>Pippo Velocity</name> | ||
<description>Velocity template engine</description> | ||
|
||
<properties> | ||
<velocity.version>1.7</velocity.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ro.pippo</groupId> | ||
<artifactId>pippo-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- Velocity --> | ||
<dependency> | ||
<groupId>org.apache.velocity</groupId> | ||
<artifactId>velocity</artifactId> | ||
<version>${velocity.version}</version> | ||
</dependency> | ||
|
||
<!-- PrettyTime --> | ||
<dependency> | ||
<groupId>org.ocpsoft.prettytime</groupId> | ||
<artifactId>prettytime</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
43 changes: 43 additions & 0 deletions
43
pippo-velocity/src/main/java/ro/pippo/velocity/PrefixedClasspathResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package ro.pippo.velocity; | ||
|
||
import org.apache.commons.collections.ExtendedProperties; | ||
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; | ||
|
||
import java.io.InputStream; | ||
|
||
/** | ||
* @see http://stackoverflow.com/a/9693749/390044 | ||
* | ||
* @author Decebal Suiu | ||
*/ | ||
public class PrefixedClasspathResourceLoader extends ClasspathResourceLoader { | ||
|
||
/** Prefix to be added to any names */ | ||
private String prefix = ""; | ||
|
||
@Override | ||
public void init(ExtendedProperties configuration) { | ||
prefix = configuration.getString("prefix", ""); | ||
} | ||
|
||
@Override | ||
public InputStream getResourceStream(String name) { | ||
return super.getResourceStream(prefix + name); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
pippo-velocity/src/main/java/ro/pippo/velocity/VelocityInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package ro.pippo.velocity; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import ro.pippo.core.Application; | ||
import ro.pippo.core.Initializer; | ||
|
||
/** | ||
* @author Decebal Suiu | ||
*/ | ||
public class VelocityInitializer implements Initializer { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(VelocityInitializer.class); | ||
|
||
@Override | ||
public void init(Application application) { | ||
application.registerTemplateEngine(VelocityTemplateEngine.class); | ||
} | ||
|
||
@Override | ||
public void destroy(Application application) { | ||
} | ||
|
||
} |
162 changes: 162 additions & 0 deletions
162
pippo-velocity/src/main/java/ro/pippo/velocity/VelocityTemplateEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright (C) 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package ro.pippo.velocity; | ||
|
||
import org.apache.velocity.Template; | ||
import org.apache.velocity.VelocityContext; | ||
import org.apache.velocity.app.VelocityEngine; | ||
import org.apache.velocity.runtime.RuntimeConstants; | ||
import org.apache.velocity.runtime.RuntimeServices; | ||
import org.apache.velocity.runtime.RuntimeSingleton; | ||
import org.apache.velocity.runtime.parser.node.SimpleNode; | ||
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; | ||
import ro.pippo.core.Application; | ||
import ro.pippo.core.Languages; | ||
import ro.pippo.core.Messages; | ||
import ro.pippo.core.PippoConstants; | ||
import ro.pippo.core.PippoRuntimeException; | ||
import ro.pippo.core.PippoSettings; | ||
import ro.pippo.core.TemplateEngine; | ||
import ro.pippo.core.util.StringUtils; | ||
|
||
import java.io.StringReader; | ||
import java.io.Writer; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @author Decebal Suiu | ||
*/ | ||
public class VelocityTemplateEngine implements TemplateEngine { | ||
|
||
public static final String VM = "vm"; | ||
public static final String FILE_SUFFIX = "." + VM; | ||
|
||
private Languages languages; | ||
private Messages messages; | ||
private VelocityEngine velocityEngine; | ||
|
||
@Override | ||
public void init(Application application) { | ||
this.languages = application.getLanguages(); | ||
this.messages = application.getMessages(); | ||
|
||
PippoSettings pippoSettings = application.getPippoSettings(); | ||
|
||
String pathPrefix = pippoSettings.getString(PippoConstants.SETTING_TEMPLATE_PATH_PREFIX, null); | ||
if (StringUtils.isNullOrEmpty(pathPrefix)) { | ||
pathPrefix = TemplateEngine.DEFAULT_PATH_PREFIX; | ||
} | ||
|
||
// create properties (see http://velocity.apache.org/engine/releases/velocity-1.7/developer-guide.html#Configuring_Resource_Loaders) | ||
// maybe we can found in classpath a file velocity.properties and load all properties from this file | ||
Properties properties = new Properties(); | ||
// properties.setProperty("pathPrefix", pathPrefix); | ||
// properties.setProperty("resource.loader", "classpath"); | ||
// properties.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); | ||
// properties.setProperty("classpath.resource.loader.prefixPath", pathPrefix); | ||
// properties.setProperty("classpath.resource.loader.cache", String.valueOf(!pippoSettings.isDev())); | ||
|
||
properties.setProperty("resource.loader", "myloader"); | ||
properties.setProperty("myloader.resource.loader.class", PrefixedClasspathResourceLoader.class.getName()); | ||
properties.setProperty("myloader.resource.loader.prefix", StringUtils.addEnd(pathPrefix, "/")); | ||
properties.setProperty("myloader.resource.loader.cache", String.valueOf(!pippoSettings.isDev())); | ||
|
||
// properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, pathPrefix); | ||
// velocity.properties | ||
// resource.loader = file | ||
// file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader | ||
// file.resource.loader.path = c:/tomcat/webapps/velocity/WEB-INF/templates | ||
// file.resource.loader.cache = true | ||
// file.resource.loader.modificationCheckInterval = 2 | ||
// properties.setProperty("input.encoding","UTF-8"); | ||
// properties.setProperty("output.encoding","UTF-8"); | ||
|
||
velocityEngine = new VelocityEngine(properties); | ||
} | ||
|
||
public VelocityEngine getVelocityEngine() { | ||
return velocityEngine; | ||
} | ||
|
||
@Override | ||
public void renderString(String templateContent, Map<String, Object> model, Writer writer) { | ||
// prepare the locale-aware i18n method | ||
String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); | ||
if (StringUtils.isNullOrEmpty(language)) { | ||
language = languages.getLanguageOrDefault(language); | ||
} | ||
// model.put("i18n", new I18nMethod(messages, language)); | ||
|
||
// prepare the locale-aware prettyTime method | ||
Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); | ||
if (locale == null) { | ||
locale = languages.getLocaleOrDefault(language); | ||
} | ||
// model.put("prettyTime", new PrettyTimeMethod(locale)); | ||
// model.put("formatTime", new FormatTimeMethod(locale)); | ||
// model.put("webjarsAt", webjarResourcesMethod); | ||
// model.put("publicAt", publicResourcesMethod); | ||
|
||
try { | ||
RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); | ||
StringReader reader = new StringReader(templateContent); | ||
SimpleNode node = runtimeServices.parse(reader, "StringTemplate"); | ||
Template template = new Template(); | ||
template.setRuntimeServices(runtimeServices); | ||
template.setData(node); | ||
template.initDocument(); | ||
VelocityContext context = new VelocityContext(model); | ||
template.merge(context, writer); | ||
} catch (Exception e) { | ||
throw new PippoRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void renderResource(String templateName, Map<String, Object> model, Writer writer) { | ||
// prepare the locale-aware i18n method | ||
String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); | ||
if (StringUtils.isNullOrEmpty(language)) { | ||
language = languages.getLanguageOrDefault(language); | ||
} | ||
// model.put("i18n", new I18nMethod(messages, language)); | ||
|
||
// prepare the locale-aware prettyTime method | ||
Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); | ||
if (locale == null) { | ||
locale = languages.getLocaleOrDefault(language); | ||
} | ||
// model.put("prettyTime", new PrettyTimeMethod(locale)); | ||
// model.put("formatTime", new FormatTimeMethod(locale)); | ||
// model.put("webjarsAt", webjarResourcesMethod); | ||
// model.put("publicAt", publicResourcesMethod); | ||
|
||
try { | ||
if (templateName.indexOf('.') == -1) { | ||
templateName += FILE_SUFFIX; | ||
} | ||
// TODO locale ?! | ||
Template template = velocityEngine.getTemplate(templateName); | ||
VelocityContext context = new VelocityContext(model); | ||
template.merge(context, writer); | ||
} catch (Exception e) { | ||
throw new PippoRuntimeException(e); | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
pippo-velocity/src/main/resources/META-INF/services/ro.pippo.core.TemplateEngine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ro.pippo.velocity.VelocityTemplateEngine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
initializer=ro.pippo.velocity.VelocityInitializer |
Oops, something went wrong.