List of usage examples for org.apache.commons.io FilenameUtils isExtension
public static boolean isExtension(String filename, Collection<String> extensions)
From source file:org.wso2.carbon.event.simulator.core.internal.generator.csv.util.FileUploader.java
/** * retrieveFileNameList() is used to retrieve the names of CSV files already uploaded * * @param extension type of files retrieved * @param directoryLocation the directory where the files reside * @throws FileOperationsException if an IO error occurs while reading file names *//* w ww. j ava 2 s . c om*/ public List<String> retrieveFileNameList(String extension, Path directoryLocation) throws FileOperationsException { try { List<File> filesInFolder = Files.walk(directoryLocation).filter(Files::isRegularFile) .filter(file -> FilenameUtils.isExtension(file.toString(), extension)).map(Path::toFile) .collect(Collectors.toList()); if (log.isDebugEnabled()) { log.debug("Retrieved files with extension " + extension + "."); } List<String> fileNameList = new ArrayList<>(); for (File file : filesInFolder) { fileNameList.add(file.getName()); } return fileNameList; } catch (IOException e) { log.error("Error occurred when retrieving '" + extension + "' file names list"); throw new FileOperationsException("Error occurred when retrieving '" + extension + "' file names list"); } }
From source file:org.wso2.carbon.event.simulator.core.service.CSVFileDeployer.java
/** * deployCSVFile() is used to deploy csv files copied to directory 'csv-files' * * @param file file copied to directory/*from w ww . ja v a2s . c om*/ */ private void deployCSVFile(File file) throws Exception { String fileName = file.getName(); if (!fileName.startsWith(".") && !FilenameUtils.isExtension(fileName, EventSimulatorConstants.TEMP_FILE_EXTENSION)) { if (FilenameUtils.isExtension(fileName, EventSimulatorConstants.CSV_FILE_EXTENSION)) { FileStore.getFileStore().addFile(fileName); log.info("Deployed CSV file '" + fileName + "'."); } else { throw new CSVFileDeploymentException("Error: File extension not supported for file name " + file.getName() + ". Support only ." + EventSimulatorConstants.CSV_FILE_EXTENSION + " ."); } } }
From source file:org.wso2.carbon.event.simulator.core.service.SimulationConfigDeployer.java
/** * deployConfigFile() is used to deploy a simulation configuration added to directory 'simulation-configs' * * @param file simulation config file added *//* www . j ava2 s .co m*/ private void deployConfigFile(File file) throws Exception { if (!file.getName().startsWith(".")) { if (FilenameUtils.isExtension(file.getName(), EventSimulatorConstants.SIMULATION_FILE_EXTENSION)) { String simulationName = FilenameUtils.getBaseName(file.getName()); try { String simulationConfig = SimulationConfigUploader.getConfigUploader().getSimulationConfig( simulationName, (Paths.get(Utils.getRuntimePath().toString(), EventSimulatorConstants.DIRECTORY_DEPLOYMENT, EventSimulatorConstants.DIRECTORY_SIMULATION_CONFIGS)).toString()); if (!simulationConfig.isEmpty()) { EventSimulator eventSimulator = new EventSimulator(simulationName, simulationConfig); EventSimulatorMap.getInstance().getActiveSimulatorMap().put(simulationName, new ActiveSimulatorData(eventSimulator, simulationConfig)); log.info("Deployed active simulation '" + simulationName + "'."); } } catch (ResourceNotFoundException e) { EventSimulatorMap eventSimulatorMap = EventSimulatorMap.getInstance(); ResourceDependencyData resourceDependencyData = eventSimulatorMap.getInActiveSimulatorMap() .get(simulationName); ResourceDependencyData newResourceDependency = new ResourceDependencyData(e.getResourceType(), e.getResourceName()); if (resourceDependencyData != null) { if (!resourceDependencyData.equals(newResourceDependency)) { eventSimulatorMap.getInActiveSimulatorMap().put(simulationName, newResourceDependency); log.error(e.getMessage(), e); log.info("Updated inactive simulation '" + simulationName + "'."); } } else { eventSimulatorMap.getInActiveSimulatorMap().put(simulationName, newResourceDependency); log.error(e.getMessage(), e); log.info("Deployed inactive simulation '" + simulationName + "'."); } } } else { throw new SimulationConfigDeploymentException("Simulation '" + file.getName() + "' has an invalid " + "content type. File type supported is '." + EventSimulatorConstants.SIMULATION_FILE_EXTENSION + "'."); } } }
From source file:org.wso2.carbon.siddhi.editor.core.internal.WorkspaceDeployer.java
private void deployConfigFile(File file) throws Exception { if (!file.getName().startsWith(".")) { InputStream inputStream = null; try {//from ww w. ja v a 2 s . c o m if (FilenameUtils.isExtension(file.getName(), FILE_EXTENSION)) { String siddhiAppName = FilenameUtils.getBaseName(file.getName()); inputStream = new FileInputStream(file); String siddhiApp = getStringFromInputStream(inputStream); EditorDataHolder.getDebugProcessorService().deploy(siddhiAppName, siddhiApp); log.info("Siddhi App " + siddhiAppName + " successfully deployed."); } else { throw new SiddhiAppDeploymentException(("Error: File extension not supported for file name " + file.getName() + ". Support only" + FILE_EXTENSION + " .")); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { throw new SiddhiAppDeploymentException("Error when closing the Siddhi QL filestream", e); } } } } }
From source file:org.yamj.core.tools.xml.DOMHelper.java
/** * Get a DOM document from the supplied file * * @param xmlFile/* w w w. j a v a 2 s. c o m*/ * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static Document getDocFromFile(File xmlFile) throws ParserConfigurationException, SAXException, IOException { URL url = xmlFile.toURI().toURL(); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // Custom error handler db.setErrorHandler(new SaxErrorHandler()); Document doc; try (InputStream in = url.openStream()) { doc = db.parse(in); } catch (SAXParseException ex) { if (FilenameUtils.isExtension(xmlFile.getName().toLowerCase(), "xml")) { throw new SAXParseException("Failed to process file as XML", null, ex); } // Try processing the file a different way doc = null; } if (doc == null) { // try wrapping the file in a root try (StringReader sr = new StringReader(wrapInXml(FileTools.readFileToString(xmlFile)))) { doc = db.parse(new InputSource(sr)); } } if (doc != null) { doc.getDocumentElement().normalize(); } return doc; }
From source file:Records.Attributes.FileTraverser.java
/** * Walk tree./*from w w w . ja v a2s . c om*/ * * @param topFile * the top file * @param fileList * the file list */ private static void walkTree(File topFile, List<File> fileList) { System.out.println("FileTraverser.walkTree()"); if (topFile.isDirectory()) { for (File f : topFile.listFiles()) { walkTree(f, fileList); } } else if (topFile.isFile()) { if (FilenameUtils.isExtension(topFile.getAbsolutePath(), "m4a") || FilenameUtils.isExtension(topFile.getAbsolutePath(), "mp3")) { fileList.add(topFile); RecordsMain.addToFilePathList(topFile.getAbsolutePath()); } } }
From source file:Records.Database.LibraryUpdater.java
/** * Adds the file.//from w w w .j a va 2 s.c o m * * @param filePath * the file path */ private static void addFile(String filePath) { System.out.println("LibraryUpdater.addFile(" + filePath + ")"); LibraryMetadataExtractor metadata; if (FilenameUtils.isExtension(filePath, "m4a") || FilenameUtils.isExtension(filePath, "mp3")) { metadata = new LibraryMetadataExtractor(new File(filePath)); LibraryDataSets LDS = new LibraryDataSets(metadata.extract()); LDS.insertLibraryData(); RecordsMain.clearFilePathList(); ResultSet res = RecordsMain.dba.getAllFilePaths(); try { while (res.next()) { String fPath = res.getString("Filepath"); RecordsMain.addToFilePathList(fPath); System.out.println("Adding file to list: " + fPath); } } catch (SQLException ex) { Logger.getLogger(DBSQL.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:ren.hankai.cordwood.core.Preferences.java
/** * ???//from w ww . j a v a 2 s .c o m * * @param extraUrls ?? URL ? URL ? * @return ? URL ? * @author hankai * @since Oct 18, 2016 3:09:42 PM */ public static URL[] getLibUrls(URL... extraUrls) { final List<URL> list = new ArrayList<>(); if ((extraUrls != null) && (extraUrls.length > 0)) { list.addAll(Arrays.asList(extraUrls)); } final File file = new File(getLibsDir()); final File[] files = file.listFiles(); if (files != null) { for (final File libFile : files) { if (FilenameUtils.isExtension(libFile.getName(), "jar")) { try { list.add(libFile.toURI().toURL()); } catch (final MalformedURLException ex) { throw new RuntimeException( String.format("Failed to get url from lib path: %s", libFile.getAbsolutePath()), ex); } } } } if (list.size() > 0) { final URL[] urls = new URL[list.size()]; list.toArray(urls); return urls; } return null; }
From source file:ryerson.daspub.utility.ImageUtils.java
/** * Convert input file, resize and write JPG output image. * @param Input Input file//from w ww . j av a 2 s . c om * @param Output Output file or folder * @param Width Maximum width * @param Height Maximum height * @throws IOException * @throws PdfException * @TODO there is a problem here when resizing * @TODO check the original image size and don't go any higher than the original */ public static void writeJPGImage(File Input, File Output, int Width, int Height) throws IOException, ImageReadException, org.jpedal.exception.PdfException { // if output is folder, create a new file object File output = Output; if (output.isDirectory()) { output = new File(Output, Input.getName()); } // write image if (FilenameUtils.isExtension(Input.getName(), "jpg")) { Thumbnails.of(Input).outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC).size(Width, Height) .toFile(output); logger.log(Level.FINE, "Wrote image {0}", output.getAbsolutePath()); } else if (FilenameUtils.isExtension(Input.getName(), "gif") || FilenameUtils.isExtension(Input.getName(), "png")) { Thumbnails.of(Input).outputFormat("jpg").outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC) .size(Width, Height).toFile(output); logger.log(Level.FINE, "Wrote image {0}", output.getAbsolutePath()); } else if (FilenameUtils.isExtension(Input.getName(), "tif")) { BufferedImage image = Sanselan.getBufferedImage(Input); Thumbnails.of(image).outputFormat("jpg").outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC) .size(Width, Height).toFile(output); } else { logger.log(Level.WARNING, "Could not write JPG for {0}. File is not a processable image.", output.getAbsolutePath()); } }
From source file:ryerson.daspub.utility.PDFUtils.java
/** * Write JPG image of page in a PDF document. If the document has multiple * pages, only the first page image will be written. * @param Input PDF input file/*w w w .j a v a 2s . c o m*/ * @param Output Output file * @param Width Maximum thumbnail width * @param Height Maximum thumbnail height * @throws IOException * @throws PDFException */ public static List<File> writeJPGImage(File Input, File Output, int Width, int Height) throws PdfException, IOException { ArrayList<File> files = new ArrayList<File>(); if (FilenameUtils.isExtension(Input.getName(), "pdf")) { // if output is a directory, change it File output = Output; if (output.isDirectory()) { output = new File(output, Input.getName()); } // if output extension is not jpg, change it if (!FilenameUtils.isExtension(output.getName(), "jpg")) { String basename = FilenameUtils.getBaseName(output.getName()) + ".jpg"; output = new File(output.getParentFile(), basename); } // @TODO configure JPedal to improve output settings // exception will be thrown if invalid value passed // Map mapValues = new HashMap(); // mapValues.put(JPedalSettings.IMAGE_HIRES,Boolean.TRUE); // write jpg image pdf.openPdfFile(Input.getAbsolutePath()); BufferedImage img = pdf.getPageAsImage(1); logger.log(Level.INFO, "Writing JPG image \"{0}\"", output.getAbsolutePath()); Thumbnails.of(img).outputFormat("jpg").outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC) .size(Width, Height).toFile(output); files.add(output); pdf.closePdfFile(); } else { logger.log(Level.WARNING, "Could not write PDF thumbnail for {0}. File is not a PDF document.", Input.getAbsolutePath()); } // return result return files; }