View Javadoc

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