PageRenderTime 42ms CodeModel.GetById 1ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/doclava/Errors.java

https://bitbucket.org/fredgrott/androidlava
Java | 370 lines | 149 code | 77 blank | 144 comment | 24 complexity | 784ee5524dacea970c1de2201533dd64 MD5 | raw file
  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
 22// TODO: Auto-generated Javadoc
 23/**
 24 * The Class Errors.
 25 */
 26public class Errors {
 27  
 28  /** The had error. */
 29  public static boolean hadError = false;
 30  
 31  /** The warnings are errors. */
 32  private static boolean warningsAreErrors = false;
 33  
 34  /** The all errors. */
 35  private static TreeSet<ErrorMessage> allErrors = new TreeSet<ErrorMessage>();
 36
 37  /**
 38     * The Class ErrorMessage.
 39     */
 40  @SuppressWarnings("rawtypes")
 41public static class ErrorMessage implements Comparable {
 42    
 43    /** The error. */
 44    Error error;
 45    
 46    /** The pos. */
 47    SourcePositionInfo pos;
 48    
 49    /** The msg. */
 50    String msg;
 51
 52    /**
 53     * Instantiates a new error message.
 54     * 
 55     * @param e the e
 56     * @param p the p
 57     * @param m the m
 58     */
 59    ErrorMessage(Error e, SourcePositionInfo p, String m) {
 60      error = e;
 61      pos = p;
 62      msg = m;
 63    }
 64
 65    /**
 66     * @see java.lang.Comparable#compareTo(java.lang.Object)
 67     */
 68    public int compareTo(Object o) {
 69      ErrorMessage that = (ErrorMessage) o;
 70      int r = this.pos.compareTo(that.pos);
 71      if (r != 0) return r;
 72      return this.msg.compareTo(that.msg);
 73    }
 74
 75    /**
 76     * @see java.lang.Object#toString()
 77     */
 78    @Override
 79    public String toString() {
 80      String whereText = this.pos == null ? "unknown: " : this.pos.toString() + ':';
 81      return whereText + this.msg;
 82    }
 83    
 84    /**
 85     * Error.
 86     * 
 87     * @return the error
 88     */
 89    public Error error() {
 90      return error;
 91    }
 92  }
 93
 94  /**
 95     * Error.
 96     * 
 97     * @param error the error
 98     * @param where the where
 99     * @param text the text
100     */
101  public static void error(Error error, SourcePositionInfo where, String text) {
102    if (error.level == HIDDEN) {
103      return;
104    }
105
106    int level = (!warningsAreErrors && error.level == WARNING) ? WARNING : ERROR;
107    String which = level == WARNING ? " warning " : " error ";
108    String message = which + error.code + ": " + text;
109
110    if (where == null) {
111      where = new SourcePositionInfo("unknown", 0, 0);
112    }
113
114    allErrors.add(new ErrorMessage(error, where, message));
115
116    if (error.level == ERROR || (warningsAreErrors && error.level == WARNING)) {
117      hadError = true;
118    }
119  }
120  
121  /**
122     * Clear errors.
123     */
124  public static void clearErrors() {
125    hadError = false;
126    allErrors.clear();
127  }
128
129  /**
130     * Prints the errors.
131     */
132  public static void printErrors() {
133    printErrors(allErrors);
134  }
135  
136  /**
137     * Prints the errors.
138     * 
139     * @param errors the errors
140     */
141  public static void printErrors(Set<ErrorMessage> errors) {
142    for (ErrorMessage m : errors) {
143      if (m.error.level == WARNING) {
144        System.err.println(m.toString());
145      }
146    }
147    for (ErrorMessage m : errors) {
148      if (m.error.level == ERROR) {
149        System.err.println(m.toString());
150      }
151    }
152  }
153  
154  /**
155     * Gets the errors.
156     * 
157     * @return the errors
158     */
159  public static Set<ErrorMessage> getErrors() {
160    return allErrors;
161  }
162
163  /** The hidden. */
164  public static int HIDDEN = 0;
165  
166  /** The warning. */
167  public static int WARNING = 1;
168  
169  /** The error. */
170  public static int ERROR = 2;
171
172  /**
173     * Sets the warnings are errors.
174     * 
175     * @param val the new warnings are errors
176     */
177  public static void setWarningsAreErrors(boolean val) {
178    warningsAreErrors = val;
179  }
180
181  /**
182     * The Class Error.
183     */
184  public static class Error {
185    
186    /** The code. */
187    public int code;
188    
189    /** The level. */
190    public int level;
191
192    /**
193     * Instantiates a new error.
194     * 
195     * @param code the code
196     * @param level the level
197     */
198    public Error(int code, int level) {
199      this.code = code;
200      this.level = level;
201    }
202    
203    /**
204     * @see java.lang.Object#toString()
205     */
206    public String toString() {
207      return "Error #" + this.code;
208    }
209  }
210
211  // Errors for API verification
212  /** The parse error. */
213  public static Error PARSE_ERROR = new Error(1, ERROR);
214  
215  /** The added package. */
216  public static Error ADDED_PACKAGE = new Error(2, WARNING);
217  
218  /** The added class. */
219  public static Error ADDED_CLASS = new Error(3, WARNING);
220  
221  /** The added method. */
222  public static Error ADDED_METHOD = new Error(4, WARNING);
223  
224  /** The added field. */
225  public static Error ADDED_FIELD = new Error(5, WARNING);
226  
227  /** The added interface. */
228  public static Error ADDED_INTERFACE = new Error(6, WARNING);
229  
230  /** The removed package. */
231  public static Error REMOVED_PACKAGE = new Error(7, WARNING);
232  
233  /** The removed class. */
234  public static Error REMOVED_CLASS = new Error(8, WARNING);
235  
236  /** The removed method. */
237  public static Error REMOVED_METHOD = new Error(9, WARNING);
238  
239  /** The removed field. */
240  public static Error REMOVED_FIELD = new Error(10, WARNING);
241  
242  /** The removed interface. */
243  public static Error REMOVED_INTERFACE = new Error(11, WARNING);
244  
245  /** The changed static. */
246  public static Error CHANGED_STATIC = new Error(12, WARNING);
247  
248  /** The changed final. */
249  public static Error CHANGED_FINAL = new Error(13, WARNING);
250  
251  /** The changed transient. */
252  public static Error CHANGED_TRANSIENT = new Error(14, WARNING);
253  
254  /** The changed volatile. */
255  public static Error CHANGED_VOLATILE = new Error(15, WARNING);
256  
257  /** The changed type. */
258  public static Error CHANGED_TYPE = new Error(16, WARNING);
259  
260  /** The changed value. */
261  public static Error CHANGED_VALUE = new Error(17, WARNING);
262  
263  /** The changed superclass. */
264  public static Error CHANGED_SUPERCLASS = new Error(18, WARNING);
265  
266  /** The changed scope. */
267  public static Error CHANGED_SCOPE = new Error(19, WARNING);
268  
269  /** The changed abstract. */
270  public static Error CHANGED_ABSTRACT = new Error(20, WARNING);
271  
272  /** The changed throws. */
273  public static Error CHANGED_THROWS = new Error(21, WARNING);
274  
275  /** The changed native. */
276  public static Error CHANGED_NATIVE = new Error(22, HIDDEN);
277  
278  /** The changed class. */
279  public static Error CHANGED_CLASS = new Error(23, WARNING);
280  
281  /** The changed deprecated. */
282  public static Error CHANGED_DEPRECATED = new Error(24, WARNING);
283  
284  /** The changed synchronized. */
285  public static Error CHANGED_SYNCHRONIZED = new Error(25, ERROR);
286
287  // Errors in javadoc generation
288  /** The Constant UNRESOLVED_LINK. */
289  public static final Error UNRESOLVED_LINK = new Error(101, WARNING);
290  
291  /** The Constant BAD_INCLUDE_TAG. */
292  public static final Error BAD_INCLUDE_TAG = new Error(102, WARNING);
293  
294  /** The Constant UNKNOWN_TAG. */
295  public static final Error UNKNOWN_TAG = new Error(103, WARNING);
296  
297  /** The Constant UNKNOWN_PARAM_TAG_NAME. */
298  public static final Error UNKNOWN_PARAM_TAG_NAME = new Error(104, WARNING);
299  
300  /** The Constant UNDOCUMENTED_PARAMETER. */
301  public static final Error UNDOCUMENTED_PARAMETER = new Error(105, HIDDEN);
302  
303  /** The Constant BAD_ATTR_TAG. */
304  public static final Error BAD_ATTR_TAG = new Error(106, ERROR);
305  
306  /** The Constant BAD_INHERITDOC. */
307  public static final Error BAD_INHERITDOC = new Error(107, HIDDEN);
308  
309  /** The Constant HIDDEN_LINK. */
310  public static final Error HIDDEN_LINK = new Error(108, WARNING);
311  
312  /** The Constant HIDDEN_CONSTRUCTOR. */
313  public static final Error HIDDEN_CONSTRUCTOR = new Error(109, WARNING);
314  
315  /** The Constant UNAVAILABLE_SYMBOL. */
316  public static final Error UNAVAILABLE_SYMBOL = new Error(110, ERROR);
317  
318  /** The Constant HIDDEN_SUPERCLASS. */
319  public static final Error HIDDEN_SUPERCLASS = new Error(111, WARNING);
320  
321  /** The Constant DEPRECATED. */
322  public static final Error DEPRECATED = new Error(112, HIDDEN);
323  
324  /** The Constant DEPRECATION_MISMATCH. */
325  public static final Error DEPRECATION_MISMATCH = new Error(113, WARNING);
326  
327  /** The Constant MISSING_COMMENT. */
328  public static final Error MISSING_COMMENT = new Error(114, WARNING);
329  
330  /** The Constant IO_ERROR. */
331  public static final Error IO_ERROR = new Error(115, HIDDEN);
332  
333  /** The Constant NO_SINCE_DATA. */
334  public static final Error NO_SINCE_DATA = new Error(116, HIDDEN);
335  
336  /** The Constant NO_FEDERATION_DATA. */
337  public static final Error NO_FEDERATION_DATA = new Error(117, WARNING);
338  
339  /** The Constant BROKEN_SINCE_FILE. */
340  public static final Error BROKEN_SINCE_FILE = new Error(118, ERROR);
341
342  /** The Constant ERRORS. */
343  public static final Error[] ERRORS =
344      {UNRESOLVED_LINK, BAD_INCLUDE_TAG, UNKNOWN_TAG, UNKNOWN_PARAM_TAG_NAME,
345          UNDOCUMENTED_PARAMETER, BAD_ATTR_TAG, BAD_INHERITDOC, HIDDEN_LINK, HIDDEN_CONSTRUCTOR,
346          UNAVAILABLE_SYMBOL, HIDDEN_SUPERCLASS, DEPRECATED, DEPRECATION_MISMATCH, MISSING_COMMENT,
347          IO_ERROR, NO_SINCE_DATA, NO_FEDERATION_DATA, PARSE_ERROR, ADDED_PACKAGE, ADDED_CLASS,
348          ADDED_METHOD, ADDED_FIELD, ADDED_INTERFACE, REMOVED_PACKAGE, REMOVED_CLASS,
349          REMOVED_METHOD, REMOVED_FIELD, REMOVED_INTERFACE, CHANGED_STATIC, CHANGED_FINAL,
350          CHANGED_TRANSIENT, CHANGED_VOLATILE, CHANGED_TYPE, CHANGED_VALUE, CHANGED_SUPERCLASS,
351          CHANGED_SCOPE, CHANGED_ABSTRACT, CHANGED_THROWS, CHANGED_NATIVE, CHANGED_CLASS,
352          CHANGED_DEPRECATED, CHANGED_SYNCHRONIZED, BROKEN_SINCE_FILE};
353
354  /**
355     * Sets the error level.
356     * 
357     * @param code the code
358     * @param level the level
359     * @return true, if successful
360     */
361  public static boolean setErrorLevel(int code, int level) {
362    for (Error e : ERRORS) {
363      if (e.code == code) {
364        e.level = level;
365        return true;
366      }
367    }
368    return false;
369  }
370}