KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005, 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
24 import edu.umd.cs.findbugs.xml.XMLAttributeList;
25 import edu.umd.cs.findbugs.xml.XMLOutput;
26
27 /**
28  * Bug annotation class for integer values.
29  *
30  * @author William Pugh
31  * @see BugAnnotation
32  */

33 public class StringAnnotation implements BugAnnotation {
34     private static final long serialVersionUID = 1L;
35
36     private static final String JavaDoc DEFAULT_ROLE = "STRING_DEFAULT";
37
38     final private String JavaDoc value;
39      private String JavaDoc description;
40
41     /**
42      * Constructor.
43      *
44      * @param value the String value
45      */

46     public StringAnnotation(String JavaDoc value) {
47         this.value = quoteCharacters(value);
48         this.description = DEFAULT_ROLE;
49     }
50     
51     //@Override
52
@Override JavaDoc
53     public Object JavaDoc clone() {
54         try {
55             return super.clone();
56         } catch (CloneNotSupportedException JavaDoc e) {
57             throw new AssertionError JavaDoc(e);
58         }
59     }
60
61     
62      private static String JavaDoc quoteCharacters(String JavaDoc s) {
63             StringBuffer JavaDoc result = null;
64             for(int i = 0, max = s.length(), delta = 0; i < max; i++) {
65                 char c = s.charAt(i);
66                 String JavaDoc replacement = null;
67
68                 if (c == '&') {
69                     replacement = "&amp;";
70                 } else if (c == '<') {
71                     replacement = "&lt;";
72                 } else if (c == '\r') {
73                     replacement = "&#13;";
74                 } else if (c == '>') {
75                     replacement = "&gt;";
76                 } else if (c == '"') {
77                     replacement = "&quot;";
78                 } else if (c == '\'') {
79                     replacement = "&apos;";
80                 }
81
82                 if (replacement != null) {
83                     if (result == null) {
84                         result = new StringBuffer JavaDoc(s);
85                     }
86                     result.replace(i + delta, i + delta + 1, replacement);
87                     delta += (replacement.length() - 1);
88                 }
89             }
90             if (result == null) {
91                 return s;
92             }
93             return result.toString();
94         }
95
96      
97     /**
98      * Get the String value.
99      *
100      * @return the String value
101      */

102     public String JavaDoc getValue() {
103         return value;
104     }
105
106     public void accept(BugAnnotationVisitor visitor) {
107         visitor.visitStringAnnotation(this);
108     }
109
110     public String JavaDoc format(String JavaDoc key, ClassAnnotation primaryClass) {
111         return value;
112     }
113
114     public void setDescription(String JavaDoc description) {
115         this.description = description;
116     }
117
118     public String JavaDoc getDescription() {
119         return description;
120     }
121
122     @Override JavaDoc
123     public int hashCode() {
124         return value.hashCode();
125     }
126
127     @Override JavaDoc
128     public boolean equals(Object JavaDoc o) {
129         if (!(o instanceof StringAnnotation))
130             return false;
131         return value.equals(((StringAnnotation) o).value);
132     }
133
134     public int compareTo(BugAnnotation o) {
135         if (!(o instanceof StringAnnotation)) // BugAnnotations must be Comparable with any type of BugAnnotation
136
return this.getClass().getName().compareTo(o.getClass().getName());
137         return value.compareTo(((StringAnnotation) o).value);
138     }
139
140     @Override JavaDoc
141     public String JavaDoc toString() {
142         String JavaDoc pattern = I18N.instance().getAnnotationDescription(description);
143         FindBugsMessageFormat format = new FindBugsMessageFormat(pattern);
144         return format.format(new BugAnnotation[]{this}, null);
145     }
146
147     /* ----------------------------------------------------------------------
148      * XML Conversion support
149      * ---------------------------------------------------------------------- */

150
151     private static final String JavaDoc ELEMENT_NAME = "String";
152
153     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
154         writeXML(xmlOutput, false);
155     }
156
157     public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException JavaDoc {
158         XMLAttributeList attributeList = new XMLAttributeList()
159             .addAttribute("value", value);
160         
161         String JavaDoc role = getDescription();
162         if (!role.equals(DEFAULT_ROLE))
163             attributeList.addAttribute("role", role);
164         
165         BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages);
166     }
167
168
169     public boolean isSignificant() {
170         return true;
171     }
172 }
173
174 // vim:ts=4
175
Popular Tags