1 package $packageName$; 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 $builderClassName$ extends IncrementalProjectBuilder { 24 25 class SampleDeltaVisitor implements IResourceDeltaVisitor { 26 31 public boolean visit(IResourceDelta delta) throws CoreException { 32 IResource resource = delta.getResource(); 33 switch (delta.getKind()) { 34 case IResourceDelta.ADDED: 35 checkXML(resource); 37 break; 38 case IResourceDelta.REMOVED: 39 break; 41 case IResourceDelta.CHANGED: 42 checkXML(resource); 44 break; 45 } 46 return true; 48 } 49 } 50 51 class SampleResourceVisitor implements IResourceVisitor { 52 public boolean visit(IResource resource) { 53 checkXML(resource); 54 return true; 56 } 57 } 58 59 class XMLErrorHandler extends DefaultHandler { 60 61 private IFile file; 62 63 public XMLErrorHandler(IFile file) { 64 this.file = file; 65 } 66 67 private void addMarker(SAXParseException e, int severity) { 68 $builderClassName$.this.addMarker(file, e.getMessage(), e 69 .getLineNumber(), severity); 70 } 71 72 public void error(SAXParseException exception) throws SAXException { 73 addMarker(exception, IMarker.SEVERITY_ERROR); 74 } 75 76 public void fatalError(SAXParseException exception) throws SAXException { 77 addMarker(exception, IMarker.SEVERITY_ERROR); 78 } 79 80 public void warning(SAXParseException exception) throws SAXException { 81 addMarker(exception, IMarker.SEVERITY_WARNING); 82 } 83 } 84 85 public static final String BUILDER_ID = "$pluginId$.$builderId$"; 86 87 private static final String MARKER_TYPE = "$pluginId$.xmlProblem"; 88 89 private SAXParserFactory parserFactory; 90 91 private void addMarker(IFile file, String 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 111 protected IProject[] build(int kind, Map 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 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 getParser() throws ParserConfigurationException , 154 SAXException { 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 delta.accept(new SampleDeltaVisitor()); 165 } 166 } 167 | Popular Tags |