The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / asm / editor / AsmDocumentValidator.java
1 package net.mograsim.plugin.asm.editor;
2
3 import java.io.StringReader;
4
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IMarker;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.jface.text.DocumentEvent;
12 import org.eclipse.jface.text.IDocumentListener;
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXParseException;
15
16 public class AsmDocumentValidator implements IDocumentListener
17 {
18
19         private final IFile file;
20         private IMarker marker;
21
22         AsmDocumentValidator(IFile file)
23         {
24                 this.file = file;
25         }
26
27         @Override
28         public void documentChanged(DocumentEvent event)
29         {
30                 if (this.marker != null)
31                 {
32                         try
33                         {
34                                 this.marker.delete();
35                         }
36                         catch (CoreException e)
37                         {
38                                 e.printStackTrace();
39                         }
40                         this.marker = null;
41                 }
42                 try (StringReader reader = new StringReader(event.getDocument().get());)
43                 {
44                         DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
45                         documentBuilder.parse(new InputSource(reader));
46                 }
47                 catch (Exception ex)
48                 {
49                         try
50                         {
51                                 this.marker = file.createMarker(IMarker.PROBLEM);
52                                 this.marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
53                                 this.marker.setAttribute(IMarker.MESSAGE, ex.getMessage());
54                                 if (ex instanceof SAXParseException)
55                                 {
56                                         SAXParseException saxParseException = (SAXParseException) ex;
57                                         int lineNumber = saxParseException.getLineNumber();
58                                         int offset = event.getDocument().getLineInformation(lineNumber - 1).getOffset() + saxParseException.getColumnNumber()
59                                                         - 1;
60                                         this.marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
61                                         this.marker.setAttribute(IMarker.CHAR_START, offset);
62                                         this.marker.setAttribute(IMarker.CHAR_END, offset + 1);
63                                 }
64                         }
65                         catch (Exception e)
66                         {
67                                 e.printStackTrace();
68                         }
69                 }
70         }
71
72         @Override
73         public void documentAboutToBeChanged(DocumentEvent event)
74         {
75         }
76
77 }