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

The following examples show how to use org.apache.commons.vfs2.FileObject#getType() . 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: ResourceAgent.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public String[] readdir(String fn) {
    try {
        final FileObject resource = resourceService.resolve(workingDir, fn);
        if(!resource.exists() || resource.getType() == FileType.FILE) {
            return new String[0];
        }
        final FileName name = resource.getName();
        final FileObject[] children = resource.getChildren();
        final String[] strings = new String[children.length];
        for(int i = 0; i < children.length; ++i) {
            final FileName absName = children[i].getName();
            strings[i] = name.getRelativeName(absName);
        }
        return strings;
    } catch(FileSystemException e) {
        throw new RuntimeException("Could not list contents of directory " + fn, e);
    }
}
 
Example 2
Source File: RepositoryOpenDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void setSelectedView( final FileObject selectedView ) {
  this.selectedView = selectedView;
  if ( selectedView != null ) {
    logger.debug( "Setting selected view to " + selectedView );
    try {
      if ( selectedView.getType() == FileType.FILE ) {
        logger.debug( "Setting filename in selected view to " + selectedView.getName().getBaseName() );
        this.fileNameTextField.setText( URLDecoder.decode( selectedView.getName().getBaseName(), "UTF-8" ) );
      }
    } catch ( Exception e ) {
      // can be ignored ..
      logger.debug( "Unable to determine file type. This is not fatal.", e );
    }
    final ComboBoxModel comboBoxModel = createLocationModel( selectedView );
    this.locationCombo.setModel( comboBoxModel );
    this.table.setSelectedPath( (FileObject) comboBoxModel.getSelectedItem() );
  } else {
    this.fileNameTextField.setText( null );
    this.table.setSelectedPath( null );
    this.locationCombo.setModel( new DefaultComboBoxModel() );
  }
}
 
Example 3
Source File: JobEntrySSH2GET.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Check existence of a local file
 *
 * @param filename
 * @return true, if file exists
 */
public boolean FileExists( String filename ) {

  FileObject file = null;
  try {
    file = KettleVFS.getFileObject( filename, this );
    if ( !file.exists() ) {
      return false;
    } else {
      if ( file.getType() == FileType.FILE ) {
        return true;
      } else {
        return false;
      }
    }
  } catch ( Exception e ) {
    return false;
  }
}
 
Example 4
Source File: RepositoryPublishDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Called whenever the value of the selection changes.
 *
 * @param e
 *          the event that characterizes the change.
 */
public void valueChanged( final ListSelectionEvent e ) {
  final int selectedRow = getTable().getSelectedRow();
  if ( selectedRow == -1 ) {
    return;
  }

  final FileObject selectedFileObject = getTable().getSelectedFileObject( selectedRow );
  if ( selectedFileObject == null ) {
    return;
  }

  try {
    if ( selectedFileObject.getType() == FileType.FILE ) {
      getFileNameTextField().setText( selectedFileObject.getName().getBaseName() );
    }
  } catch ( FileSystemException e1 ) {
    // ignore ..
  }
}
 
Example 5
Source File: RepositoryTreeCellRenderer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Component getTreeCellRendererComponent( final JTree tree,
                                               final Object value,
                                               final boolean sel,
                                               final boolean expanded,
                                               final boolean leaf,
                                               final int row,
                                               final boolean hasFocus ) {
  final JLabel component =
    (JLabel) super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
  if ( value instanceof FileObject ) {
    final FileObject node = (FileObject) value;
    try {
      if ( leaf && ( node.getType() == FileType.FOLDER ) ) {
        component.setIcon( getOpenIcon() );
      }
    } catch ( FileSystemException fse ) {
      // ignore exception here
    }
    component.setText( node.getName().getBaseName() );
  } else {
    component.setText( "/" );
    component.setIcon( getOpenIcon() );
  }
  return component;
}
 
