Java Code Examples for org.apache.commons.vfs2.FileObject#toString()

The following examples show how to use org.apache.commons.vfs2.FileObject#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionCopyFiles.java    From hop with Apache License 2.0 6 votes vote down vote up
public TextFileSelector( FileObject sourcefolderin, FileObject destinationfolderin, String filewildcard,
                         IWorkflowEngine<WorkflowMeta> parentWorkflow ) {

  if ( sourcefolderin != null ) {
    sourceFolder = sourcefolderin.toString();
  }
  if ( destinationfolderin != null ) {
    destinationFolderObject = destinationfolderin;
    destinationFolder = destinationFolderObject.toString();
  }
  if ( !Utils.isEmpty( filewildcard ) ) {
    fileWildcard = filewildcard;
    pattern = Pattern.compile( fileWildcard );
  }
  parentjob = parentWorkflow;
}
 
Example 2
Source File: PluginPropertiesUtil.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a properties file from the plugin directory for the plugin interface provided
 *
 * @param plugin
 * @return
 * @throws KettleFileException
 * @throws IOException
 */
protected Properties loadProperties( PluginInterface plugin, String relativeName ) throws KettleFileException,
  IOException {
  if ( plugin == null ) {
    throw new NullPointerException();
  }
  FileObject propFile =
    KettleVFS.getFileObject( plugin.getPluginDirectory().getPath() + Const.FILE_SEPARATOR + relativeName );
  if ( !propFile.exists() ) {
    throw new FileNotFoundException( propFile.toString() );
  }
  try {
    return new PropertiesConfigurationProperties( propFile );
  } catch ( ConfigurationException e ) {
    throw new IOException( e );
  }
}
 
Example 3
Source File: LegacyShimLocator.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a properties file from the plugin directory for the plugin interface provided
 *
 * @param plugin
 * @return
 * @throws KettleFileException
 * @throws IOException
 */
private static Properties loadProperties( PluginInterface plugin, String relativeName ) throws KettleFileException,
  IOException {
  if ( plugin == null ) {
    throw new NullPointerException();
  }
  FileObject propFile =
    KettleVFS.getFileObject( plugin.getPluginDirectory().getPath() + Const.FILE_SEPARATOR + relativeName );
  if ( !propFile.exists() ) {
    throw new FileNotFoundException( propFile.toString() );
  }
  try {
    Properties pluginProperties = new Properties();
    pluginProperties.load( new FileInputStream( propFile.getName().getPath() ) );
    return pluginProperties;
  } catch ( Exception e ) {
    // Do not catch ConfigurationException. Different shims will use different
    // packages for this exception.
    throw new IOException( e );
  }
}
 
Example 4
Source File: JobEntryCopyFiles.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public TextFileSelector( FileObject sourcefolderin, FileObject destinationfolderin, String filewildcard,
  Job parentJob ) {

  if ( sourcefolderin != null ) {
    sourceFolder = sourcefolderin.toString();
  }
  if ( destinationfolderin != null ) {
    destinationFolderObject = destinationfolderin;
    destinationFolder = destinationFolderObject.toString();
  }
  if ( !Utils.isEmpty( filewildcard ) ) {
    fileWildcard = filewildcard;
    pattern = Pattern.compile( fileWildcard );
  }
  parentjob = parentJob;
}
 
Example 5
Source File: LocalFile.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * rename this file
 */
@Override
protected void doRename(final FileObject newFile) throws Exception {
    final LocalFile newLocalFile = (LocalFile) FileObjectUtils.getAbstractFileObject(newFile);

    if (!file.renameTo(newLocalFile.getLocalFile())) {
        throw new FileSystemException("vfs.provider.local/rename-file.error", file.toString(), newFile.toString());
    }
}
 
Example 6
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private void readAndAssert(final FileObject fileObject, final InputStream inputStream, final String expectedId)
        throws IOException {
    final String streamData = IOUtils.toString(inputStream, "UTF-8");
    final String fileObjectString = fileObject.toString();
    Assert.assertNotNull(fileObjectString, streamData);
    Assert.assertEquals(
            fileObjectString, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Root"
                    + expectedId + ">foo" + expectedId + "</Root" + expectedId + ">\r\n",
            streamData);
}
 
