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.mongeez.reader.FilesetXMLReader.java

public List<Resource> getFiles(Resource file) {
    List<Resource> files = new ArrayList<Resource>();

    try {//  ww w  .j  a  v a  2 s  .  co  m
        Digester digester = new Digester();

        digester.setValidating(false);

        digester.addObjectCreate("changeFiles", ChangeFileSet.class);
        digester.addObjectCreate("changeFiles/file", ChangeFile.class);
        digester.addSetProperties("changeFiles/file");
        digester.addSetNext("changeFiles/file", "add");

        logger.info("Parsing XML Fileset file {}", file.getFilename());
        ChangeFileSet changeFileSet = (ChangeFileSet) digester.parse(file.getInputStream());
        if (changeFileSet != null) {
            logger.info("Num of changefiles found " + changeFileSet.getChangeFiles().size());
            for (ChangeFile changeFile : changeFileSet.getChangeFiles()) {
                files.add(file.createRelative(changeFile.getPath()));
            }
        } else {
            logger.error("The file {} doesn't seem to contain a changeFiles declaration. Are you "
                    + "using the correct file to initialize Mongeez?", file.getFilename());
        }
    } catch (IOException e) {
        logger.error("IOException", e);
    } catch (org.xml.sax.SAXException e) {
        logger.error("SAXException", e);
    }
    return files;
}

From source file:org.ojbc.bundles.adapters.staticmock.ClasspathXmlDataSource.java

private void loadDocumentMap() throws ParserConfigurationException, IOException, SAXException {
    if (documents.isEmpty()) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);//from   www .j  av a2 s . co  m
        DocumentBuilder db = dbf.newDocumentBuilder();

        Bundle bundle = FrameworkUtil.getBundle(getClass());

        PathMatchingResourcePatternResolver resolver = null;
        if (bundle != null) {
            LOG.info("ClasspathXmlDataSource running in OSGi context, bundle=" + bundle.getSymbolicName());
            resolver = new OsgiBundleResourcePatternResolver(bundle);
        } else {
            LOG.info("ClasspathXmlDataSource running in non-OSGi context");
            resolver = new PathMatchingResourcePatternResolver();
        }
        Resource[] resources = resolver.getResources("classpath:" + directory + "/*.xml");
        LOG.info("Loaded " + resources.length + " instance files from " + directory);

        for (Resource resource : resources) {
            Document d = db.parse(resource.getInputStream());
            String filename = resource.getFilename();
            documents.put(filename, new IdentifiableDocumentWrapper(d, filename));
        }
    }
}

From source file:org.olat.core.util.FileUtils.java

public static String load(Resource source, String encoding) {
    try {//from   w ww . j  av  a 2  s  .c  om
        return load(source.getInputStream(), encoding);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("File not found: " + source.getFilename());
    } catch (IOException e) {
        throw new RuntimeException("File not found: " + source.getFilename());
    }
}

From source file:org.openehealth.ipf.commons.map.config.CustomMappings.java

public void setMappingScripts(Collection<Resource> mappingScripts) {
    this.mappingScripts = new ArrayList<Resource>(mappingScripts.size());
    for (Resource mappingScript : mappingScripts) {
        if (mappingScript.exists() && mappingScript.isReadable()) {
            this.mappingScripts.add(mappingScript);
        } else {//  ww  w.  ja v a2  s .c  o  m
            LOG.warn("Could not read mapping script " + mappingScript.getFilename());
        }
    }
    this.mappingScripts = mappingScripts;
}

From source file:org.openehealth.ipf.commons.map.config.CustomMappings.java

public void setMappingScript(Resource mappingScript) {
    if (mappingScript.exists() && mappingScript.isReadable()) {
        this.mappingScript = mappingScript;
    } else {//from   w w w . j a  v  a  2  s  .c  o  m
        LOG.warn("Could not read mapping script " + mappingScript.getFilename());
    }
}

From source file:org.owasp.dependencytrack.listener.DefaultObjectGenerator.java

/**
 * Loads the default licenses into the database if no license data exists. @throws IOException An exception if the license file cannot be found
 *//* ww w  .j av a2s  .  c om*/
