KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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 import java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24
25 import edu.umd.cs.findbugs.annotations.NonNull;
26 import edu.umd.cs.findbugs.annotations.CheckForNull;
27 import edu.umd.cs.findbugs.xml.XMLAttributeList;
28 import edu.umd.cs.findbugs.xml.XMLWriteable;
29 import edu.umd.cs.findbugs.xml.XMLOutput;
30
31 /**
32  * class to hold the user annotation and user designation for a BugInstance
33  */

34 public class BugDesignation implements XMLWriteable, Serializable JavaDoc {
35
36     /** The default key for the user designation.
37      * Bad things could happen if this key isn't in getUserDesignations() */

38     public static final String JavaDoc UNCLASSIFIED = "UNCLASSIFIED";
39
40
41     /** user designation -- value should be one of the keys
42      * returned by I18N.getInstance().getUserDesignations() */

43     @NonNull private String JavaDoc designation = UNCLASSIFIED;
44
45     // TODO: make this @CheckForNull
46
private String JavaDoc user;
47
48     private long timestamp;
49
50     /** free text from the user */
51     //TODO: make this @CheckForNull
52
private String JavaDoc annotationText;
53     
54     /** return the user designation
55      * E.g., "MOSTLY_HARMLESS", "CRITICAL", "NOT_A_BUG", etc.
56      *
57      * Note that this is the key, suitable for writing to XML,
58      * but not for showing to the user.
59      * @see I18N#getUserDesignation(String key) */

60     @NonNull public String JavaDoc getDesignationKey() {
61         return designation;
62     }
63
64     /** set the user designation
65      * E.g., "MOSTLY_HARMLESS", "CRITICAL", "NOT_A_BUG", etc.
66      *
67      * If the argument is null, it will be treated as UNCLASSIFIED.
68      *
69      * Note that this is the key, suitable for writing to XML,
70      * but not what the user sees. Strange things could happen
71      * if designationKey is not one of the keys returned by
72      * I18N.instance().getUserDesignations().
73      * @see I18N#getUserDesignationKeys() */

74     public void setDesignationKey(String JavaDoc designationKey) {
75         designation = (designationKey!=null ? designationKey : UNCLASSIFIED);
76     }
77
78
79     @CheckForNull public String JavaDoc getUser() {
80         return user;
81     }
82     public void setUser(String JavaDoc u) {
83         user = u;
84     }
85
86     public long getTimestamp() {
87             return timestamp;
88     }
89     public void setTimestamp(long ts) {
90             timestamp = ts;
91     }
92
93     @CheckForNull public String JavaDoc getAnnotationText() {
94             return annotationText;
95     }
96     public void setAnnotationText(String JavaDoc s) {
97             annotationText = s;
98     }
99
100     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
101         XMLAttributeList attributeList = new XMLAttributeList();
102         // all three of these xml attributes are optional
103
if (designation != null && !UNCLASSIFIED.equals(designation))
104             attributeList.addAttribute("designation", designation);
105         if (user != null && !"".equals(user))
106             attributeList.addAttribute("user", user);
107         if (timestamp > 0)
108             attributeList.addAttribute("timestamp", String.valueOf(timestamp));
109
110         if ((annotationText != null && !"".equals(annotationText))) {
111             xmlOutput.openTag("UserAnnotation", attributeList);
112             xmlOutput.writeCDATA(annotationText);
113             xmlOutput.closeTag("UserAnnotation");
114         } else {
115             xmlOutput.openCloseTag("UserAnnotation", attributeList);
116         }
117     }
118
119     /** replace unset fields of this user designation with values set in the other */
120     public void merge(@CheckForNull BugDesignation other) {
121         if (other == null) return;
122         boolean changed = false;
123         if ( (annotationText==null || annotationText.length()==0)
124                 && other.annotationText!=null && other.annotationText.length()>0) {
125             annotationText = other.annotationText;
126             changed = true;
127         }
128         if ( (designation==null || UNCLASSIFIED.equals(designation) || designation.length()==0)
129                 && other.designation!=null && other.designation.length()>0) {
130             designation = other.designation;
131             changed = true;
132         }
133         if (!changed) return; // if no changes don't even try to copy user or timestamp
134

135         if ( (user==null || user.length()==0) && other.user!=null && other.user.length()>0) {
136             user = other.user;
137         }
138         if (timestamp==0 && other.timestamp!=0) {
139             timestamp = other.timestamp;
140         }
141     }
142
143 }
144
Popular Tags