Example 7
Source File: ParseXmlInZipTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private DocumentBuilder newDocumentBuilder(final FileObject containerFile, final FileObject sourceFile,
        final String pathToXsdInZip) throws IOException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final boolean validate = pathToXsdInZip != null;
    documentBuilderFactory.setValidating(validate);
    documentBuilderFactory.setNamespaceAware(true);
    if (validate) {
        documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                "http://www.w3.org/2001/XMLSchema");
        @SuppressWarnings("resource")
        final FileObject schema = containerFile.resolveFile(pathToXsdInZip);
        if (schema.exists()) {
            documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
                    schema.getContent().getInputStream());
        } else {
            schema.close();
            throw new FileNotFoundException(schema.toString());
        }
    }
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setEntityResolver(new TestEntityResolver(containerFile, sourceFile));
    } catch (final ParserConfigurationException e) {
        throw new IOException("Cannot read Java Connector configuration: " + e, e);
    }
    documentBuilder.setErrorHandler(new TestErrorHandler(containerFile + " - " + sourceFile));
    return documentBuilder;
}
 
Example 8
Source File: ProviderRandomSetLengthTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a file
 */
public void testRandomSetLength() throws Exception {
    FileObject file = null;
    try {
        file = this.createScratchFolder().resolveFile("random_write.txt");
        file.createFile();
        final String fileString = file.toString();
        final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);

        // Write long string
        ra.writeBytes(TEST_DATA);
        Assert.assertEquals(fileString, TEST_DATA.length(), ra.length());

        // Shrink to length 1
        ra.setLength(1);
        Assert.assertEquals(fileString, 1, ra.length());
        // now read 1
        ra.seek(0);
        Assert.assertEquals(fileString, TEST_DATA.charAt(0), ra.readByte());

        try {
            ra.readByte();
            Assert.fail("Expected " + Exception.class.getName());
        } catch (final IOException e) {
            // Expected
        }

        // Grow to length 2
        ra.setLength(2);
        Assert.assertEquals(fileString, 2, ra.length());
        // We have an undefined extra byte
        ra.seek(1);
        ra.readByte();

    } finally {
        if (file != null) {
            file.close();
        }
    }
}
 
Example 9
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectId renameRepositoryDirectory( ObjectId id, RepositoryDirectoryInterface newParentDir, String newName ) throws KettleException {
  if ( newParentDir != null || newName != null ) {
    try {
      // In case of a root object, the ID is the same as the relative filename...
      RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
      RepositoryDirectoryInterface dir = tree.findDirectory( id );

      if ( dir == null ) {
        throw new KettleException( "Could not find folder [" + id + "]" );
      }

      // If newName is null, keep the current name
      newName = ( newName != null ) ? newName : dir.getName();

      FileObject folder = KettleVFS.getFileObject( dir.getPath() );

      String newFolderName = null;

      if ( newParentDir != null ) {
        FileObject newParentFolder = KettleVFS.getFileObject( newParentDir.getPath() );

        newFolderName = newParentFolder.toString() + "/" + newName;
      } else {
        newFolderName = folder.getParent().toString() + "/" + newName;
      }

      FileObject newFolder = KettleVFS.getFileObject( newFolderName );
      folder.moveTo( newFolder );

      return new StringObjectId( dir.getObjectId() );
    } catch ( Exception e ) {
      throw new KettleException( "Unable to rename directory folder to [" + id + "]" );
    }
  }
  return ( id );
}
 
Example 10
Source File: XMLHandler.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Load a file into an XML document
 *
 * @param fileObject     The fileObject to load into a document
 * @param systemID       Provide a base for resolving relative URIs.
 * @param ignoreEntities Ignores external entities and returns an empty dummy.
 * @param namespaceAware support XML namespaces.
 * @return the Document if all went well, null if an error occured!
 */
public static Document loadXMLFile( FileObject fileObject, String systemID, boolean ignoreEntities,
                                    boolean namespaceAware ) throws KettleXMLException {

  //[PDI-18528] Retry opening the inputstream on first error. Because of the way DefaultFileContent handles open streams,
  //in multithread executions, the stream could be closed by another stream without notice. The retry is a way to recover the stream.
  int reties = Const.toInt( EnvUtil.getSystemProperty( Const.KETTLE_RETRY_OPEN_XML_STREAM ), DEFAULT_RETRY_ATTEMPTS );
  if ( reties < 0 ) {
    reties = 0;
  }
  int attempts = 0;
  Exception lastException = null;
  while ( attempts <= reties ) {
    try {
      return loadXMLFile( KettleVFS.getInputStream( fileObject ), systemID, ignoreEntities, namespaceAware );
    } catch ( Exception ex ) {
      lastException = ex;
      try {
        java.lang.Thread.sleep( 1000 );
      } catch ( InterruptedException e ) {
        //Sonar squid S2142 requires the handling of the InterruptedException instead of ignoring it
        Thread.currentThread().interrupt();
      }
    }
    attempts++;
  }

  throw new KettleXMLException( "Unable to read file [" + fileObject.toString() + "]", lastException );
}
 