private void loadDefaultLicenses() throws IOException {
    if (getCount(session, License.class) > 0) {
        return;
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Adding default licenses to datastore.");
    }
    session.beginTransaction();
    for (Map.Entry<String, String> entry : LICENSES.entrySet()) {
        final String licenseName = entry.getKey();
        final String licenseFile = entry.getValue();
        final String contentType = (licenseFile.endsWith(".html")) ? "text/html" : "text/plain";
        final License license = new License();
        license.setLicensename(licenseName);
        InputStream inputStream = null;
        Resource resource;
        try {
            resource = new ClassPathResource(licenseFile);
            license.setFilename(resource.getFilename());
            license.setContenttype(contentType);

            inputStream = resource.getInputStream();

            String licenceFileContent = new String(IOUtils.toCharArray(inputStream));
            final Blob blob = Hibernate.getLobCreator(session).createBlob(licenceFileContent.getBytes());

            license.setText(blob);
            session.save(license);
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Added: " + licenseName);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }
    session.getTransaction().commit();
}

From source file:org.pentaho.platform.web.http.context.PentahoSolutionSpringApplicationContextTest.java

@Test
public void testGetResourceByPath() throws Exception {
    appContext.setServletContext(context);
    File tempFile = File.createTempFile("PentahoSolutionSpringApplicationContextTest", ".tmp");
    tempFile.deleteOnExit();/*from www  .  ja v  a  2 s  .  com*/

    when(context.getRealPath("")).thenReturn(tempFile.getParent());

    Resource resourceByPath = appContext.getResourceByPath(tempFile.getName());
    assertNotNull(resourceByPath);
    assertEquals(tempFile.getName(), resourceByPath.getFilename());

}

From source file:org.phoenicis.repository.types.ClasspathRepository.java

@Override
public RepositoryDTO fetchInstallableApplications() {
    try {//from w  w  w.  j  av a 2 s .  c o  m
        final List<TypeDTO> typeDTOS = new ArrayList<>();
        Resource[] resources = resourceResolver.getResources(packagePath + "/*");
        for (Resource resource : resources) {
            final TypeDTO type = buildType(resource.getFilename());
            if (!type.getCategories().isEmpty()) {
                typeDTOS.add(type);
            }
        }
        typeDTOS.sort(Comparator.comparing(TypeDTO::getName));

        final RepositoryDTO.Builder repositoryDTOBuilder = new RepositoryDTO.Builder()
                .withName("classpath repository").withTypes(typeDTOS);
        return repositoryDTOBuilder.build();
    } catch (IOException e) {
        LOGGER.warn("Error while reading resource directory", e);
        return new RepositoryDTO.Builder().build();
    }
}

From source file:org.phoenicis.repository.types.ClasspathRepository.java

private List<CategoryDTO> buildCategories(String typeId, String typeFileName) throws RepositoryException {
    try {/*w  w w  . j ava  2  s .  co  m*/
        final String categoryScanClassPath = packagePath + "/" + typeFileName;
        Resource[] resources = resourceResolver.getResources(categoryScanClassPath + "/*");
        final List<CategoryDTO> categoryDTOS = new ArrayList<>();

        for (Resource resource : resources) {
            final String fileName = resource.getFilename();
            if (!"icon.png".equals(fileName) && !"category.json".equals(fileName)) {
                final CategoryDTO category = buildCategory(typeId, typeFileName, fileName);
                if (!category.getApplications().isEmpty()) {
                    categoryDTOS.add(category);
                }
            }
        }

        categoryDTOS.sort(Comparator.comparing(CategoryDTO::getName));
        return categoryDTOS;
    } catch (IOException e) {
        throw new RepositoryException("Could not build categories", e);
    }
}

From source file:org.phoenicis.repository.types.ClasspathRepository.java

private List<ApplicationDTO> buildApplications(String typeId, String categoryId, String typeFileName,
        String categoryFileName) throws RepositoryException {
    try {/*from  ww  w .  j a  v  a 2s. c om*/
        final String categoryScanClassPath = packagePath + "/" + typeFileName + "/" + categoryFileName;
        Resource[] resources = resourceResolver.getResources(categoryScanClassPath + "/*");
        final List<ApplicationDTO> applicationDTOS = new ArrayList<>();

        for (Resource resource : resources) {
            final String fileName = resource.getFilename();
            if (!"icon.png".equals(fileName) && !"category.json".equals(fileName)) {
                final ApplicationDTO application = buildApplication(typeId, categoryId, typeFileName,
                        categoryFileName, fileName);
                if (!application.getScripts().isEmpty()) {
                    applicationDTOS.add(application);
                }
            }
        }

        applicationDTOS.sort(Comparator.comparing(ApplicationDTO::getName));
        return applicationDTOS;
    } catch (IOException e) {
        throw new RepositoryException("Could not build applications", e);
    }
}