Search in sources :

Example 1 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project jfinal by jfinal.

the class VelocityRender method render.

public void render() {
    if (notInit) {
        // Velocity.init("velocity.properties");	// setup
        Velocity.init(properties);
        notInit = false;
    }
    PrintWriter writer = null;
    try {
        /*
             *  Make a context object and populate with the data.  This
             *  is where the Velocity engine gets the data to resolve the
             *  references (ex. $list) in the template
             */
        VelocityContext context = new VelocityContext();
        // Map root = new HashMap();
        for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements(); ) {
            String attrName = attrs.nextElement();
            context.put(attrName, request.getAttribute(attrName));
        }
        /*
             *  get the Template object.  This is the parsed version of your
             *  template input file.  Note that getTemplate() can throw
             *   ResourceNotFoundException : if it doesn't find the template
             *   ParseErrorException : if there is something wrong with the VTL
             *   Exception : if something else goes wrong (this is generally
             *        indicative of as serious problem...)
             */
        Template template = Velocity.getTemplate(view);
        /*
             *  Now have the template engine process your template using the
             *  data placed into the context.  Think of it as a  'merge'
             *  of the template and the data to produce the output stream.
             */
        response.setContentType(getContentType());
        // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        writer = response.getWriter();
        template.merge(context, writer);
        // flush and cleanup
        writer.flush();
    } catch (ResourceNotFoundException e) {
        throw new RenderException("Example : error : cannot find template " + view, e);
    } catch (ParseErrorException e) {
        throw new RenderException("Example : Syntax error in template " + view + ":" + e, e);
    } catch (Exception e) {
        throw new RenderException(e);
    } finally {
        if (writer != null)
            writer.close();
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) PrintWriter(java.io.PrintWriter) Template(org.apache.velocity.Template)

Example 2 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project jena by apache.

the class SimpleVelocity method process.

/** Process a template */
public static void process(String base, String path, Writer out, VelocityContext context) {
    VelocityEngine velocity = new VelocityEngine();
    // Turn off logging - catch exceptions and log ourselves
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute);
    velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
    velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base);
    velocity.init();
    try {
        Template temp = velocity.getTemplate(path);
        temp.merge(context, out);
        out.flush();
    } catch (ResourceNotFoundException ex) {
        velocityLog.error("Resource not found: " + ex.getMessage());
    } catch (ParseErrorException ex) {
        velocityLog.error("Parse error (" + path + ") : " + ex.getMessage());
    } catch (MethodInvocationException ex) {
        velocityLog.error("Method invocation exception (" + path + ") : " + ex.getMessage());
    } catch (IOException ex) {
        velocityLog.warn("IOException", ex);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 3 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project bazel by bazelbuild.

the class Page method write.

/**
   * Renders the template and writes the output to the given file.
   *
   * Strips all trailing whitespace before writing to file.
   */
public void write(File outputFile) throws IOException {
    StringWriter stringWriter = new StringWriter();
    try {
        engine.mergeTemplate(template, "UTF-8", context, stringWriter);
    } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
        throw new IOException(e);
    }
    stringWriter.close();
    String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
    try (FileWriter fileWriter = new FileWriter(outputFile)) {
        for (String line : lines) {
            // Strip trailing whitespace then append newline before writing to file.
            fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ParseErrorException(org.apache.velocity.exception.ParseErrorException) FileWriter(java.io.FileWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 4 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project cachecloud by sohutv.

the class VelocityUtils method createText.

/**
     * 邮件模板
     *
     * @param appDesc       应用信息
     * @param appAudit      处理信息
     * @param templatePath  模板路径
     * @param customCharset 编码
     */
public static synchronized String createText(VelocityEngine engine, AppDesc appDesc, AppAudit appAudit, AppDailyData appDailyData, List<InstanceAlertValueResult> instanceAlertValueResultList, String templatePath, String customCharset) {
    if (!StringUtils.isEmpty(customCharset)) {
        charset = customCharset;
    }
    Properties p = new Properties();
    p.setProperty("file.resource.loader.path", Thread.currentThread().getContextClassLoader().getResource("").getPath());
    p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
    p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
    p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
    Velocity.init(p);
    logger.info("velocity: init done.");
    VelocityContext context = new VelocityContext();
    context.put("appDesc", appDesc);
    context.put("appAudit", appAudit);
    context.put("appDailyData", appDailyData);
    context.put("instanceAlertValueResultList", instanceAlertValueResultList);
    context.put("numberTool", new NumberTool());
    context.put("ccDomain", ConstUtils.CC_DOMAIN);
    context.put("decimalFormat", new DecimalFormat("###,###"));
    context.put("StringUtils", StringUtils.class);
    FileOutputStream fos = null;
    StringWriter writer = null;
    try {
        Template template = engine.getTemplate(templatePath);
        writer = new StringWriter();
        template.merge(context, writer);
    } catch (ResourceNotFoundException ex) {
        logger.error("error: velocity vm resource not found.", ex);
    } catch (ParseErrorException ex) {
        logger.error("error: velocity parse vm file error.", ex);
    } catch (MethodInvocationException ex) {
        logger.error("error: velocity template merge.", ex);
    } catch (Exception ex) {
        logger.error("error", ex);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            logger.error("error: close writer", e);
        }
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            logger.error("error: close output stream.", e);
        }
    }
    logger.info("velocity: create text done.");
    if (writer != null) {
        return writer.toString();
    }
    return null;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) Properties(java.util.Properties) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 5 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project Asqatasun by Asqatasun.

the class CodeGeneratorMojo method execute.

@Override
public void execute() {
    try {
        isContextValid();
    } catch (InvalidParameterException ipe) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ipe);
        return;
    }
    // Initializes engine
    VelocityEngine ve = initializeVelocity();
    Iterable<CSVRecord> records = getCsv();
    if (records == null) {
        return;
    }
    try {
        initializeContext();
        generate(ve, records);
        cleanUpUnusedFiles();
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ResourceNotFoundException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseErrorException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : InvalidParameterException(org.asqatasun.referential.creator.exception.InvalidParameterException) VelocityEngine(org.apache.velocity.app.VelocityEngine) ParseErrorException(org.apache.velocity.exception.ParseErrorException) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) InvalidParameterException(org.asqatasun.referential.creator.exception.InvalidParameterException) I18NLanguageNotFoundException(org.asqatasun.referential.creator.exception.I18NLanguageNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

ParseErrorException (org.apache.velocity.exception.ParseErrorException)6 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)6 IOException (java.io.IOException)4 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)4 Template (org.apache.velocity.Template)3 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 VelocityContext (org.apache.velocity.VelocityContext)2 VelocityEngine (org.apache.velocity.app.VelocityEngine)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1