Example 11
Source File: ActionDosToUnix.java    From hop with Apache License 2.0 4 votes vote down vote up
private boolean convertOneFile( FileObject file, int convertion, Result result, IWorkflowEngine<WorkflowMeta> parentWorkflow ) throws HopException {
  boolean retval = false;
  try {
    // We deal with a file..

    boolean convertToUnix = true;

    if ( convertion == CONVERTION_TYPE_GUESS ) {
      // Get file Type
      int fileType = getFileType( file );
      if ( fileType == TYPE_DOS_FILE ) {
        // File type is DOS
        // We need to convert it to UNIX
        convertToUnix = true;
      } else {
        // File type is not DOS
        // so let's convert it to DOS
        convertToUnix = false;
      }
    } else if ( convertion == CONVERTION_TYPE_DOS_TO_UNIX ) {
      convertToUnix = true;
    } else {
      convertToUnix = false;
    }

    retval = convert( file, convertToUnix );

    if ( !retval ) {
      logError( BaseMessages.getString( PKG, "JobDosToUnix.Error.FileNotConverted", file.toString() ) );
      // Update Bad files number
      updateBadFormed();
      if ( resultfilenames.equals( ADD_ALL_FILENAMES ) || resultfilenames.equals( ADD_ERROR_FILES_ONLY ) ) {
        addFileToResultFilenames( file, result, parentWorkflow );
      }
    } else {
      if ( isDetailed() ) {
        logDetailed( "---------------------------" );
        logDetailed( BaseMessages.getString( PKG, "JobDosToUnix.Error.FileConverted", file, convertToUnix
          ? "UNIX" : "DOS" ) );
      }
      // Update processed files number
      updateProcessedFormed();
      if ( resultfilenames.equals( ADD_ALL_FILENAMES ) || resultfilenames.equals( ADD_PROCESSED_FILES_ONLY ) ) {
        addFileToResultFilenames( file, result, parentWorkflow );
      }
    }

  } catch ( Exception e ) {
    throw new HopException( "Unable to convert file '" + file.toString() + "'", e );
  }
  return retval;
}
 
Example 12
Source File: PipelineUnitTest.java    From hop with Apache License 2.0 4 votes vote down vote up
public void setRelativeFilename( String referencePipelineFilename ) throws HopException {
  // Build relative path whenever a pipeline is saved
  //
  if ( StringUtils.isEmpty( referencePipelineFilename ) ) {
    return; // nothing we can do
  }

  // Set the filename to be safe
  //
  setPipelineFilename( referencePipelineFilename );

  String base = getBasePath();
  if ( StringUtils.isEmpty( base ) ) {
    base = getVariable( DataSetConst.VARIABLE_UNIT_TESTS_BASE_PATH );
  }
  base = environmentSubstitute( base );
  if ( StringUtils.isNotEmpty( base ) ) {
    // See if the base path is present in the filename
    // Then replace the filename
    //
    try {
      FileObject baseFolder = HopVfs.getFileObject( base );
      FileObject pipelineFile = HopVfs.getFileObject( referencePipelineFilename );
      FileObject parent = pipelineFile.getParent();
      while ( parent != null ) {
        if ( parent.equals( baseFolder ) ) {
          // Here we are, we found the base folder in the pipeline file
          //
          String pipelineFileString = pipelineFile.toString();
          String baseFolderName = parent.toString();

          // Final validation & unit test filename correction
          //
          if ( pipelineFileString.startsWith( baseFolderName ) ) {
            String relativeFile = pipelineFileString.substring( baseFolderName.length() );
            String relativeFilename;
            if ( relativeFile.startsWith( "/" ) ) {
              relativeFilename = "." + relativeFile;
            } else {
              relativeFilename = "./" + relativeFile;
            }
            // Set the pipeline filename to the relative path
            //
            setPipelineFilename( relativeFilename );

            LogChannel.GENERAL.logDetailed( "Unit test '" + getName() + "' : saved relative path to pipeline: " + relativeFilename );
          }
        }
        parent = parent.getParent();
      }
    } catch ( Exception e ) {
      throw new HopException( "Error calculating relative unit test file path", e );
    }
  }

}
 