Example 6
Source File: JarUtils.java    From spring-boot-jar-resources with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
private static File copyToDir(FileObject jarredFile, File destination, boolean retryIfImaginary) {
    switch (jarredFile.getType()) {
        case FILE:
            return copyFileToDir(jarredFile, destination);
        case FOLDER:
            return copyDirToDir(jarredFile, destination);
        case IMAGINARY:
            if (retryIfImaginary) {
                log.debug("Imaginary file found, retrying extraction");
                VFS.getManager().getFilesCache().removeFile(jarredFile.getFileSystem(), jarredFile.getName());
                FileObject newJarredFile = VFS.getManager().resolveFile(jarredFile.getName().getURI());
                return copyToDir(newJarredFile, destination, false);
            } else {
                log.debug("Imaginary file found after retry, abandoning retry");
            }

        default:
            throw new IllegalStateException("File Type not supported: " + jarredFile.getType());
    }
}
 
Example 7
Source File: FtpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the type of the file, returns null if the file does not exist.
 */
@Override
protected FileType doGetType() throws Exception {
    // VFS-210
    synchronized (getFileSystem()) {
        if (this.fileInfo == null) {
            getInfo(false);
        }

        if (this.fileInfo == UNKNOWN) {
            return FileType.IMAGINARY;
        } else if (this.fileInfo.isDirectory()) {
            return FileType.FOLDER;
        } else if (this.fileInfo.isFile()) {
            return FileType.FILE;
        } else if (this.fileInfo.isSymbolicLink()) {
            final FileObject linkDest = getLinkDestination();
            // VFS-437: We need to check if the symbolic link links back to the symbolic link itself
            if (this.isCircular(linkDest)) {
                // If the symbolic link links back to itself, treat it as an imaginary file to prevent following
                // this link. If the user tries to access the link as a file or directory, the user will end up with
                // a FileSystemException warning that the file cannot be accessed. This is to prevent the infinite
                // call back to doGetType() to prevent the StackOverFlow
                return FileType.IMAGINARY;
            }
            return linkDest.getType();

        }
    }
    throw new FileSystemException("vfs.provider.ftp/get-type.error", getName());
}
 
Example 8
Source File: RepositoryTableModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public FileObject getElementForRow( final int row ) {
  if ( selectedPath == null ) {
    return null;
  }

  try {
    if ( selectedPath.getType() != FileType.FOLDER ) {
      return null;
    }

    final FileObject[] children = selectedPath.getChildren();
    int count = 0;
    for ( int i = 0; i < children.length; i++ ) {
      final FileObject child = children[i];
      if ( isShowHiddenFiles() == false && child.isHidden() ) {
        continue;
      }
      if ( child.getType() != FileType.FOLDER ) {
        if ( PublishUtil.acceptFilter( filters, child.getName().getBaseName() ) == false ) {
          continue;
        }
      }

      if ( count == row ) {
        return child;
      }
      count += 1;
    }
    return null;
  } catch ( FileSystemException fse ) {
    UncaughtExceptionsModel.getInstance().addException( fse );
    return null;
  }
}
 
Example 9
Source File: FTPRemoteDownloadSourceDelegate.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void queueFiles(FileQueueChecker fqc, NavigableSet<RemoteFile> fileQueue, FileFilter fileFilter) throws
    IOException, StageException {
  verifyAndReconnect();

  FileObject[] theFiles = remoteDir.getChildren();
  if (conf.processSubDirectories) {
    theFiles = ArrayUtils.addAll(theFiles, remoteDir.findFiles(fileFilter));
  }

  for (FileObject file : theFiles) {
    String path = relativizeToRoot(file.getName().getPath());
    LOG.debug("Checking {}", path);
    if (file.getType() != FileType.FILE) {
      LOG.trace("Skipping {} because it is not a file", path);
      continue;
    }

    //check if base name matches - not full path.
    Matcher matcher = fileFilter.getRegex().matcher(file.getName().getBaseName());
    if (!matcher.matches()) {
      LOG.trace("Skipping {} because it does not match the regex", path);
      continue;
    }

    RemoteFile tempFile = new FTPRemoteFile(path, getModTime(file), file);
    if (fqc.shouldQueue(tempFile)) {
      LOG.debug("Queuing file {} with modtime {}", tempFile.getFilePath(), tempFile.getLastModified());
      // If we are done with all files, the files with the final mtime might get re-ingested over and over.
      // So if it is the one of those, don't pull it in.
      fileQueue.add(tempFile);
    }
  }
}
 
Example 10
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Does a 'cp' command.
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}
 
