blob: dfeac8867cdf327c2c648b520ba08f6c31b2e978 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
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
17import java.util.SortedSet;
18import java.util.TreeSet;
19
20public class Errors
21{
22 public static boolean hadError = false;
23 private static boolean warningsAreErrors = false;
24 private static TreeSet<Message> allErrors = new TreeSet<Message>();
25
26 private static class Message implements Comparable {
27 SourcePositionInfo pos;
28 int level;
29 String msg;
30
31 Message(SourcePositionInfo p, int l, String m) {
32 pos = p;
33 level = l;
34 msg = m;
35 }
36
37 public int compareTo(Object o) {
38 Message that = (Message)o;
39 int r = this.pos.compareTo(that.pos);
40 if (r != 0) return r;
41 return this.msg.compareTo(that.msg);
42 }
43
44 public String toString() {
45 String whereText = this.pos == null ? "unknown: " : this.pos.toString() + ':';
46 return whereText + this.msg;
47 }
48 }
49
50 public static void error(Error error, SourcePositionInfo where, String text) {
51 if (error.level == HIDDEN) {
52 return;
53 }
54
55 int level = (!warningsAreErrors && error.level == WARNING) ? WARNING : ERROR;
56 String which = level == WARNING ? " warning " : " error ";
57 String message = which + error.code + ": " + text;
58
59 if (where == null) {
60 where = new SourcePositionInfo("unknown", 0, 0);
61 }
62
63 allErrors.add(new Message(where, level, message));
64
65 if (error.level == ERROR || (warningsAreErrors && error.level == WARNING)) {
66 hadError = true;
67 }
68 }
69
70 public static void printErrors() {
71 for (Message m: allErrors) {
72 if (m.level == WARNING) {
73 System.err.println(m.toString());
74 }
75 }
76 for (Message m: allErrors) {
77 if (m.level == ERROR) {
78 System.err.println(m.toString());
79 }
80 }
81 }
82
83 public static int HIDDEN = 0;
84 public static int WARNING = 1;
85 public static int ERROR = 2;
86
87 public static void setWarningsAreErrors(boolean val) {
88 warningsAreErrors = val;
89 }
90
91 public static class Error {
92 public int code;
93 public int level;
94
95 public Error(int code, int level)
96 {
97 this.code = code;
98 this.level = level;
99 }
100 }
101
102 public static Error UNRESOLVED_LINK = new Error(1, WARNING);
103 public static Error BAD_INCLUDE_TAG = new Error(2, WARNING);
104 public static Error UNKNOWN_TAG = new Error(3, WARNING);
105 public static Error UNKNOWN_PARAM_TAG_NAME = new Error(4, WARNING);
106 public static Error UNDOCUMENTED_PARAMETER = new Error(5, HIDDEN);
107 public static Error BAD_ATTR_TAG = new Error(6, ERROR);
108 public static Error BAD_INHERITDOC = new Error(7, HIDDEN);
109 public static Error HIDDEN_LINK = new Error(8, WARNING);
110 public static Error HIDDEN_CONSTRUCTOR = new Error(9, WARNING);
111 public static Error UNAVAILABLE_SYMBOL = new Error(10, ERROR);
112 public static Error HIDDEN_SUPERCLASS = new Error(11, WARNING);
113 public static Error DEPRECATED = new Error(12, HIDDEN);
114 public static Error DEPRECATION_MISMATCH = new Error(13, WARNING);
115 public static Error MISSING_COMMENT = new Error(14, WARNING);
116 public static Error IO_ERROR = new Error(15, HIDDEN);
117
118 public static Error[] ERRORS = {
119 UNRESOLVED_LINK,
120 BAD_INCLUDE_TAG,
121 UNKNOWN_TAG,
122 UNKNOWN_PARAM_TAG_NAME,
123 UNDOCUMENTED_PARAMETER,
124 BAD_ATTR_TAG,
125 BAD_INHERITDOC,
126 HIDDEN_LINK,
127 HIDDEN_CONSTRUCTOR,
128 UNAVAILABLE_SYMBOL,
129 HIDDEN_SUPERCLASS,
130 DEPRECATED,
131 IO_ERROR,
132 };
133
134 public static boolean setErrorLevel(int code, int level) {
135 for (Error e: ERRORS) {
136 if (e.code == code) {
137 e.level = level;
138 return true;
139 }
140 }
141 return false;
142 }
143}