Skip to content

Commit 7802bd6

Browse files
committed
Issue #10 - Resolved
- Added ConstraintViolation.loadErrorMessageFile(InputStream) method. - Also improved handling of error messages in ConstraintViolation.
1 parent 39b39c9 commit 7802bd6

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed
115 Bytes
Binary file not shown.
115 Bytes
Binary file not shown.

org.modeldriven.alf/src/org/modeldriven/alf/execution/AlfBase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ public void printConstraintViolations(Collection<ConstraintViolation> violations
102102
lineNum = violation.getLine();
103103
this.println("");
104104
line = this.printLine(reader, line, prevLineNum, lineNum);
105-
this.printConstraintViolation(
106-
lineNum, violation.getColumn(), violation.getErrorMessage());;
105+
this.printConstraintViolation(violation);
107106
}
108107
}
109108

@@ -127,13 +126,13 @@ protected String printLine(BufferedReader reader, String prevLine, int prevLineN
127126
return line;
128127
}
129128

130-
protected void printConstraintViolation(int lineNum, int columnNum, String errorMessage) {
129+
protected void printConstraintViolation(ConstraintViolation violation) {
131130
StringBuffer indent = new StringBuffer();
132-
for (int n = columnNum; n > 1; n--) {
131+
for (int n = violation.getColumn(); n > 1; n--) {
133132
indent.append(" ");
134133
}
135134
this.println(indent + "^");
136-
this.println("[" + lineNum + ":" + columnNum + "] " + errorMessage);
135+
this.println(violation.getErrorMessage());
137136
}
138137

139138
protected void printVerbose(String message) {

org.modeldriven.alf/src/org/modeldriven/alf/syntax/common/ConstraintViolation.java

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import java.io.BufferedReader;
1212
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.InputStreamReader;
1315
import java.nio.charset.Charset;
1416
import java.nio.file.Files;
1517
import 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

Comments
 (0)