Example 11
Source File: RepositoryEntryCellRenderer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Component getTableCellRendererComponent( final JTable table,
                                                final Object value,
                                                final boolean isSelected,
                                                final boolean hasFocus,
                                                final int row,
                                                final int column ) {
  final JLabel component =
    (JLabel) super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
  try {
    if ( column == 0 ) {
      final RepositoryTableModel tableModel = (RepositoryTableModel) table.getModel();
      final RowSorter rowSorter = table.getRowSorter();
      final FileObject e;
      if ( rowSorter != null ) {
        e = tableModel.getElementForRow( rowSorter.convertRowIndexToModel( row ) );
      } else {
        e = tableModel.getElementForRow( row );
      }

      if ( e.getType() == FileType.FOLDER ) {
        component.setIcon( closedIcon );
      } else {
        component.setIcon( leafIcon );
      }
    } else {
      component.setIcon( null );
    }
  } catch ( FileSystemException fse ) {
    // ok, ugly, but not fatal.
  }
  return component;
}
 
Example 12
Source File: PubmedArchiveCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
private NextArticle getNextArticle() throws IOException {

		while (directoryIterator.hasNext()) {
			File f = directoryIterator.next();
			LOG.debug("extracting " + f.getAbsolutePath());

			try {
				FileObject archive = fsManager.resolveFile("tgz:file://"
						+ f.getAbsolutePath());

				// List the children of the archive file
				FileObject[] children = archive.getChildren()[0].getChildren();
				for (int i = 0; i < children.length; i++) {
					FileObject fo = children[i];
					if (fo.isReadable() && fo.getType() == FileType.FILE
							&& fo.getName().getExtension().equals("nxml")) {
						FileContent fc = fo.getContent();
						Article article = archiveArticleParser.parse(fc
								.getInputStream());

						NextArticle nextArticle = new NextArticle();
						nextArticle.article = article;
						nextArticle.file = f.getAbsolutePath();

						return nextArticle;
					}
				}
			} catch (Exception e) {
				LOG.error("Error extracting " + f.getAbsolutePath() + ", " + e);
			}
		}
		return null;
	}
 
Example 13
Source File: RepositoryOpenDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String performOpen( final AuthenticationData loginData, final String previousSelection )
  throws FileSystemException, UnsupportedEncodingException {
  fileSystemRoot = PublishUtil.createVFSConnection( VFS.getManager(), loginData );
  if ( previousSelection == null ) {
    setSelectedView( fileSystemRoot );
  } else {
    final FileObject view = fileSystemRoot.resolveFile( previousSelection );
    if ( view == null ) {
      setSelectedView( fileSystemRoot );
    } else {
      if ( view.exists() == false ) {
        setSelectedView( fileSystemRoot );
      } else if ( view.getType() == FileType.FOLDER ) {
        setSelectedView( view );
      } else {
        setSelectedView( view.getParent() );
      }
    }
  }

  if ( StringUtils.isEmpty( fileNameTextField.getText(), true ) && previousSelection != null ) {
    final String fileName = IOUtils.getInstance().getFileName( previousSelection );
    DebugLog.log( "Setting filename to " + fileName );
    fileNameTextField.setText( fileName );
  }

  getConfirmAction().setEnabled( validateInputs( false ) );
  if ( super.performEdit() == false || selectedView == null ) {
    return null;
  }

  return getSelectedFile();
}
 
Example 14
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Does an 'ls' command.
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}
 
Example 15
Source File: RepositoryTreeModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the number of children of <code>parent</code>. Returns 0 if the node is a leaf or if it has no children.
 * <code>parent</code> must be a node previously obtained from this data source.
 *
 * @param parent
 *          a node in the tree, obtained from this data source
 * @return the number of children of the node <code>parent</code>
 */
public int getChildCount( Object parent ) {
  if ( parent instanceof RepositoryTreeRoot ) {
    final RepositoryTreeRoot root1 = (RepositoryTreeRoot) parent;
    parent = root1.getRoot();
    if ( parent == null ) {
      return 0;
    }
  }
  try {
    final FileObject parElement = (FileObject) parent;
    if ( parElement.getType() != FileType.FOLDER ) {
      return 0;
    }

    final FileObject[] children = parElement.getChildren();
    int count = 0;
    for ( int i = 0; i < children.length; i++ ) {
      final FileObject child = children[i];
      if ( isShowFoldersOnly() && child.getType() != FileType.FOLDER ) {
        continue;
      }
      if ( isShowHiddenFiles() == false && child.isHidden() ) {
        continue;
      }
      if ( child.getType() != FileType.FOLDER
          && PublishUtil.acceptFilter( filters, child.getName().getBaseName() ) == false ) {
        continue;
      }

      count += 1;
    }
    return count;
  } catch ( FileSystemException fse ) {
    logger.debug( "Failed", fse );
    return 0;
  }
}
 
