use of org.apache.velocity.VelocityContext in project xwiki-platform by xwiki.
the class DefaultWikiMacroTest method testDefaultParameterValues.
/**
* Test default parameter value injection.
*/
@Test
public void testDefaultParameterValues() throws Exception {
// Velocity Manager mock.
final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
descriptorVM.setRoleType(VelocityManager.class);
getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
// Initialize velocity engine.
final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
Properties properties = new Properties();
properties.setProperty("resource.loader", "file");
vEngine.initialize(properties);
// Hack into velocity context.
Execution execution = getComponentManager().getInstance(Execution.class);
Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
final VelocityContext vContext = new VelocityContext();
vContext.put("xcontext", xwikiContext);
getMockery().checking(new Expectations() {
{
oneOf(mockVelocityManager).getCurrentVelocityContext();
will(returnValue(vContext));
oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
will(new Action() {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
}
@Override
public void describeTo(Description description) {
}
});
}
});
List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value"));
registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.param1 $xcontext.macro.params.paraM1{{/velocity}}", Syntax.XWIKI_2_0, parameterDescriptors);
Converter converter = getComponentManager().getInstance(Converter.class);
DefaultWikiPrinter printer = new DefaultWikiPrinter();
converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
Assert.assertEquals("default_value default_value", printer.toString());
}
use of org.apache.velocity.VelocityContext in project xwiki-platform by xwiki.
the class StubVelocityManager method evaluate.
@Override
public boolean evaluate(Writer out, String templateName, Reader source) throws XWikiVelocityException {
// Get up to date Velocity context
VelocityContext velocityContext = getVelocityContext();
// Execute Velocity context
boolean result = getVelocityEngine().evaluate(velocityContext, out, templateName, source);
// Update current script context with potentially modified Velocity context
ScriptContext scontext = this.scriptContextManager.getCurrentScriptContext();
for (Object vkey : velocityContext.getKeys()) {
if (vkey instanceof String) {
String svkey = (String) vkey;
// context is a reserved binding in JSR223 specification
if (!"context".equals(svkey)) {
scontext.setAttribute(svkey, velocityContext.get(svkey), ScriptContext.ENGINE_SCOPE);
}
}
}
return result;
}
use of org.apache.velocity.VelocityContext in project xwiki-platform by xwiki.
the class XWikiWebappResourceLoaderTest method testVelocityInitialization.
@Test
public void testVelocityInitialization() throws Exception {
// Fake the initialization of the Servlet Environment
ServletEnvironment environment = (ServletEnvironment) getComponentManager().getInstance(Environment.class);
environment.setServletContext(getMockery().mock(ServletContext.class));
Properties properties = new Properties();
properties.setProperty("resource.loader", "xwiki");
properties.setProperty("xwiki.resource.loader.class", XWikiWebappResourceLoader.class.getName());
VelocityFactory factory = getComponentManager().getInstance(VelocityFactory.class);
VelocityEngine engine = factory.createVelocityEngine("key", properties);
// Ensure Velocity has been correctly initialized by trying to evaluate some content.
StringWriter out = new StringWriter();
engine.evaluate(new VelocityContext(), out, "template", "#set ($var = 'value')$var");
Assert.assertEquals("value", out.toString());
}
use of org.apache.velocity.VelocityContext in project xwiki-platform by xwiki.
the class PackageMojo method expandJettyDistribution.
private void expandJettyDistribution() throws MojoExecutionException {
Artifact jettyArtifact = resolveJettyArtifact();
unzip(jettyArtifact.getFile(), this.outputPackageDirectory);
// Replace properties in start shell scripts
Collection<File> startFiles = org.apache.commons.io.FileUtils.listFiles(this.outputPackageDirectory, new WildcardFileFilter("start_xwiki*.*"), null);
VelocityContext velocityContext = createVelocityContext();
for (File startFile : startFiles) {
getLog().info(String.format(" Replacing variables in [%s]...", startFile));
try {
String content = org.apache.commons.io.FileUtils.readFileToString(startFile, StandardCharsets.UTF_8);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(startFile));
writer.write(replaceProperty(content, velocityContext));
writer.close();
} catch (Exception e) {
// Failed to read or write file...
throw new MojoExecutionException(String.format("Failed to process start shell script [%s]", startFile), e);
}
}
}
use of org.apache.velocity.VelocityContext in project xwiki-platform by xwiki.
the class PackageMojo method generateConfigurationFiles.
private void generateConfigurationFiles(File configurationFileTargetDirectory) throws MojoExecutionException {
VelocityContext context = createVelocityContext();
Artifact configurationResourcesArtifact = this.repositorySystem.createArtifact("org.xwiki.platform", "xwiki-platform-tool-configuration-resources", getXWikiPlatformVersion(), "", "jar");
resolveArtifact(configurationResourcesArtifact);
configurationFileTargetDirectory.mkdirs();
try (JarInputStream jarInputStream = new JarInputStream(new FileInputStream(configurationResourcesArtifact.getFile()))) {
JarEntry entry;
while ((entry = jarInputStream.getNextJarEntry()) != null) {
if (entry.getName().endsWith(".vm")) {
String fileName = entry.getName().replace(".vm", "");
File outputFile = new File(configurationFileTargetDirectory, fileName);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile));
getLog().info("Writing config file: " + outputFile);
// Note: Init is done once even if this method is called several times...
Velocity.init();
Velocity.evaluate(context, writer, "", IOUtils.toString(jarInputStream, StandardCharsets.UTF_8));
writer.close();
jarInputStream.closeEntry();
}
}
} catch (Exception e) {
throw new MojoExecutionException("Failed to extract configuration files", e);
}
}
Aggregations