package com.bloatit.framework.webprocessor.annotations;

public class Message {
    public enum What {
        UNKNOWN, NOT_FOUND, CONVERSION_ERROR, MIN_ERROR, MAX_ERROR, NO_ERROR, LENGTH_ERROR, OPTIONAL_ERROR, PRECISION_ERROR
    }

    private final String message;
    private final What what;

    public Message(final String message, final What what, final String name, final String value) {
        super();
        if (what == null || message == null) {
            throw new NullPointerException();
        }
        this.message = extractErrorMessage(message, name, value);
        this.what = what;
    }

    public Message(final String message) {
        this.message = message;
        this.what = What.UNKNOWN;
    }

    private String extractErrorMessage(final String aMessage, final String name, final String value) {
        String errorMsg = aMessage.replaceAll("%param", name);
        if (!value.isEmpty()) {
            errorMsg = errorMsg.replaceAll("%value", value);
        } else {
            errorMsg = errorMsg.replaceAll("%value", "null");
        }
        return errorMsg;
    }

    public String getMessage() {
        return message;
    }

    public What getWhat() {
        return what;
    }
}

@@ -50,16 +50,49 @@
 tions;%0A%0A
+import java.util.regex.Matcher;%0A%0A
 public c
@@ -921,13 +921,39 @@
 m%22, 
+Matcher.quoteReplacement(
 name)
+)
 ;%0A  
@@ -1035,22 +1035,48 @@
 value%22, 
+Matcher.quoteReplacement(
 value)
+)
 ;%0A      

Fix message generation: when the value contained replacement group for regex it was crashing