1010
1111import java .io .BufferedReader ;
1212import java .io .IOException ;
13+ import java .io .InputStream ;
14+ import java .io .InputStreamReader ;
1315import java .nio .charset .Charset ;
1416import java .nio .file .Files ;
1517import java .nio .file .Paths ;
@@ -24,40 +26,52 @@ public class ConstraintViolation implements Comparable<ConstraintViolation> {
2426 DEFAULT_ERROR_MESSAGE_FILE_DIRECTORY + "/" + DEFAULT_ERROR_MESSAGE_FILE_NAME ;
2527
2628 protected static String errorMessageFileName = null ;
27- protected static Map <String , String > errorMessages = null ;
29+ protected static Map <String , String > errorMessages = new HashMap < String , String >() ;
2830
31+ public static void loadErrorMessageFile () throws IOException {
32+ loadErrorMessageFile (DEFAULT_ERROR_MESSAGE_FILE_NAME );
33+ }
34+
2935 public static void loadErrorMessageFile (String path ) throws IOException {
3036 if (path == null ) {
31- errorMessages = null ;
32- } else if (errorMessages == null || path != errorMessageFileName ) {
33- BufferedReader reader =
34- Files .newBufferedReader (Paths .get (path ), Charset .defaultCharset ());
35- errorMessages = new HashMap <String , String >();
36- String line ;
37- do {
38- do {
39- line = reader .readLine ();
40- } while (line != null && line .equals ("" ));
41- if (line != null ) {
42- String constraintName = line ;
43- line = reader .readLine ();
44- if (line != null && !line .equals ("" )) {
45- errorMessages .put (constraintName , line );
46- }
47- }
48- } while (line != null );
37+ clearErrorMessages ();
38+ } else if (path != errorMessageFileName ) {
39+ loadErrorMessageFile (Files .newInputStream (Paths .get (path )));
4940 errorMessageFileName = path ;
50- reader .close ();
5141 }
5242 }
43+
44+ private static void loadErrorMessageFile (InputStream inputStream ) throws IOException {
45+ BufferedReader reader = new BufferedReader (new InputStreamReader (
46+ inputStream , Charset .defaultCharset ()));
47+ clearErrorMessages ();
48+ String line ;
49+ do {
50+ do {
51+ line = reader .readLine ();
52+ } while (line != null && line .equals ("" ));
53+ if (line != null ) {
54+ String constraintName = line ;
55+ line = reader .readLine ();
56+ if (line != null && !line .equals ("" )) {
57+ putErrorMessage (constraintName , line );
58+ }
59+ }
60+ } while (line != null );
61+ reader .close ();
62+ }
5363
54- public static void loadErrorMessageFile () throws IOException {
55- loadErrorMessageFile (DEFAULT_ERROR_MESSAGE_FILE_NAME );
64+ public static void clearErrorMessages () {
65+ errorMessageFileName = null ;
66+ errorMessages = new HashMap <String , String >();
67+ }
68+
69+ public static void putErrorMessage (String constraintName , String errorMessage ) {
70+ errorMessages .put (constraintName , errorMessage );
5671 }
5772
58- public String getErrorMessage (String constraintName ) {
59- String errorMessage = errorMessages == null ? null :
60- errorMessages .get (constraintName );
73+ public static String getErrorMessage (String constraintName ) {
74+ String errorMessage = errorMessages .get (constraintName );
6175 return errorMessage == null ? constraintName :
6276 errorMessage + " (" + constraintName + ")" ;
6377 }
@@ -75,7 +89,8 @@ public String getConstraintName() {
7589 }
7690
7791 public String getErrorMessage () {
78- return getErrorMessage (this .constraintName );
92+ return "[" + this .getLine () + ":" + this .getColumn () + "] " +
93+ getErrorMessage (this .constraintName );
7994 }
8095
8196 public ParsedElement getViolatingElement () {
@@ -96,8 +111,7 @@ public int getColumn() {
96111
97112 @ Override
98113 public String toString () {
99- return this .getErrorMessage () + " in " + this .getFileName () +
100- " at line " + this .getLine () + ", column " + this .getColumn ();
114+ return this .getFileName () + this .getErrorMessage ();
101115 }
102116
103117 @ Override
0 commit comments