KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > main > Report


1 package polyglot.main;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Stack JavaDoc;
11
12 /** Class used for reporting debug messages. */
13 public class Report {
14   /** A collection of string names of topics which can be used with the
15       -report command-line switch */

16   public final static Collection JavaDoc topics = new HashSet JavaDoc();
17
18   /** A collection of string names of topics which we should always check
19       if we should report. */

20   public final static Stack JavaDoc should_report = new Stack JavaDoc();
21
22   /**
23    * The topics that the user has selected to report, mapped to the level
24    * they want to report them to.
25    */

26   protected final static Map JavaDoc reportTopics = new HashMap JavaDoc(); // Map[String, Integer]
27

28
29   /**
30    * Indicates if there is no reporting at all.
31    * The normal case is that we do not report anything, so for efficiency
32    * reasons, since <code>should_report</code> is called so often, we'll use
33    * this flag to bypass a lot of the checking. When the options are processed,
34    * this flag should be changed.
35    */

36   static boolean noReporting = true;
37   
38   /** Report topics understood by the base compiler. */
39   public final static String JavaDoc cfg = "cfg";
40   public final static String JavaDoc context = "context";
41   public final static String JavaDoc dataflow = "dataflow";
42   public final static String JavaDoc errors = "errors";
43   public final static String JavaDoc frontend = "frontend";
44   public final static String JavaDoc imports = "imports";
45   public final static String JavaDoc loader = "loader";
46   public final static String JavaDoc resolver = "resolver";
47   public final static String JavaDoc serialize = "serialize";
48   public final static String JavaDoc time = "time";
49   public final static String JavaDoc types = "types";
50   public final static String JavaDoc visit = "visit";
51   public final static String JavaDoc verbose = "verbose";
52   
53   // This topic is the level of detail that should be in messages.
54
public final static String JavaDoc debug = "debug";
55
56   static {
57     topics.add(cfg);
58     topics.add(context);
59     topics.add(dataflow);
60     topics.add(errors);
61     topics.add(frontend);
62     topics.add(imports);
63     topics.add(loader);
64     topics.add(resolver);
65     topics.add(serialize);
66     topics.add(time);
67     topics.add(types);
68     topics.add(visit);
69     topics.add(verbose);
70     topics.add(debug);
71
72     should_report.push(verbose);
73   }
74
75   /**
76    * Return whether a message on <code>topic</code> of obscurity
77    * <code>level</code> should be reported, based on use of the
78    * -report command-line switches given by the user.
79    */

80   public static boolean should_report(String JavaDoc topic, int level) {
81     if (noReporting)
82         return false;
83     return should_report(Collections.singletonList(topic), level);
84   }
85
86   /**
87    * Return whether a message on <code>topics</code> of obscurity
88    * <code>level</code> should be reported, based on use of the
89    * -report command-line switches given by the user.
90    */

91   public static boolean should_report(String JavaDoc[] topics, int level) {
92       if (noReporting)
93           return false;
94     return should_report(Arrays.asList(topics), level);
95   }
96
97   /**
98    * Return whether a message on <code>topics</code> of obscurity
99    * <code>level</code> should be reported, based on use of the
100    * -report command-line switches given by the user.
101    */

102   public static boolean should_report(Collection JavaDoc topics, int level) {
103       if (noReporting)
104           return false;
105     for (Iterator JavaDoc i = should_report.iterator(); i.hasNext();) {
106         String JavaDoc topic = (String JavaDoc) i.next();
107         if (level(topic) >= level) return true;
108     }
109     if (topics != null) {
110     for (Iterator JavaDoc i = topics.iterator(); i.hasNext();) {
111         String JavaDoc topic = (String JavaDoc) i.next();
112         if (level(topic) >= level) return true;
113     }
114     }
115     return false;
116   }
117   
118   public static void addTopic(String JavaDoc topic, int level) {
119       Integer JavaDoc i = (Integer JavaDoc)reportTopics.get(topic);
120       if (i == null || i.intValue() < level) {
121           reportTopics.put(topic, new Integer JavaDoc(level));
122       }
123       noReporting = false;
124   }
125
126   protected static int level(String JavaDoc name) {
127       Object JavaDoc i = reportTopics.get(name);
128       if (i == null) return 0;
129       else return ((Integer JavaDoc)i).intValue();
130   }
131
132   /** This is the standard way to report debugging information in the
133    * compiler. It reports a message of the specified level (which
134    * controls the presentation of the message. To test whether such
135    * message should be reported, use "should_report"
136    *
137    * NOTE: This is a change of spec from earlier versions of Report.
138    */

139   public static void report(int level, String JavaDoc message) {
140     for (int j = 1; j < level; j++) System.err.print(" ");
141     System.err.println(message);
142   }
143 }
144
Popular Tags