0
0

JSON処理2

Last updated at Posted at 2024-05-22
public static String processIfna(String key, String value) {
        // keyの形式チェック
        if (!key.matches(".*IFNA:(\\d*|\\s*)/(\\d*|\\s*|\\*)")) {
            throw new RuntimeException("500000エラー: " + key + " のIFNAパターンが不正です");
        }

        String[] parts = key.split("IFNA:")[1].split("/");
        if (parts.length != 2) {
            // "IFNA:0/" のようなパターンを許容するための修正
            if (parts.length == 1 && key.endsWith("/")) {
                parts = new String[]{parts[0], ""};
            } else {
                throw new RuntimeException("500000エラー: " + key + " のIFNAパターンが不正です");
            }
        }

        // 空文字の場合には空文字を設定
        String nullValueReplacement = parts[0].isEmpty() ? "" : parts[0];
        String nonNullValueReplacement = parts[1].isEmpty() ? "" : parts[1];

        if (key.contains("/*")) {
            return (value == null || value.isEmpty()) ? nullValueReplacement : value;
        } else {
            return (value == null || value.isEmpty()) ? nullValueReplacement : nonNullValueReplacement;
        }
    }

json

    /**
     * キーに基づいて値を加工するメソッド。
     *
     * @param key 加工処理のパターン
     * @param value 元の値
     * @return 加工された値
     * @throws ParseException 日付のパースエラーが発生した場合
     */
    public static String processValue(String key, String value) throws ParseException {
        if (key.contains("HEAD:")) {
            return processHead(key, value);
        } else if (key.contains("ERA:yymmdd")) {
            // ERA:yymmddの場合、和暦に変換
            return convertToJapaneseEra(value, "yyyyMMdd");
        } else if (key.contains("ERA:yymm")) {
            // ERA:yymmの場合、和暦に変換
            return convertToJapaneseEra(value, "yyyyMM");
        } else if (key.contains("IFNA:")) {
            return processIfna(key, value);
        } else {
            throw new RuntimeException("500000エラー: " + key + " に該当しません");
        }
    }

    /**
     * HEADパターンに基づいて値を加工するメソッド。
     *
     * @param key 加工処理のパターン
     * @param value 元の値
     * @return 加工された値
     */
    public static String processHead(String key, String value) {
        String[] parts = key.split("HEAD:");
        if (parts.length != 2 || !parts[1].matches("\\d+")) {
            throw new RuntimeException("500000エラー: " + key + " のHEADパターンが不正です");
        }
        int headLength = Integer.parseInt(parts[1]);
        if (value.length() <= headLength) {
            return value;  // valueがheadLength以下の場合はそのまま返す
        }
        return value.substring(0, headLength);
    }

    /**
     * IFNAパターンに基づいて値を加工するメソッド。
     *
     * @param key 加工処理のパターン
     * @param value 元の値
     * @return 加工された値
     */
    public static String processIfna(String key, String value) {
        String[] parts = key.split("IFNA:")[1].split("/");
        if (parts.length != 2 || !parts[0].matches("\\d+")) {
            throw new RuntimeException("500000エラー: " + key + " のIFNAパターンが不正です");
        }
        if (key.contains("/*")) {
            int replacementValue = Integer.parseInt(parts[0]);
            return (value == null || value.isEmpty()) ? String.valueOf(replacementValue) : value;
        } else {
            if (!parts[1].matches("\\d+")) {
                throw new RuntimeException("500000エラー: " + key + " のIFNAパターンが不正です");
            }
            int nullValueReplacement = Integer.parseInt(parts[0]);
            int nonNullValueReplacement = Integer.parseInt(parts[1]);
            return (value == null || value.isEmpty()) ? String.valueOf(nullValueReplacement) : String.valueOf(nonNullValueReplacement);
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0