Java File Attribute patchInfoPList(final File infoPList, final String executable)

Here you can find the source of patchInfoPList(final File infoPList, final String executable)

Description

patch Info P List

License

Open Source License

Declaration

public static boolean patchInfoPList(final File infoPList, final String executable) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static boolean patchInfoPList(final File infoPList, final String executable) throws IOException {
        if (!infoPList.exists())
            return false;
        String contents = readFile(infoPList);
        final Pattern pattern = Pattern.compile(".*<key>CFBundleExecutable</key>[^<]*<string>([^<]*).*",
                Pattern.DOTALL | Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(contents);
        if (!matcher.matches())
            return false;
        contents = contents.substring(0, matcher.start(1)) + executable + contents.substring(matcher.end(1));
        writeFile(infoPList, contents);/* w w  w  . jav a  2s.c  om*/
        return true;
    }

    protected static String readFile(final File file) throws IOException {
        final StringBuilder builder = new StringBuilder();
        final BufferedReader reader = new BufferedReader(new FileReader(file));
        for (;;) {
            final String line = reader.readLine();
            if (line == null)
                break;
            builder.append(line).append('\n');
        }
        reader.close();

        return builder.toString();
    }

    protected static void writeFile(final File file, final String contents) throws IOException {
        final File result = new File(file.getAbsoluteFile().getParentFile(), file.getName() + ".new");
        final FileOutputStream out = new FileOutputStream(result);
        out.write(contents.getBytes());
        out.close();
        result.renameTo(file);
    }
}

Related

  1. makeExecutable(File file)
  2. makeExecutable(File file)
  3. makeExecutable(File target)
  4. makeExecutable(String path)
  5. parallel(ExecutorService executor, Collection> tasks)
  6. persistExecutionTimesCsv(String filePath, LinkedList> executionTimes)
  7. setExecutable(File f, final String pattern)
  8. setExecutable(File file)
  9. setExecutable(File file, boolean executable)