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();
 }
Ejemplo n.º 2
0
  static {
    // Ensure that $undefinedvar will produce an exception rather than outputting $undefinedvar.
    velocityRuntimeInstance.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, "true");
    velocityRuntimeInstance.setProperty(
        RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, new NullLogChute());
    velocityRuntimeInstance.setProperty(
        RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS, ResourceCacheImpl.class.getName());
    // Setting ResourceCacheImpl is should not be necessary since that is the default value, but
    // ensures that Maven shading sees that Apache Commons classes referenced from ResourceCacheImpl
    // are indeed referenced and cannot be removed during minimization.

    // Disable any logging that Velocity might otherwise see fit to do.
    velocityRuntimeInstance.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());

    // Velocity likes its "managers", LogManager and ResourceManager, which it loads through the
    // context class loader. If that loader can see another copy of Velocity then that will lead
    // to hard-to-diagnose exceptions during initialization.
    Thread currentThread = Thread.currentThread();
    ClassLoader oldContextLoader = currentThread.getContextClassLoader();
    try {
      currentThread.setContextClassLoader(TemplateVars.class.getClassLoader());
      velocityRuntimeInstance.init();
    } finally {
      currentThread.setContextClassLoader(oldContextLoader);
    }
  }
Ejemplo n.º 3
0
 static SimpleNode parsedTemplateForResource(String templateStr, String resourceName) {
   try {
     return velocityRuntimeInstance.parse(templateStr, resourceName);
   } catch (ParseException e) {
     throw new AssertionError(e);
   }
 }
Ejemplo n.º 4
0
  /**
   * Initialize Velocity engine instance, disables logging, sets bundle-relative resource loader.
   */
  public static RuntimeInstance createInstance(String bundleID, String templatePrefix) {
    try {
      final ExtendedProperties p = new ExtendedProperties();
      p.setProperty("resource.loader", "bundle");
      p.setProperty(
          "bundle.resource.loader.instance", new BundleResourceLoader(bundleID, templatePrefix));
      p.setProperty(RuntimeConstants.SET_NULL_ALLOWED, "true");

      // Disable separate Velocity logging.
      p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());

      final RuntimeInstance velocity = new RuntimeInstance();
      velocity.setConfiguration(p);
      return velocity;
    } catch (Exception e) {
      throw new RuntimeException("Velocity initialization failed.", e);
    }
  }
Ejemplo n.º 5
0
 /**
  * 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();
 }
  public VelocitySQLTemplateProcessor() {
    this.renderingUtils = new SQLTemplateRenderingUtils();
    this.velocityRuntime = new RuntimeInstance();

    // set null logger
    velocityRuntime.addProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());

    velocityRuntime.addProperty(
        RuntimeConstants.RESOURCE_MANAGER_CLASS, SQLTemplateResourceManager.class.getName());
    velocityRuntime.addProperty("userdirective", BindDirective.class.getName());
    velocityRuntime.addProperty("userdirective", BindEqualDirective.class.getName());
    velocityRuntime.addProperty("userdirective", BindNotEqualDirective.class.getName());
    velocityRuntime.addProperty("userdirective", BindObjectEqualDirective.class.getName());
    velocityRuntime.addProperty("userdirective", BindObjectNotEqualDirective.class.getName());
    velocityRuntime.addProperty("userdirective", ResultDirective.class.getName());
    velocityRuntime.addProperty("userdirective", ChainDirective.class.getName());
    velocityRuntime.addProperty("userdirective", ChunkDirective.class.getName());
    try {
      velocityRuntime.init();
    } catch (Exception ex) {
      throw new CayenneRuntimeException("Error setting up Velocity RuntimeInstance.", ex);
    }
  }
Ejemplo n.º 7
0
 static SimpleNode parsedTemplateForResource(String resourceName) {
   InputStream in = RetrofitTemplateVars.class.getResourceAsStream(resourceName);
   if (in == null) {
     throw new IllegalArgumentException("Could not find resource: " + resourceName);
   }
   try {
     Reader reader = new InputStreamReader(in, "UTF-8");
     return velocityRuntimeInstance.parse(reader, resourceName);
   } catch (UnsupportedEncodingException e) {
     throw new AssertionError(e);
   } catch (ParseException e) {
     throw new AssertionError(e);
   }
 }
  private SimpleNode parse(String template) {

    SimpleNode nodeTree = null;

    try {
      nodeTree = velocityRuntime.parse(new StringReader(template), template);
    } catch (ParseException pex) {
      throw new CayenneRuntimeException(
          "Error parsing template '" + template + "' : " + pex.getMessage());
    }

    if (nodeTree == null) {
      throw new CayenneRuntimeException("Error parsing template " + template);
    }

    return nodeTree;
  }