| 1 | |
|
| 2 | |
package org.galagosearch.tupleflow.execution; |
| 3 | |
|
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Collections; |
| 6 | |
import org.xml.sax.SAXParseException; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | 0 | public class ErrorStore { |
| 13 | 0 | public static class Statement implements Comparable<Statement> { |
| 14 | 0 | public Statement(FileLocation location, String message) { |
| 15 | 0 | this.location = location; |
| 16 | 0 | this.message = message; |
| 17 | 0 | } |
| 18 | |
|
| 19 | |
public String toString(String messageType) { |
| 20 | |
String result; |
| 21 | |
|
| 22 | 0 | if (location == null) { |
| 23 | 0 | result = String.format("[unknown location] %s: %s\n", messageType, |
| 24 | |
message); |
| 25 | |
} else { |
| 26 | 0 | result = String.format("%s [Line %d Column %d] %s: %s\n", location.fileName, |
| 27 | |
location.lineNumber, location.columnNumber, messageType, |
| 28 | |
message); |
| 29 | |
} |
| 30 | 0 | return result; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public String toString() { |
| 34 | 0 | return toString("INFO"); |
| 35 | |
} |
| 36 | |
|
| 37 | |
public int compareTo(Statement other) { |
| 38 | 0 | if (location == null) { |
| 39 | 0 | if (other.location == null) { |
| 40 | 0 | return 0; |
| 41 | |
} else { |
| 42 | 0 | return -1; |
| 43 | |
} |
| 44 | |
} else { |
| 45 | 0 | return location.compareTo(other.location); |
| 46 | |
} |
| 47 | |
} |
| 48 | |
FileLocation location; |
| 49 | |
String message; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public class LocatedHandler implements ErrorHandler { |
| 53 | 0 | public LocatedHandler(FileLocation location) { |
| 54 | 0 | this.location = location; |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public void addError(String message) { |
| 58 | 0 | ErrorStore.this.addError(location, message); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public void addWarning(String message) { |
| 62 | 0 | ErrorStore.this.addWarning(location, message); |
| 63 | 0 | } |
| 64 | |
FileLocation location; |
| 65 | |
} |
| 66 | 0 | ArrayList<Statement> errors = new ArrayList(); |
| 67 | 0 | ArrayList<Statement> warnings = new ArrayList(); |
| 68 | |
|
| 69 | |
public void addError(FileLocation location, String message) { |
| 70 | 0 | errors.add(new Statement(location, message)); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public void addWarning(FileLocation location, String message) { |
| 74 | 0 | warnings.add(new Statement(location, message)); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
public LocatedHandler getErrorHandler(FileLocation location) { |
| 78 | 0 | return new LocatedHandler(location); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public ArrayList<Statement> getErrors() { |
| 82 | 0 | return errors; |
| 83 | |
} |
| 84 | |
|
| 85 | |
public ArrayList<Statement> getWarnings() { |
| 86 | 0 | return warnings; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public boolean hasStatements() { |
| 90 | 0 | return errors.size() + warnings.size() > 0; |
| 91 | |
} |
| 92 | |
|
| 93 | |
@Override |
| 94 | |
public String toString() { |
| 95 | 0 | StringBuilder builder = new StringBuilder(); |
| 96 | 0 | Collections.sort(errors); |
| 97 | 0 | Collections.sort(warnings); |
| 98 | |
|
| 99 | 0 | for (Statement s : errors) { |
| 100 | 0 | builder.append(s.toString("Error")); |
| 101 | |
} |
| 102 | |
|
| 103 | 0 | for (Statement s : warnings) { |
| 104 | 0 | builder.append(s.toString("Warning")); |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | return builder.toString(); |
| 108 | |
} |
| 109 | |
|
| 110 | |
void addError(String filename, SAXParseException e) { |
| 111 | 0 | addError(new FileLocation(filename, e.getLineNumber(), e.getColumnNumber()), e.getMessage()); |
| 112 | 0 | } |
| 113 | |
} |