List of usage examples for org.apache.commons.io FilenameUtils getExtension
public static String getExtension(String filename)
From source file:com.pentaho.repository.importexport.PdiContentProvider.java
private NamedParams getMeta(RepositoryFile file) throws KettleException { NamedParams meta = null;/*from w ww . j ava 2s .c om*/ if (file != null) { String extension = FilenameUtils.getExtension(file.getName()); Repository repo = PDIImportUtil.connectToRepository(null); if ("ktr".equalsIgnoreCase(extension)) { meta = new TransMeta(convertTransformation(file.getId()), repo, true, null, null); } else if ("kjb".equalsIgnoreCase(extension)) { meta = new JobMeta(convertJob(file.getId()), repo, null); } } return meta; }
From source file:ca.on.oicr.pde.workflows.GATK3Workflow.java
@Override public Map<String, SqwFile> setupFiles() { List<String> inputFilesList = Arrays.asList(StringUtils.split(getProperty("input_files"), ",")); Set<String> inputFilesSet = new HashSet<>(inputFilesList); if (inputFilesList.size() != inputFilesSet.size()) { throw new RuntimeException("Duplicate files detected in input_files"); }/* w w w . j a v a 2s. c om*/ if ((inputFilesSet.size() % 2) != 0) { throw new RuntimeException("Each bam should have a corresponding index"); } Map<String, String> bams = new HashMap<>(); Map<String, String> bais = new HashMap<>(); for (String f : inputFilesSet) { String fileExtension = FilenameUtils.getExtension(f); String fileKey = FilenameUtils.removeExtension(f); if (null != fileExtension) { switch (fileExtension) { case "bam": bams.put(fileKey, f); break; case "bai": bais.put(fileKey, f); break; default: throw new RuntimeException("Unsupported input file type"); } } } int id = 0; for (Entry<String, String> e : bams.entrySet()) { String key = e.getKey(); String bamFilePath = e.getValue(); String baiFilePath = bais.get(key); if (baiFilePath == null) { throw new RuntimeException("Missing index for " + FilenameUtils.getName(bamFilePath)); } SqwFile bam = this.createFile("file_in_" + id++); bam.setSourcePath(bamFilePath); bam.setType("application/bam"); bam.setIsInput(true); SqwFile bai = this.createFile("file_in_" + id++); bai.setSourcePath(baiFilePath); bai.setType("application/bam-index"); bai.setIsInput(true); //FIXME: this seems to work for now, it would be better to be able to set the provisionedPath as //bai.getProvisionedPath != bai.getOutputPath ... //at least with seqware 1.1.0, setting output path changes where the output file will be stored, //but the commonly used get provisioned path will return the incorrect path to the file bai.setOutputPath(FilenameUtils.getPath(bam.getProvisionedPath())); inputBamFiles.add(bam.getProvisionedPath()); } return this.getFiles(); }
From source file:mj.ocraptor.extraction.tika.parser.epub.EpubParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { // Because an EPub file is often made up of multiple XHTML files, // we need explicit control over the start and end of the document XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument();/*from www.j a v a2 s.c om*/ ContentHandler childHandler = new EmbeddedContentHandler(new BodyContentHandler(xhtml)); ZipInputStream zip = new ZipInputStream(stream); ZipEntry entry = zip.getNextEntry(); TikaImageHelper helper = new TikaImageHelper(metadata); try { while (entry != null) { // TODO: images String entryExtension = null; try { entryExtension = FilenameUtils.getExtension(new File(entry.getName()).getName()); } catch (Exception e) { e.printStackTrace(); } if (entryExtension != null && FileType.isValidImageFileExtension(entryExtension) && Config.inst().getProp(ConfigBool.ENABLE_IMAGE_OCR)) { File imageFile = null; try { imageFile = TikaImageHelper.saveZipEntryToTemp(zip, entry); helper.addImage(imageFile); } catch (Exception e) { e.printStackTrace(); } finally { if (imageFile != null) { imageFile.delete(); } } } else if (entry.getName().equals("mimetype")) { String type = IOUtils.toString(zip, "UTF-8"); metadata.set(Metadata.CONTENT_TYPE, type); } else if (entry.getName().equals("metadata.xml")) { meta.parse(zip, new DefaultHandler(), metadata, context); } else if (entry.getName().endsWith(".opf")) { meta.parse(zip, new DefaultHandler(), metadata, context); } else if (entry.getName().endsWith(".html") || entry.getName().endsWith(".xhtml")) { content.parse(zip, childHandler, metadata, context); } entry = zip.getNextEntry(); } helper.addTextToHandler(xhtml); } catch (Exception e) { e.printStackTrace(); } finally { if (helper != null) { helper.close(); } } // Finish everything xhtml.endDocument(); }
From source file:com.github.cbismuth.fdupes.io.PathOrganizer.java
private void onNoTimestampPath(final Path destination, final PathElement pathElement, final AtomicInteger counter) { final Path path = pathElement.getPath(); final String baseName = FilenameUtils.getBaseName(path.toString()); final int count = counter.getAndIncrement(); final String extension = FilenameUtils.getExtension(path.toString()); final String newName = String.format("%s-%d.%s", baseName, count, extension); final Path sibling = path.resolveSibling(newName); try {//w w w . j av a 2s . co m FileUtils.moveFile(path.toFile(), sibling.toFile()); FileUtils.moveFileToDirectory(sibling.toFile(), Paths.get(destination.toString(), "misc").toFile(), true); } catch (final IOException e) { LOGGER.error(e.getMessage()); } }
From source file:com.iyonger.apm.web.model.FileEntry.java
/** * Get file type. If fileType is set, it returns the set fileType. * Otherwise, it tries to detect the file type by the extension. * * @return file type./* w w w . j a va 2 s . c o m*/ */ public FileType getFileType() { if (fileType == null) { fileType = FileType.getFileTypeByExtension(FilenameUtils.getExtension(getPath())); } return fileType; }
From source file:com.ostrichemulators.jfxhacc.engine.impl.RdfDataEngine.java
/** * Dumps the data to the given file. If the file is null, dumps it in NTriples * format to stdout. If the file isn't null, the extension is used to * determine the format (either nt, ttl, or rdf). If the extension parsing * fails for whatever reason, the dump will be in NTriples format * * @param de/* ww w.ja v a2 s . c om*/ * @param out * @throws IOException */ public void dump(File out) throws RepositoryException, IOException { String ext = (null == out ? "nt" : FilenameUtils.getExtension(out.getName()).toLowerCase()); Map<String, RDFFormat> map = new HashMap<>(); map.put("ttl", RDFFormat.TURTLE); map.put("rdf", RDFFormat.RDFXML); RDFFormat fmt = map.getOrDefault(ext, NTRIPLES); try (Writer w = (null == out ? new PrintWriter(System.out) : new FileWriter(out))) { RDFHandler handler = null; if (RDFFormat.RDFXML == fmt) { handler = new RDFXMLWriter(w); } else if (RDFFormat.TURTLE == fmt) { handler = new TurtleWriter(w); } else { handler = new NTriplesWriter(w); } try { rc.export(handler); } catch (RDFHandlerException re) { log.error(re, re); } } }
From source file:net.morphbank.mbsvc3.mapsheet.MapSpreadsheetToXml.java
public Request createRequestFromFile(String fileName, Credentials submitter, Credentials owner, PrintWriter report, int numLines, int firstLine) { String extension = FilenameUtils.getExtension(fileName); if ("xls".equals(extension)) { XlsFieldMapper xlsFieldMapper = new XlsFieldMapper(fileName); getCredentials(xlsFieldMapper);/*from w w w . ja v a 2 s. c o m*/ this.submitter = getSubmitter(submitter); this.contributor = getContributor(owner); fieldMapper = xlsFieldMapper; fields = new SpreadsheetFields(); } else if ("csv".equals(extension)) { fieldMapper = new TextFieldMapper(fileName); } else { // dwc-Archive folder. filename is the actual folder, not the // zipped archive // MapDwcaToXml mapDwca = new MapDwcaToXml(); // return mapDwca.createRequestFromFile(fileName, null, null, null); } fieldMapper.moveToLine(firstLine - 1); request = new Request(); request.setSubmitter(this.submitter); Insert insert = new Insert(); insert.setSubmitter(this.submitter); request.getInsert().add(insert); Set<String> specimenIds = new HashSet<String>();// keep track of // specimen ids int lineNumber = 0; while (fieldMapper.hasNext()) { int spreadSheetLineNumber = firstLine + lineNumber + 1; fieldMapper.getNextLine(); String origFileName = fieldMapper.getValue("Original File Name"); if (origFileName.length() > 0) { if (fieldMapper.getValue(VIEW_MORPHBANK_ID_FIELD).equalsIgnoreCase("")) { XmlBaseObject xmlView; if (fieldMapper instanceof XlsFieldMapper) xmlView = createXmlView(spreadSheetLineNumber); else xmlView = createXmlViewFromDwca(spreadSheetLineNumber); if (xmlView != null) { xmlView.setOwner(this.contributor); insert.getXmlObjectList().add(xmlView); } } // switched image and specimen order XmlBaseObject xmlImage; if (fieldMapper instanceof XlsFieldMapper) { xmlImage = createXmlImage(spreadSheetLineNumber); } else { xmlImage = createXmlImageFromDwca(spreadSheetLineNumber); } xmlImage.setOwner(this.contributor); insert.getXmlObjectList().add(xmlImage); XmlBaseObject xmlSpecimen; if (fieldMapper instanceof XlsFieldMapper) { xmlSpecimen = createXmlSpecimen(spreadSheetLineNumber); } else { xmlSpecimen = createXmlSpecimenFromDwca(spreadSheetLineNumber); } xmlSpecimen.setOwner(this.contributor); insert.getXmlObjectList().add(xmlSpecimen); } lineNumber++; if (numLines >= 1 && lineNumber >= numLines) break; } if (0 == lineNumber) { // no lines! return null; } System.out.println("Lines: " + firstLine + " to line " + (firstLine + lineNumber - 1) + " processed"); return request; }
From source file:com.googlesource.gerrit.plugins.xdocs.formatter.Formatters.java
public FormatterProvider get(ProjectState project, String fileName) { XDocGlobalConfig globalCfg = new XDocGlobalConfig(pluginCfgFactory.getGlobalPluginConfig(pluginName)); MimeType mimeType = fileTypeRegistry.getMimeType(fileName, null); mimeType = new MimeType( FileContentUtil.resolveContentType(project, fileName, FileMode.FILE, mimeType.toString())); String extension = FilenameUtils.getExtension(fileName); FormatterProvider formatter = null;// ww w. j av a 2s . com int formatterPrio = 0; for (String pluginName : formatters.plugins()) { for (Entry<String, Provider<Formatter>> e : formatters.byPlugin(pluginName).entrySet()) { if (!globalCfg.getFormatterConfig(e.getKey()).getBoolean(KEY_ENABLED, true)) { continue; } ConfigSection formatterCfg = getFormatterConfig(e.getKey(), project); String[] prefixes = formatterCfg.getStringList(KEY_PREFIX); if (prefixes.length > 0) { boolean match = false; for (String prefix : prefixes) { if (fileName.startsWith(prefix)) { match = true; break; } } if (!match) { continue; } } for (String mt : formatterCfg.getStringList(KEY_MIME_TYPE)) { MimeType configuredMimeType = new MimeType(mt); if (mimeType.equals(configuredMimeType) || ("*".equals(configuredMimeType.getSubType()) && mimeType.getMediaType().equals(configuredMimeType.getMediaType()))) { int prio = formatterCfg.getInt(KEY_PRIO, 0); if (formatter == null || prio > formatterPrio) { formatter = new FormatterProvider(e.getKey(), e.getValue()); formatterPrio = prio; } } } for (String ext : formatterCfg.getStringList(KEY_EXT)) { if (extension.equals(ext) || "*".equals(ext)) { int prio = formatterCfg.getInt(KEY_PRIO, 0); if (formatter == null || prio > formatterPrio) { formatter = new FormatterProvider(e.getKey(), e.getValue()); formatterPrio = prio; } } } } } return formatter; }
From source file:com.maskyn.fileeditorpro.adapter.AdapterDetailedList.java
private void setIcon(final ViewHolder viewHolder, final FileDetail fileDetail) { final String fileName = fileDetail.getName(); final String ext = FilenameUtils.getExtension(fileName); if (fileDetail.isFolder()) { viewHolder.icon.setImageResource(R.color.file_folder); } else if (Arrays.asList(MimeTypes.MIME_HTML).contains(ext) || ext.endsWith("html")) { viewHolder.icon.setImageResource(R.color.file_html); } else if (Arrays.asList(MimeTypes.MIME_CODE).contains(ext) || fileName.endsWith("css") || fileName.endsWith("js")) { viewHolder.icon.setImageResource(R.color.file_code); } else if (Arrays.asList(MimeTypes.MIME_ARCHIVE).contains(ext)) { viewHolder.icon.setImageResource(R.color.file_archive); } else if (Arrays.asList(MimeTypes.MIME_MUSIC).contains(ext)) { viewHolder.icon.setImageResource(R.color.file_media_music); } else if (Arrays.asList(MimeTypes.MIME_PICTURE).contains(ext)) { viewHolder.icon.setImageResource(R.color.file_media_picture); } else if (Arrays.asList(MimeTypes.MIME_VIDEO).contains(ext)) { viewHolder.icon.setImageResource(R.color.file_media_video); } else {/*from www. j ava 2s .com*/ viewHolder.icon.setImageResource(R.color.file_text); } }
From source file:com.s2g.pst.resume.importer.PSTFileHelper.java
/** * */* w w w. j a va 2s . co m*/ * * @param email * @throws FileNotFoundException * @throws PSTException * @throws IOException */ public void saveAttachmentFromEmail(PSTMessage email, FilterInfo filterInfo) throws FileNotFoundException, PSTException, IOException, Exception { FileOutputStream out = null; this.uniqueFileName = null; int numberOfAttachments = email.getNumberOfAttachments(); for (int x = 0; x < numberOfAttachments; x++) { PSTAttachment attach = email.getAttachment(x); //System.out.println("file size : " + attach.getAttachSize()); //check file size if ((filterInfo.emailAttachFileSizeFrom == 0 || filterInfo.emailAttachFileSizeFrom <= attach.getAttachSize()) && (filterInfo.emailAttachFileSizeTo == 0 || attach.getAttachSize() <= filterInfo.emailAttachFileSizeTo)) { InputStream attachmentStream = attach.getFileInputStream(); // both long and short filenames can be used for attachments String filename = attach.getLongFilename(); if (filename.isEmpty()) { filename = attach.getFilename(); } FileHelper fileHelper = new FileHelper(); filename = fileHelper.getUniqueFileName(this.emailAttachmentSavePath, filename); String fullFileName = this.emailAttachmentSavePath + filename; out = new FileOutputStream(fullFileName); // 8176 is the block size used internally and should give the best performance int bufferSize = 8176; byte[] buffer = new byte[bufferSize]; int count = attachmentStream.read(buffer); while (count == bufferSize) { out.write(buffer); count = attachmentStream.read(buffer); } byte[] endBuffer = new byte[count]; System.arraycopy(buffer, 0, endBuffer, 0, count); out.write(endBuffer); out.close(); //System.out.println("saved fullFileName : " + fullFileName); attachmentStream.close(); //extract if zip file if ("zip".equals(FilenameUtils.getExtension(filename))) { UnZipHelper unZipHelper = new UnZipHelper(); unZipHelper.unZipFolder(fullFileName, this.emailAttachmentSavePath); } } } }