Fixed the MograsimBuilder a bit, but it will likely get removed
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / MograsimBuilder.java
1 package net.mograsim.plugin.nature;
2
3 import java.util.Map;
4
5 import javax.xml.parsers.ParserConfigurationException;
6 import javax.xml.parsers.SAXParser;
7 import javax.xml.parsers.SAXParserFactory;
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;
20 import org.xml.sax.SAXParseException;
21 import org.xml.sax.helpers.DefaultHandler;
22
23 public class MograsimBuilder extends IncrementalProjectBuilder
24 {
25         private MachineContext machineContext;
26
27         class SampleDeltaVisitor implements IResourceDeltaVisitor
28         {
29                 @Override
30                 public boolean visit(IResourceDelta delta) throws CoreException
31                 {
32                         IResource resource = delta.getResource();
33                         switch (delta.getKind())
34                         {
35                         case IResourceDelta.ADDED:
36                                 // handle added resource
37                                 checkXML(resource);
38                                 break;
39                         case IResourceDelta.REMOVED:
40                                 // handle removed resource
41                                 break;
42                         case IResourceDelta.CHANGED:
43                                 // handle changed resource
44                                 checkXML(resource);
45                                 break;
46                         }
47                         // return true to continue visiting children.
48                         return true;
49                 }
50         }
51
52         class SampleResourceVisitor implements IResourceVisitor
53         {
54                 @Override
55                 public boolean visit(IResource resource)
56                 {
57                         checkXML(resource);
58                         // return true to continue visiting children.
59                         return true;
60                 }
61         }
62
63         class XMLErrorHandler extends DefaultHandler
64         {
65
66                 private IFile file;
67
68                 public XMLErrorHandler(IFile file)
69                 {
70                         this.file = file;
71                 }
72
73                 private void addMarker(SAXParseException e, int severity)
74                 {
75                         MograsimBuilder.this.addMarker(file, e.getMessage(), e.getLineNumber(), severity);
76                 }
77
78                 @Override
79                 public void error(SAXParseException exception) throws SAXException
80                 {
81                         addMarker(exception, IMarker.SEVERITY_ERROR);
82                 }
83
84                 @Override
85                 public void fatalError(SAXParseException exception) throws SAXException
86                 {
87                         addMarker(exception, IMarker.SEVERITY_ERROR);
88                 }
89
90                 @Override
91                 public void warning(SAXParseException exception) throws SAXException
92                 {
93                         addMarker(exception, IMarker.SEVERITY_WARNING);
94                 }
95         }
96
97         public static final String BUILDER_ID = "net.mograsim.plugin.core.mograsimBuilder";
98
99         private static final String MARKER_TYPE = "net.mograsim.plugin.core.asmProblem";
100
101         private SAXParserFactory parserFactory;
102
103         private void addMarker(IFile file, String message, int lineNumber, int severity)
104         {
105                 try
106                 {
107                         IMarker marker = file.createMarker(MARKER_TYPE);
108                         marker.setAttribute(IMarker.MESSAGE, message);
109                         marker.setAttribute(IMarker.SEVERITY, severity);
110                         if (lineNumber == -1)
111                         {
112                                 lineNumber = 1;
113                         }
114                         marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
115                 }
116                 catch (CoreException e)
117                 {
118                 }
119         }
120
121         @Override
122         protected void startupOnInitialize()
123         {
124                 super.startupOnInitialize();
125                 machineContext = ProjectMachineContext.getMachineContextOf(getProject());
126         }
127
128         @Override
129         protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException
130         {
131                 if (kind == FULL_BUILD)
132                 {
133                         fullBuild(monitor);
134                 } else
135                 {
136                         IResourceDelta delta = getDelta(getProject());
137                         if (delta == null)
138                         {
139                                 fullBuild(monitor);
140                         } else
141                         {
142                                 incrementalBuild(delta, monitor);
143                         }
144                 }
145                 return null;
146         }
147
148         @Override
149         protected void clean(IProgressMonitor monitor) throws CoreException
150         {
151                 // delete markers set and files created
152                 getProject().deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_INFINITE);
153         }
154
155         void checkXML(IResource resource)
156         {
157                 if (resource instanceof IFile && resource.getName().endsWith(".asm"))
158                 {
159                         IFile file = (IFile) resource;
160                         deleteMarkers(file);
161                         XMLErrorHandler reporter = new XMLErrorHandler(file);
162                         try
163                         {
164                                 getParser().parse(file.getContents(), reporter);
165                         }
166                         catch (Exception e1)
167                         {
168                         }
169                 }
170         }
171
172         private void deleteMarkers(IFile file)
173         {
174                 try
175                 {
176                         file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);
177                 }
178                 catch (CoreException ce)
179                 {
180                 }
181         }
182
183         protected void fullBuild(final IProgressMonitor monitor) throws CoreException
184         {
185                 try
186                 {
187                         getProject().accept(new SampleResourceVisitor());
188                 }
189                 catch (CoreException e)
190                 {
191                 }
192         }
193
194         private SAXParser getParser() throws ParserConfigurationException, SAXException
195         {
196                 if (parserFactory == null)
197                 {
198                         parserFactory = SAXParserFactory.newInstance();
199                 }
200                 return parserFactory.newSAXParser();
201         }
202
203         protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException
204         {
205                 // the visitor does the work.
206                 delta.accept(new SampleDeltaVisitor());
207         }
208 }