Example usage for org.apache.commons.io FilenameUtils isExtension

List of usage examples for org.apache.commons.io FilenameUtils isExtension

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils isExtension.

Prototype

public static boolean isExtension(String filename, Collection<String> extensions) 

Source Link

Document

Checks whether the extension of the filename is one of those specified.

Usage

From source file:ryerson.daspub.utility.PDFUtils.java

/**
 * Write a JPG image of a specific page in a PDF document. If the page
 * number does not exist in the document, nothing will be written.
 * @param Input PDF input file/*from   ww  w  .jav  a2  s.co  m*/
 * @param Output Output file
 * @param Width Maximum thumbnail width
 * @param Height Maximum thumbnail height
 * @param Page Page number. Zero based page index.
 */
public static void writeJPGImage(File Input, File Output, int Width, int Height, int Page)
        throws PdfException, IOException {
    if (FilenameUtils.isExtension(Input.getName(), "pdf")) {
        pdf.openPdfFile(Input.getAbsolutePath());
        if (Page < pdf.getPageCount()) {
            BufferedImage img = pdf.getPageAsImage(Page + 1); // PDF page index starts at 1
            logger.log(Level.INFO, "Writing JPG image \"{0}\"", Output.getAbsolutePath());
            Thumbnails.of(img).outputFormat("jpg").outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC)
                    .toFile(Output);
        } else {
            logger.log(Level.WARNING,
                    "Could not write PDF thumbnail for \"{0}\". Requested page number does not exist.",
                    Input.getAbsolutePath());
        }
        pdf.closePdfFile();
    } else {
        logger.log(Level.WARNING, "Could not write PDF thumbnail for {0}.", Input.getAbsolutePath());
    }
}

From source file:ryerson.daspub.utility.PDFUtils.java

/**
 * Write JPG image of each document page.
 * @param Input PDF input file/*from  w w  w.  j  a v  a 2s .c  om*/
 * @param Output Output folder or output file name.
 * @param Width Maximum thumbnail width
 * @param Height Maximum thumbnail height
 * @throws PdfException
 * @throws IOException
 */
public static List<File> writeJPGImageAllPDFPages(File Input, File Output, int Width, int Height)
        throws PdfException, IOException {
    ArrayList<File> files = new ArrayList<File>();
    if (FilenameUtils.isExtension(Input.getName(), "pdf")) {
        pdf.openPdfFile(Input.getAbsolutePath());
        int count = pdf.getPageCount();
        for (int i = 0; i < count; i++) {
            BufferedImage img = pdf.getPageAsImage(i + 1); // PDF page index starts at 1
            File output = getIncrementedFileName(Output, i, "jpg");
            logger.log(Level.INFO, "Writing JPG image \"{0}\"", output.getAbsolutePath());
            Thumbnails.of(img).outputFormat("jpg").outputQuality(1.0f).scalingMode(ScalingMode.BICUBIC)
                    .size(Width, Height).toFile(output);
            files.add(output);
        }
        pdf.closePdfFile();
    } else {
        logger.log(Level.WARNING, "Could not write JPG images for PDF {0}.", Input.getAbsolutePath());
    }
    // return result
    return files;
}

From source file:se.trixon.pacoma.ui.MainFrame.java