Example 16
Source File: JobEntryGetPOP.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
String createOutputDirectory( int folderType ) throws KettleException, FileSystemException, IllegalArgumentException {
  if ( ( folderType != JobEntryGetPOP.FOLDER_OUTPUT ) && ( folderType != JobEntryGetPOP.FOLDER_ATTACHMENTS ) ) {
    throw new IllegalArgumentException( "Invalid folderType argument" );
  }
  String folderName = "";
  switch ( folderType ) {
    case JobEntryGetPOP.FOLDER_OUTPUT:
      folderName = getRealOutputDirectory();
      break;
    case JobEntryGetPOP.FOLDER_ATTACHMENTS:
      if ( isSaveAttachment() && isDifferentFolderForAttachment() ) {
        folderName = getRealAttachmentFolder();
      } else {
        folderName = getRealOutputDirectory();
      }
      break;
  }
  if ( Utils.isEmpty( folderName ) ) {
    switch ( folderType ) {
      case JobEntryGetPOP.FOLDER_OUTPUT:
        throw new KettleException( BaseMessages
          .getString( PKG, "JobGetMailsFromPOP.Error.OutputFolderEmpty" ) );
      case JobEntryGetPOP.FOLDER_ATTACHMENTS:
        throw new KettleException( BaseMessages
          .getString( PKG, "JobGetMailsFromPOP.Error.AttachmentFolderEmpty" ) );
    }
  }
  FileObject folder = KettleVFS.getFileObject( folderName, this );
  if ( folder.exists() ) {
    if ( folder.getType() != FileType.FOLDER ) {
      switch ( folderType ) {
        case JobEntryGetPOP.FOLDER_OUTPUT:
          throw new KettleException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.NotAFolderNot", folderName ) );
        case JobEntryGetPOP.FOLDER_ATTACHMENTS:
          throw new KettleException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.AttachmentFolderNotAFolder", folderName ) );
      }
    }
    if ( isDebug() ) {
      switch ( folderType ) {
        case JobEntryGetPOP.FOLDER_OUTPUT:
          logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.Log.OutputFolderExists", folderName ) );
          break;
        case JobEntryGetPOP.FOLDER_ATTACHMENTS:
          logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.Log.AttachmentFolderExists", folderName ) );
          break;
      }
    }
  } else {
    if ( isCreateLocalFolder() ) {
      folder.createFolder();
    } else {
      switch ( folderType ) {
        case JobEntryGetPOP.FOLDER_OUTPUT:
          throw new KettleException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.OutputFolderNotExist", folderName ) );
        case JobEntryGetPOP.FOLDER_ATTACHMENTS:
          throw new KettleException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.AttachmentFolderNotExist", folderName ) );
      }
    }
  }

  String returnValue = KettleVFS.getFilename( folder );
  try {
    folder.close();
  } catch ( IOException ignore ) {
    //Ignore error, as the folder was created successfully
  }
  return returnValue;
}
 
