alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.checkstyle;

import java.io.*;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.Properties;
import java.io.File;
import java.io.IOException;

import org.openide.cookies.SourceCookie;
import org.openide.explorer.view.*;
import org.openide.nodes.*;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.FileObject;
import org.openide.ErrorManager;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.text.Line;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.text.DataEditorSupport;

import com.puppycrawl.tools.checkstyle.*;
import com.puppycrawl.tools.checkstyle.api.*;

import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.client.*;
import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider;
import org.netbeans.modules.tasklist.providers.SuggestionContext;

/**
 * This class uses the Checkstyle rule checker to provide rule violation
 * suggestions.
 * 

* @todo This version only operates on the disk-versions of * the source files! Either get checkstyle modified to * have a Reader interface, or save files to temporary buffers. * @todo Add automatic fixers for some of these rules. * @todo Refactor so I can share some code with the pmd bridge *

* @author Tor Norbye */ public class ViolationProvider extends DocumentSuggestionProvider implements AuditListener { final private static String TYPE = "checkstyle-violations"; // NOI18N public String getType() { return TYPE; } /** List "owned" by the scan() method and updated by the audit listener * methods. */ private List tasks = null; public List scan(SuggestionContext env) { tasks = null; SuggestionManager manager = SuggestionManager.getDefault(); if (!manager.isEnabled(TYPE)) { return null; } /* This code is for looking up the dynamic content of the document - but see below - we don't need it yet... SourceCookie cookie = (SourceCookie)dobj.getCookie(SourceCookie.class); // The file is not a java file if(cookie == null) { return null; } String text = null; try { int len = doc.getLength(); text = doc.getText(0, len); } catch (BadLocationException e) { ErrorManager.getDefault().notify(ErrorManager.WARNING, e); return null; } Reader reader = new StringReader(text); //String name = cookie.getSource().getClasses()[0].getName().getFullName(); */ // Checkstyle doesn't seem to have an API where I can pass in // a string reader - it wants to read the files directly! FileObject fo = env.getFileObject(); DataObject dobj = null; try { dobj = DataObject.find(fo); } catch (DataObjectNotFoundException e) { ErrorManager.getDefault().notify(e); } File file = (dobj != null && dobj.isModified() == false) ? FileUtil.toFile(fo) : null; if (file != null) { try { if (callCheckstyle(file) == false) { return null; } } catch (Exception e) { ErrorManager.getDefault().notify(e); return null; } } else { Writer out = null; try { File tmp = File.createTempFile("tl_cs", "tmp"); // NOI18N tmp.deleteOnExit(); out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(tmp))); CharSequence chars = env.getCharSequence(); for (int i=0; i

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.