Search in sources :

Example 26 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method copyResourceIfExists.

protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context) throws IOException, MojoExecutionException {
    for (Resource resource : resources) {
        File resourceDirectory = new File(resource.getDirectory());
        if (!resourceDirectory.exists()) {
            continue;
        }
        // TODO - really should use the resource includes/excludes and name mapping
        File source = new File(resourceDirectory, relFileName);
        File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);
        if (!source.exists() && templateSource.exists()) {
            source = templateSource;
        }
        if (source.exists() && !source.equals(file)) {
            if (source == templateSource) {
                Reader reader = null;
                Writer writer = null;
                DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, file);
                try {
                    if (encoding != null) {
                        reader = new InputStreamReader(new FileInputStream(source), encoding);
                        writer = new OutputStreamWriter(os, encoding);
                    } else {
                        reader = ReaderFactory.newPlatformReader(source);
                        writer = WriterFactory.newPlatformWriter(os);
                    }
                    velocity.evaluate(context, writer, "", reader);
                    writer.close();
                    writer = null;
                    reader.close();
                    reader = null;
                } catch (ParseErrorException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (MethodInvocationException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (ResourceNotFoundException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } finally {
                    IOUtil.close(writer);
                    IOUtil.close(reader);
                }
                fileWriteIfDiffers(os);
            } else if (resource.isFiltering()) {
                MavenFileFilterRequest req = setupRequest(resource, source, file);
                try {
                    fileFilter.copyFile(req);
                } catch (MavenFilteringException e) {
                    throw new MojoExecutionException("Error filtering resource: " + source, e);
                }
            } else {
                FileUtils.copyFile(source, file);
            }
            // exclude the original (so eclipse doesn't complain about duplicate resources)
            resource.addExclude(relFileName);
            return true;
        }
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) Resource(org.apache.maven.model.Resource) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) FileInputStream(java.io.FileInputStream) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Example 27 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException 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 28 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project jop by jop-devel.

the class Report method generateFile.

private void generateFile(String templateName, File outFile, VelocityContext ctx) throws Exception {
    Template template;
    try {
        template = Velocity.getTemplate(templateName);
    } catch (ResourceNotFoundException ignored) {
        template = Velocity.getTemplate("com/jopdesign/wcet/report/" + templateName);
    }
    FileWriter fw = new FileWriter(outFile);
    template.merge(ctx, fw);
    fw.close();
}
Also used : FileWriter(java.io.FileWriter) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 29 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException 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)

Example 30 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project cayenne by apache.

the class ClassGeneratorResourceLoader method getResourceReader.

/**
 * Returns resource as InputStream. Searches resource in a following places:
 * <ol>
 *     <li>thread class loader</li>
 *     <li>this class's class loader</li>
 *     <li>tries a path relative to a <i>root</i> path, if set</li>
 *     <li>an absolute path</li>
 * </ol>
 */
@Override
public synchronized Reader getResourceReader(String name, String charset) throws ResourceNotFoundException {
    Reader stream;
    stream = loadFromThreadClassLoader(name);
    if (stream != null) {
        return stream;
    }
    stream = loadFromThisClassLoader(name);
    if (stream != null) {
        return stream;
    }
    stream = loadFromRelativePath(name);
    if (stream != null) {
        return stream;
    }
    stream = loadFromAbsPath(name);
    if (stream != null) {
        return stream;
    }
    throw new ResourceNotFoundException("Couldn't find resource '" + name + "'. Searched filesystem path and classpath");
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)31 ParseErrorException (org.apache.velocity.exception.ParseErrorException)21 VelocityContext (org.apache.velocity.VelocityContext)17 Template (org.apache.velocity.Template)16 IOException (java.io.IOException)15 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)14 StringWriter (java.io.StringWriter)11 File (java.io.File)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 Writer (java.io.Writer)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Context (org.apache.velocity.context.Context)3 VelocityException (org.apache.velocity.exception.VelocityException)3 WComponent (com.github.bordertech.wcomponents.WComponent)2 FileInputStream (java.io.FileInputStream)2