org.apache.velocity.exception.VelocityException Java Examples
The following examples show how to use
org.apache.velocity.exception.VelocityException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ASTComparisonNode.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * Always false by default, != and == subclasses must override this. * @param left * @param right * @return comparison result */ public boolean compareNull(Object left, Object right) { // if either side is null, log and bail String msg = (left == null ? "Left" : "Right") + " side (" + jjtGetChild( (left == null? 0 : 1) ).literal() + ") of comparison operation has null value at " + StringUtils.formatFileString(this); if (rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false)) { throw new VelocityException(msg, null, rsvc.getLogContext().getStackTrace()); } log.error(msg); return false; }
Example #2
Source File: VelocimacroProxy.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * Check whether the number of arguments given matches the number defined. * @param node * @param callArgNum */ protected void checkArgumentCount(Node node, int callArgNum) { // Check if we have more calling arguments then the macro accepts if (callArgNum > macroArgs.size() - 1) { if (strictArguments) { throw new VelocityException("Provided " + callArgNum + " arguments but macro #" + macroArgs.get(0).name + " accepts at most " + (macroArgs.size()-1) + " at " + StringUtils.formatFileString(node), null, rsvc.getLogContext().getStackTrace()); } // Backward compatibility logging, Mainly for MacroForwardDefinedTestCase log.debug("VM #{}: too many arguments to macro. Wanted {} got {}", macroArgs.get(0).name, macroArgs.size() - 1, callArgNum); } }
Example #3
Source File: VelocityConfigurerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void velocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException { VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean(); Properties props = new Properties(); props.setProperty("myprop", "/mydir"); vefb.setVelocityProperties(props); Object value = new Object(); Map<String, Object> map = new HashMap<>(); map.put("myentry", value); vefb.setVelocityPropertiesMap(map); vefb.afterPropertiesSet(); assertThat(vefb.getObject(), instanceOf(VelocityEngine.class)); VelocityEngine ve = vefb.getObject(); assertEquals("/mydir", ve.getProperty("myprop")); assertEquals(value, ve.getProperty("myentry")); }
Example #4
Source File: VelocityConfigurerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void velocityConfigurerWithCsvPathAndNonFileAccess() throws IOException, VelocityException { VelocityConfigurer vc = new VelocityConfigurer(); vc.setResourceLoaderPath("file:/mydir,file:/yourdir"); vc.setResourceLoader(new ResourceLoader() { @Override public Resource getResource(String location) { if ("file:/yourdir/test".equals(location)) { return new DescriptiveResource(""); } return new ByteArrayResource("test".getBytes(), "test"); } @Override public ClassLoader getClassLoader() { return getClass().getClassLoader(); } }); vc.setPreferFileSystemAccess(false); vc.afterPropertiesSet(); assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class)); VelocityEngine ve = vc.createVelocityEngine(); assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap())); }
Example #5
Source File: ResourceLoaderFactory.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * Gets the loader specified in the configuration file. * @param rs * @param loaderClassName * @return TemplateLoader */ public static ResourceLoader getLoader(RuntimeServices rs, String loaderClassName) { ResourceLoader loader = null; try { loader = (ResourceLoader) ClassUtils.getNewInstance( loaderClassName ); rs.getLog().debug("ResourceLoader instantiated: {}", loader.getClass().getName()); return loader; } // The ugly three strike again: ClassNotFoundException,IllegalAccessException,InstantiationException catch(Exception e) { String msg = "Problem instantiating the template loader: "+loaderClassName+"." + System.lineSeparator() + "Look at your properties file and make sure the" + System.lineSeparator() + "name of the template loader is correct."; rs.getLog().error(msg, e); throw new VelocityException(msg, e); } }
Example #6
Source File: JarHolder.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * */ public void close() { try { theJar.close(); } catch ( Exception e ) { String msg = "JarHolder: error closing the JAR file"; log.error(msg, e); throw new VelocityException(msg, e); } theJar = null; conn = null; log.trace("JarHolder: JAR file closed"); }
Example #7
Source File: JarHolder.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * */ public void init() { try { log.debug("JarHolder: attempting to connect to {}", urlpath); URL url = new URL( urlpath ); conn = (JarURLConnection) url.openConnection(); conn.setAllowUserInteraction(false); conn.setDoInput(true); conn.setDoOutput(false); conn.connect(); theJar = conn.getJarFile(); } catch (IOException ioe) { String msg = "JarHolder: error establishing connection to JAR at \"" + urlpath + "\""; log.error(msg, ioe); throw new VelocityException(msg, ioe); } }
Example #8
Source File: ASTReference.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * Computes boolean value of this reference * Returns the actual value of reference return type * boolean, and 'true' if value is not null * * @param context context to compute value with * @return True if evaluation was ok. * @throws MethodInvocationException */ public boolean evaluate(InternalContextAdapter context) throws MethodInvocationException { Object value = execute(this, context); // non-null object as first parameter by convention for 'evaluate' if (value == null) { return false; } try { rsvc.getLogContext().pushLogContext(this, uberInfo); return DuckType.asBoolean(value, checkEmpty); } catch(Exception e) { throw new VelocityException("Reference evaluation threw an exception at " + StringUtils.formatFileString(this), e, rsvc.getLogContext().getStackTrace()); } finally { rsvc.getLogContext().popLogContext(); } }
Example #9
Source File: Break.java From velocity-engine with Apache License 2.0 | 6 votes |
/** * This directive throws a StopCommand which signals either * the nearest Scope or the specified scope to stop rendering * its content. * @return never, always throws a StopCommand or Exception */ public boolean render(InternalContextAdapter context, Writer writer, Node node) { if (!scoped) { throw new StopCommand(); } Object argument = node.jjtGetChild(0).value(context); if (argument instanceof Scope) { ((Scope)argument).stop(); throw new IllegalStateException("Scope.stop() failed to throw a StopCommand"); } else { throw new VelocityException(node.jjtGetChild(0).literal()+ " is not a valid " + Scope.class.getName() + " instance at " + StringUtils.formatFileString(this), null, rsvc.getLogContext().getStackTrace()); } }
Example #10
Source File: SpringConfig.java From quartz-glass with Apache License 2.0 | 6 votes |
@Bean public VelocityConfig velocityConfig() throws IOException, VelocityException { Properties config = new Properties(); config.setProperty("input.encoding", "UTF-8"); config.setProperty("output.encoding", "UTF-8"); config.setProperty("default.contentType", "text/html;charset=UTF-8"); config.setProperty("resource.loader", "class"); config.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); velocityConfigurer.setVelocityProperties(config); velocityConfigurer.afterPropertiesSet(); return velocityConfigurer; }
Example #11
Source File: FileTemplateUtil.java From consulo with Apache License 2.0 | 6 votes |
private static String mergeTemplate(String templateContent, final VelocityContext context, boolean useSystemLineSeparators) throws IOException { final StringWriter stringWriter = new StringWriter(); try { VelocityWrapper.evaluate(null, context, stringWriter, templateContent); } catch (final VelocityException e) { LOG.error("Error evaluating template:\n" + templateContent, e); ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(IdeBundle.message("error.parsing.file.template", e.getMessage()), IdeBundle.message("title.velocity.error"))); } 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; }
Example #12
Source File: VelocityEngineFactory.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Prepare the VelocityEngine instance and return it. * @return the VelocityEngine instance * @throws IOException if the config file wasn't found * @throws VelocityException on Velocity initialization failure */ public VelocityEngine createVelocityEngine() throws IOException, VelocityException { VelocityEngine velocityEngine = newVelocityEngine(); Map<String, Object> props = new HashMap<String, Object>(); // Load config file if set. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading Velocity config from [" + this.configLocation + "]"); } CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation), props); } // Merge local properties if set. if (!this.velocityProperties.isEmpty()) { props.putAll(this.velocityProperties); } // Set a resource loader path, if required. if (this.resourceLoaderPath != null) { initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath); } // Log via Commons Logging? if (this.overrideLogging) { velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute()); } // Apply properties to VelocityEngine. for (Map.Entry<String, Object> entry : props.entrySet()) { velocityEngine.setProperty(entry.getKey(), entry.getValue()); } postProcessVelocityEngine(velocityEngine); // Perform actual initialization. velocityEngine.init(); return velocityEngine; }
Example #13
Source File: VelocityConfigurer.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Initialize VelocityEngineFactory's VelocityEngine * if not overridden by a pre-configured VelocityEngine. * @see #createVelocityEngine * @see #setVelocityEngine */ @Override public void afterPropertiesSet() throws IOException, VelocityException { if (this.velocityEngine == null) { this.velocityEngine = createVelocityEngine(); } }
Example #14
Source File: VelocityEngineFactory.java From scoold with Apache License 2.0 | 5 votes |
/** * Prepare the VelocityEngine instance and return it. * * @return the VelocityEngine instance * @throws IOException if the config file wasn't found * @throws VelocityException on Velocity initialization failure */ public VelocityEngine createVelocityEngine() throws IOException, VelocityException { VelocityEngine velocityEngine = newVelocityEngine(); Map<String, Object> props = new HashMap<String, Object>(); // Load config file if set. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading Velocity config from [" + this.configLocation + "]"); } CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation), props); } // Merge local properties if set. if (!this.velocityProperties.isEmpty()) { props.putAll(this.velocityProperties); } // Set a resource loader path, if required. if (this.resourceLoaderPath != null) { initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath); } // Apply properties to VelocityEngine. for (Map.Entry<String, Object> entry : props.entrySet()) { velocityEngine.setProperty(entry.getKey(), entry.getValue()); } postProcessVelocityEngine(velocityEngine); // Perform actual initialization. velocityEngine.init(); return velocityEngine; }
Example #15
Source File: VelocityConfigurer.java From scoold with Apache License 2.0 | 5 votes |
/** * Initialize VelocityEngineFactory's VelocityEngine * if not overridden by a pre-configured VelocityEngine. * @see #createVelocityEngine * @see #setVelocityEngine */ @Override public void afterPropertiesSet() throws IOException, VelocityException { if (this.velocityEngine == null) { this.velocityEngine = createVelocityEngine(); } }
Example #16
Source File: StrictForeachTestCase.java From velocity-engine with Apache License 2.0 | 5 votes |
public void testUgly() { try { evaluate("#foreach( $i in $ugly )$i#end"); fail("Doing #foreach on $ugly should have exploded!"); } catch (VelocityException ve) { // success! } }
Example #17
Source File: StrictForeachTestCase.java From velocity-engine with Apache License 2.0 | 5 votes |
public void testGood() { try { evaluate("#foreach( $i in $good )$i#end"); } catch (VelocityException ve) { fail("Doing #foreach on $good should not have exploded!"); } }
Example #18
Source File: StrictForeachTestCase.java From velocity-engine with Apache License 2.0 | 5 votes |
public void testBad() { try { evaluate("#foreach( $i in $bad )$i#end"); fail("Doing #foreach on $bad should have exploded!"); } catch (VelocityException ve) { // success! } }
Example #19
Source File: Include.java From velocity-engine with Apache License 2.0 | 5 votes |
/** * iterates through the argument list and renders every * argument that is appropriate. Any non appropriate * arguments are logged, but render() continues. * @param context * @param writer * @param node * @return True if the directive rendered successfully. * @throws IOException * @throws MethodInvocationException * @throws ResourceNotFoundException */ public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, MethodInvocationException, ResourceNotFoundException { /* * get our arguments and check them */ int argCount = node.jjtGetNumChildren(); for( int i = 0; i < argCount; i++) { /* * we only handle StringLiterals and References right now */ Node n = node.jjtGetChild(i); if ( n.getType() == ParserTreeConstants.JJTSTRINGLITERAL || n.getType() == ParserTreeConstants.JJTREFERENCE ) { if (!renderOutput( n, context, writer )) outputErrorToStream( writer, "error with arg " + i + " please see log."); } else { String msg = "invalid #include() argument '" + n.toString() + "' at " + StringUtils.formatFileString(this); log.error(msg); outputErrorToStream( writer, "error with arg " + i + " please see log."); throw new VelocityException(msg, null, rsvc.getLogContext().getStackTrace()); } } return true; }
Example #20
Source File: VelocityTemplateEngine.java From rice with Educational Community License v2.0 | 5 votes |
/** * Evaluates a template with a map of objects. * * @param mapContext Map of Objects to be used in the template * @param template Velocity Template * @return Evaluated template */ public String evaluate(final Map<String, Object> mapContext, final Reader template) throws VelocityException { VelocityContext context = new VelocityContext(mapContext, defaultContext); StringWriter writerOut = new StringWriter(); try { velocityEngine.evaluate(context, writerOut, "VelocityEngine", template); return writerOut.toString(); } catch(Exception e) { throw new VelocityException(e); } }
Example #21
Source File: RuntimeInstance.java From velocity-engine with Apache License 2.0 | 5 votes |
/** * Returns a JavaCC generated Parser. * * @return Parser javacc generated parser */ public Parser createNewParser() { requireInitialization(); try { return (Parser)parserConstructor.newInstance((RuntimeServices)this); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new VelocityException("could not build new parser class", e); } }
Example #22
Source File: VelocityEngineFactory.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Prepare the VelocityEngine instance and return it. * @return the VelocityEngine instance * @throws IOException if the config file wasn't found * @throws VelocityException on Velocity initialization failure */ public VelocityEngine createVelocityEngine() throws IOException, VelocityException { VelocityEngine velocityEngine = newVelocityEngine(); Map<String, Object> props = new HashMap<String, Object>(); // Load config file if set. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading Velocity config from [" + this.configLocation + "]"); } CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation), props); } // Merge local properties if set. if (!this.velocityProperties.isEmpty()) { props.putAll(this.velocityProperties); } // Set a resource loader path, if required. if (this.resourceLoaderPath != null) { initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath); } // Log via Commons Logging? if (this.overrideLogging) { velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute()); } // Apply properties to VelocityEngine. for (Map.Entry<String, Object> entry : props.entrySet()) { velocityEngine.setProperty(entry.getKey(), entry.getValue()); } postProcessVelocityEngine(velocityEngine); // Perform actual initialization. velocityEngine.init(); return velocityEngine; }
Example #23
Source File: ASTComparisonNode.java From velocity-engine with Apache License 2.0 | 5 votes |
public boolean compareNonNumber(Object left, Object right) { // by default, log and bail String msg = (right instanceof Number ? "Left" : "Right") + " side of comparison operation is not a number at " + StringUtils.formatFileString(this); if (rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false)) { throw new VelocityException(msg, null, rsvc.getLogContext().getStackTrace()); } log.error(msg); return false; }
Example #24
Source File: VelocityEngineFactory.java From MaxKey with Apache License 2.0 | 5 votes |
/** * Prepare the VelocityEngine instance and return it. * @return the VelocityEngine instance * @throws IOException if the config file wasn't found * @throws VelocityException on Velocity initialization failure */ public VelocityEngine createVelocityEngine() throws IOException, VelocityException { VelocityEngine velocityEngine = newVelocityEngine(); Map<String, Object> props = new HashMap<String, Object>(); // Load config file if set. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading Velocity config from [" + this.configLocation + "]"); } CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(this.configLocation), props); } // Merge local properties if set. if (!this.velocityProperties.isEmpty()) { props.putAll(this.velocityProperties); } // Set a resource loader path, if required. if (this.resourceLoaderPath != null) { initVelocityResourceLoader(velocityEngine, this.resourceLoaderPath); } // Log via Commons Logging? if (this.overrideLogging) { velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute()); } // Apply properties to VelocityEngine. for (Map.Entry<String, Object> entry : props.entrySet()) { velocityEngine.setProperty(entry.getKey(), entry.getValue()); } postProcessVelocityEngine(velocityEngine); // Perform actual initialization. velocityEngine.init(); return velocityEngine; }
Example #25
Source File: EventHandlerUtil.java From velocity-engine with Apache License 2.0 | 5 votes |
/** * Called when a method exception is generated during Velocity merge. Only * the first valid event handler in the sequence is called. The default * implementation simply rethrows the exception. * * @param claz * Class that is causing the exception * @param method * method called that causes the exception * @param e * Exception thrown by the method * @param rsvc current instance of RuntimeServices * @param context The internal context adapter. * @param info exception location informations * @return Object to return as method result * @throws Exception * to be wrapped and propagated to app */ public static Object methodException(RuntimeServices rsvc, InternalContextAdapter context, Class claz, String method, Exception e, Info info) throws Exception { try { EventCartridge ev = rsvc.getApplicationEventCartridge(); if (ev.hasMethodExceptionEventHandler()) { return ev.methodException(context, claz, method, e, info); } EventCartridge contextCartridge = context.getEventCartridge(); if (contextCartridge != null) { contextCartridge.setRuntimeServices(rsvc); return contextCartridge.methodException(context, claz, method, e, info); } } catch (RuntimeException re) { throw re; } catch (Exception ex) { throw new VelocityException("Exception in event handler.", ex, rsvc.getLogContext().getStackTrace()); } /* default behaviour is to re-throw exception */ throw e; }
Example #26
Source File: Saml20AutoConfiguration.java From MaxKey with Apache License 2.0 | 5 votes |
/** * VelocityEngineFactoryBean. * @return velocityEngine * @throws IOException * @throws VelocityException */ @Bean(name = "velocityEngine") public VelocityEngine velocityEngine() throws VelocityException, IOException { VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean(); factory.setPreferFileSystemAccess(false); Properties velocityProperties = new Properties(); velocityProperties.put("resource.loader", "classpath"); velocityProperties.put("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); factory.setVelocityProperties(velocityProperties); return factory.createVelocityEngine(); }
Example #27
Source File: MailAppConfig.java From logsniffer with GNU Lesser General Public License v3.0 | 5 votes |
@Bean public VelocityEngine velocityEngine() throws VelocityException, IOException { VelocityEngineFactory factory = new VelocityEngineFactory(); Properties props = new Properties(); props.put("resource.loader", "class"); props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); factory.setVelocityProperties(props); return factory.createVelocityEngine(); }
Example #28
Source File: HtmlReporter.java From AppiumTestDistribution with GNU General Public License v3.0 | 5 votes |
public void generateReports() throws VelocityException, IOException { listFilesForFolder(reportOutputDirectory); String jenkinsBasePath = ""; String buildNumber = "1"; String projectName = "cucumber-jvm"; Configuration configuration = new Configuration(reportOutputDirectory, projectName); configuration.setBuildNumber(buildNumber); ReportBuilder reportBuilder = new ReportBuilder(list, configuration); reportBuilder.generateReports(); }
Example #29
Source File: EmailTransformer.java From website with GNU Affero General Public License v3.0 | 5 votes |
private String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateContents, Map model) throws VelocityException { StringWriter writer = new StringWriter(); VelocityContext velocityContext = new VelocityContext(model); velocityEngine.evaluate(velocityContext, writer, this.getClass().getName(), templateContents); return writer.toString(); }
Example #30
Source File: VelocityTemplateEngine.java From rice with Educational Community License v2.0 | 5 votes |
/** * Initializes Velocity engine */ private void init() { velocityEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "class"); velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); setLogFile(); DateTool dateTool = new DateTool(); dateTool.configure(this.configMap); MathTool mathTool = new MathTool(); NumberTool numberTool = new NumberTool(); numberTool.configure(this.configMap); SortTool sortTool = new SortTool(); defaultContext = new VelocityContext(); defaultContext.put("dateTool", dateTool); defaultContext.put("dateComparisonTool", new ComparisonDateTool()); defaultContext.put("mathTool", mathTool); defaultContext.put("numberTool", numberTool); defaultContext.put("sortTool", sortTool); // Following tools need VelocityTools version 2.0+ //defaultContext.put("displayTool", new DisplayTool()); //defaultContext.put("xmlTool", new XmlTool()); try { velocityEngine.init(); } catch (Exception e) { throw new VelocityException(e); } }