Commit 0a1d1014 by Jessica Hawkwell

added Velocity support

1 parent ba317d60
Pipeline #189 passed
in 3 minutes 5 seconds
package me.felinewith.lang_toolkit.apps.langbuilder.ui;
import java.awt.BorderLayout;
import java.util.HashMap;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import me.felinewith.lang_toolkit.library.IStrappedPlugin;
import me.felinewith.lang_toolkit.library.velocity.VelocityHelper;
/**
*
......@@ -23,6 +25,11 @@ public class WelcomeWindow extends IStrappedPlugin {
jInternalFrame.add(jScrollPane); // */
viewer.setContentType("text/html; charset=UTF-8");
viewer.setEditable(false);
VelocityHelper vh = VelocityHelper.getInstance();
HashMap<String, Object> hm = new HashMap<>();
hm.put("page.title", "Welcome!");
viewer.setText(vh.generate(null, hm));
}
@Override public String getName() { return "WelconeWindow"; }
......
......@@ -30,6 +30,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
</dependency>
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
</dependency>
......
package me.felinewith.lang_toolkit.library.velocity;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.resource.loader.ClasspathResourceLoader;
/**
*
* @author jlhawkwell
*/
public class VelocityHelper {
private static VelocityHelper helperrrrrr;
private Logger log;
private VelocityEngine speeder;
private VelocityHelper() {
log = LogManager.getLogger();
speeder = new VelocityEngine();
speeder.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
speeder.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
speeder.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
speeder.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
speeder.init();
}
public static VelocityHelper getInstance() {
if ( helperrrrrr == null ) { helperrrrrr = new VelocityHelper(); }
return helperrrrrr;
}
public static VelocityHelper newInstance() { return getInstance(); }
public String generate(String template, Properties content) {
String pt = tplFix(template);
VelocityContext vc = new VelocityContext();
content.entrySet().iterator().forEachRemaining((t) -> {
vc.put(t.getKey().toString().replaceAll("\\.", "_"), t.getValue());
});
return generate(pt, vc);
}
public String generate(String template, HashMap<String, Object> content) {
String pt = tplFix(template);
VelocityContext vc = new VelocityContext();
content.entrySet().iterator().forEachRemaining((t) -> {
vc.put(t.getKey().replaceAll("\\.", "_"), t.getValue());
});
return generate(pt, vc);
}
private String generate(String template, VelocityContext context) {
StringWriter sw = new StringWriter();
context.put("main_template", template);
generate("/VELOCITY/page.vm", context, sw);
return sw.toString();
}
private void generate(String template, VelocityContext context, Writer writer) {
Template tpl = speeder.getTemplate(template);
tpl.merge(context, writer);
}
private String tplFix(String input) {
StringBuilder sb;
String pt;
if ( input == null ) {
sb = new StringBuilder();
sb.append(new Exception().getStackTrace()[2].getClassName());
pt = sb.toString().replaceAll("\\.", "/");
}
else { pt = input; }
sb = new StringBuilder();
if ( !pt.startsWith("/VELOCITY/") ) { sb.append("/VELOCITY/"); }
sb.append(pt);
if ( !pt.endsWith(".vm") ) { sb.append(".vm"); }
return sb.toString();
}
}
<html>
<head>
<title>${page_title}</title>
</head>
<body>
<h2>${page_title}</h2>
<hr>
#include(${main_template})
</body>
</html>
......@@ -33,7 +33,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<license>
<distribution>repo</distribution>
<name>Apache License 2.0</name>
<url>LICENSE</url>
<url>https://felinewith.me/LadySerenaKitty/lang-toolkit/raw/master/LICENSE</url>
</license>
</licenses>
......@@ -53,6 +53,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<name>${project.name}</name>
<url>${gl.pages}</url>
</site>
<downloadUrl>https://mvn.felinewith.me/#artifact-details-download-content/${project.groupId}/${project.artifactId}</downloadUrl>
</distributionManagement>
<issueManagement>
......@@ -80,6 +81,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<module>apps/langbuilder</module>
</modules>
<organization>
<name>StarPhoenix Media</name>
<url>https://starphoenixmedia.com/</url>
</organization>
<developers>
<developer>
<id>jlhawkwell</id>
......@@ -93,28 +99,6 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<repositories>
<repository>
<id>releases</id>
<name>SPM Releases</name>
<url>https://mvn.felinewith.me/repository/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>SPM Snapshots</name>
<url>https://mvn.felinewith.me/repository/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!-- repository>
<id>external</id>
<name>SPM Externals</name>
<url>https://mvn.felinewith.me/repository/external</url>
......@@ -124,7 +108,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository -->
</repository>
</repositories>
<dependencyManagement>
......@@ -156,6 +140,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
<version>0.8.90</version>
......@@ -350,6 +339,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
<configuration>
<version>1.0-SNAPSHOT</version>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!