1 19 20 package org.netbeans.modules.tasklist.checkstyle; 21 22 import com.puppycrawl.tools.checkstyle.Checker; 23 import com.puppycrawl.tools.checkstyle.ConfigurationLoader; 24 import com.puppycrawl.tools.checkstyle.ModuleFactory; 25 import com.puppycrawl.tools.checkstyle.PropertiesExpander; 26 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 27 import com.puppycrawl.tools.checkstyle.api.AuditListener; 28 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 29 import com.puppycrawl.tools.checkstyle.api.Configuration; 30 import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 31 import java.io.BufferedOutputStream ; 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.util.ArrayList ; 35 import java.util.Properties ; 36 import java.io.IOException ; 37 import java.io.OutputStreamWriter ; 38 import java.io.Writer ; 39 import java.util.List ; 40 import javax.swing.text.Document ; 41 import org.netbeans.modules.tasklist.checkstyle.options.CheckstyleSettings; 42 import org.netbeans.modules.tasklist.client.SuggestionAgent; 43 import org.netbeans.modules.tasklist.client.SuggestionManager; 44 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 45 import org.netbeans.modules.tasklist.client.SuggestionPriority; 46 47 import org.openide.filesystems.FileUtil; 48 import org.openide.filesystems.FileObject; 49 import org.openide.ErrorManager; 50 import org.openide.loaders.DataObject; 51 import org.openide.loaders.DataObjectNotFoundException; 52 import org.openide.text.Line; 53 import org.openide.util.SharedClassObject; 54 55 import org.netbeans.modules.tasklist.core.TLUtils; 56 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider; 57 import org.netbeans.modules.tasklist.providers.SuggestionContext; 58 59 71 72 73 public class ViolationProvider extends DocumentSuggestionProvider 74 implements AuditListener { 75 76 77 final private static String TYPE = "checkstyle-violations"; 79 80 public String getType() { 81 return TYPE; 82 } 83 84 86 private List tasks = null; 87 88 89 public List scan(final SuggestionContext env) { 90 tasks = null; 91 this.env = env; 92 final SuggestionManager manager = SuggestionManager.getDefault(); 93 if (!manager.isEnabled(TYPE)) { 94 return null; 95 } 96 97 117 118 final FileObject fo = env.getFileObject(); 121 try { 122 dataobject = DataObject.find(fo); 123 } catch (DataObjectNotFoundException e) { 124 ErrorManager.getDefault().notify(e); 125 } 126 final File file = (dataobject != null && !dataobject.isModified()) ? FileUtil.toFile(fo) : null; 127 128 if (file != null) { 129 try { 130 if (callCheckstyle(file) == false) { 131 return null; 132 } 133 } catch (Exception e) { 134 ErrorManager.getDefault().notify(e); 135 return null; 136 } 137 } else { 138 Writer out = null; 139 try { 140 final File tmp = File.createTempFile("tl_cs", "tmp"); tmp.deleteOnExit(); 142 out = new OutputStreamWriter (new BufferedOutputStream (new FileOutputStream (tmp))); 143 final CharSequence chars = env.getCharSequence(); 144 for (int i = 0; i < chars.length(); i++) { 145 out.write(chars.charAt(i)); 146 } 147 if (callCheckstyle(file) == false) { 148 return null; 149 } 150 tmp.delete(); 151 } catch (IOException e) { 152 ErrorManager.getDefault().notify(e); 153 return null; 154 } finally { 155 if (out != null) { 156 try { 157 out.close(); 158 } catch (IOException e) { 159 ErrorManager.getDefault().notify(e); 160 } 161 } 162 } 163 164 } 165 return tasks; 166 } 167 168 169 private boolean callCheckstyle(final File file) { 170 try { 172 final Checker checker = new Checker(); 173 ModuleFactory moduleFactory = null; 174 checker.setModuleFactory(moduleFactory); 175 Configuration config = null; 176 final Properties props = System.getProperties(); 177 178 final CheckstyleSettings settings 179 = (CheckstyleSettings) SharedClassObject.findObject(CheckstyleSettings.class, true); 180 181 config = ConfigurationLoader.loadConfiguration(settings.getCheckstyle(), new PropertiesExpander(props)); 182 checker.configure(config); 183 checker.addListener(this); 184 checker.process(new File [] {file }); return true; 186 } catch (CheckstyleException e) { 187 ErrorManager.getDefault().notify(e); 188 return false; 189 } 190 } 191 192 194 198 public void auditStarted(final AuditEvent aEvt) { 199 } 201 202 206 public void auditFinished(final AuditEvent aEvt) { 207 } 209 210 214 public void fileStarted(final AuditEvent aEvt) { 215 } 217 218 222 public void fileFinished(final AuditEvent aEvt) { 223 } 225 226 231 public void addException(final AuditEvent aEvt, final Throwable aThrowable) { 232 } 234 235 236 240 public void addError(AuditEvent aEvt) { 241 243 try { 244 final int lineNo = Math.max(1, aEvt.getLine()); 245 final Line line = TLUtils.getLineByNumber(dataobject, lineNo); 246 247 final SuggestionPerformer action = getAction(aEvt); 248 final String description = aEvt.getLocalizedMessage().getMessage(); 249 250 final SuggestionManager manager = SuggestionManager.getDefault(); 251 252 final SuggestionAgent s = manager.createSuggestion( 253 env.getFileObject(), 254 TYPE, 255 description, 256 action, 257 this); 258 259 final SeverityLevel sv = aEvt.getSeverityLevel(); 260 261 if (sv != SeverityLevel.IGNORE) { 262 263 if (sv == SeverityLevel.INFO) { 264 s.setPriority(SuggestionPriority.LOW); 265 266 } else if (sv == SeverityLevel.WARNING) { 267 s.setPriority(SuggestionPriority.MEDIUM_LOW); 268 269 } else if (sv == SeverityLevel.ERROR) { 270 s.setPriority(SuggestionPriority.MEDIUM); 275 } 276 277 s.setLine(line); 278 s.setDetails(aEvt.getLocalizedMessage().getKey()); 279 280 if (tasks == null) { 281 tasks = new ArrayList (40); } 283 tasks.add(s.getSuggestion()); 284 } 285 286 } catch (Exception e) { 287 ErrorManager.getDefault().notify(e); 288 } 289 } 290 291 292 private SuggestionPerformer getAction( 293 final AuditEvent aEvt) { 294 295 final String key = aEvt.getLocalizedMessage().getKey(); 296 if (key != null) { 297 298 299 if (key.contains("trailing") && key.contains("spaces")) { return new TrailingSpacesSuggestionPerformer(env.getDocument(),aEvt.getLine()); 302 303 } else if ("import.unused".equals(key) || "import.avoidStar".equals(key)) { 304 return new DeleteLineSuggestionPerformer(env.getDocument(),aEvt.getLine()); 305 306 } else if ("ws.notPreceded".equals(key) || "ws.notFollowed".equals(key)) { 307 return new InsertStringSuggestionPerformer(env.getDocument(), aEvt.getLine(), aEvt.getColumn()) 308 .setString(" "); 309 310 } else if ("ws.preceded".equals(key) || "ws.followed".equals(key)) { 311 return new DeleteSpaceSuggestionPerformer(env.getDocument(), aEvt.getLine(), aEvt.getColumn()); 312 313 } else if ("final.parameter".equalsIgnoreCase(key)) { 314 return new InsertStringSuggestionPerformer(env.getDocument(), aEvt.getLine(), aEvt.getColumn()) 315 .setString("final "); 316 317 } else if ("final.variable".equalsIgnoreCase(key)) { 318 return new InsertFinalVariableKeywordSuggestionPerformer(env.getDocument(),aEvt.getLine(),aEvt.getColumn()); 319 320 } else if ("javadoc.missing".equalsIgnoreCase(key)){ 321 return new InsertStringSuggestionPerformer(env.getDocument(),aEvt.getLine(),aEvt.getColumn()) 322 .setString("/** TODO comment me. **/\n "); 323 324 } else if ("javadoc.noperiod".equalsIgnoreCase(key)) { 325 return new InsertStringSuggestionPerformer(env.getDocument(), aEvt.getLine(), aEvt.getColumn()) 326 .setString("."); 327 328 } 329 330 } 331 return null; 332 } 333 334 335 336 private List showingTasks = null; 337 338 339 private SuggestionContext env; 340 341 private DataObject dataobject = null; 342 343 private Document document = null; 344 345 private Object request = null; 346 } 347 348 | Popular Tags |