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

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

Introduction

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

Prototype

public static String getPath(String filename) 

Source Link

Document

Gets the path from a full filename, which excludes the prefix.

Usage

From source file:logic.core.file.FileSystemInit.java

public static void init() {
    try {//  ww w.j  a v  a2  s . c  o  m
        jarPathOnSystem = FilenameUtils.getPath(URLDecoder.decode(
                GameRunner.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8"));

        String dataPath = jarPathOnSystem + "/data/";

        File temp = new File(dataPath);
        temp.mkdir();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:minij.assembler.Assembler.java

public static void assemble(Configuration config, String assembly) throws AssemblerException {

    try {/*w  w w  . ja v  a 2s.c  om*/
        new File(FilenameUtils.getPath(config.outputFile)).mkdirs();

        // -xc specifies the input language as C and is required for GCC to read from stdin
        ProcessBuilder processBuilder = new ProcessBuilder("gcc", "-o", config.outputFile, "-m32", "-xc",
                MiniJCompiler.RUNTIME_DIRECTORY.toString() + File.separator + "runtime_32.c", "-m32",
                "-xassembler", "-");
        Process gccCall = processBuilder.start();
        // Write C code to stdin of C Compiler
        OutputStream stdin = gccCall.getOutputStream();
        stdin.write(assembly.getBytes());
        stdin.close();

        gccCall.waitFor();

        // Print error messages of GCC
        if (gccCall.exitValue() != 0) {

            StringBuilder errOutput = new StringBuilder();
            InputStream stderr = gccCall.getErrorStream();
            String line;
            BufferedReader bufferedStderr = new BufferedReader(new InputStreamReader(stderr));
            while ((line = bufferedStderr.readLine()) != null) {
                errOutput.append(line + System.lineSeparator());
            }
            bufferedStderr.close();
            stderr.close();

            throw new AssemblerException(
                    "Failed to compile assembly:" + System.lineSeparator() + errOutput.toString());
        }

        Logger.logVerbosely("Successfully compiled assembly");
    } catch (IOException e) {
        throw new AssemblerException("Failed to transfer assembly to gcc", e);
    } catch (InterruptedException e) {
        throw new AssemblerException("Failed to invoke gcc", e);
    }

}

From source file:minij.codeemission.CodeEmitter.java

public static String emitCode(Configuration config, List<Fragment<List<Assem>>> assemFragments,
        MachineSpecifics machineSpecifics) throws CodeEmitterException {

    try {//from www  .j  av a  2s .  co m
        String assembly = machineSpecifics.printAssembly(assemFragments);
        ;

        if (config.codeEmission) {
            new File(FilenameUtils.getPath(config.outputFile)).mkdirs();
            PrintWriter out = new PrintWriter(config.outputFile);
            out.print(assembly);
            out.close();
        }

        Logger.logVerbosely("Successfully generated assembly");

        if (config.printAssembly) {
            Logger.log(assembly);
        }

        return assembly;
    } catch (Exception e) {
        throw new CodeEmitterException("Failed to generate assembly", e);
    }
}

From source file:com.kamike.misc.FsNameUtils.java

public static String getLocalUrl(String disk, String fileName, String fid, String uid, Date date) {

    //?/*www .  j  av  a  2  s .c  o m*/
    String validPath = getName(disk, fileName, fid, uid, date);

    return FilenameUtils.getPath(validPath);
}

From source file:de.thomasvolk.genexample.bericht.templates.Template.java

public Template(String path) {
    this.name = FilenameUtils.getBaseName(path);
    this.dir = FilenameUtils.getPath(path);
    this.extension = FilenameUtils.getExtension(path);
    this.path = path;
}

From source file:com.r573.enfili.common.io.file.PathBuilder.java

private void init(String initialPath) {
    this.separator = File.separator;
    this.prefix = FilenameUtils.getPrefix(initialPath);

    this.elements = new LinkedList<String>();

    String pathWithoutPrefix = FilenameUtils.getPath(initialPath);
    String[] initialPathParts = pathWithoutPrefix.split("\\" + separator);
    for (int i = 0; i < initialPathParts.length; i++) {
        String thisPart = initialPathParts[i];
        if ((thisPart != null) && (!thisPart.isEmpty())) {
            elements.add(thisPart);/*from w  ww  . j  a v  a 2 s .  c  o m*/
        }
    }
    elements.add(FilenameUtils.getName(initialPath));
}

From source file:hoot.services.utils.MultipartSerializer.java

private static void serializeFGDB(List<BodyPart> fileItems, String jobId, Map<String, String> uploadedFiles,
        Map<String, String> uploadedFilesPaths) {
    try {//from   www. j ava 2  s.  c om
        Map<String, String> folderMap = new HashMap<>();

        for (BodyPart fileItem : fileItems) {
            String fileName = fileItem.getContentDisposition().getFileName();

            String relPath = FilenameUtils.getPath(fileName);
            if (relPath.endsWith("/")) {
                relPath = relPath.substring(0, relPath.length() - 1);
            }
            fileName = FilenameUtils.getName(fileName);

            String fgdbFolderPath = HOME_FOLDER + "/upload/" + jobId + "/" + relPath;
            boolean isPathSafe = validatePath(HOME_FOLDER + "/upload", fgdbFolderPath);

            if (isPathSafe) {
                String pathVal = folderMap.get(fgdbFolderPath);
                if (pathVal == null) {
                    File folderPath = new File(fgdbFolderPath);
                    FileUtils.forceMkdir(folderPath);
                    folderMap.put(fgdbFolderPath, relPath);
                }

                if (fileName == null) {
                    throw new IOException("File name cannot be null!");
                }

                String uploadedPath = fgdbFolderPath + "/" + fileName;
                boolean isFileSafe = validatePath(fgdbFolderPath, uploadedPath);
                if (isFileSafe) {
                    try (InputStream fileStream = fileItem.getEntityAs(InputStream.class)) {
                        File file = new File(uploadedPath);
                        FileUtils.copyInputStreamToFile(fileStream, file);
                    }
                } else {
                    throw new IOException("Illegal file path:" + uploadedPath);
                }
            } else {
                throw new IOException("Illegal path:" + fgdbFolderPath);
            }
        }

        for (Map.Entry<String, String> pairs : folderMap.entrySet()) {
            String nameOnly = "";
            String fgdbName = pairs.getValue();
            String[] nParts = fgdbName.split("\\.");

            for (int i = 0; i < (nParts.length - 1); i++) {
                if (!nameOnly.isEmpty()) {
                    nameOnly += ".";
                }
                nameOnly += nParts[i];
            }
            uploadedFiles.put(nameOnly, "GDB");
            uploadedFilesPaths.put(nameOnly, fgdbName);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error trying to serialize FGDB!", e);
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.PerfAlyzerFile.java

public static PerfAlyzerFile create(final File file) {
    checkState(!file.isAbsolute(), "File must not be absolute: %s", file);

    String fileName = file.getName();

    List<String> parts = newArrayListWithExpectedSize(3);
    List<String> partsWithBrackets = newArrayListWithExpectedSize(3);

    Matcher m = PATTERN_FILE_NAME_PARTS.matcher(fileName);
    while (m.find()) {
        partsWithBrackets.add(m.group());
        parts.add(m.group(1));//  w  w  w .j  av a  2  s . co m
    }
    m = PATTERN_MARKER.matcher(fileName);
    String marker = m.find() ? m.group(1) : null;

    return new PerfAlyzerFile(FilenameUtils.getPath(file.getPath()), getExtension(fileName), parts, marker);
}

From source file:com.siberhus.web.ckeditor.utils.PathUtils.java

public static String getFilePath(String fileName) {
    return FilenameUtils.getPath(fileName);
}

From source file:edu.ur.ir.ir_export.service.DefaultContributorTypeExportService.java

/** 
 * Create the xml file for the set of collections.
 * //from  ww  w  .  j  a va  2s . c om
 * @param xmlFile - file to write the xml to
 * @param contributor types - set of contributor types to export
 * 
 * @throws IOException - if writing to the file fails.
 */
public void createXmlFile(File xmlFile, Collection<ContributorType> contributorTypes) throws IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    String path = FilenameUtils.getPath(xmlFile.getCanonicalPath());
    if (!path.equals("")) {
        File pathOnly = new File(FilenameUtils.getFullPath(xmlFile.getCanonicalPath()));
        FileUtils.forceMkdir(pathOnly);
    }

    if (!xmlFile.exists()) {
        if (!xmlFile.createNewFile()) {
            throw new IllegalStateException("could not create file");
        }
    }

    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    }

    DOMImplementation impl = builder.getDOMImplementation();
    DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0");
    LSSerializer serializer = domLs.createLSSerializer();
    LSOutput lsOut = domLs.createLSOutput();

    Document doc = impl.createDocument(null, "contributor_types", null);
    Element root = doc.getDocumentElement();

    FileOutputStream fos;
    OutputStreamWriter outputStreamWriter;
    BufferedWriter writer;

    try {
        fos = new FileOutputStream(xmlFile);

        try {
            outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
        writer = new BufferedWriter(outputStreamWriter);
        lsOut.setCharacterStream(writer);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    }

    // create XML for the child collections
    for (ContributorType ct : contributorTypes) {
        Element contributorType = doc.createElement("contributor_type");

        this.addIdElement(contributorType, ct.getId().toString(), doc);
        this.addNameElement(contributorType, ct.getName(), doc);
        this.addDescription(contributorType, ct.getDescription(), doc);
        this.addSystemCode(contributorType, ct.getUniqueSystemCode(), doc);
    }
    serializer.write(root, lsOut);

    try {
        fos.close();
        writer.close();
        outputStreamWriter.close();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

}