private void initListeners() {
    mActionManager.addAppListener(new ActionManager.AppListener() {
        @Override/* w  ww.  j  av  a2  s  . c  o m*/
        public void onCancel(ActionEvent actionEvent) {
        }

        @Override
        public void onMenu(ActionEvent actionEvent) {
            if (actionEvent.getSource() != menuButton) {
                menuButtonMousePressed(null);
            }
        }

        @Override
        public void onOptions(ActionEvent actionEvent) {
            showOptions();
        }

        @Override
        public void onQuit(ActionEvent actionEvent) {
            quit();
        }

        @Override
        public void onRedo(ActionEvent actionEvent) {
            mCollage.nextHistory();
            updateToolButtons();
        }

        @Override
        public void onStart(ActionEvent actionEvent) {
        }

        @Override
        public void onUndo(ActionEvent actionEvent) {
            mCollage.prevHistory();
            updateToolButtons();
        }
    });

    mActionManager.addProfileListener(new ActionManager.ProfileListener() {
        @Override
        public void onAdd(ActionEvent actionEvent) {
            addImages();
        }

        @Override
        public void onClear(ActionEvent actionEvent) {
            mCollage.clearFiles();
        }

        @Override
        public void onClose(ActionEvent actionEvent) {
            setTitle("pacoma");
            mActionManager.setEnabledDocumentActions(false);
            canvasPanel.close();
        }

        @Override
        public void onEdit(ActionEvent actionEvent) {
            editCollage(mCollage);
        }

        @Override
        public void onRegenerate(ActionEvent actionEvent) {
            //TODO
        }

        @Override
        public void onNew(ActionEvent actionEvent) {
            editCollage(null);
            if (mCollage != null && mCollage.getName() != null) {
                setTitle(mCollage);
                canvasPanel.open(mCollage);
                mActionManager.getAction(ActionManager.CLEAR).setEnabled(false);
                mActionManager.getAction(ActionManager.REGENERATE).setEnabled(false);
            }
        }

        @Override
        public void onOpen(ActionEvent actionEvent) {
            initFileDialog(mCollageFileNameExtensionFilter);

            if (SimpleDialog.openFile()) {
                try {
                    open(SimpleDialog.getPath());
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        @Override
        public void onSave(ActionEvent actionEvent) {
            save();
        }

        @Override
        public void onSaveAs(ActionEvent actionEvent) {
            saveAs();
        }
    });

    mCollagePropertyChangeListener = () -> {
        if (mCollage != null) {
            setTitle(mCollage);
            mActionManager.getAction(ActionManager.SAVE).setEnabled(true);
            mActionManager.getAction(ActionManager.CLEAR).setEnabled(mCollage.hasImages());
            mActionManager.getAction(ActionManager.REGENERATE).setEnabled(mCollage.hasImages());
        }
    };

    mDropTarget = new DropTarget() {
        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                LinkedList<File> droppedFiles = new LinkedList<>(
                        (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                List<File> invalidFiles = new LinkedList<>();

                droppedFiles.forEach((droppedFile) -> {
                    if (droppedFile.isFile() && FilenameUtils.isExtension(
                            droppedFile.getName().toLowerCase(Locale.getDefault()), Collage.FILE_EXT)) {
                        //all ok
                    } else {
                        invalidFiles.add(droppedFile);
                    }
                });

                invalidFiles.forEach((invalidFile) -> {
                    droppedFiles.remove(invalidFile);
                });

                switch (droppedFiles.size()) {
                case 0:
                    Message.error(MainFrame.this, Dict.Dialog.TITLE_IO_ERROR.toString(),
                            "Not a valid collage file.");
                    break;
                case 1:
                    open(droppedFiles.getFirst());
                    break;
                default:
                    Message.error(MainFrame.this, Dict.Dialog.TITLE_IO_ERROR.toString(),
                            "Too many files dropped.");
                    break;
                }
            } catch (UnsupportedFlavorException | IOException ex) {
                System.err.println(ex.getMessage());
            }
        }
    };

    canvasPanel.setDropTarget(mDropTarget);
}

From source file:se.trixon.pacoma.ui.PagePanel.java

private void init() {
    mDropTarget = new DropTarget() {
        @Override//  w  w w.  j  a va2s  .c  om
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                LinkedList<File> droppedFiles = new LinkedList<>(
                        (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                List<File> invalidFiles = new LinkedList<>();

                droppedFiles.forEach((droppedFile) -> {
                    if (droppedFile.isFile()
                            && FilenameUtils.isExtension(droppedFile.getName().toLowerCase(Locale.getDefault()),
                                    new String[] { "jpg", "jpeg", "png" })) {
                        //all ok
                    } else {
                        invalidFiles.add(droppedFile);
                    }
                });

                invalidFiles.forEach((invalidFile) -> {
                    droppedFiles.remove(invalidFile);
                    System.out.println("remomve invalid file: " + invalidFile.getAbsolutePath());
                });

                droppedFiles.forEach((droppedFile) -> {
                    System.out.println("accept: " + droppedFile.getAbsolutePath());
                });

                mCollage.addFiles(droppedFiles);
            } catch (UnsupportedFlavorException | IOException ex) {
                System.err.println(ex.getMessage());
            }
        }
    };

    setDropTarget(mDropTarget);
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploader.XmlFileImporter.java

public boolean importFiles(final File fileOrDirectory, final Progress progress)
        throws IOException, DicomException {
    boolean anyFiles = false;

    // Check if we are importing a file or directory. For a directory we import all files recursively
    if (fileOrDirectory.isDirectory()) {
        Iterator it = FileUtils.iterateFiles(fileOrDirectory, new String[] { "xml" }, true);
        while (it.hasNext()) {
            final File nextXmlFile = (File) it.next();
            anyFiles = importXmlFile(progress, nextXmlFile) || anyFiles;
        }//  ww  w . j  av  a2 s. c o  m
    } else {
        if (fileOrDirectory.isFile() && FilenameUtils.isExtension(fileOrDirectory.getName(), "xml")) {
            anyFiles = importXmlFile(progress, fileOrDirectory) || anyFiles;
        }
    }
    return anyFiles;
}