import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class PxToRpx {


    public static void main(String[] args) {
        if (args.length > 1) {
            File root = new File(args[1]);
            scale = 750D / Double.parseDouble(args[0]);
            exec(root);
        } else {
            System.out.println("java className pageWidth pageAbsolutePath");
        }
    }

    private static final char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    private static final String[] widthParams = new String[]{"Left", "Right", "left", "right", "width",
            "Top", "Bottom", "top", "bottom", "height"};

    private static Double scale;

    private static void exec(File root) {
        if (root.isDirectory()) {
            File[] files = root.listFiles();
            if (files != null) {
                for (File file : files) {
                    exec(file);
                }
            }
        } else {
            StringBuilder content = new StringBuilder(), number;
            try (FileReader fileReader = new FileReader(root);
                 BufferedReader bufferedReader = new BufferedReader(fileReader)) {
                final String PX = "px", R = "r";
                String line, value;
                int start, paramEnd;
                Double scale;
                while ((line = bufferedReader.readLine()) != null) {
                    start = 0;
                    while ((start = line.indexOf(PX, start)) != -1) {
                        if (isDigit(line.charAt(start - 1))) {
                            paramEnd = getNumber((number = new StringBuilder()), line, start);
                            scale = getScale(paramEnd, line);
                            if (scale == null) {
                                start += 3;
                                continue;
                            }
                            value = calculate(number, scale)
                                    + R + line.substring(start);
                            line = line.substring(0, paramEnd)
                                    + value;
                            start = paramEnd + value.length() + 3;
                        } else {
                            start += 3;
                        }
                    }
                    content.append(line).append('\n');
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (content.length() > 0) {
                try (FileWriter fileWriter = new FileWriter(root);) {
                    fileWriter.write(content.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static double calculate(StringBuilder number, double scale) {
        BigDecimal two = new BigDecimal(Double.parseDouble(number.toString()) * scale);
        return two.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    private static Double getScale(int paramEnd, String line) {
        paramEnd = ignoreBlank(paramEnd, line);

        if (paramEnd != -1) {
            for (String param : widthParams) {
                if (compare(line, param, paramEnd)) {
                    return scale;
                }
            }
        }

        return null;
    }

    private static boolean compare(String line, String param, int paramEnd) {
        return paramEnd >= param.length()
                && param.equals(line.substring(paramEnd - param.length(), paramEnd));
    }

    private static int ignoreBlank(int paramEnd, String line) {
        if (line.charAt(paramEnd) == ':') {
            return paramEnd;
        }
        if (paramEnd > 0) {
            return ignoreBlank(paramEnd - 1, line);
        }
        return -1;
    }

    private static int getNumber(StringBuilder result, String line, int start) {
        char c;
        if (isDigit((c = line.charAt(start - 1)))) {
            result.insert(0, c);
            if (start > 0) {
                return getNumber(result, line, start - 1);
            }
        }
        if (result.length() > 0 && result.charAt(0) == '.') {
            result.insert(0, 0);
        }
        return start;
    }

    private static boolean isDigit(char target) {
        for (char c : digits) {
            if (target == c) {
                return true;
            }
        }
        return false;
    }
}

  

posted on 2023-09-10 05:08  悟叶随风  阅读(57)  评论(0编辑  收藏  举报