66
77import java .io .BufferedReader ;
88import java .io .ByteArrayInputStream ;
9- import java .io .ByteArrayOutputStream ;
109import java .io .IOException ;
1110import java .io .InputStreamReader ;
1211import java .io .PrintWriter ;
12+ import java .io .StringWriter ;
1313import java .lang .reflect .InvocationTargetException ;
1414import java .util .Iterator ;
15+ import java .util .Locale ;
1516
1617import org .eclipse .core .resources .IFile ;
1718import org .eclipse .core .resources .IFolder ;
2829import org .eclipse .jface .viewers .ISelection ;
2930import org .eclipse .jface .viewers .IStructuredSelection ;
3031import org .eclipse .swt .widgets .Display ;
31- import org .eclipse .ui .IEditorInput ;
32- import org .eclipse .ui .IEditorPart ;
33- import org .eclipse .ui .IFileEditorInput ;
3432import org .eclipse .ui .IViewActionDelegate ;
3533import org .eclipse .ui .IViewPart ;
3634import org .slf4j .Logger ;
3937import net .sourceforge .pmd .eclipse .runtime .PMDRuntimeConstants ;
4038import net .sourceforge .pmd .eclipse .runtime .cmd .AbstractDefaultCommand ;
4139import net .sourceforge .pmd .eclipse .ui .nls .StringKeys ;
42- import net .sourceforge .pmd .eclipse .util .IOUtil ;
4340
4441/**
4542 * Implements the clear reviews action
@@ -52,21 +49,18 @@ public class ClearReviewsAction extends AbstractUIAction implements IResourceVis
5249 private static final Logger LOG = LoggerFactory .getLogger (ClearReviewsAction .class );
5350 private IProgressMonitor monitor ;
5451
55- /**
56- * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
57- */
52+ @ Override
5853 public void init (IViewPart view ) {
5954 setActivePart (null , view .getSite ().getPage ().getActivePart ());
6055 }
6156
62- /**
63- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
64- */
57+ @ Override
6558 public void run (IAction action ) {
6659 LOG .info ("Remove violation reviews requested." );
6760 ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog (Display .getCurrent ().getActiveShell ());
6861 try {
6962 monitorDialog .run (false , false , new IRunnableWithProgress () {
63+ @ Override
7064 public void run (IProgressMonitor monitor ) throws InvocationTargetException , InterruptedException {
7165 setMonitor (monitor );
7266 clearReviews ();
@@ -80,13 +74,6 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
8074 }
8175 }
8276
83- /**
84- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
85- * org.eclipse.jface.viewers.ISelection)
86- */
87- public void selectionChanged (IAction action , ISelection selection ) {
88- }
89-
9077 /**
9178 * Get the monitor
9279 *
@@ -131,56 +118,36 @@ protected void monitorSubTask(String message) {
131118 protected void clearReviews () {
132119
133120 try {
134- // If action is started from a view, the process all selected
135- // resource
136- if (isViewPart ()) {
137- ISelection selection = targetSelection ();
138-
139- if (selection != null && selection instanceof IStructuredSelection ) {
140- IStructuredSelection structuredSelection = (IStructuredSelection ) selection ;
141- if (getMonitor () != null ) {
142- getMonitor ().beginTask (getString (StringKeys .MONITOR_REMOVE_REVIEWS ), IProgressMonitor .UNKNOWN );
143-
144- Iterator <?> i = structuredSelection .iterator ();
145- while (i .hasNext ()) {
146- Object object = i .next ();
147- IResource resource = null ;
148-
149- if (object instanceof IMarker ) {
150- resource = ((IMarker ) object ).getResource ();
151- } else if (object instanceof IAdaptable ) {
152- IAdaptable adaptable = (IAdaptable ) object ;
153- resource = (IResource ) adaptable .getAdapter (IResource .class );
154- } else {
155- LOG .warn ("The selected object is not adaptable" );
156- LOG .debug (" -> selected object = " + object );
157- }
158-
159- if (resource != null ) {
160- resource .accept (this );
161- } else {
162- LOG .warn ("The selected object cannot adapt to a resource." );
163- LOG .debug (" -> selected object" + object );
164- }
121+ ISelection selection = targetSelection ();
122+
123+ if (selection instanceof IStructuredSelection ) {
124+ IStructuredSelection structuredSelection = (IStructuredSelection ) selection ;
125+ if (getMonitor () != null ) {
126+ getMonitor ().beginTask (getString (StringKeys .MONITOR_REMOVE_REVIEWS ), IProgressMonitor .UNKNOWN );
127+
128+ Iterator <?> i = structuredSelection .iterator ();
129+ while (i .hasNext ()) {
130+ Object object = i .next ();
131+ IResource resource = null ;
132+
133+ if (object instanceof IMarker ) {
134+ resource = ((IMarker ) object ).getResource ();
135+ } else if (object instanceof IAdaptable ) {
136+ IAdaptable adaptable = (IAdaptable ) object ;
137+ resource = (IResource ) adaptable .getAdapter (IResource .class );
138+ } else {
139+ LOG .warn ("The selected object is not adaptable" );
140+ LOG .debug (" -> selected object = " + object );
165141 }
166- }
167- }
168- }
169142
170- // If action is started from an editor, process the file currently
171- // edited
172- if (isEditorPart ()) {
173- IEditorInput editorInput = ((IEditorPart ) this .targetPart ()).getEditorInput ();
174- if (editorInput instanceof IFileEditorInput ) {
175- ((IFileEditorInput ) editorInput ).getFile ().accept (this );
176- } else {
177- LOG .debug ("The kind of editor input is not supported. The editor input if of type: "
178- + editorInput .getClass ().getName ());
143+ if (resource != null ) {
144+ resource .accept (this );
145+ } else {
146+ LOG .warn ("The selected object cannot adapt to a resource." );
147+ LOG .debug (" -> selected object" + object );
148+ }
149+ }
179150 }
180- } else {
181- // else this is not supported
182- LOG .debug ("This action is not supported on this kind of part. This part type is: "
183- + targetPartClassName ());
184151 }
185152 } catch (CoreException e ) {
186153 logError ("Core Exception when clearing violations reviews" , e );
@@ -208,7 +175,7 @@ private static boolean isReviewable(IFile file) {
208175 if (AbstractDefaultCommand .isJavaFile (file )) {
209176 return true ;
210177 }
211- return file .getName ().toLowerCase ().endsWith (".jsp" );
178+ return file .getName ().toLowerCase (Locale . ROOT ).endsWith (".jsp" );
212179 }
213180
214181 /**
@@ -223,13 +190,11 @@ private String removeReviews(IFile file) {
223190 return null ;
224191 }
225192
226- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
227- PrintWriter out = null ;
193+ StringWriter modified = new StringWriter ();
228194 boolean noChange = true ;
229- try {
195+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (file .getContents (), file .getCharset ()));
196+ PrintWriter out = new PrintWriter (modified )) {
230197 boolean comment = false ;
231- BufferedReader reader = new BufferedReader (new InputStreamReader (file .getContents ()));
232- out = new PrintWriter (baos );
233198
234199 while (reader .ready ()) {
235200 String origLine = reader .readLine ();
@@ -265,12 +230,9 @@ private String removeReviews(IFile file) {
265230 logError (StringKeys .ERROR_CORE_EXCEPTION , e );
266231 } catch (IOException e ) {
267232 logError (StringKeys .ERROR_IO_EXCEPTION , e );
268- } finally {
269- IOUtil .closeQuietly (baos );
270- IOUtil .closeQuietly (out );
271233 }
272234
273- return noChange ? null : baos .toString ();
235+ return noChange ? null : modified .toString ();
274236 }
275237
276238 /**
@@ -281,15 +243,15 @@ private String removeReviews(IFile file) {
281243 */
282244 private void saveNewContent (IFile file , String newContent ) {
283245 try {
284- file .setContents (new ByteArrayInputStream (newContent .getBytes ()), false , true , getMonitor ());
246+ file .setContents (new ByteArrayInputStream (newContent .getBytes (file . getCharset () )), false , true , getMonitor ());
285247 } catch (CoreException e ) {
286248 logError (StringKeys .ERROR_CORE_EXCEPTION , e );
249+ } catch (IOException e ) {
250+ logError (StringKeys .ERROR_IO_EXCEPTION , e );
287251 }
288252 }
289253
290- /**
291- * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource)
292- */
254+ @ Override
293255 public boolean visit (IResource resource ) throws CoreException {
294256 if (resource instanceof IFile ) {
295257 clearReviews ((IFile ) resource );
0 commit comments