List of usage examples for org.apache.commons.vfs FileObject getName
public FileName getName();
From source file:org.vivoweb.harvester.util.FileAide.java
/** * Deletes the file at the given path/*from ww w . j a v a 2 s .c o m*/ * @param path the path to delete * @return true if deleted, false otherwise * @throws IOException error resolving path */ public static boolean delete(String path) throws IOException { FileObject file = getFileObject(path); if (!file.exists()) { return true; } if (isFolder(path)) { try { for (FileObject subFile : file.findFiles(new AllFileSelector())) { try { String subPath = subFile.getName().getPath(); if (!subPath.equals(path)) { delete(subPath); } } catch (FileSystemException e) { //log.trace(e.getMessage(), e); } } file.delete(new AllFileSelector()); } catch (FileSystemException e) { //log.trace(e.getMessage(), e); throw new IOException("Error deleting file!"); } } return file.delete(); }
From source file:org.vivoweb.harvester.util.FileAide.java
/** * Get a set of non-hidden direct children of the given path * @param path the path to search under//from w w w . j a va 2 s. c om * @return a set of non-hidden direct children * @throws IOException error resolving path */ public static Set<String> getNonHiddenChildren(String path) throws IOException { Set<String> allFileListing = new HashSet<String>(); for (FileObject file : getFileObject(path).findFiles(Selectors.SELECT_CHILDREN)) { if (!file.isHidden() && (file.getType() == FileType.FILE)) { allFileListing.add(file.getName().getBaseName()); } } return allFileListing; }
From source file:org.vivoweb.harvester.util.FileAide.java
/** * Get an inputstream from the first file under the given path with a matching fileName * @param path the path to search under/*from w w w .jav a2s . c o m*/ * @param fileName the file name to match * @return an input stream to the matched file, null if none found * @throws IOException error resolving path */ public static InputStream getFirstFileNameChildInputStream(String path, String fileName) throws IOException { for (FileObject file : FileAide.getFileObject(path).findFiles(new AllFileSelector())) { if (file.getName().getBaseName().equals(fileName)) { return file.getContent().getInputStream(); } } return null; }
From source file:plugin.games.data.trans.step.fileoutput.TextFileOutput.java
private void createParentFolder(String filename) throws Exception { // Check for parent folder FileObject parentfolder = null; try {/*w w w. j a va 2s.c om*/ // Get parent folder parentfolder = KettleVFS.getFileObject(filename).getParent(); if (parentfolder.exists()) { if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderExist", parentfolder.getName())); } else { if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderNotExist", parentfolder.getName())); if (meta.isCreateParentFolder()) { parentfolder.createFolder(); if (isDetailed()) logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderCreated", parentfolder.getName())); } else { throw new KettleException(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderNotExistCreateIt", parentfolder.getName(), filename)); } } } finally { if (parentfolder != null) { try { parentfolder.close(); } catch (Exception ex) { } ; } } }
From source file:pt.webdetails.di.baserver.utils.repositoryPlugin.ui.ToolbarController.java
public void openFileFromPentahoRepository() { Spoon spoon = this.getSpoon(); VfsDialogFileInfo fileInfo = this.getLastOpenedFile(); VfsFileChooserDialog vfsFileChooserDialog = spoon.getVfsFileChooserDialog(fileInfo.getRootFile(), fileInfo.getInitialFile());/*from w ww. j av a 2s . c o m*/ FileObject selectedFile = vfsFileChooserDialog.open(spoon.getShell(), null, this.getConstants().getVfsScheme(), true, null, Const.STRING_TRANS_AND_JOB_FILTER_EXT, Const.getTransformationAndJobFilterNames(), VfsFileChooserDialog.VFS_DIALOG_OPEN_FILE); if (selectedFile != null) { String uri = selectedFile.getName().getFriendlyURI(); spoon.setLastFileOpened(uri); spoon.openFile(uri, false); } }
From source file:pt.webdetails.di.baserver.utils.repositoryPlugin.ui.ToolbarController.java
private boolean saveXMLFileToVfs(EngineMetaInterface meta) { Spoon spoon = this.getSpoon(); LogChannelInterface spoonLog = spoon.getLog(); Shell spoonShell = spoon.getShell(); if (spoonLog.isBasic()) { spoonLog.logBasic("Save file as..."); }// w w w.j av a 2s . c o m VfsDialogFileInfo fileInfo = this.getLastOpenedFile(); FileObject rootFile = fileInfo.getRootFile(); FileObject initialFile = fileInfo.getInitialFile(); if (rootFile == null || initialFile == null) { return false; } String filename = null; FileObject selectedFile = spoon.getVfsFileChooserDialog(rootFile, initialFile).open(spoonShell, null, this.getConstants().getVfsScheme(), true, "Untitled", Const.STRING_TRANS_AND_JOB_FILTER_EXT, Const.getTransformationAndJobFilterNames(), VfsFileChooserDialog.VFS_DIALOG_SAVEAS); if (selectedFile != null) { filename = selectedFile.getName().getFriendlyURI(); } if (filename != null) { Collection<String> extensionMasks = new ArrayList<String>(); for (String composedExtension : meta.getFilterExtensions()) { // extension given by meta.getFilterExtensions may contain elements // with more than one extension mask separated by ';'. E.g. "*.ktr;*.kjb" String[] extensions = composedExtension.split(";"); for (String simpleExtension : extensions) { extensionMasks.add(simpleExtension); } } filename = this.addDefaultExtensionIfMissing(filename, extensionMasks, meta.getDefaultExtension()); // See if the file already exists... boolean overrideFile = true; try { FileObject f = KettleVFS.getFileObject(filename); if (f.exists()) { overrideFile = this.promptShouldOverrideFile(); } } catch (Exception e) { // TODO do we want to show an error dialog here? My first guess // is not, but we might. } if (overrideFile) { spoon.save(meta, filename, false); } } return false; }
From source file:r.base.Files.java
/** * Utility function to extract information about files on the user's file systems. * * @param context current call Context/* w ww . j a va 2 s . co m*/ * @param paths the list of files for which to return information * @return list column-oriented table of file information * @throws FileSystemException */ @Primitive("file.info") public static ListVector fileInfo(@Current Context context, StringVector paths) throws FileSystemException { DoubleVector.Builder size = new DoubleVector.Builder(); LogicalVector.Builder isdir = new LogicalVector.Builder(); IntVector.Builder mode = (IntVector.Builder) new IntVector.Builder().setAttribute(Symbols.CLASS, new StringVector("octmode")); DoubleVector.Builder mtime = new DoubleVector.Builder(); StringVector.Builder exe = new StringVector.Builder(); for (String path : paths) { FileObject file = context.resolveFile(path); if (file.exists()) { if (file.getType() == FileType.FILE) { size.add((int) file.getContent().getSize()); } else { size.add(0); } isdir.add(file.getType() == FileType.FOLDER); mode.add(mode(file)); try { mtime.add(file.getContent().getLastModifiedTime()); } catch (Exception e) { mtime.add(0); } exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no"); } else { size.add(IntVector.NA); isdir.add(IntVector.NA); mode.add(IntVector.NA); mtime.add(DoubleVector.NA); exe.add(StringVector.NA); } } return ListVector.newNamedBuilder().add("size", size).add("isdir", isdir).add("mode", mode) .add("mtime", mtime).add("ctime", mtime).add("atime", mtime).add("exe", exe).build(); }
From source file:r.base.Files.java
/** * {@code list.files} produce a character vector of the names of files in the named directory. * * @param paths a character vector of full path names; the default corresponds to the working * directory getwd(). Missing values will be ignored. * @param pattern an optional regular expression. Only file names which match the regular * expression will be returned./*from ww w .j a v a2s. co m*/ * @param allFiles If FALSE, only the names of visible files are returned. If TRUE, all * file names will be returned. * @param fullNames If TRUE, the directory path is prepended to the file names. If FALSE, * only the file names are returned. * @param recursive Should the listing recurse into directories? * @param ignoreCase Should pattern-matching be case-insensitive? * * If a path does not exist or is not a directory or is unreadable it is skipped, with a warning. * The files are sorted in alphabetical order, on the full path if full.names = TRUE. Directories are included only if recursive = FALSE. * * @return */ @Primitive("list.files") public static StringVector listFiles(@Current final Context context, final StringVector paths, final String pattern, final boolean allFiles, final boolean fullNames, boolean recursive, final boolean ignoreCase) throws IOException { return new Object() { private final StringVector.Builder result = new StringVector.Builder(); private final RE filter = pattern == null ? null : new ExtendedRE(pattern).ignoreCase(ignoreCase); public StringVector list() throws IOException { for (String path : paths) { FileObject folder = context.resolveFile(path); if (folder.getType() == FileType.FOLDER) { if (allFiles) { add(folder, "."); add(folder, ".."); } for (FileObject child : folder.getChildren()) { if (filter(child)) { add(child); } } } } return result.build(); } void add(FileObject file) { if (fullNames) { result.add(file.getName().getURI()); } else { result.add(file.getName().getBaseName()); } } void add(FileObject folder, String name) throws FileSystemException { if (fullNames) { result.add(folder.resolveFile(name).getName().getURI()); } else { result.add(name); } } boolean filter(FileObject child) throws FileSystemException { if (!allFiles && isHidden(child)) { return false; } if (filter != null && !filter.match(child.getName().getBaseName())) { return false; } return true; } private boolean isHidden(FileObject file) throws FileSystemException { return file.isHidden() || file.getName().getBaseName().startsWith("."); } }.list(); }
From source file:r.base.Files.java
/** * Helper function to extract a zip entry to the given folder. *///from w w w .jav a 2 s .com private static void unzipExtract(ZipInputStream zin, ZipEntry entry, FileObject exdir, boolean junkpaths, boolean overwrite) throws IOException { if (junkpaths) { throw new EvalException("unzip(junpaths=false) not yet implemented"); } FileObject exfile = exdir.resolveFile(entry.getName()); if (exfile.exists() && !overwrite) { throw new EvalException("file to be extracted '%s' already exists", exfile.getName().getURI()); } OutputStream out = exfile.getContent().getOutputStream(); try { byte buffer[] = new byte[64 * 1024]; int bytesRead; while ((bytesRead = zin.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { out.close(); } }
From source file:uk.ac.liv.shaman.vfsme.CommonVFSMediaAdaptor.java
private void processChild(FileObject f, FileSystemManager m, StringBuilder sb) throws FileSystemException, UnsupportedEncodingException { if (f.getType() == FileType.FOLDER || f.getType() == FileType.IMAGINARY) { sb.append("<tr>"); sb.append("<td><b>").append(f.getName()).append("</b></td>"); sb.append(/* ww w. j a v a 2 s . com*/ "<td align='right'><span Behavior='ElideSpan'>0</span> --<td align='right'><span Behavior='ElideSpan'>0</span> --"); FileObject[] children = f.getChildren(); for (FileObject subfile : children) processChild(subfile, m, sb); } else { sb.append("<tr>"); FileName fname = f.getName(); sb.append("<td><a href='").append(fname.getURI().replaceAll(" ", "%20")).append("'>").append(fname) .append("</a>"); DateFormat outdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long size = 0; Date last = new Date(); try { size = f.getContent().getSize(); last = new Date(f.getContent().getLastModifiedTime()); } catch (Exception e) { // TODO: handle exception } sb.append("<td align='right'>").append(Long.toString(size)).append("<td align='right'>") .append(outdfm.format(last)); } }