Example 17
Source File: JobEntryCreateFolder.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  result.setResult( false );

  if ( foldername != null ) {
    //Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if ( parentJobMeta.getNamedClusterEmbedManager() != null ) {
      parentJobMeta.getNamedClusterEmbedManager()
        .passEmbeddedMetastoreKey( this, parentJobMeta.getEmbeddedMetastoreProviderKey() );
    }

    String realFoldername = getRealFoldername();
    FileObject folderObject = null;
    try {
      folderObject = KettleVFS.getFileObject( realFoldername, this );

      if ( folderObject.exists() ) {
        boolean isFolder = false;

        // Check if it's a folder
        if ( folderObject.getType() == FileType.FOLDER ) {
          isFolder = true;
        }

        if ( isFailOfFolderExists() ) {
          // Folder exists and fail flag is on.
          result.setResult( false );
          if ( isFolder ) {
            logError( "Folder [" + realFoldername + "] exists, failing." );
          } else {
            logError( "File [" + realFoldername + "] exists, failing." );
          }
        } else {
          // Folder already exists, no reason to try to create it
          result.setResult( true );
          if ( log.isDetailed() ) {
            logDetailed( "Folder [" + realFoldername + "] already exists, not recreating." );
          }
        }

      } else {
        // No Folder yet, create an empty Folder.
        folderObject.createFolder();
        if ( log.isDetailed() ) {
          logDetailed( "Folder [" + realFoldername + "] created!" );
        }
        result.setResult( true );
      }
    } catch ( Exception e ) {
      logError( "Could not create Folder [" + realFoldername + "]", e );
      result.setResult( false );
      result.setNrErrors( 1 );
    } finally {
      if ( folderObject != null ) {
        try {
          folderObject.close();
        } catch ( IOException ex ) { /* Ignore */
        }
      }
    }
  } else {
    logError( "No Foldername is defined." );
  }

  return result;
}
 
Example 18
Source File: ActionDeleteFiles.java    From hop with Apache License 2.0 4 votes vote down vote up
boolean processFile( String path, String wildcard, IWorkflowEngine<WorkflowMeta> parentWorkflow ) {
  boolean isDeleted = false;
  FileObject fileFolder = null;

  try {
    fileFolder = HopVfs.getFileObject( path );

    if ( fileFolder.exists() ) {
      if ( fileFolder.getType() == FileType.FOLDER ) {

        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "ActionDeleteFiles.ProcessingFolder", path ) );
        }

        int totalDeleted = fileFolder.delete( new TextFileSelector( fileFolder.toString(), wildcard, parentWorkflow ) );

        if ( log.isDetailed() ) {
          logDetailed(
            BaseMessages.getString( PKG, "ActionDeleteFiles.TotalDeleted", String.valueOf( totalDeleted ) ) );
        }
        isDeleted = true;
      } else {

        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "ActionDeleteFiles.ProcessingFile", path ) );
        }
        isDeleted = fileFolder.delete();
        if ( !isDeleted ) {
          logError( BaseMessages.getString( PKG, "ActionDeleteFiles.CouldNotDeleteFile", path ) );
        } else {
          if ( log.isBasic() ) {
            logBasic( BaseMessages.getString( PKG, "ActionDeleteFiles.FileDeleted", path ) );
          }
        }
      }
    } else {
      // File already deleted, no reason to try to delete it
      if ( log.isBasic() ) {
        logBasic( BaseMessages.getString( PKG, "ActionDeleteFiles.FileAlreadyDeleted", path ) );
      }
      isDeleted = true;
    }
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "ActionDeleteFiles.CouldNotProcess", path, e
      .getMessage() ), e );
  } finally {
    if ( fileFolder != null ) {
      try {
        fileFolder.close();
      } catch ( IOException ex ) {
        // Ignore
      }
    }
  }

  return isDeleted;
}
 
