1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.doclava;
18
19import java.util.Set;
20import java.util.TreeSet;
21
22public class Errors {
23  public static boolean hadError = false;
24  private static boolean warningsAreErrors = false;
25  private static TreeSet<ErrorMessage> allErrors = new TreeSet<ErrorMessage>();
26
27  public static class ErrorMessage implements Comparable {
28    Error error;
29    SourcePositionInfo pos;
30    String msg;
31
32    ErrorMessage(Error e, SourcePositionInfo p, String m) {
33      error = e;
34      pos = p;
35      msg = m;
36    }
37
38    public int compareTo(Object o) {
39      ErrorMessage that = (ErrorMessage) o;
40      int r = this.pos.compareTo(that.pos);
41      if (r != 0) return r;
42      return this.msg.compareTo(that.msg);
43    }
44
45    @Override
46    public String toString() {
47      String whereText = this.pos == null ? "unknown: " : this.pos.toString() + ':';
48      return whereText + this.msg;
49    }
50
51    public Error error() {
52      return error;
53    }
54  }
55
56  public static void error(Error error, SourcePositionInfo where, String text) {
57    if (error.level == HIDDEN) {
58      return;
59    }
60
61    int level = (!warningsAreErrors && error.level == WARNING) ? WARNING : ERROR;
62    String which = level == WARNING ? " warning " : " error ";
63    String message = which + error.code + ": " + text;
64
65    if (where == null) {
66      where = new SourcePositionInfo("unknown", 0, 0);
67    }
68
69    allErrors.add(new ErrorMessage(error, where, message));
70
71    if (error.level == ERROR || (warningsAreErrors && error.level == WARNING)) {
72      hadError = true;
73    }
74  }
75
76  public static void clearErrors() {
77    hadError = false;
78    allErrors.clear();
79  }
80
81  public static void printErrors() {
82    printErrors(allErrors);
83  }
84
85  public static void printErrors(Set<ErrorMessage> errors) {
86    for (ErrorMessage m : errors) {
87      if (m.error.level == WARNING) {
88        System.err.println(m.toString());
89      }
90    }
91    for (ErrorMessage m : errors) {
92      if (m.error.level == ERROR) {
93        System.err.println(m.toString());
94      }
95    }
96  }
97
98  public static Set<ErrorMessage> getErrors() {
99    return allErrors;
100  }
101
102  public static int HIDDEN = 0;
103  public static int WARNING = 1;
104  public static int ERROR = 2;
105
106  public static void setWarningsAreErrors(boolean val) {
107    warningsAreErrors = val;
108  }
109
110  public static class Error {
111    public int code;
112    public int level;
113
114    public Error(int code, int level) {
115      this.code = code;
116      this.level = level;
117    }
118
119    public String toString() {
120      return "Error #" + this.code;
121    }
122  }
123
124  // Errors for API verification
125  public static Error PARSE_ERROR = new Error(1, ERROR);
126  public static Error ADDED_PACKAGE = new Error(2, WARNING);
127  public static Error ADDED_CLASS = new Error(3, WARNING);
128  public static Error ADDED_METHOD = new Error(4, WARNING);
129  public static Error ADDED_FIELD = new Error(5, WARNING);
130  public static Error ADDED_INTERFACE = new Error(6, WARNING);
131  public static Error REMOVED_PACKAGE = new Error(7, WARNING);
132  public static Error REMOVED_CLASS = new Error(8, WARNING);
133  public static Error REMOVED_METHOD = new Error(9, WARNING);
134  public static Error REMOVED_FIELD = new Error(10, WARNING);
135  public static Error REMOVED_INTERFACE = new Error(11, WARNING);
136  public static Error CHANGED_STATIC = new Error(12, WARNING);
137  public static Error ADDED_FINAL = new Error(13, WARNING);
138  public static Error CHANGED_TRANSIENT = new Error(14, WARNING);
139  public static Error CHANGED_VOLATILE = new Error(15, WARNING);
140  public static Error CHANGED_TYPE = new Error(16, WARNING);
141  public static Error CHANGED_VALUE = new Error(17, WARNING);
142  public static Error CHANGED_SUPERCLASS = new Error(18, WARNING);
143  public static Error CHANGED_SCOPE = new Error(19, WARNING);
144  public static Error CHANGED_ABSTRACT = new Error(20, WARNING);
145  public static Error CHANGED_THROWS = new Error(21, WARNING);
146  public static Error CHANGED_NATIVE = new Error(22, HIDDEN);
147  public static Error CHANGED_CLASS = new Error(23, WARNING);
148  public static Error CHANGED_DEPRECATED = new Error(24, WARNING);
149  public static Error CHANGED_SYNCHRONIZED = new Error(25, ERROR);
150  public static Error ADDED_FINAL_UNINSTANTIABLE = new Error(26, WARNING);
151  public static Error REMOVED_FINAL = new Error(27, WARNING);
152
153  // Errors in javadoc generation
154  public static final Error UNRESOLVED_LINK = new Error(101, WARNING);
155  public static final Error BAD_INCLUDE_TAG = new Error(102, WARNING);
156  public static final Error UNKNOWN_TAG = new Error(103, WARNING);
157  public static final Error UNKNOWN_PARAM_TAG_NAME = new Error(104, WARNING);
158  public static final Error UNDOCUMENTED_PARAMETER = new Error(105, HIDDEN);
159  public static final Error BAD_ATTR_TAG = new Error(106, ERROR);
160  public static final Error BAD_INHERITDOC = new Error(107, HIDDEN);
161  public static final Error HIDDEN_LINK = new Error(108, WARNING);
162  public static final Error HIDDEN_CONSTRUCTOR = new Error(109, WARNING);
163  public static final Error UNAVAILABLE_SYMBOL = new Error(110, ERROR);
164  public static final Error HIDDEN_SUPERCLASS = new Error(111, WARNING);
165  public static final Error DEPRECATED = new Error(112, HIDDEN);
166  public static final Error DEPRECATION_MISMATCH = new Error(113, WARNING);
167  public static final Error MISSING_COMMENT = new Error(114, WARNING);
168  public static final Error IO_ERROR = new Error(115, HIDDEN);
169  public static final Error NO_SINCE_DATA = new Error(116, HIDDEN);
170  public static final Error NO_FEDERATION_DATA = new Error(117, WARNING);
171  public static final Error BROKEN_SINCE_FILE = new Error(118, ERROR);
172  public static final Error INVALID_CONTENT_TYPE = new Error(119, ERROR);
173  public static final Error INVALID_SAMPLE_INDEX = new Error(120, ERROR);
174  public static final Error HIDDEN_TYPE_PARAMETER = new Error(121, HIDDEN);
175  public static final Error PRIVATE_SUPERCLASS = new Error(122, ERROR);
176
177  public static final Error[] ERRORS =
178      {UNRESOLVED_LINK, BAD_INCLUDE_TAG, UNKNOWN_TAG, UNKNOWN_PARAM_TAG_NAME,
179          UNDOCUMENTED_PARAMETER, BAD_ATTR_TAG, BAD_INHERITDOC, HIDDEN_LINK, HIDDEN_CONSTRUCTOR,
180          UNAVAILABLE_SYMBOL, HIDDEN_SUPERCLASS, DEPRECATED, DEPRECATION_MISMATCH, MISSING_COMMENT,
181          IO_ERROR, NO_SINCE_DATA, NO_FEDERATION_DATA, PARSE_ERROR, ADDED_PACKAGE, ADDED_CLASS,
182          ADDED_METHOD, ADDED_FIELD, ADDED_INTERFACE, REMOVED_PACKAGE, REMOVED_CLASS,
183          REMOVED_METHOD, REMOVED_FIELD, REMOVED_INTERFACE, CHANGED_STATIC, ADDED_FINAL,
184          CHANGED_TRANSIENT, CHANGED_VOLATILE, CHANGED_TYPE, CHANGED_VALUE, CHANGED_SUPERCLASS,
185          CHANGED_SCOPE, CHANGED_ABSTRACT, CHANGED_THROWS, CHANGED_NATIVE, CHANGED_CLASS,
186          CHANGED_DEPRECATED, CHANGED_SYNCHRONIZED, ADDED_FINAL_UNINSTANTIABLE, REMOVED_FINAL,
187          BROKEN_SINCE_FILE, INVALID_CONTENT_TYPE, HIDDEN_TYPE_PARAMETER, PRIVATE_SUPERCLASS};
188
189  public static boolean setErrorLevel(int code, int level) {
190    for (Error e : ERRORS) {
191      if (e.code == code) {
192        e.level = level;
193        return true;
194      }
195    }
196    return false;
197  }
198}
199