Example usage for org.springframework.core.io Resource getFilename

List of usage examples for org.springframework.core.io Resource getFilename

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getFilename.

Prototype

@Nullable
String getFilename();

Source Link

Document

Determine a filename for this resource, i.e.

Usage

From source file:org.spring.resource.FileSourceExample.java

public static void main(String[] args) {
    try {//from   w w w . j a  va  2 s  . c  o  m
        String filePath = "E:\\JEELearning\\ideaworlplace\\springdream\\springdream.ioc\\src\\main\\resources\\FileSource.txt";
        // ?
        Resource res1 = new FileSystemResource(filePath);
        // ?
        Resource res2 = new ClassPathResource("FileSource.txt");

        System.out.println("?:" + res1.getFilename());
        System.out.println("?:" + res2.getFilename());

        InputStream in1 = res1.getInputStream();
        InputStream in2 = res2.getInputStream();
        //            System.out.println("?:" + read(in1));
        //            System.out.println("?:" + read(in2));
        System.out.println("?:" + new String(FileCopyUtils.copyToByteArray(in1)));
        System.out.println("?:" + new String(FileCopyUtils.copyToByteArray(in2)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.mongeez.reader.ChangeSetReaderUtil.java

static void populateChangeSetResourceInfo(ChangeSet changeSet, Resource file) {
    changeSet.setFile(file.getFilename());
    if (file instanceof ClassPathResource) {
        changeSet.setResourcePath(((ClassPathResource) file).getPath());
    }/*from  ww w .  java 2 s  . c  o m*/
}

From source file:org.paxml.file.FileHelper.java

public static IFile load(Resource file) {
    String ext = FilenameUtils.getExtension(file.getFilename()).toLowerCase();
    IFileFactory fact = factories.get(ext);
    if (fact == null) {
        fact = factories.get("*");
        if (fact == null) {
            throw new PaxmlRuntimeException(
                    "Unknown file type based on its extension: " + PaxmlUtils.getResourceFile(file));
        }//w w w.  j  a  v  a 2  s.  c om
        if (log.isDebugEnabled()) {
            log.debug("Using the default file factory '" + fact.getClass().getName() + "' to load file: "
                    + PaxmlUtils.getResourceFile(file));
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Using registered file factory '" + fact.getClass().getName() + "' to load file: "
                    + PaxmlUtils.getResourceFile(file));
        }
    }
    IFile result = fact.load(file);
    Context.getCurrentContext().registerCloseable(result);
    return result;
}

From source file:de.ingrid.iplug.ckan.utils.ScriptEngine.java

/**
 * Get the scripting engine for the given script file
 * @param script The script file/*ww  w  . j  a v  a 2s  . c o m*/
 * @return javax.script.ScriptEngine
 */
protected static javax.script.ScriptEngine getEngine(Resource script) {
    String scriptName = script.getFilename();
    String extension = scriptName.substring(scriptName.lastIndexOf('.') + 1, scriptName.length());
    if (!engines.containsKey(extension)) {
        javax.script.ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
        engines.put(extension, engine);
    }
    return engines.get(extension);
}

From source file:de.ingrid.iplug.ckan.utils.ScriptEngine.java

/**
 * Get the compiled version of the given script
 * @param script The script file// w  ww. j ava2  s  .c  om
 * @return CompiledScript
 * @throws ScriptException
 * @throws IOException 
 */
protected static CompiledScript getCompiledScript(Resource script) throws ScriptException, IOException {
    String filename = script.getFilename();
    if (!compiledScripts.containsKey(filename)) {
        javax.script.ScriptEngine engine = getEngine(script);
        if (engine instanceof Compilable) {
            Compilable compilable = (Compilable) engine;
            CompiledScript compiledScript = compilable.compile(new InputStreamReader(script.getInputStream()));
            compiledScripts.put(filename, compiledScript);
        }
    }
    return compiledScripts.get(filename);
}

From source file:org.openmrs.module.pihmalawi.activator.ReportInitializer.java

public static void loadSqlReports() {
    PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    try {//from   ww  w  .  j  a va 2  s . c om
        Resource[] resources = resourceResolver
                .getResources("classpath*:/org/openmrs/module/pihmalawi/reporting/reports/sql/*");
        if (resources != null) {
            for (Resource r : resources) {
                log.info("Loading " + r.getFilename());
                List<String> lineByLineContents = IOUtils.readLines(r.getInputStream(), "UTF-8");

                ReportDefinition rd = new ReportDefinition();
                String designUuid = null;
                StringBuilder sql = new StringBuilder();

                for (String line : lineByLineContents) {
                    if (line.startsWith("-- ##")) {
                        String[] keyValue = StringUtils.splitByWholeSeparator(line.substring(5, line.length()),
                                "=");
                        String key = keyValue[0].trim().toLowerCase();
                        String value = keyValue[1].trim();
                        if (key.equals("report_uuid")) {
                            rd.setUuid(value);
                        } else if (key.equals("report_name")) {
                            rd.setName(value);
                        } else if (key.equals("report_description")) {
                            rd.setDescription(value);
                        } else if (key.equals("parameter")) {
                            String[] paramElements = StringUtils.splitByWholeSeparator(value, "|");
                            Parameter p = new Parameter();
                            p.setName(paramElements[0]);
                            p.setLabel(paramElements[1]);
                            p.setType(Context.loadClass(paramElements[2]));
                            rd.addParameter(p);
                        } else if (key.equals("design_uuid")) {
                            designUuid = value;
                        }
                    }
                    sql.append(line).append(System.getProperty("line.separator"));
                }

                if (rd.getUuid() == null || rd.getName() == null || designUuid == null) {
                    throw new IllegalArgumentException("SQL resource" + r.getFilename()
                            + " must define a report_name, report_uuid and design_uuid at minimum");
                }

                MysqlCmdDataSetDefinition dsd = new MysqlCmdDataSetDefinition();
                dsd.setSql(sql.toString());
                dsd.setParameters(rd.getParameters());

                rd.addDataSetDefinition(r.getFilename(), Mapped.mapStraightThrough(dsd));

                List<ReportDesign> designs = new ArrayList<ReportDesign>();
                designs.add(ApzuReportUtil.createExcelDesign(designUuid, rd));

                ReportManagerUtil.setupReportDefinition(rd, designs, null);
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Unable to load SQL reports from classpath", e);
    }
}

From source file:com.github.mjeanroy.springmvc.view.mustache.commons.IOUtils.java

private static InputStream getInputStreamWithResolver(ResourcePatternResolver resolver, String name) {
    try {//from  w  ww . j  ava2  s  . c  o m
        Resource[] resources = resolver.getResources(name);

        // Not Found
        if (resources.length == 0) {
            log.debug("Found zero results with pattern: {}", name);
            return null;
        }

        log.debug("Found {} results with pattern: {}", resources.length, name);

        if (log.isTraceEnabled()) {
            for (Resource resource : resources) {
                log.trace("  -> Found: {}", resource.getFilename());
            }
        }

        return resources[0].getInputStream();
    } catch (IOException ex) {
        throw new MustacheIOException("I/O Error with " + name, ex);
    }
}

From source file:org.sakaiproject.genericdao.springutil.ResourceFinder.java

/**
 * Resolves a list of paths into resources within the current classloader
 * @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
 * @return an array of File objects//from  w  ww  .j  a  va 2 s.c  o  m
 */
public static File[] getFiles(List<String> paths) {
    List<Resource> rs = makeResources(paths);
    File[] files = new File[rs.size()];
    for (int i = 0; i < rs.size(); i++) {
        Resource r = rs.get(i);
        try {
            files[i] = r.getFile();
        } catch (IOException e) {
            throw new RuntimeException("Failed to get file for: " + r.getFilename(), e);
        }
    }
    return files;
}

From source file:org.sakaiproject.genericdao.springutil.ResourceFinder.java

/**
 * Resolves a list of paths into resources within the current classloader
 * @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
 * @return an array of InputStreams/*from  w ww .  j a va2  s .  co  m*/
 */
public static InputStream[] getInputStreams(List<String> paths) {
    List<Resource> rs = makeResources(paths);
    InputStream[] streams = new InputStream[rs.size()];
    for (int i = 0; i < rs.size(); i++) {
        Resource r = rs.get(i);
        try {
            streams[i] = r.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException("Failed to get inputstream for: " + r.getFilename(), e);
        }
    }
    return streams;
}

From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java

/**
 * Fill the given properties from the given resource (in ISO-8859-1 encoding).
 * @param props the Properties instance to fill
 * @param resource the resource to load from
 * @throws IOException if loading failed
 *///from  w ww.j  av a 2s .co  m
public static void fillProperties(Properties props, Resource resource) throws IOException {
    InputStream is = resource.getInputStream();
    try {
        String filename = resource.getFilename();
        if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
            props.loadFromXML(is);
        } else {
            props.load(is);
        }
    } finally {
        is.close();
    }
}