Example 13
Source File: DataSetHelper.java    From pentaho-pdi-dataset with Apache License 2.0 4 votes vote down vote up
private void saveUnitTest( MetaStoreFactory<TransUnitTest> testFactory, TransUnitTest unitTest, TransMeta transMeta ) throws MetaStoreException {

    // Build relative path whenever a transformation is saved
    //
    if ( StringUtils.isNotEmpty( transMeta.getFilename() ) ) {
      // Set the filename to be safe
      //
      unitTest.setTransFilename( transMeta.getFilename() );

      String basePath = unitTest.getBasePath();
      if ( StringUtils.isEmpty( basePath ) ) {
        basePath = transMeta.getVariable( DataSetConst.VARIABLE_UNIT_TESTS_BASE_PATH );
      }
      basePath = transMeta.environmentSubstitute( basePath );
      if ( StringUtils.isNotEmpty( basePath ) ) {
        // See if the basePath is present in the filename
        // Then replace the filename
        //
        try {
          FileObject baseFolder = KettleVFS.getFileObject( basePath );
          FileObject transFile = KettleVFS.getFileObject( transMeta.getFilename() );
          FileObject parent = transFile.getParent();
          while ( parent != null ) {
            if ( parent.equals( baseFolder ) ) {
              // Here we are, we found the base folder in the transformation file
              //
              String transFilename = transFile.toString();
              String baseFoldername = parent.toString();

              // Final validation & unit test filename correction
              //
              if ( transFilename.startsWith( baseFoldername ) ) {
                String relativeFile = transFilename.substring( baseFoldername.length() );
                String filename;
                if ( relativeFile.startsWith( "/" ) ) {
                  filename = "." + relativeFile;
                } else {
                  filename = "./" + relativeFile;
                }
                // Set the transformation filename to the relative path
                //
                unitTest.setTransFilename( filename );
                LogChannel.GENERAL.logBasic( "Unit test '" + unitTest.getName() + "' : Saved relative path to transformation: " + filename );
              }
            }
            parent = parent.getParent();
          }
        } catch ( Exception e ) {
          throw new MetaStoreException( "Error calculating relative unit test file path", e );
        }
      }
    }

    testFactory.saveElement( unitTest );
  }
 
Example 14
Source File: JobEntryDosToUnix.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private boolean convertOneFile( FileObject file, int convertion, Result result, Job parentJob ) throws KettleException {
  boolean retval = false;
  try {
    // We deal with a file..

    boolean convertToUnix = true;

    if ( convertion == CONVERTION_TYPE_GUESS ) {
      // Get file Type
      int fileType = getFileType( file );
      if ( fileType == TYPE_DOS_FILE ) {
        // File type is DOS
        // We need to convert it to UNIX
        convertToUnix = true;
      } else {
        // File type is not DOS
        // so let's convert it to DOS
        convertToUnix = false;
      }
    } else if ( convertion == CONVERTION_TYPE_DOS_TO_UNIX ) {
      convertToUnix = true;
    } else {
      convertToUnix = false;
    }

    retval = convert( file, convertToUnix );

    if ( !retval ) {
      logError( BaseMessages.getString( PKG, "JobDosToUnix.Error.FileNotConverted", file.toString() ) );
      // Update Bad files number
      updateBadFormed();
      if ( resultfilenames.equals( ADD_ALL_FILENAMES ) || resultfilenames.equals( ADD_ERROR_FILES_ONLY ) ) {
        addFileToResultFilenames( file, result, parentJob );
      }
    } else {
      if ( isDetailed() ) {
        logDetailed( "---------------------------" );
        logDetailed( BaseMessages.getString( PKG, "JobDosToUnix.Error.FileConverted", file, convertToUnix
          ? "UNIX" : "DOS" ) );
      }
      // Update processed files number
      updateProcessedFormed();
      if ( resultfilenames.equals( ADD_ALL_FILENAMES ) || resultfilenames.equals( ADD_PROCESSED_FILES_ONLY ) ) {
        addFileToResultFilenames( file, result, parentJob );
      }
    }

  } catch ( Exception e ) {
    throw new KettleException( "Unable to convert file '" + file.toString() + "'", e );
  }
  return retval;
}
 
Example 15
Source File: XmlHandler.java    From hop with Apache License 2.0 3 votes vote down vote up
/**
 * Load a file into an XML document
 *
 * @param fileObject     The fileObject to load into a document
 * @param systemID       Provide a base for resolving relative URIs.
 * @param ignoreEntities Ignores external entities and returns an empty dummy.
 * @param namespaceAware support XML namespaces.
 * @return the Document if all went well, null if an error occured!
 */
public static Document loadXmlFile( FileObject fileObject, String systemID, boolean ignoreEntities,
                                    boolean namespaceAware ) throws HopXmlException {
  try {
    return loadXmlFile( HopVfs.getInputStream( fileObject ), systemID, ignoreEntities, namespaceAware );
  } catch ( IOException e ) {
    throw new HopXmlException( "Unable to read file [" + fileObject.toString() + "]", e );
  }
}