KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > builders > ErrorReporter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.builders;
12
13 import org.eclipse.core.filebuffers.FileBuffers;
14 import org.eclipse.core.filebuffers.ITextFileBuffer;
15 import org.eclipse.core.filebuffers.ITextFileBufferManager;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.pde.internal.core.PDECore;
24
25 public abstract class ErrorReporter {
26     
27     protected static final String JavaDoc[] BOOLEAN_VALUES =
28         new String JavaDoc[] { "true", "false" }; //$NON-NLS-1$ //$NON-NLS-2$
29

30     private int fErrorCount;
31     protected IFile fFile;
32     protected IProject fProject;
33     private PDEMarkerFactory fMarkerFactory;
34
35     public ErrorReporter(IFile file) {
36         fErrorCount = 0;
37         fFile = file;
38         if (fFile != null) {
39             fProject = fFile.getProject();
40         }
41     }
42
43     protected IMarker addMarker(String JavaDoc message, int lineNumber, int severity,
44             int problemID, String JavaDoc category) {
45         try {
46             IMarker marker = getMarkerFactory().createMarker(fFile, problemID, category);
47             marker.setAttribute(IMarker.MESSAGE, message);
48             marker.setAttribute(IMarker.SEVERITY, severity);
49             if (lineNumber == -1)
50                 lineNumber = 1;
51             marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
52             if (severity == IMarker.SEVERITY_ERROR) {
53                 fErrorCount += 1;
54             }
55             return marker;
56         } catch (CoreException e) {
57             PDECore.logException(e);
58         }
59         return null;
60     }
61
62     protected IDocument createDocument(IFile file) {
63         if (!file.exists()) {
64             return null;
65         }
66         ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
67         if (manager == null) {
68             return null;
69         }
70         try {
71             manager.connect(file.getFullPath(), null);
72             ITextFileBuffer textBuf = manager.getTextFileBuffer(file
73                     .getFullPath());
74             IDocument document = textBuf.getDocument();
75             manager.disconnect(file.getFullPath(), null);
76             return document;
77         } catch (CoreException e) {
78             PDECore.log(e);
79         }
80         return null;
81     }
82
83     public int getErrorCount() {
84         return fErrorCount;
85     }
86
87     private PDEMarkerFactory getMarkerFactory() {
88         if (fMarkerFactory == null)
89             fMarkerFactory = new PDEMarkerFactory();
90         return fMarkerFactory;
91     }
92
93
94     private void removeFileMarkers() {
95         try {
96             fFile.deleteMarkers(IMarker.PROBLEM, false, IResource.DEPTH_ZERO);
97             fFile.deleteMarkers(PDEMarkerFactory.MARKER_ID, false, IResource.DEPTH_ZERO);
98         } catch (CoreException e) {
99             PDECore.logException(e);
100         }
101     }
102
103     public IMarker report(String JavaDoc message, int line, int severity, int problemID, String JavaDoc category) {
104         if (severity == CompilerFlags.ERROR)
105             return addMarker(message, line, IMarker.SEVERITY_ERROR, problemID, category);
106         else if (severity == CompilerFlags.WARNING)
107             return addMarker(message, line, IMarker.SEVERITY_WARNING, problemID, category);
108         return null;
109     }
110
111     public IMarker report(String JavaDoc message, int line, int severity, String JavaDoc category) {
112         return report(message, line, severity, PDEMarkerFactory.NO_RESOLUTION, category);
113     }
114
115     protected IMarker report(String JavaDoc message, int line, String JavaDoc compilerFlag,
116             int problemID, String JavaDoc category) {
117         int severity = CompilerFlags.getFlag(fProject, compilerFlag);
118         if (severity != CompilerFlags.IGNORE) {
119             return report(message, line, severity, problemID, category);
120         }
121         return null;
122     }
123
124     protected void report(String JavaDoc message, int line, String JavaDoc compilerFlag, String JavaDoc category) {
125         report(message, line, compilerFlag, PDEMarkerFactory.NO_RESOLUTION, category);
126     }
127
128     public void validateContent(IProgressMonitor monitor) {
129         removeFileMarkers();
130         validate(monitor);
131     }
132     
133     protected abstract void validate(IProgressMonitor monitor);
134 }
135
Popular Tags