KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > $builderClassName$


1 package $packageName$;
2
3 import java.util.Map JavaDoc;
4
5 import javax.xml.parsers.ParserConfigurationException JavaDoc;
6 import javax.xml.parsers.SAXParser JavaDoc;
7 import javax.xml.parsers.SAXParserFactory JavaDoc;
8
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.resources.IMarker;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.IResourceDelta;
14 import org.eclipse.core.resources.IResourceDeltaVisitor;
15 import org.eclipse.core.resources.IResourceVisitor;
16 import org.eclipse.core.resources.IncrementalProjectBuilder;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.SAXParseException JavaDoc;
21 import org.xml.sax.helpers.DefaultHandler JavaDoc;
22
23 public class $builderClassName$ extends IncrementalProjectBuilder {
24
25     class SampleDeltaVisitor implements IResourceDeltaVisitor {
26         /*
27          * (non-Javadoc)
28          *
29          * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
30          */

31         public boolean visit(IResourceDelta delta) throws CoreException {
32             IResource resource = delta.getResource();
33             switch (delta.getKind()) {
34             case IResourceDelta.ADDED:
35                 // handle added resource
36
checkXML(resource);
37                 break;
38             case IResourceDelta.REMOVED:
39                 // handle removed resource
40
break;
41             case IResourceDelta.CHANGED:
42                 // handle changed resource
43
checkXML(resource);
44                 break;
45             }
46             //return true to continue visiting children.
47
return true;
48         }
49     }
50
51     class SampleResourceVisitor implements IResourceVisitor {
52         public boolean visit(IResource resource) {
53             checkXML(resource);
54             //return true to continue visiting children.
55
return true;
56         }
57     }
58
59     class XMLErrorHandler extends DefaultHandler JavaDoc {
60         
61         private IFile file;
62
63         public XMLErrorHandler(IFile file) {
64             this.file = file;
65         }
66
67         private void addMarker(SAXParseException JavaDoc e, int severity) {
68             $builderClassName$.this.addMarker(file, e.getMessage(), e
69                     .getLineNumber(), severity);
70         }
71
72         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
73             addMarker(exception, IMarker.SEVERITY_ERROR);
74         }
75
76         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
77             addMarker(exception, IMarker.SEVERITY_ERROR);
78         }
79
80         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
81             addMarker(exception, IMarker.SEVERITY_WARNING);
82         }
83     }
84
85     public static final String JavaDoc BUILDER_ID = "$pluginId$.$builderId$";
86
87     private static final String JavaDoc MARKER_TYPE = "$pluginId$.xmlProblem";
88
89     private SAXParserFactory JavaDoc parserFactory;
90
91     private void addMarker(IFile file, String JavaDoc message, int lineNumber,
92             int severity) {
93         try {
94             IMarker marker = file.createMarker(MARKER_TYPE);
95             marker.setAttribute(IMarker.MESSAGE, message);
96             marker.setAttribute(IMarker.SEVERITY, severity);
97             if (lineNumber == -1) {
98                 lineNumber = 1;
99             }
100             marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
101         } catch (CoreException e) {
102         }
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
109      * java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
110      */

111     protected IProject[] build(int kind, Map JavaDoc args, IProgressMonitor monitor)
112             throws CoreException {
113         if (kind == FULL_BUILD) {
114             fullBuild(monitor);
115         } else {
116             IResourceDelta delta = getDelta(getProject());
117             if (delta == null) {
118                 fullBuild(monitor);
119             } else {
120                 incrementalBuild(delta, monitor);
121             }
122         }
123         return null;
124     }
125
126     void checkXML(IResource resource) {
127         if (resource instanceof IFile && resource.getName().endsWith(".xml")) {
128             IFile file = (IFile) resource;
129             deleteMarkers(file);
130             XMLErrorHandler reporter = new XMLErrorHandler(file);
131             try {
132                 getParser().parse(file.getContents(), reporter);
133             } catch (Exception JavaDoc e1) {
134             }
135         }
136     }
137
138     private void deleteMarkers(IFile file) {
139         try {
140             file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);
141         } catch (CoreException ce) {
142         }
143     }
144
145     protected void fullBuild(final IProgressMonitor monitor)
146             throws CoreException {
147         try {
148             getProject().accept(new SampleResourceVisitor());
149         } catch (CoreException e) {
150         }
151     }
152
153     private SAXParser JavaDoc getParser() throws ParserConfigurationException JavaDoc,
154             SAXException JavaDoc {
155         if (parserFactory == null) {
156             parserFactory = SAXParserFactory.newInstance();
157         }
158         return parserFactory.newSAXParser();
159     }
160
161     protected void incrementalBuild(IResourceDelta delta,
162             IProgressMonitor monitor) throws CoreException {
163         // the visitor does the work.
164
delta.accept(new SampleDeltaVisitor());
165     }
166 }
167
Popular Tags