public void widgetSelected( SelectionEvent e ) { try { FileObject fileName = null; try { String curFile = wFilename.getText(); if ( curFile.trim().length() > 0 ) { fileName = KettleVFS.getInstance().getFileSystemManager().resolveFile( jobMeta.environmentSubstitute( wFilename.getText() ) ); } else { fileName = KettleVFS.getInstance().getFileSystemManager().resolveFile( Const.getUserHomeDirectory() ); } } catch ( FileSystemException ex ) { fileName = KettleVFS.getInstance().getFileSystemManager().resolveFile( Const.getUserHomeDirectory() ); } VfsFileChooserDialog vfsFileChooser = Spoon.getInstance().getVfsFileChooserDialog( fileName.getParent(), fileName ); FileObject selected = vfsFileChooser.open( shell, null, EXTENSIONS, FILETYPES, VfsFileChooserDialog.VFS_DIALOG_OPEN_FILE ); wFilename.setText( selected != null ? selected.getURL().toString() : Const.EMPTY_STRING ); } catch ( FileSystemException ex ) { ex.printStackTrace(); } } } );
String[] schemes = fsManager.getSchemes(); for ( int i = 0; i < schemes.length && relativeFilename; i++ ) { if ( vfsFilename.startsWith( schemes[i] + ":" ) ) { return fsManager.resolveFile( filename, fsOptions ); } else { return fsManager.resolveFile( filename );
@Test @Ignore public void testFileNameWithSpaces() throws URISyntaxException, IOException { final File file = new File("target", "a name.txt"); final String fileURL = file.toURI().toURL().toExternalForm(); assertEquals(file.getAbsoluteFile(), new File(file.toURI().getPath())); assertEquals(file.getAbsoluteFile(), new File(new URL(fileURL).toURI().getPath())); final FileSystemManager manager = VFS.getManager(); final FileObject fo = manager.resolveFile(fileURL); assertEquals(file.getAbsoluteFile(), new File(new URL(fo.getURL().toExternalForm()).toURI().getPath())); }
FileSystemManager fsManager = VFS.getManager(); if (fsManager == null) { throw newError("Cannot get virtual file system manager"); return new URL(url).openStream(); } catch (IOException e) { throw newError( FileObject file = fsManager.resolveFile(userDir, url); FileContent fileContent = null; try { file.refresh(); && !file.getName().getURI().equals(url)) fsManager.getFilesCache().removeFile( file.getFileSystem(), file.getName()); file = fsManager.resolveFile(userDir, url); return fileContent.getInputStream();
@Override public InputStream getInputStream(final URL url) throws ConfigurationException { FileObject file; try { final FileSystemOptions opts = getOptions(url.getProtocol()); file = (opts == null) ? VFS.getManager().resolveFile(url.toString()) : VFS.getManager().resolveFile(url.toString(), opts); if (file.getType() != FileType.FILE) { throw new ConfigurationException("Cannot load a configuration from a directory"); } final FileContent content = file.getContent(); if (content == null) { final String msg = "Cannot access content of " + file.getName().getFriendlyURI(); throw new ConfigurationException(msg); } return content.getInputStream(); } catch (final FileSystemException fse) { final String msg = "Unable to access " + url.toString(); throw new ConfigurationException(msg, fse); } }
final FileSystemManager fsManager = VFS.getManager(); FileObject base = (opts == null) ? fsManager.resolveFile(basePath) : fsManager.resolveFile(basePath, opts); if (base.getType() == FileType.FILE) base = base.getParent(); file = fsManager.resolveFile(base, fileName); file = (opts == null) ? fsManager.resolveFile(fileName) : fsManager.resolveFile(fileName, opts); if (!file.exists()) final FileName path = file.getName(); final URLStreamHandler handler = new VFSURLStreamHandler(path); return new URL(null, path.getURI(), handler);
@Override public URL getURL(final String basePath, final String file) throws MalformedURLException { if ((basePath != null && UriParser.extractScheme(basePath) == null) || (basePath == null && UriParser.extractScheme(file) == null)) { return super.getURL(basePath, file); } try { final FileSystemManager fsManager = VFS.getManager(); FileName path; if (basePath != null && UriParser.extractScheme(file) == null) { final FileName base = fsManager.resolveURI(basePath); path = fsManager.resolveName(base, file); } else { path = fsManager.resolveURI(file); } final URLStreamHandler handler = new VFSURLStreamHandler(path); return new URL(null, path.getURI(), handler); } catch (final FileSystemException fse) { throw new ConfigurationRuntimeException("Could not parse basePath: " + basePath + " and fileName: " + file, fse); } }
public static InputStream getImageInputStream( Display display, String location ) { // assume the classloader for the active thread ClassLoader cl = Thread.currentThread().getContextClassLoader(); if ( cl == null ) { // Can't count on Thread.currentThread().getContextClassLoader() being non-null on Mac // Have to provide some fallback cl = ImageUtil.class.getClassLoader(); } URL res = cl.getResource( location ); if ( res != null ) { try { java.io.InputStream s = res.openStream(); return s; } catch ( IOException e ) { // do nothing. just move on to trying to load via file system } } try { FileObject imageFileObject = KettleVFS.getInstance().getFileSystemManager().resolveFile( base, location ); return KettleVFS.getInputStream( imageFileObject ); } catch ( FileSystemException e ) { throw new RuntimeException( "Unable to load image with name [" + location + "]", e ); } }
@Override public OutputStream getOutputStream(final URL url) throws ConfigurationException { try { final FileSystemOptions opts = getOptions(url.getProtocol()); final FileSystemManager fsManager = VFS.getManager(); final FileObject file = (opts == null) ? fsManager.resolveFile(url.toString()) : fsManager.resolveFile(url.toString(), opts); // throw an exception if the target URL is a directory if (file == null || file.getType() == FileType.FOLDER) { throw new ConfigurationException("Cannot save a configuration to a directory"); } final FileContent content = file.getContent(); if (content == null) { throw new ConfigurationException("Cannot access content of " + url); } return content.getOutputStream(); } catch (final FileSystemException fse) { throw new ConfigurationException("Unable to access " + url, fse); } }
public SimpleJsonExtractor(WorkUnitState workUnitState) throws FileSystemException { this.workUnitState = workUnitState; // Resolve the file to pull if (workUnitState.getPropAsBoolean(ConfigurationKeys.SOURCE_CONN_USE_AUTHENTICATION, false)) { // Add authentication credential if authentication is needed UserAuthenticator auth = new StaticUserAuthenticator(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_DOMAIN, ""), workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME), PasswordManager.getInstance(workUnitState) .readPassword(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD))); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY), opts); } else { this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY)); } // Open the file for reading LOGGER.info("Opening file " + this.fileObject.getURL().toString()); this.bufferedReader = this.closer.register(new BufferedReader(new InputStreamReader(this.fileObject.getContent().getInputStream(), ConfigurationKeys.DEFAULT_CHARSET_ENCODING))); }
final FileSystemManager fsManager = VFS.getManager(); if (url != null) final FileName name = fsManager.resolveURI(url.toString()); if (name != null) final FileName base = fsManager.resolveURI(basePath); return fsManager.resolveName(base, fileName).getURI(); final FileName name = fsManager.resolveURI(fileName); final FileName base = name.getParent(); return fsManager.resolveName(base, name.getBaseName()).getURI(); fse.printStackTrace(); return null;
/** * Returns the file that is monitored by this strategy. Note that the return * value can be <b>null </b> under some circumstances. * * @return the monitored file */ protected FileObject getFileObject() { if (!getFileHandler().isLocationDefined()) { return null; } try { final FileSystemManager fsManager = VFS.getManager(); final String uri = resolveFileURI(); if (uri == null) { throw new ConfigurationRuntimeException("Unable to determine file to monitor"); } return fsManager.resolveFile(uri); } catch (final FileSystemException fse) { final String msg = "Unable to monitor " + getFileHandler().getURL().toString(); log.error(msg); throw new ConfigurationRuntimeException(msg, fse); } }
/** * Resolve file from test directory. */ protected FileObject getFile( String filename ) throws Exception { URL res = this.getClass().getResource( inPrefix + filename ); assertNotNull( "There is no file", res ); FileObject file = fs.resolveFile( res.toExternalForm() ); assertNotNull( "There is no file", file ); return file; }
@Test public void parseURLs() throws Exception { HadoopConfigurationLocator locator = new HadoopConfigurationLocator(); FileObject root = VFS.getManager().resolveFile( HADOOP_CONFIGURATIONS_PATH ); List<URL> urls = locator.parseURLs( root, "a,b" ); assertEquals( 2, urls.size() ); assertEquals( root.getURL().toURI().resolve( "hadoop-configurations/a/" ), urls.get( 0 ).toURI() ); assertEquals( root.getURL().toURI().resolve( "hadoop-configurations/a/a-config.jar" ), urls.get( 1 ).toURI() ); } }
@Override public boolean matches(URL url) throws Exception { try { final FileSystemManager manager = VFS.getManager(); final FileObject fileObject = manager.resolveFile(url.toExternalForm()); return fileObject.exists() && fileObject.getType() == FileType.FOLDER; } catch (FileSystemException e) { Reflections.log.warn("Could not create CommonsVfs2UrlType from url " + url.toExternalForm(), e); return false; } }
/** * Locates a file object, by absolute URI. * * @param baseFile The base file. * @param uri The URI of the file to locate. * @param fileSystemOptions The FileSystem options. * @return the FileObject. * @throws FileSystemException if an error occurs. */ @Override public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions) throws FileSystemException { final StringBuilder buf = new StringBuilder(BUFFER_SIZE); UriParser.extractScheme(VFS.getManager().getSchemes(), uri, buf); final String resourceName = buf.toString(); ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions); if (classLoader == null) { classLoader = getClass().getClassLoader(); } FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri); final URL url = classLoader.getResource(resourceName); FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri); return getContext().getFileSystemManager().resolveFile(url.toExternalForm()); }
/** * Locates a file object, by absolute URI. * * @param baseFile The base file. * @param uri The URI of the file to locate. * @param fileSystemOptions The FileSystem options. * @return the FileObject. * @throws FileSystemException if an error occurs. */ @Override public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions) throws FileSystemException { final StringBuilder buf = new StringBuilder(BUFFER_SIZE); UriParser.extractScheme(uri, buf); final String resourceName = buf.toString(); ClassLoader cl = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions); if (cl == null) { cl = getClass().getClassLoader(); } final URL url = cl.getResource(resourceName); if (url == null) { throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri); } return getContext().getFileSystemManager().resolveFile(url.toExternalForm()); }
private void testResolveAndParseXmlInZip(final String xmlPathInZip, final String xsdPathInZip) throws IOException, FileSystemException, SAXException { final File newZipFile = createTempFile(); final String zipFilePath = "zip:file:" + newZipFile.getAbsolutePath(); final FileSystemManager manager = VFS.getManager(); try (final FileObject zipFileObject = manager.resolveFile(zipFilePath)) { try (final FileObject xmlFileObject = zipFileObject.resolveFile(xmlPathInZip)) { try (final InputStream inputStream = xmlFileObject.getContent().getInputStream()) { final Document document = newDocumentBuilder(zipFileObject, xmlFileObject, xsdPathInZip) .parse(inputStream); Assert.assertNotNull(document); } } } }
protected Record(FileObject image) throws FileSystemException, IOException { final FileSystemManager fsManager = VFS.getManager(); final FileObject imagesBase = fsManager.resolveFile(downloadAndGetImagePath()); final FileObject annotationsBase = fsManager.resolveFile(downloadAndGetAnnotationPath()); // get the id id = imagesBase.getName().getRelativeName(image.getName()); // the class objectClass = image.getParent().getName().getBaseName(); // find the annotation file final String annotationFileName = id.replace("image_", "annotation_").replace(".jpg", ".mat"); final FileObject annotationFile = annotationsBase.resolveFile(annotationFileName); parseAnnotations(annotationFile); }
continue; FileObject fo = vfs.resolveFile(path); switch (fo.getType()) { case FILE: case FOLDER: case IMAGINARY: String pattern = fo.getName().getBaseName(); if (fo.getParent() != null && fo.getParent().getType() == FileType.FOLDER) { FileObject[] children = fo.getParent().getChildren(); for (FileObject child : children) { if (child.getType() == FileType.FILE && child.getName().getBaseName().matches(pattern)) { classpath.add(child);