private void initTemplate(@NonNull final String verifyExpression) throws ParseException { // Init pattern ... final RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); final StringReader reader = new StringReader(verifyExpression); SimpleNode node = runtimeServices.parse(reader, this.getDocumentation().getId()); final Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); this.template = template; // Parse expression looking for variable ... final Pattern pattern = Pattern.compile(VELOCITY_VARIABLE); final Matcher matcher = pattern.matcher(verifyExpression); while (matcher.find()) { final String key = matcher.group(1); final ClassProperty property = ClassProperty.to(key); this.templateProperties.add(property); } }
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { // create the velocity context VelocityContext context = createVelocityContext(model); // merge the template 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(); template.merge(context, writer); } catch (Exception e) { throw new PippoRuntimeException(e); } }
/** * Constructor based on a Velocity 'encoded' representation. * * @param templateRepresentation * The representation to 'decode'. * @param dataModel * The Velocity template's data model. * @param mediaType * The representation's media type. * @throws IOException * @throws ParseErrorException * @throws ResourceNotFoundException */ public TemplateRepresentation(Representation templateRepresentation, Map<String, Object> dataModel, MediaType mediaType) throws ResourceNotFoundException, ParseErrorException, IOException { super(mediaType); setDataModel(dataModel); this.engine = null; this.template = new Template(); this.template .setEncoding((templateRepresentation.getCharacterSet() == null) ? Charset .defaultCharset().name() : templateRepresentation.getCharacterSet().getName()); if (templateRepresentation.getModificationDate() != null) { this.template.setLastModified(templateRepresentation .getModificationDate().getTime()); } this.template.setName("org.restlet.resource.representation"); this.template.setRuntimeServices(RuntimeSingleton.getRuntimeServices()); this.template.setResourceLoader(new RepresentationResourceLoader( templateRepresentation)); this.template.process(); this.templateName = null; }
/** * Constructor based on a Velocity 'encoded' representation. * * @param templateRepresentation * The representation to 'decode'. * @param mediaType * The representation's media type. * @throws IOException * @throws ParseErrorException * @throws ResourceNotFoundException */ public TemplateRepresentation(Representation templateRepresentation, MediaType mediaType) throws ResourceNotFoundException, ParseErrorException, IOException { super(mediaType); this.engine = null; this.template = new Template(); this.template .setEncoding((templateRepresentation.getCharacterSet() == null) ? Charset .defaultCharset().name() : templateRepresentation.getCharacterSet().getName()); this.template.setLastModified((templateRepresentation .getModificationDate() == null) ? new Date().getTime() : templateRepresentation.getModificationDate().getTime()); this.template.setName("org.restlet.resource.representation"); this.template.setRuntimeServices(RuntimeSingleton.getRuntimeServices()); this.template.setResourceLoader(new RepresentationResourceLoader( templateRepresentation)); this.template.process(); this.templateName = null; }
/** * 根据模板名字和编码完成Velocity模板对象的创建工作 * * @param templateName * @param encoding * @return * @throws ResourceNotFoundException * @throws ParseErrorException * @throws MethodInvocationException * @throws Exception */ protected Template getTemplate(String templateName, String encoding) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception { String name = parseTemplateName(templateName); if (FrameworkEngine.getWebConfig().isDebug()) return RuntimeSingleton.getTemplate(name, encoding);// 若为Debug状态,则每次都重新载入模板 String templatePath = (String) Velocity .getProperty("file.resource.loader.path"); java.io.File f = new java.io.File(new java.io.File(templatePath), name); Template template = (Template) templateCache.get(name);// 先从Cache中读取模板文件 // 这里得进一步完善,当用户已经更改模板文件后,需要能够自动加载,同时增加Cache数量的限制 if (template == null || (f.exists() && f.lastModified() > template.getLastModified())) synchronized (templateCache) { { logger.info(I18n.getLocaleMessage("core.web.template.file.reloads") + name); template = RuntimeSingleton.getTemplate(name, encoding);// name templateCache.put(name, template); } } return template; }
public static Template create(String source, String templateName) throws ParseException { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(source); SimpleNode node = runtimeServices.parse(reader, templateName); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); return template; }
public VelocitySqlSource(Configuration configuration, String scriptText) { this.configuration = configuration; try { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(scriptText); SimpleNode node = runtimeServices.parse(reader, "Template name"); script = new Template(); script.setRuntimeServices(runtimeServices); script.setData(node); script.initDocument(); } catch (Exception ex) { throw new BuilderException("Error parsing velocity script", ex); } }
/** * @작성자 : KYJ * @작성일 : 2017. 10. 14. * @param url * @return * @throws Exception */ public static Template createTemplate(InputStream is) throws Exception { String readFileToString = ValueUtil.toString(is); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(readFileToString); SimpleNode node = runtimeServices.parse(reader, "URLTemplate"); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); return template; }
private static String[] calculateAttributes(String templateContent, Set<String> propertiesNames, boolean includeDummies, Project project) throws ParseException { final Set<String> unsetAttributes = new LinkedHashSet<String>(); final Set<String> definedAttributes = new HashSet<String>(); //noinspection HardCodedStringLiteral SimpleNode template = RuntimeSingleton.parse(new StringReader(templateContent), "MyTemplate"); collectAttributes(unsetAttributes, definedAttributes, template, propertiesNames, includeDummies, new HashSet<String>(), project); for (String definedAttribute : definedAttributes) { unsetAttributes.remove(definedAttribute); } return ArrayUtil.toStringArray(unsetAttributes); }
static SimpleNode parsedTemplateForString(String string) { try { Reader reader = new StringReader(string); return RuntimeSingleton.parse(reader, string); } catch (ParseException e) { throw new AssertionError(e); } }
public static String[] calculateAttributes(String templateContent, Set<String> propertiesNames, boolean includeDummies) throws ParseException { final Set<String> unsetAttributes = new LinkedHashSet<String>(); final Set<String> definedAttributes = new HashSet<String>(); //noinspection HardCodedStringLiteral SimpleNode template = RuntimeSingleton.parse(new StringReader(templateContent), "MyTemplate"); collectAttributes(unsetAttributes, definedAttributes, template, propertiesNames, includeDummies, new HashSet<String>()); for (String definedAttribute : definedAttributes) { unsetAttributes.remove(definedAttribute); } return ArrayUtil.toStringArray(unsetAttributes); }
@Override public boolean process() throws ResourceNotFoundException, ParseErrorException, Exception { super.rsvc=RuntimeSingleton.getRuntimeServices(); if(content==null) throw new ResourceNotFoundException("Unknown resource error for resource " + super.name); super.data = null; InputStream is = new java.io.ByteArrayInputStream(content); if(is != null) { try { BufferedReader br = new BufferedReader(new InputStreamReader(is, super.encoding)); super.data = super.rsvc.parse(br, super.name); initDocument(); boolean flag = true; return flag; } catch(UnsupportedEncodingException uce) { String msg = "Template.process : Unsupported input encoding : " + super.encoding + " for template " + super.name; throw new ParseErrorException(msg); } catch(ParseException pex) { throw new ParseErrorException(pex.getMessage()); } catch(Exception e) { throw e; } finally { is.close(); } } else { throw new ResourceNotFoundException("Unknown resource error for resource " + super.name); } }
/** * Configures a restlet component * * @return * @throws Exception */ @Bean public Component restletComponent() throws Exception { org.apache.commons.configuration.Configuration configuration = beanFactory .getBean(org.apache.commons.configuration.Configuration.class); // we must initialise velocity before hand. // a cleaner solution for this must be found in the future. final String templateDir = configuration.getString( CollectorProperties.WEB.VELOCITY_TEMPLATE_DIR.toString(), (String) CollectorProperties.WEB.VELOCITY_TEMPLATE_DIR .getDefaultValue()); String logFile = configuration.getString( CollectorProperties.WEB.VELOCITY_LOG_FILE.toString(), (String) CollectorProperties.WEB.VELOCITY_LOG_FILE .getDefaultValue()); RuntimeSingleton.setProperty( RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir); RuntimeSingleton.setProperty(RuntimeConstants.RUNTIME_LOG, logFile); RuntimeSingleton.init(); LOG.info("Using templates: " + templateDir); LOG.info("Using display log file : " + logFile); Component component = new Component(); int port = configuration.getInt( CollectorProperties.WRITER.COLLECTOR_MON_PORT.toString(), (Integer) CollectorProperties.WRITER.COLLECTOR_MON_PORT .getDefaultValue()); LOG.info("Using collector monitoring port: " + port); component.getServers().add(org.restlet.data.Protocol.HTTP, port); component.getClients().add(org.restlet.data.Protocol.FILE); Application staticApp = new Application(component.getContext()) { @Override public Restlet createRoot() { return new Directory(getContext(), "file://" + new File(templateDir).getAbsolutePath()); } }; component.getDefaultHost().attach("/view", restApplication()); component.getDefaultHost().attach("/static", staticApp); component.getDefaultHost().attach("/images", staticApp); return component; }
static SimpleNode parse(Reader reader, String templateName) throws ParseException { Template template = new Template(); template.setName(templateName); return RuntimeSingleton.parse(reader, template); }