Example 19
Source File: ActionGetPOP.java    From hop with Apache License 2.0 4 votes vote down vote up
String createOutputDirectory( int folderType ) throws HopException, FileSystemException, IllegalArgumentException {
  if ( ( folderType != ActionGetPOP.FOLDER_OUTPUT ) && ( folderType != ActionGetPOP.FOLDER_ATTACHMENTS ) ) {
    throw new IllegalArgumentException( "Invalid folderType argument" );
  }
  String folderName = "";
  switch ( folderType ) {
    case ActionGetPOP.FOLDER_OUTPUT:
      folderName = getRealOutputDirectory();
      break;
    case ActionGetPOP.FOLDER_ATTACHMENTS:
      if ( isSaveAttachment() && isDifferentFolderForAttachment() ) {
        folderName = getRealAttachmentFolder();
      } else {
        folderName = getRealOutputDirectory();
      }
      break;
  }
  if ( Utils.isEmpty( folderName ) ) {
    switch ( folderType ) {
      case ActionGetPOP.FOLDER_OUTPUT:
        throw new HopException( BaseMessages
          .getString( PKG, "JobGetMailsFromPOP.Error.OutputFolderEmpty" ) );
      case ActionGetPOP.FOLDER_ATTACHMENTS:
        throw new HopException( BaseMessages
          .getString( PKG, "JobGetMailsFromPOP.Error.AttachmentFolderEmpty" ) );
    }
  }
  FileObject folder = HopVfs.getFileObject( folderName );
  if ( folder.exists() ) {
    if ( folder.getType() != FileType.FOLDER ) {
      switch ( folderType ) {
        case ActionGetPOP.FOLDER_OUTPUT:
          throw new HopException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.NotAFolderNot", folderName ) );
        case ActionGetPOP.FOLDER_ATTACHMENTS:
          throw new HopException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.AttachmentFolderNotAFolder", folderName ) );
      }
    }
    if ( isDebug() ) {
      switch ( folderType ) {
        case ActionGetPOP.FOLDER_OUTPUT:
          logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.Log.OutputFolderExists", folderName ) );
          break;
        case ActionGetPOP.FOLDER_ATTACHMENTS:
          logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.Log.AttachmentFolderExists", folderName ) );
          break;
      }
    }
  } else {
    if ( isCreateLocalFolder() ) {
      folder.createFolder();
    } else {
      switch ( folderType ) {
        case ActionGetPOP.FOLDER_OUTPUT:
          throw new HopException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.OutputFolderNotExist", folderName ) );
        case ActionGetPOP.FOLDER_ATTACHMENTS:
          throw new HopException( BaseMessages.getString(
            PKG, "JobGetMailsFromPOP.Error.AttachmentFolderNotExist", folderName ) );
      }
    }
  }

  String returnValue = HopVfs.getFilename( folder );
  try {
    folder.close();
  } catch ( IOException ignore ) {
    //Ignore error, as the folder was created successfully
  }
  return returnValue;
}
 
Example 20
Source File: AbstractReader.java    From bluima with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        FileSystemManager fsManager = VFS.getManager();
        final Pattern pmidP = Pattern
                .compile("<PMID Version=\"\\d+\">(.*?)</PMID>");

        DirectoryIterator it = new DefaultDirectoryIterator();
        it.setDirectory(new File(
        // "/Volumes/HDD2/ren_scratch/Dropbox/dev_shared/tmp3/"));
                "/Volumes/simulation/nip/pubmed_gzip/medline14/"));
        it.setExtensionFilter("xml.gz");
        it.setRecursive(false);
        Iterator<File> fit = it.iterator();

        while (fit.hasNext()) {
            File f = fit.next();

            // "medline14n0456.xml.gz" --> 456
            int archiveId = parseInt(f.getName().substring(10, 14));
            if (archiveId > 755) {

                try {
                    FileObject archive = fsManager.resolveFile("gz:file://"
                            + f.getAbsolutePath());
                    FileObject fo = archive.getChildren()[0];
                    LOG.debug("extracted file {} from archive {}",
                            fo.getName(), f.getName());
                    if (fo.isReadable() && fo.getType() == FileType.FILE) {
                        FileContent fc = fo.getContent();

                        String articles = IOUtils.toString(fc.getInputStream(),
                                "UTF-8");
                        String[] split = articles.split("<MedlineCitation");

                        for (int i = 1; i < split.length - 1; i++) {

                            String article = "<MedlineCitation" + split[i];
                            if (!article.startsWith("<MedlineCitationSet>")) {

                                Matcher matcher = pmidP.matcher(article);
                                if (matcher.find()) {
                                    int pmid = Integer.parseInt(matcher
                                            .group(1));
                                    String filePath = StructuredDirectory
                                            .getFilePath(pmid, "xml");
                                    File file = new File(
                                    // "/Volumes/HDD2/ren_scratch/Dropbox/dev_shared/tmp3/xml/"
                                            "/Volumes/simulation/nip/pubmed_xml/"
                                                    + filePath);
                                    file.getParentFile().mkdirs();

                                    // System.out.println(pmid);
                                    FileOutputStream out = new FileOutputStream(
                                            file);
                                    IOUtils.write(article, out);
                                    IOUtils.closeQuietly(out);
                                } else {
                                    System.err
                                            .println("could not extract pmid from "
                                                    + article);
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    LOG.error("error with " + f.getName(), e);
                }
            }
        }
    }