1 19 20 package edu.umd.cs.findbugs.ba.interproc; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.OutputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.io.Writer ; 32 import java.nio.charset.Charset ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.TreeSet ; 37 38 import edu.umd.cs.findbugs.ba.AnalysisContext; 39 import edu.umd.cs.findbugs.ba.ClassMember; 40 import edu.umd.cs.findbugs.ba.XFactory; 41 import edu.umd.cs.findbugs.ba.XField; 42 import edu.umd.cs.findbugs.ba.XMethod; 43 44 51 public abstract class PropertyDatabase<KeyType extends ClassMember, ValueType> { 52 private Map <KeyType, ValueType> propertyMap; 53 54 58 protected PropertyDatabase() { 59 this.propertyMap = new HashMap <KeyType, ValueType>(); 60 } 61 62 68 public void setProperty(KeyType key, ValueType property) { 69 propertyMap.put(key, property); 70 } 71 72 78 public ValueType getProperty(KeyType key) { 79 return propertyMap.get(key); 80 } 81 82 public Set <KeyType> getKeys() { 83 return propertyMap.keySet(); 84 } 85 90 public boolean isEmpty() { 91 return propertyMap.isEmpty(); 92 } 93 94 100 public ValueType removeProperty(KeyType key) { 101 return propertyMap.remove(key); 102 } 103 104 111 public void readFromFile(String fileName) throws IOException , PropertyDatabaseFormatException { 112 read(new FileInputStream (fileName)); 113 } 114 115 124 public void read(InputStream in) throws IOException , PropertyDatabaseFormatException { 125 BufferedReader reader = null; 126 127 try { 128 reader = new BufferedReader ( 129 new InputStreamReader (in, Charset.forName("UTF-8"))); 130 String line; 131 while ((line = reader.readLine()) != null) { 132 line = line.trim(); 133 if (line.equals("")) 134 continue; 135 int bar = line.indexOf('|'); 136 if (bar < 0) { 137 throw new PropertyDatabaseFormatException( 138 "Invalid property database: missing separator"); 139 } 140 KeyType key = parseKey(line.substring(0, bar)); 141 ValueType property = decodeProperty(line.substring(bar+1)); 142 143 setProperty(key, property); 144 } 145 } finally { 146 try { 147 if (reader != null) 148 reader.close(); 149 } catch (IOException e) { 150 } 152 } 153 } 154 155 161 public void writeToFile(String fileName) throws IOException { 162 write(new FileOutputStream (fileName)); 163 } 164 165 @SuppressWarnings ("unchecked") 166 private KeyType intern(XFactory xFactory, KeyType key) { 167 KeyType result = key; 168 try { 169 if (key instanceof XField) 170 return (KeyType) xFactory.intern((XField)key); 171 else if (key instanceof XMethod) 172 return (KeyType) xFactory.intern((XMethod)key); 173 } catch (Exception e) { 174 return key; 175 } 176 return result; 177 } 178 186 public void write(OutputStream out) throws IOException { 187 BufferedWriter writer = null; 188 boolean missingClassWarningsSuppressed = AnalysisContext.currentAnalysisContext().setMissingClassWarningsSuppressed(true); 189 190 try { 191 writer = new BufferedWriter ( 192 new OutputStreamWriter (out, Charset.forName("UTF-8"))); 193 194 TreeSet <KeyType> sortedMethodSet = new TreeSet <KeyType>(); 195 XFactory xFactory = AnalysisContext.currentXFactory(); 196 sortedMethodSet.addAll(propertyMap.keySet()); 197 for (KeyType key : sortedMethodSet) { 198 if (AnalysisContext.currentAnalysisContext().isApplicationClass(key.getClassName())) { 199 200 ValueType property = propertyMap.get(key); 201 writeKey(writer, intern(xFactory, key)); 202 writer.write("|"); 203 writer.write(encodeProperty(property)); 204 writer.write("\n"); 205 } 206 } 207 } finally { 208 AnalysisContext.currentAnalysisContext().setMissingClassWarningsSuppressed(missingClassWarningsSuppressed); 209 210 try { 211 if (writer != null) 212 writer.close(); 213 } catch (IOException e) { 214 } 216 } 217 } 218 219 226 protected abstract KeyType parseKey(String s) throws PropertyDatabaseFormatException; 227 228 234 protected abstract void writeKey(Writer writer, KeyType key) throws IOException ; 235 236 244 protected abstract ValueType decodeProperty(String propStr) 245 throws PropertyDatabaseFormatException; 246 247 254 protected abstract String encodeProperty(ValueType property); 255 256 } 257 | Popular Tags |