private void parse() {
   String name = fEditor.getEditorInput().getName();
   Reader reader = new StringReader(fEditor.getDocument().get());
   Template template = null;
   try {
     parseHtml();
     if (fEditor.getEditorInput() instanceof IFileEditorInput)
       ((IFileEditorInput) fEditor.getEditorInput())
           .getFile()
           .deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
     RuntimeInstance runtime = VelocityEditorEnvironment.getParser();
     SimpleNode root = runtime.parse(reader, name);
     // Create tree model
     NodeVisitor visitor = new NodeVisitor(name);
     root.jjtAccept(visitor, null);
     template = visitor.getTemplate();
     fError = "";
   } catch (ParseException e) {
     if (!name.matches(".*?\\.jsp.*")) {
       if (e.getMessage() != null) {
         fError = e.getMessage();
         Token token = e.currentToken;
         if (token != null) {
           fEditor.addProblemMarker(e.getMessage(), token.next.beginLine, IMarker.SEVERITY_ERROR);
         }
       } else {
         fError = "";
       }
     }
   } catch (Exception e) {
     fError = "";
     VelocityPlugin.log(e);
   } finally {
     try {
       reader.close();
     } catch (IOException e) {
       VelocityPlugin.log(e);
     }
   }
   // Replace saved template with the new parsed one
   synchronized (this) {
     if (template != null) {
       fTemplate = template;
       // Save last successful parse tree
       fLastTemplate = template;
     } else {
       fTemplate = null;
     }
   }
   // Update outline view and display error message in status line
   Display.getDefault()
       .syncExec(
           new Runnable() {
             public void run() {
               fEditor.updateOutlinePage();
               fEditor.displayErrorMessage(fError);
             }
           });
   reconcile();
 }
Exemple #2
0
  /*
   * (non-Javadoc)
   *
   * @see Directive#render(InternalContextAdapter, java.io.Writer, Node)
   */
  @Override
  public boolean render(InternalContextAdapter context, Writer writer, Node node)
      throws IOException, ResourceNotFoundException, ParseErrorException,
          MethodInvocationException {
    // 获得缓存信息
    SimpleNode sn_region = (SimpleNode) node.jjtGetChild(0);
    String region = (String) sn_region.value(context);
    SimpleNode sn_key = (SimpleNode) node.jjtGetChild(1);
    Serializable key = (Serializable) sn_key.value(context);

    Node body = node.jjtGetChild(2);
    // 检查内容是否有变化
    String tpl_key = key + "@" + region;
    String body_tpl = body.literal();
    String old_body_tpl = body_templates.get(tpl_key);
    String cache_html = CacheManager.get(String.class, region, key);
    if (cache_html == null || !StringUtils.equals(body_tpl, old_body_tpl)) {
      StringWriter sw = new StringWriter();
      body.render(context, sw);
      cache_html = sw.toString();
      CacheManager.set(region, key, cache_html);
      body_templates.put(tpl_key, body_tpl);
    }
    writer.write(cache_html);
    return true;
  }
 /**
  * Returns the result of substituting the variables defined by the fields of this class (a
  * concrete subclass of TemplateVars) into the template returned by {@link #parsedTemplate()}.
  */
 String toText() {
   VelocityContext velocityContext = toVelocityContext();
   StringWriter writer = new StringWriter();
   SimpleNode parsedTemplate = parsedTemplate();
   boolean rendered =
       velocityRuntimeInstance.render(
           velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate);
   if (!rendered) {
     // I don't know when this happens. Usually you get an exception during rendering.
     throw new IllegalArgumentException("Template rendering failed");
   }
   return writer.toString();
 }
  @Override
  public SQLStatement processTemplate(String template, List<Object> positionalParameters) {

    SimpleNode parsedTemplate = parse(template);

    Map<String, Object> internalParameters = new HashMap<>();

    PositionalParamMapper visitor =
        new PositionalParamMapper(positionalParameters, internalParameters);
    parsedTemplate.jjtAccept(visitor, null);
    visitor.onFinish();

    return processTemplate(template, parsedTemplate, internalParameters);
  }
  String buildStatement(VelocityContext context, String template, SimpleNode parsedTemplate)
      throws Exception {

    // ... not sure what InternalContextAdapter is for...
    InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context);
    ica.pushCurrentTemplateName(template);

    StringWriter out = new StringWriter(template.length());
    try {
      parsedTemplate.init(ica, velocityRuntime);
      parsedTemplate.render(ica, out);
      return out.toString();
    } finally {
      ica.popCurrentTemplateName();
    }
  }