use of org.apache.velocity.exception.VelocityException in project intellij-community by JetBrains.
the class FileTemplateUtil method mergeTemplate.
private static String mergeTemplate(String templateContent, final VelocityContext context, boolean useSystemLineSeparators, @Nullable Consumer<VelocityException> exceptionHandler) throws IOException {
final StringWriter stringWriter = new StringWriter();
try {
Project project = null;
final Object projectName = context.get(FileTemplateManager.PROJECT_NAME_VARIABLE);
if (projectName instanceof String) {
Project[] projects = ProjectManager.getInstance().getOpenProjects();
project = ContainerUtil.find(projects, project1 -> projectName.equals(project1.getName()));
}
VelocityWrapper.evaluate(project, context, stringWriter, templateContent);
} catch (final VelocityException e) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
LOG.error(e);
}
LOG.info("Error evaluating template:\n" + templateContent, e);
if (exceptionHandler == null) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(IdeBundle.message("error.parsing.file.template", e.getMessage()), IdeBundle.message("title.velocity.error")));
} else {
exceptionHandler.consume(e);
}
}
final String result = stringWriter.toString();
if (useSystemLineSeparators) {
final String newSeparator = CodeStyleSettingsManager.getSettings(ProjectManagerEx.getInstanceEx().getDefaultProject()).getLineSeparator();
if (!"\n".equals(newSeparator)) {
return StringUtil.convertLineSeparators(result, newSeparator);
}
}
return result;
}
use of org.apache.velocity.exception.VelocityException in project tdi-studio-se by Talend.
the class TemplateProcessor method processTemplate.
public static boolean processTemplate(String logTag, Map<String, Object> contextParams, OutputStream outputStream, InputStream templateStream) throws IOException {
final Reader templateReader = new InputStreamReader(templateStream);
final Writer outputWriter = new OutputStreamWriter(outputStream);
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(TemplateProcessor.class.getClassLoader());
VelocityEngine engine = new VelocityEngine();
// engine.setProperty("resource.loader", "classpath"); //$NON-NLS-1$ //$NON-NLS-2$
// engine.setProperty("classpath.resource.loader.class", //$NON-NLS-1$
// "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //$NON-NLS-1$
engine.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, EscapeXmlReference.class.getName());
engine.init();
VelocityContext context = new VelocityContext(contextParams);
return engine.evaluate(context, outputWriter, logTag, templateReader);
} catch (VelocityException ve) {
// org.apache.velocity.exception.MethodInvocationException;
throw new IOException(ve);
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
templateReader.close();
outputWriter.close();
}
}
use of org.apache.velocity.exception.VelocityException in project xwiki-platform by xwiki.
the class WebJarsResourceReferenceHandlerTest method failingResourceEvaluation.
@Test
public void failingResourceEvaluation() throws Exception {
WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("angular", "2.1.11", "angular.js"));
reference.addParameter("evaluate", "true");
ByteArrayInputStream resourceStream = new ByteArrayInputStream("content".getBytes());
when(this.classLoader.getResourceAsStream("META-INF/resources/webjars/angular/2.1.11/angular.js")).thenReturn(resourceStream);
VelocityManager velocityManager = this.componentManager.getInstance(VelocityManager.class);
VelocityEngine velocityEngine = mock(VelocityEngine.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEngine.evaluate(any(), any(), eq("angular/2.1.11/angular.js"), any(Reader.class))).thenThrow(new VelocityException("Bad code!"));
this.handler.handle(reference, this.chain);
// Verify the exception is logged.
verify(this.componentManager.getMockedLogger()).error(eq("Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]"), any(ResourceReferenceHandlerException.class));
// Verify that the client is properly notified about the failure.
verify(this.response.getHttpServletResponse()).sendError(500, "Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]");
// The next handlers are still called.
verify(this.chain).handleNext(reference);
}
use of org.apache.velocity.exception.VelocityException in project maven-plugins by apache.
the class VelocityTemplate method generate.
/**
* Using a specified Velocity Template and provided context, create the outputFilename.
*
* @param outputFilename the file to be generated.
* @param template the velocity template to use.
* @param context the velocity context map.
* @throws VelocityException if the template was not found or any other Velocity exception.
* @throws MojoExecutionException if merging the velocity template failed.
* @throws IOException if there was an error when writing to the output file.
*/
public void generate(String outputFilename, String template, Context context) throws VelocityException, MojoExecutionException, IOException {
Writer writer = null;
try {
File f = new File(outputFilename);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
writer = new FileWriter(f);
getVelocity().getEngine().mergeTemplate(templateDirectory + "/" + template, context, writer);
} catch (ResourceNotFoundException e) {
throw new ResourceNotFoundException("Template not found: " + templateDirectory + "/" + template, e);
} catch (VelocityException | IOException e) {
// to get past generic catch for Exception.
throw e;
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (writer != null) {
writer.flush();
writer.close();
getLog().debug("File " + outputFilename + " created...");
}
}
}
Aggregations