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

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

Introduction

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

Prototype

public static String removeExtension(String filename) 

Source Link

Document

Removes the extension from a filename.

Usage

From source file:com.ephesoft.dcma.webservice.util.WebServiceHelper.java

public void processCreateHOCRXML(final HttpServletRequest req, final HttpServletResponse resp)
        throws InternalServerException, ValidationException {
    LOGGER.info("Start processing web service for create HOCR-XML for Batch Class");
    String respStr = WebServiceUtil.EMPTY_STRING;
    String workingDir = WebServiceUtil.EMPTY_STRING;
    File zipInFolder = null;/*  ww w . jav  a2s  .  co m*/
    File zipOutFolder = null;
    int responseCode = 0;
    WebServiceParams webServiceParams = null;
    if (req instanceof DefaultMultipartHttpServletRequest) {
        try {
            final String webServiceFolderPath = batchSchemaService.getWebServicesFolderPath();
            workingDir = WebServiceUtil.createWebServiceWorkingDir(webServiceFolderPath);
            final String outputDir = WebServiceUtil.createWebServiceOutputDir(workingDir);
            final DefaultMultipartHttpServletRequest multiPartRequest = (DefaultMultipartHttpServletRequest) req;
            final MultiValueMap<String, MultipartFile> fileMap = multiPartRequest.getMultiFileMap();
            if (fileMap.size() != 2) {
                LOGGER.error("Invalid Number of files sent to the server");
                respStr = WebServiceConstants.INVALID_ARGUMENTS_FOR_CREATE_HOCR;
                responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE;
            } else {
                String xmlFileName = WebServiceUtil.EMPTY_STRING;
                String zipOutputLocation = WebServiceUtil.EMPTY_STRING;
                String zipFileNameWithOutExt = WebServiceUtil.EMPTY_STRING;
                for (final String fileName : fileMap.keySet()) {
                    InputStream instream = null;
                    OutputStream outStream = null;
                    ZipInputStream zipstream = null;
                    try {

                        if (!(fileName.endsWith(FileType.ZIP.getExtensionWithDot())
                                || fileName.endsWith(FileType.XML.getExtensionWithDot())
                                || fileName.endsWith(FileType.TIF.getExtensionWithDot())
                                || fileName.endsWith(FileType.TIFF.getExtensionWithDot()))) {
                            respStr = WebServiceConstants.INVALID_ARGUMENTS_FOR_CREATE_OCR_BATCH_CLASS;
                            responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE;
                        }
                        final MultipartFile multiPartFile = multiPartRequest.getFile(fileName);
                        instream = multiPartFile.getInputStream();

                        if (fileName.endsWith(FileType.XML.getExtensionWithDot())) {
                            xmlFileName = fileName;
                            final File file = new File(workingDir + File.separator + fileName);
                            outStream = new FileOutputStream(file);
                            final byte[] buf = new byte[WebServiceUtil.bufferSize];
                            int len;
                            while ((len = instream.read(buf)) > 0) {
                                outStream.write(buf, 0, len);
                            }
                        }
                        if (fileName.endsWith(FileType.ZIP.getExtensionWithDot())) {
                            zipFileNameWithOutExt = FilenameUtils.removeExtension(fileName);
                            zipInFolder = new File(workingDir + File.separator + zipFileNameWithOutExt);
                            if (zipInFolder != null) {
                                zipInFolder.mkdirs();
                            }
                            zipstream = new ZipInputStream(instream);
                            ZipEntry ze = zipstream.getNextEntry();
                            if (ze == null) {
                                respStr = WebServiceConstants.NO_FILES_IN_ZIP_DIR;
                                responseCode = WebServiceConstants.NO_FILES_IN_ZIP_DIR_CODE;
                                LOGGER.error(respStr + " No files in the zip directory ");
                            }
                            while (ze != null) {
                                String upzipFileName = ze.getName();
                                LOGGER.info("Unzipping " + upzipFileName);

                                if (!(upzipFileName.endsWith(FileType.TIF.getExtensionWithDot())
                                        || upzipFileName.endsWith(FileType.TIFF.getExtensionWithDot())
                                        || upzipFileName.endsWith(FileType.PDF.getExtensionWithDot()))) {
                                    respStr = WebServiceConstants.UNSUPPORTED_FILE_TYPE_EXCEPTION_MESSAGE;
                                    responseCode = WebServiceConstants.UNSUPPORTED_FILE_TYPE_EXCEPTION_CODE;
                                    LOGGER.error("File name should be a valid tif, tiff or pdf file name only");
                                }
                                final File filePath = new File(zipInFolder + File.separator + upzipFileName);
                                outStream = new FileOutputStream(filePath);
                                final byte[] buf = new byte[WebServiceUtil.bufferSize];
                                int len;
                                while ((len = zipstream.read(buf)) > 0) {
                                    outStream.write(buf, 0, len);
                                }
                                final int pageCount = TIFFUtil
                                        .getTIFFPageCount(zipInFolder + File.separator + upzipFileName);
                                if (pageCount > 1
                                        || upzipFileName.endsWith(FileType.PDF.getExtensionWithDot())) {
                                    final BatchInstanceThread threadList = new BatchInstanceThread(
                                            new File(zipInFolder.toString()).getName() + Math.random());
                                    LOGGER.info(
                                            "Start spliting multipage tiff/pdf file into tiffs using image magick");
                                    imService.convertPdfOrMultiPageTiffToTiffUsingIM("", filePath, "",
                                            new File(EphesoftStringUtil.concatenate(zipInFolder.toString(),
                                                    File.separator, upzipFileName)),
                                            threadList);
                                    threadList.execute();
                                }
                                ze = zipstream.getNextEntry();
                            }
                        }
                        if (fileName.endsWith(FileType.TIFF.getExtensionWithDot())
                                || fileName.endsWith(FileType.TIF.getExtensionWithDot())) {
                            zipFileNameWithOutExt = WebServiceUtil.EMPTY_STRING;
                            final File file = new File(workingDir + File.separator + fileName);
                            zipInFolder = new File(workingDir);
                            outStream = new FileOutputStream(file);
                            final byte[] buf = new byte[WebServiceUtil.bufferSize];
                            int len;
                            while ((len = instream.read(buf)) > 0) {
                                outStream.write(buf, 0, len);
                            }
                            final int pageCount = TIFFUtil
                                    .getTIFFPageCount(workingDir + File.separator + fileName);
                            if (pageCount > 1) {
                                final BatchInstanceThread threadList = new BatchInstanceThread(
                                        zipInFolder.getName() + Math.random());
                                LOGGER.info(
                                        "Start spliting multipage tiff/pdf file into tiffs using image magick");
                                imService.convertPdfOrMultiPageTiffToTiffUsingIM(WebServiceUtil.EMPTY_STRING,
                                        file, WebServiceUtil.EMPTY_STRING, new File(EphesoftStringUtil
                                                .concatenate(workingDir.toString(), File.separator, fileName)),
                                        threadList);
                                threadList.execute();
                            }
                        }
                    } finally {
                        IOUtils.closeQuietly(instream);
                        IOUtils.closeQuietly(outStream);
                        IOUtils.closeQuietly(zipstream);
                    }
                }
                final File xmlFile = new File(workingDir + File.separator + xmlFileName);
                if (StringUtils.isNotEmpty(xmlFileName) && xmlFile.exists()) {
                    final FileInputStream inputStream = new FileInputStream(xmlFile);
                    final Source source = XMLUtil.createSourceFromStream(inputStream);
                    final Object unmarshelledObject = batchSchemaDao.getJAXB2Template().getJaxb2Marshaller()
                            .unmarshal(source);
                    if (!(unmarshelledObject instanceof WebServiceParams)) {
                        respStr = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_MESSAGE;
                        responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE;
                    } else {
                        webServiceParams = (WebServiceParams) unmarshelledObject;
                        if (null != webServiceParams.getParams()) {
                            final List<Param> paramList = webServiceParams.getParams().getParam();
                            if (paramList == null || paramList.isEmpty()) {
                                final HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
                                respStr = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_MESSAGE;
                                responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE;
                                LOGGER.error(respStr + "\n No Parameters in the request" + status);
                            } else {
                                String batchClassId = WebServiceUtil.EMPTY_STRING;
                                for (final Param param : paramList) {
                                    if (WebServiceConstants.BATCH_CLASS_IDENTIFIER
                                            .equalsIgnoreCase(param.getName())) {
                                        batchClassId = param.getValue();
                                        continue;
                                    }
                                    if (WebServiceUtil.ZIP_OUTPUT_LOCATION.equalsIgnoreCase(param.getName())) {
                                        zipOutputLocation = param.getValue();
                                        if (StringUtils.isBlank(zipOutputLocation)
                                                || !(new File(zipOutputLocation).isDirectory())) {
                                            respStr = WebServiceConstants.ZIP_OUTPUT_LOCATION_INVALID_MESSAGE;
                                            responseCode = WebServiceConstants.ZIP_OUTPUT_LOCATION_INVALID_CODE;
                                            LOGGER.error(
                                                    "Zip output location is blank or invalid in xml input file");
                                        }
                                        continue;
                                    }
                                    if (WebServiceUtil.ZIP_NAME.equalsIgnoreCase(param.getName())) {
                                        if (!((zipFileNameWithOutExt + FileType.ZIP.getExtensionWithDot())
                                                .equals(param.getValue()))) {
                                            respStr = WebServiceConstants.INPUT_ZIP_NOT_FOUND_MESSAGE;
                                            responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE;
                                            LOGGER.error(
                                                    "Zip file name doesn't match zip file name in xml input file");
                                        } else
                                            continue;
                                    }
                                }
                                if (respStr == null || respStr.isEmpty()) {
                                    if (batchClassId != null && !batchClassId.isEmpty()) {
                                        final BatchClass batchClass = batchClassService
                                                .getBatchClassByIdentifier(batchClassId);
                                        if (batchClass != null) {

                                            final String ocrEngine = getDefaultHOCRPlugin(batchClassId);
                                            String colorSwitch = WebServiceUtil.EMPTY_STRING;
                                            String cmdLanguage = WebServiceUtil.EMPTY_STRING;
                                            LOGGER.info(EphesoftStringUtil.concatenate("Ocr engine used is : ",
                                                    ocrEngine));

                                            if (WebServiceConstants.TESSERACT_HOCR_PLUGIN
                                                    .equalsIgnoreCase(ocrEngine)) {
                                                final File dir = new File(zipInFolder + "");
                                                File[] directoryListing = dir.listFiles();
                                                if (directoryListing != null) {
                                                    zipOutFolder = new File(
                                                            outputDir + File.separator + zipFileNameWithOutExt);
                                                    if (zipOutFolder != null) {
                                                        zipOutFolder.mkdirs();
                                                    }
                                                    for (File inputFile : directoryListing) {
                                                        String inputFileName = inputFile.getName();
                                                        if (inputFileName != null && !inputFileName.isEmpty()) {
                                                            if (inputFileName.endsWith(
                                                                    FileType.TIFF.getExtensionWithDot())
                                                                    || inputFileName.endsWith(FileType.TIF
                                                                            .getExtensionWithDot())) {
                                                                final int pageCountTiff = TIFFUtil
                                                                        .getTIFFPageCount(inputFile.toString());
                                                                if (pageCountTiff > 1) {
                                                                    continue;
                                                                }

                                                                if (WebServiceConstants.TESSERACT_HOCR_PLUGIN
                                                                        .equalsIgnoreCase(ocrEngine)) {
                                                                    final BatchPlugin pluginProperties = classPluginPropertiesService
                                                                            .getPluginProperties(batchClassId,
                                                                                    WebServiceConstants.TESSERACT_HOCR_PLUGIN);
                                                                    if (pluginProperties != null) {
                                                                        if (pluginProperties
                                                                                .getPluginConfigurations(
                                                                                        TesseractProperties.TESSERACT_COLOR_SWITCH) != null) {
                                                                            colorSwitch = classPluginPropertiesService
                                                                                    .getPropertyValue(
                                                                                            batchClassId,
                                                                                            WebServiceConstants.TESSERACT_HOCR_PLUGIN,
                                                                                            TesseractProperties.TESSERACT_COLOR_SWITCH);
                                                                            if (pluginProperties
                                                                                    .getPluginConfigurations(
                                                                                            TesseractProperties.TESSERACT_LANGUAGE) != null) {
                                                                                cmdLanguage = classPluginPropertiesService
                                                                                        .getPropertyValue(
                                                                                                batchClassId,
                                                                                                WebServiceConstants.TESSERACT_HOCR_PLUGIN,
                                                                                                TesseractProperties.TESSERACT_LANGUAGE);
                                                                                tesseractService.createOCR(
                                                                                        zipInFolder.toString(),
                                                                                        colorSwitch,
                                                                                        inputFileName,
                                                                                        zipOutFolder.toString(),
                                                                                        cmdLanguage,
                                                                                        WebServiceConstants.TESSERACT_CURRENT_VERSION);
                                                                            } else {
                                                                                respStr = WebServiceConstants.NO_TESSERACT_LANGUAGE_SUPPORT;
                                                                                responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                                                LOGGER.error(
                                                                                        "No Language Support");
                                                                            }

                                                                        } else {
                                                                            respStr = WebServiceConstants.NO_TESSERACT_COLOR_SWITCH;
                                                                            responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                                            LOGGER.error(
                                                                                    "Colour Switch Not Found");
                                                                        }
                                                                    } else {
                                                                        respStr = WebServiceConstants.NO_PROPERTY_FOR_TESSERACT_HOCR;
                                                                        responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                                    }
                                                                }

                                                            }
                                                        } else {
                                                            respStr = WebServiceConstants.INVALID_FILE_NAME;
                                                            responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE;
                                                            LOGGER.error(
                                                                    "File Name should not be NULL or empty ");
                                                        }
                                                    } // End of for loop

                                                }

                                                else {
                                                    respStr = WebServiceConstants.NO_FILES_IN_ZIP_DIR;
                                                    responseCode = WebServiceConstants.NO_FILES_IN_ZIP_DIR_CODE;
                                                    LOGGER.error(respStr + " No files in the zip directory ");
                                                }

                                            }
                                            if (respStr.isEmpty()) {
                                                ServletOutputStream out = null;
                                                ZipOutputStream zout = null;
                                                final String zipFileName = WebServiceUtil.serverOutputFolderName;
                                                resp.setContentType(WebServiceUtil.APPLICATION_X_ZIP);
                                                resp.setHeader(WebServiceUtil.CONTENT_DISPOSITION,
                                                        WebServiceUtil.ATTACHMENT_FILENAME + zipFileName
                                                                + FileType.ZIP.getExtensionWithDot()
                                                                + "\"\r\n");
                                                resp.setStatus(HttpServletResponse.SC_OK);
                                                try {
                                                    out = resp.getOutputStream();
                                                    zout = new ZipOutputStream(out);
                                                    FileUtils.zipDirectory(zipOutFolder.toString(), zout,
                                                            zipFileName);
                                                } catch (final FileNotFoundException fileNotFoundException) {
                                                    String messageString = fileNotFoundException.getMessage();
                                                    messageString = messageString.substring(
                                                            messageString.lastIndexOf(File.separator));
                                                    respStr = WebServiceConstants.FILE_NOT_FOUND
                                                            + messageString;
                                                    responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                    LOGGER.error("Could Not Copy the File "
                                                            + fileNotFoundException.getMessage());
                                                } catch (final IOException ioExcpetion) {
                                                    respStr = WebServiceConstants.ERROR_WHILE_CREATING_ZIPPED_FILE
                                                            + ioExcpetion;
                                                    responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                    LOGGER.error(respStr);
                                                } finally {
                                                    IOUtils.closeQuietly(zout);
                                                    IOUtils.closeQuietly(out);
                                                }
                                            }
                                            if (respStr.isEmpty()) {
                                                FileOutputStream fos = null;
                                                ZipOutputStream zos = null;
                                                final File out = new File(zipOutputLocation + File.separator
                                                        + zipFileNameWithOutExt
                                                        + FileType.ZIP.getExtensionWithDot());
                                                try {
                                                    fos = new FileOutputStream(out);
                                                    zos = new ZipOutputStream(fos);
                                                    FileUtils.zipDirectory(zipOutFolder.toString(), zos,
                                                            zipFileNameWithOutExt);

                                                } catch (final FileNotFoundException fileNotFoundException) {
                                                    String messageString = fileNotFoundException.getMessage();
                                                    messageString = messageString.substring(
                                                            messageString.lastIndexOf(File.separator));
                                                    respStr = WebServiceConstants.FILE_NOT_FOUND
                                                            + messageString;
                                                    responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                    LOGGER.error("Could Not Copy the File "
                                                            + fileNotFoundException.getMessage());
                                                } catch (final IOException ioExcpetion) {
                                                    respStr = WebServiceConstants.ERROR_WHILE_CREATING_ZIPPED_FILE
                                                            + ioExcpetion;
                                                    responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
                                                    LOGGER.error(respStr);
                                                } finally {
                                                    IOUtils.closeQuietly(zos);
                                                    IOUtils.closeQuietly(fos);
                                                }
                                            }

                                        } else {
                                            respStr = WebServiceConstants.INVALID_BATCH_CLASS_ID_MESSAGE;
                                            responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE;
                                            LOGGER.error(respStr + " Batch Class ID doesnot exist ");
                                        }
                                    } else {
                                        respStr = WebServiceConstants.INVALID_BATCH_CLASS_ID_MESSAGE;
                                        responseCode = WebServiceConstants.INVALID_ARGUMENTS_IN_XML_INPUT_CODE;
                                        LOGGER.error(respStr + " No input of Batch Class ID ");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    respStr = WebServiceConstants.INPUT_FILES_NOT_FOUND_MESSAGE;
                    responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE;
                }
            }
        } catch (final FileNotFoundException fileNotFoundException) {
            String message = fileNotFoundException.getMessage();
            message = message.substring(message.lastIndexOf(File.separator));
            respStr = WebServiceConstants.FILE_NOT_FOUND + message;
        } catch (final ValidationException validationException) {
            throw validationException;
        } catch (final InternalServerException internalServerError) {
            throw internalServerError;
        } catch (final Exception exception) {
            respStr = WebServiceConstants.INTERNAL_SERVER_ERROR_MESSAGE + exception;
            responseCode = WebServiceConstants.INTERNAL_SERVER_ERROR_CODE;
            exception.printStackTrace();
        } finally {
            FileUtils.deleteDirectoryAndContentsRecursive(new File(workingDir));
        }
    } else {
        respStr = WebServiceConstants.INVALID_MULTIPART_REQUEST;
        responseCode = WebServiceConstants.INVALID_PARAMETERS_CODE;
    }
    validateResponse(responseCode, respStr);
}

From source file:net.sourceforge.subsonic.service.PodcastService.java

private synchronized File getFile(PodcastChannel channel, PodcastEpisode episode) {

    File podcastDir = new File(settingsService.getPodcastFolder());
    File channelDir = new File(podcastDir, StringUtil.fileSystemSafe(channel.getTitle()));

    if (!channelDir.exists()) {
        boolean ok = channelDir.mkdirs();
        if (!ok) {
            throw new RuntimeException("Failed to create directory " + channelDir);
        }/*  w  ww. j av a 2 s. c  om*/

        MediaFile mediaFile = mediaFileService.getMediaFile(channelDir);
        mediaFile.setComment(channel.getDescription());
        mediaFileService.updateMediaFile(mediaFile);
    }

    String filename = StringUtil.getUrlFile(episode.getUrl());
    if (filename == null) {
        filename = episode.getTitle();
    }
    filename = StringUtil.fileSystemSafe(filename);
    String extension = FilenameUtils.getExtension(filename);
    filename = FilenameUtils.removeExtension(filename);
    if (StringUtils.isBlank(extension)) {
        extension = "mp3";
    }

    File file = new File(channelDir, filename + "." + extension);
    for (int i = 0; file.exists(); i++) {
        file = new File(channelDir, filename + i + "." + extension);
    }

    if (!securityService.isWriteAllowed(file)) {
        throw new SecurityException("Access denied to file " + file);
    }
    return file;
}

From source file:net.stargraph.server.KBResourceImpl.java

@Override
public Response upload(String id, String type, FormDataMultiPart form) {
    KBCore core = stargraph.getKBCore(id);
    final KBId kbId = KBId.of(id, type);
    Indexer indexer = core.getIndexer(type);

    // get file information
    FormDataBodyPart filePart = form.getField("file");
    InputStream fileInputStream = filePart.getValueAs(InputStream.class);
    ContentDisposition contentDisposition = filePart.getContentDisposition();
    String fileName = contentDisposition.getFileName();
    String content;/* www .java2 s  .c o  m*/
    try {
        content = IOUtils.toString(fileInputStream, "UTF-8");
    } catch (IOException e) {
        logger.error(marker, "Failed to retrieve document content");
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

    // index
    try {
        if (type.equals("documents")) {
            String docId = fileName; //TODO get from other source?
            String docTitle = FilenameUtils.removeExtension(fileName); //TODO get from other source?
            indexer.index(new Indexable(new Document(docId, docTitle, null, content), kbId));
            indexer.flush();
        } else {
            logger.error(marker, "Type not supported yet: " + type);
            return Response.status(Response.Status.NOT_IMPLEMENTED).build();
        }
    } catch (InterruptedException e) {
        logger.error(marker, "Indexing failed.");
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

    return ResourceUtils.createAckResponse(true);
}

From source file:nl.fontys.sofa.limo.view.project.actions.ExportChainAction.java

private void openFileChooser() throws HeadlessException, IOException {
    JFileChooser fc = new ChainSaveFileChooser();
    FileNameExtensionFilter chainFilter = new FileNameExtensionFilter("Supply chains (*.lsc)", "lsc");

    if (supplyChain.getFilepath() != null) { //This happens if a supply chain is loaded.
        fc.setCurrentDirectory(new File(supplyChain.getFilepath()));
    }//w w  w.  jav a2s.co m

    fc.setFileFilter(chainFilter);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setSelectedFile(new File(FilenameUtils.removeExtension(supplyChain.getName()))); // This sets the name without the extension
    int result = fc.showOpenDialog(null);
    String fileName = fc.getSelectedFile().getName(); //name with extension
    if (result == JFileChooser.APPROVE_OPTION) { //If folder is selected than save the supply chain.
        supplyChain.setName(fileName);
        File file = fc.getSelectedFile();
        supplyChain.saveToFile(file.getAbsolutePath() + ".lsc");
    } else { //If no folder is selected throw an exception so the saving process is cancelled.
        throw new IOException("The supply chain " + supplyChain.getName() + " is invalid.");
    }
}

From source file:nl.tudelft.goal.SimpleIDE.FilePanel.java

/**
 * Renames a file to a new name. THe new name will be asked from the user.
 * If the selected target file exists we overwrite the existing file with
 * the given (after user's confirmation).
 *
 * @param oldFile/*from  www  . j  a va  2  s .  co  m*/
 *            is the file to be renamed.
 * @throws GOALException
 * @throws ParserException
 */
public void rename(File oldFile)
        throws GOALException, ParserException, InvalidEmotionConfigFile, FileNotFoundException {
    // step 1. ask new filename.
    // use the general getExtension to support unknown extensions properly.
    String extension = FilenameUtils.getExtension(oldFile.getName());
    String oldfilename = FilenameUtils.removeExtension(oldFile.getAbsolutePath());
    File newFile = null;
    try {
        newFile = SimpleIDE.askFile(this.mainpanel, false, "Save as", //$NON-NLS-1$
                JFileChooser.FILES_ONLY, extension, oldfilename, PMPreferences.getAgentBrowsePath(), true);
    } catch (GOALCommandCancelledException ignore) {
        return;
    }
    if (PMPreferences.getRememberLastUsedAgentDir()) {
        PMPreferences.setAgentBrowsePath(newFile.getParent());
    }
    Extension ext = Extension.getFileExtension(newFile);
    // step 1b. disallow renaming to an existing project present in the IDE
    if (ext == Extension.MAS && containsFile(newFile)) {
        throw new GOALUserError("Cannot rename a MAS project to " //$NON-NLS-1$
                + "another MAS project present in the IDE."); //$NON-NLS-1$
    }

    // this is a bit tricky, as we should try to keep this functionality
    // separate
    // from the editor panel functionality...
    // step 2. close editor(s)
    boolean isEditingOldFile = EditManager.getInstance().isOpenEditor(oldFile);
    // mainPanel.getEditPanel().saveAs(newFile.getAbsolutePath());
    // edit panel close old editor and open new one.
    if (isEditingOldFile) {
        EditManager.getInstance().close(oldFile);
    }
    boolean isEditingNewFile = EditManager.getInstance().isOpenEditor(newFile);
    // mainPanel.getEditPanel().saveAs(newFile.getAbsolutePath());
    // edit panel close old editor and open new one.
    if (isEditingNewFile) {
        EditManager.getInstance().close(newFile);
    }

    // step 3. make copy
    try {
        FileInputStream input = new FileInputStream(oldFile);
        FileOutputStream output = new FileOutputStream(newFile);
        IOUtils.copy(input, output);
        input.close();
        output.close();
    } catch (IOException e) {
        throw new GOALUserError("Cannot copy " + oldFile + " to " + newFile, e); //$NON-NLS-1$ //$NON-NLS-2$
    }

    // step 4. reload some stuff
    // HACK, #1061
    switch (ext) {
    case GOAL:
        KRInterface language = this.platform.getAgentProgram(oldFile).getKRInterface();
        this.platform.parseGOALFile(newFile, language);
        this.platform.removeParsedProgram(oldFile);
        break;
    case MAS:
        // add new file, remove old file
        this.platform.parseMASFile(newFile);
        this.platform.removeParsedProgram(oldFile);
        break;
    default:
        // other files are not parsed
        break;
    }

    // step 5. fix file panel.
    boolean oldFileInUse = !handleFileRename(oldFile, newFile);

    // step 6. refresh nodes, but only if any of the nodes reference the
    // new file
    if (containsFile(newFile)) {
        if (ext == Extension.MAS) {
            // refresh the MAS file node. placed here since it is
            // not part of the #1061 hack
            refreshMASFile(newFile);
        } else if (ext == Extension.GOAL) {
            refreshGOALFile(newFile);
        }
    }

    // step 7. restore editing configuration if possible.
    if (isEditingOldFile && oldFileInUse) {
        EditManager.getInstance().editFile(oldFile);
    }
    if (isEditingOldFile || isEditingNewFile) {
        EditManager.getInstance().editFile(newFile);
    }

    // step 8. delete old file if not used anymore.
    if (!oldFileInUse) {
        try {
            oldFile.delete();
        } catch (SecurityException e) {
            new Warning(Resources.get(WarningStrings.FAILED_REMOVE_AFTER_RENAME), e);
        }
    }
}

From source file:nl.uva.sne.classifier.Main.java

private static void nameClusters(String clustersOutDir)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    //                   String className = "nl.uva.sne.extractors.JtopiaExtractor";
    String className = "nl.uva.sne.extractors.LuceneExtractor";
    Class c = Class.forName(className);

    Object obj = c.newInstance();
    TermExtractor termExtractor = (TermExtractor) obj;
    termExtractor.configure(FileUtils.getProperties(propertiesPath));

    JtopiaExtractor e = new JtopiaExtractor();
    e.configure(FileUtils.getProperties(propertiesPath));
    File dir = new File(clustersOutDir);
    for (File f : dir.listFiles()) {
        if (FilenameUtils.getExtension(f.getName()).endsWith("txt")) {
            Logger.getLogger(Main.class.getName()).log(Level.INFO, "Processing {0}", f.getAbsolutePath());
            Map<String, Double> terms = termExtractor.termXtraction(f.getAbsolutePath());
            ValueComparator bvc = new ValueComparator(terms);
            Map<String, Double> sorted_map = new TreeMap(bvc);
            sorted_map.putAll(terms);//from  ww  w .  j  a  va2s  .co  m
            StringBuilder name = new StringBuilder();
            int count = 0;
            for (String s : sorted_map.keySet()) {
                name.append(s).append("_");
                if (count > 4) {
                    break;
                }
                count++;
            }
            Logger.getLogger(Main.class.getName()).log(Level.INFO, "Writing cluster: {0}", name.toString());
            FileUtils.writeDictionary2File(terms, clustersOutDir + File.separator
                    + FilenameUtils.removeExtension(f.getName()) + name.toString() + ".csv");
        }
    }
}

From source file:nl.uva.sne.classifier.Main.java

private static void nameClusterFolders(String clustersOutDir) {
    File clusterDir = new File(clustersOutDir);
    Map<File, File> fromTo = new HashMap<>();
    for (File cluster : clusterDir.listFiles()) {
        if (cluster.isDirectory()) {
            int count = 0;
            String termName = null;
            for (File f : cluster.listFiles()) {
                if (FilenameUtils.getExtension(f.getName()).endsWith("term")) {
                    count++;/*from w  ww  .  j a v  a  2  s .  c o  m*/
                    termName = FilenameUtils.removeExtension(f.getName());
                }
            }
            if (count == 1) {
                fromTo.put(cluster, new File(cluster.getParent() + File.separator + termName));
            }
        }
    }
    for (File f : fromTo.keySet()) {
        f.renameTo(fromTo.get(f));
    }
}

From source file:nl.uva.sne.classifiers.CosineSimilarity.java

private Map<String, Map<String, Double>> buildClassesMap(String modelDir, String dataDirPath)
        throws IOException, ParseException, JWNLException {
    File dir = new File(modelDir);
    File[] files = dir.listFiles();
    Map<String, Map<String, Double>> classFeatureVectors = new TreeMap<>();
    for (File featureFile : files) {
        if (FilenameUtils.getExtension(featureFile.getName()).endsWith("csv")) {
            try (BufferedReader br = new BufferedReader(new FileReader(featureFile))) {
                String line;/*from w  ww.  ja  v a  2s  .  c  o m*/
                Map<String, Double> classVector = new TreeMap<>();
                while ((line = br.readLine()) != null) {
                    //                        System.err.println(featureFile + " " + line);
                    String[] parts = line.split(",");
                    String key = parts[0];
                    String val = parts[1];
                    //                        System.err.println(featureFile.getAbsolutePath() + " " + line);
                    classVector.put(key, Double.valueOf(val));
                }
                classFeatureVectors.put(FilenameUtils.removeExtension(featureFile.getName()), classVector);
            }
        }
    }

    File[] textFiles = new File(dataDirPath).listFiles();
    List<List<String>> allDocs = new ArrayList<>();
    Map<String, List<String>> docs = new HashMap<>();
    for (File f : textFiles) {
        if (FilenameUtils.getExtension(f.getName()).endsWith("txt")) {
            Logger.getLogger(CosineSimilarity.class.getName()).log(Level.INFO, "Processing {0}",
                    f.getAbsolutePath());
            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                for (String text; (text = br.readLine()) != null;) {
                    sb.append(text.toLowerCase()).append(" ");
                }
            }
            Logger.getLogger(CosineSimilarity.class.getName()).log(Level.INFO, "Processing text{0}",
                    sb.length());
            List<String> doc = SemanticUtils.tokenize(sb.toString(), true);
            allDocs.add(new ArrayList<>(doc));
            docs.put(f.getName(), new ArrayList<>(doc));
            Logger.getLogger(CosineSimilarity.class.getName()).log(Level.INFO, "Processed {0} documants",
                    docs.size());
        }
    }
    //        List<Term> terms = ClusterUtils.dir2Terms(dataDir.getAbsolutePath());
    //
    //        for (Term tv : terms) {
    //            Set<String> doc = SemanticUtils.getDocument(tv);
    //            allDocs.add(new ArrayList<>(doc));
    //            docs.put(tv.getUID(), new ArrayList<>(doc));
    //        }

    Map<String, Map<String, Double>> termVectors = new HashMap<>();
    for (String k : docs.keySet()) {
        Logger.getLogger(CosineSimilarity.class.getName()).log(Level.INFO, "Calculating tfidf for: {0}", k);
        List<String> doc = docs.get(k);
        Map<String, Double> featureVector = new TreeMap<>();
        for (String term : doc) {
            if (!featureVector.containsKey(term)) {
                double tfidf = tfIdf(doc, allDocs, term);
                featureVector.put(term, tfidf);
            }
        }
        termVectors.put(k, featureVector);
    }

    Map<String, Map<String, Double>> classesMap = new HashMap<>();
    for (String docName : termVectors.keySet()) {
        Logger.getLogger(CosineSimilarity.class.getName()).log(Level.INFO, "Setting score for: {0}", docName);
        Map<String, Double> scoreMap = new TreeMap<>();
        Map<String, Double> tVector = termVectors.get(docName);
        for (String className : classFeatureVectors.keySet()) {
            Map<String, Double> classVector = classFeatureVectors.get(className);
            Double similarity = cosineSimilarity(classVector, tVector);
            scoreMap.put(className, similarity);
        }
        classesMap.put(docName, scoreMap);
    }
    return classesMap;
}

From source file:no.digipost.android.api.ApiAccess.java

public static void uploadFile(Context context, String uri, File file) throws DigipostClientException {
    try {//w  ww . jav a  2s  . com
        try {

            FileBody filebody = new FileBody(file, ApiConstants.CONTENT_OCTET_STREAM);
            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
                    Charset.forName(ApiConstants.ENCODING));
            multipartEntity.addPart("subject", new StringBody(FilenameUtils.removeExtension(file.getName()),
                    ApiConstants.MIME, Charset.forName(ApiConstants.ENCODING)));
            multipartEntity.addPart("file", filebody);
            multipartEntity.addPart("token", new StringBody(TokenStore.getAccess()));

            URL url = new URL(uri);
            HttpsURLConnection httpsClient = (HttpsURLConnection) url.openConnection();
            httpsClient.setRequestMethod("POST");
            httpsClient.setDoOutput(true);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                httpsClient.setFixedLengthStreamingMode(multipartEntity.getContentLength());
            } else {
                httpsClient.setChunkedStreamingMode(0);
            }
            httpsClient.setRequestProperty("Connection", "Keep-Alive");
            httpsClient.addRequestProperty("Content-length", multipartEntity.getContentLength() + "");
            httpsClient.setRequestProperty(ApiConstants.AUTHORIZATION,
                    ApiConstants.BEARER + TokenStore.getAccess());
            httpsClient.addRequestProperty(multipartEntity.getContentType().getName(),
                    multipartEntity.getContentType().getValue());

            try {
                OutputStream outputStream = new BufferedOutputStream(httpsClient.getOutputStream());
                multipartEntity.writeTo(outputStream);
                outputStream.flush();
                NetworkUtilities.checkHttpStatusCode(context, httpsClient.getResponseCode());
            } finally {
                httpsClient.disconnect();
            }

        } catch (DigipostInvalidTokenException e) {
            OAuth.updateAccessTokenWithRefreshToken(context);
            uploadFile(context, uri, file);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, context.getString(R.string.error_your_network));
        throw new DigipostClientException(context.getString(R.string.error_your_network));
    }
}

From source file:opensource.zeocompanion.ZeoCompanionApplication.java

public BackupReturnResults saveCopyOfDB(String includePrefix) {
    // is external storage available, read/write, and App has been granted permission
    int r = checkExternalStorage();
    if (r == -2) {
        return new BackupReturnResults(null,
                "Permission for App to write to external storage has not been granted; please grant the permission");
    } else if (r != 0) {
        return new BackupReturnResults(null, "No writable external storage is available");
    }//from  w  w w  .  j av a  2  s.c o  m

    // get the name and folder preference
    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    String name = sPrefs.getString("profile_name", "");
    String folder = sPrefs.getString("backup_directory", "Android/data/opensource.zeocompanion/internals");

    // ensure the destination directory structure is present
    File backupsDir = null;
    if (folder != null) {
        backupsDir = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + folder);
    } else {
        backupsDir = new File(mBaseExtStorageDir + File.separator + "internals");
    }
    backupsDir.mkdirs();

    // compose source and destination File instances
    File source = getDatabasePath(CompanionDatabase.DATABASE_NAME);
    String newName = FilenameUtils.removeExtension(CompanionDatabase.DATABASE_NAME);
    if (name != null) {
        if (!name.isEmpty()) {
            newName = newName + "_" + name;
        }
    }
    newName = newName + "_DBVer" + mDatabaseHandler.mVersion + "_AppVer" + BuildConfig.VERSION_NAME + "_"
            + mFileDateFormatter.format(new Date()) + ".db";
    File dest = new File(backupsDir.getPath() + File.separator + includePrefix + newName);
    Log.d(_CTAG + ".saveCopyOfDB", "Dest=" + dest.getAbsolutePath());

    // perform the copy
    try {
        FileUtils.copyFile(source, dest);
        ZeoCompanionApplication.forceShowOnPC(dest);
    } catch (Exception e) {
        return new BackupReturnResults(null,
                "Failed to backup database because of filesystem error: " + e.getMessage());
    }
    return new BackupReturnResults(dest, "");
}