Search in sources :

Example 6 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.

@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) ConfigContext(org.wso2.carbon.apimgt.core.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) Template(org.apache.velocity.Template)

Example 7 with ParseErrorException

use of org.apache.velocity.exception.ParseErrorException in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.

@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) Template(org.apache.velocity.Template)

Example 8 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 9 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 10 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)14 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)11 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)6 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)6 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 PrintWriter (java.io.PrintWriter)3 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)3 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 FileWriter (java.io.FileWriter)2 ParameterBinding (org.apache.cayenne.access.translator.ParameterBinding)2 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)2 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)2 CompositeAPIConfigContext (org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext)2 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WTemplate (com.github.bordertech.wcomponents.WTemplate)1 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)1