1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.IOException ; 23 24 import edu.umd.cs.findbugs.xml.XMLAttributeList; 25 import edu.umd.cs.findbugs.xml.XMLOutput; 26 27 33 public class StringAnnotation implements BugAnnotation { 34 private static final long serialVersionUID = 1L; 35 36 private static final String DEFAULT_ROLE = "STRING_DEFAULT"; 37 38 final private String value; 39 private String description; 40 41 46 public StringAnnotation(String value) { 47 this.value = quoteCharacters(value); 48 this.description = DEFAULT_ROLE; 49 } 50 51 @Override 53 public Object clone() { 54 try { 55 return super.clone(); 56 } catch (CloneNotSupportedException e) { 57 throw new AssertionError (e); 58 } 59 } 60 61 62 private static String quoteCharacters(String s) { 63 StringBuffer result = null; 64 for(int i = 0, max = s.length(), delta = 0; i < max; i++) { 65 char c = s.charAt(i); 66 String replacement = null; 67 68 if (c == '&') { 69 replacement = "&"; 70 } else if (c == '<') { 71 replacement = "<"; 72 } else if (c == '\r') { 73 replacement = " "; 74 } else if (c == '>') { 75 replacement = ">"; 76 } else if (c == '"') { 77 replacement = """; 78 } else if (c == '\'') { 79 replacement = "'"; 80 } 81 82 if (replacement != null) { 83 if (result == null) { 84 result = new StringBuffer (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 102 public String getValue() { 103 return value; 104 } 105 106 public void accept(BugAnnotationVisitor visitor) { 107 visitor.visitStringAnnotation(this); 108 } 109 110 public String format(String key, ClassAnnotation primaryClass) { 111 return value; 112 } 113 114 public void setDescription(String description) { 115 this.description = description; 116 } 117 118 public String getDescription() { 119 return description; 120 } 121 122 @Override 123 public int hashCode() { 124 return value.hashCode(); 125 } 126 127 @Override 128 public boolean equals(Object 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)) return this.getClass().getName().compareTo(o.getClass().getName()); 137 return value.compareTo(((StringAnnotation) o).value); 138 } 139 140 @Override 141 public String toString() { 142 String pattern = I18N.instance().getAnnotationDescription(description); 143 FindBugsMessageFormat format = new FindBugsMessageFormat(pattern); 144 return format.format(new BugAnnotation[]{this}, null); 145 } 146 147 150 151 private static final String ELEMENT_NAME = "String"; 152 153 public void writeXML(XMLOutput xmlOutput) throws IOException { 154 writeXML(xmlOutput, false); 155 } 156 157 public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException { 158 XMLAttributeList attributeList = new XMLAttributeList() 159 .addAttribute("value", value); 160 161 String 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 | Popular Tags |