码迷,mamicode.com
首页 > 其他好文 > 详细

MANIFEST.MF文件对Import-Package/Export-Package重排列

时间:2019-11-10 14:06:36      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:inf   builder   ima   pen   转换   mamicode   open   bak   http   

众所周知,MANIFEST.MF文件中的空格开头的行是相当于拼接在上一行末尾的。很多又长又乱的Import-Package或者Export-Package,有时候想要搜索某个package却可能被换行截断而搜不到。

这时候咱们可以对它进行格式化重新排列,同时又不影响它的正常运行。再排个序方便查找。

 

排列前 vs 排列后

技术图片

技术图片

 

 

 附上干货 !!!

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class ManifestFormatter {

    private final static String IMPORT_PACKAGE = "Import-Package";
    private final static String EXPORT_PACKAGE = "Export-Package";
    private final static char SEPARATOR = ‘#‘;

    public static void main(String[] args) {
        File mf = new File(args[0]);
        File tmpMf = new File(mf.getParentFile(), "TEMP.MF");
        File backMf = new File(mf.getParentFile(), "MANIFEST.MF.bak");
        if (!mf.exists()) {
            System.out.println(mf.getAbsolutePath() + " is format failed: " + mf.getAbsolutePath() + " is not exits");
            System.exit(2);
        }
        if (backMf.exists()) {
            System.out.println(mf.getAbsolutePath() + " is format failed: " + backMf.getName() + " is exits");
            System.exit(3);
        }
        try (BufferedReader br = new BufferedReader(new FileReader(mf)); BufferedWriter bw = new BufferedWriter(new FileWriter(tmpMf));) {
            StringBuilder fileToString = new StringBuilder();
            char[] chars = new char[1024];
            int len;
            while ((len = br.read(chars, 0, chars.length)) != -1) {
                fileToString.append(new String(chars, 0, len));
            }

            // 转换MANIFEST.MF文件中的属性为单行模式,并保持原来的文档格式
            String lineSeparator = fileToString.indexOf("\r\n") != -1 ? "\r\n" : "\n";
            String formatStr = fileToString.toString().replaceAll(lineSeparator + " ", "");
            ByteArrayInputStream bi = new ByteArrayInputStream(formatStr.getBytes());
            Properties properties = new Properties();
            properties.load(bi);

            // 回写MANIFEST.MF文件,重新写入Import-Package与Export-Package
            List<String> importPackageList = getPackageList(properties.getProperty(IMPORT_PACKAGE));
            List<String> exportPackageList = getPackageList(properties.getProperty(EXPORT_PACKAGE));
            String newImportPackageStr = createNewPackage(importPackageList, IMPORT_PACKAGE, lineSeparator);
            String newExportPackageStr = createNewPackage(exportPackageList, EXPORT_PACKAGE, lineSeparator);
            formatStr = formatStr.replaceAll("Import-Package:.*" + lineSeparator, newImportPackageStr);
            formatStr = formatStr.replaceAll("Export-Package:.*" + lineSeparator, newExportPackageStr);
            bw.write(formatStr);
        } catch (IOException e) {
            System.out.println(mf.getAbsolutePath() + " is format failed: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println(mf.getAbsolutePath() + " is format successed");
    }

    /**
     * 重新创建Import-Package与Export-Package
     * 
     * @param packageList
     * @param packageType
     * @param lineSeparator
     * @return
     */
    private static String createNewPackage(List<String> packageList, String packageType, String lineSeparator) {
        StringBuilder newPackageString = new StringBuilder(packageType).append(":").append(lineSeparator);
        int size = packageList.size();
        for (int i = 0; i < size; i++) {
            newPackageString.append(" ");
            newPackageString.append(packageList.get(i));
            if (i < size - 1) {
                newPackageString.append(",");
            }
            newPackageString.append(lineSeparator);
        }
        return newPackageString.toString();
    }

    /**
     * 单行的Import-Package或Export-Package,转化为list数组
     * 
     * @param packageStr  单行的Import-Package或Export-Package
     * @param packageType "Import-Package"或"Export-Package"
     * @return
     */
    private static List<String> getPackageList(String packageStr) {
        boolean isOpenQuotes = false;
        char[] chArr = packageStr.toCharArray();
        int len = chArr.length;
        // 双引号外面的逗号,转为分隔符
        for (int i = 0; i < len; i++) {
            if (chArr[i] == ‘"‘) {
                isOpenQuotes = !isOpenQuotes;
            }
            if (chArr[i] == ‘,‘ && !isOpenQuotes) {
                chArr[i] = SEPARATOR;
            }
        }
        List<String> packageList = Arrays.asList(new String(chArr).split(SEPARATOR + ""));
        Collections.sort(packageList);
        return packageList;
    }

}

 

MANIFEST.MF文件对Import-Package/Export-Package重排列

标签:inf   builder   ima   pen   转换   mamicode   open   bak   http   

原文地址:https://www.cnblogs.com/zhangzongjian/p/11829599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!