KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > FindBugsMessageFormat


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 /**
23  * Format the message for a BugInstance.
24  * This class works in much the same way as <code>java.text.MessageFormat</code>;
25  * however, each placeholder may have an optional "key" which specifies
26  * how the object at that position should be formatted.
27  * <p/>
28  * <p> Example:
29  * <pre>
30  * new FindBugsMessageFormat("BUG: {1} does something bad to field {2.fullField}")
31  * </pre>
32  * In this example, the method annotation at position 1 is formatted using
33  * the empty (default) key. The field annotation at position 2 is formatted
34  * using the "fullField" key, which uses the long format for the field rather
35  * than the usual "class.fieldname" format.
36  *
37  * @author David Hovemeyer
38  * @see BugInstance
39  */

40 public class FindBugsMessageFormat {
41     private String JavaDoc pattern;
42
43     /**
44      * Constructor.
45      *
46      * @param pattern the pattern for the message
47      */

48     public FindBugsMessageFormat(String JavaDoc pattern) {
49         this.pattern = pattern;
50     }
51
52     /**
53      * Format the message using the given array of BugAnnotations as arguments
54      * to bind to the placeholders in the pattern string.
55      *
56      * @param args the BugAnnotations used as arguments
57      * @param primaryClass TODO
58      * @return the formatted message
59      */

60     public String JavaDoc format(BugAnnotation[] args, ClassAnnotation primaryClass) {
61         String JavaDoc pat = pattern;
62         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
63
64         while (pat.length() > 0) {
65             int subst = pat.indexOf('{');
66             if (subst < 0) {
67                 result.append(pat);
68                 break;
69             }
70
71             result.append(pat.substring(0, subst));
72             pat = pat.substring(subst + 1);
73
74             int end = pat.indexOf('}');
75             if (end < 0)
76                 throw new IllegalStateException JavaDoc("bad pattern " + pattern);
77
78             String JavaDoc substPat = pat.substring(0, end);
79
80             int dot = substPat.indexOf('.');
81             String JavaDoc key = "";
82             if (dot >= 0) {
83                 key = substPat.substring(dot + 1);
84                 substPat = substPat.substring(0, dot);
85             }
86
87             int fieldNum;
88             try {
89                 fieldNum = Integer.parseInt(substPat);
90             } catch (NumberFormatException JavaDoc e) {
91                 throw new IllegalArgumentException JavaDoc("bad pattern " + pattern);
92             }
93
94             // System.out.println("fn: " + fieldNum);
95
if (fieldNum < 0) {
96                 result.append("?<?" + fieldNum + "/" + args.length + "???");
97             } else if (fieldNum > args.length) {
98                     result.append("?>?" + fieldNum + "/" + args.length + "???");
99             } else {
100                 BugAnnotation field = args[fieldNum];
101                 String JavaDoc formatted = "";
102                 try { formatted = field.format(key, primaryClass);
103                 } catch (IllegalArgumentException JavaDoc iae) {
104                     // unknown key -- not unprecedented when reading xml generated by older versions of findbugs
105
formatted = "\u00BF"+fieldNum+".(key="+key+")?"; // "\u00BF" is inverted question mark
106
System.err.println(iae.getMessage()+" in FindBugsMessageFormat"); // FIXME: log this error better
107
}
108                 result.append(formatted);
109             }
110
111             pat = pat.substring(end + 1);
112         }
113
114         return result.toString();
115     }
116 }
117
118 // vim:ts=4
119
Popular Tags