|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2010 Sven Kiera |
| 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 | + |
1 | 9 | package org.phpsrc.eclipse.pti.tools.phpdepend.validator;
|
2 | 10 |
|
| 11 | +import org.eclipse.core.resources.IFile; |
| 12 | +import org.eclipse.core.resources.IMarker; |
3 | 13 | import org.eclipse.core.resources.IResource;
|
| 14 | +import org.eclipse.core.runtime.CoreException; |
4 | 15 | import org.eclipse.core.runtime.IProgressMonitor;
|
| 16 | +import org.eclipse.dltk.compiler.problem.IProblem; |
5 | 17 | import org.eclipse.wst.validation.AbstractValidator;
|
6 | 18 | import org.eclipse.wst.validation.ValidationResult;
|
7 | 19 | import org.eclipse.wst.validation.ValidationState;
|
| 20 | +import org.phpsrc.eclipse.pti.tools.phpdepend.IPHPDependConstants; |
| 21 | +import org.phpsrc.eclipse.pti.tools.phpdepend.core.PHPDepend; |
| 22 | +import org.phpsrc.eclipse.pti.ui.Logger; |
8 | 23 |
|
9 | 24 | public class PHPDependValidator extends AbstractValidator {
|
10 |
| - public ValidationResult validate(IResource resource, int kind, ValidationState state, |
11 |
| - IProgressMonitor monitor) { |
| 25 | + public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) { |
| 26 | + // process only PHP files |
| 27 | + if (resource.getType() != IResource.FILE) { |
| 28 | + return null; |
| 29 | + } |
12 | 30 |
|
13 | 31 | ValidationResult result = new ValidationResult();
|
| 32 | + validateFile(result, (IFile) resource, kind); |
14 | 33 | return result;
|
15 | 34 | }
|
| 35 | + |
| 36 | + protected void validateFile(ValidationResult result, IFile file, int kind) { |
| 37 | + // remove the markers currently existing for this resource |
| 38 | + // in case of project/folder, the markers are deleted recursively |
| 39 | + try { |
| 40 | + file.deleteMarkers(IPHPDependConstants.VALIDATOR_PHP_DEPEND_MARKER, false, IResource.DEPTH_INFINITE); |
| 41 | + } catch (CoreException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + |
| 45 | + PHPDepend pDepend = PHPDepend.getInstance(); |
| 46 | + IProblem[] problems; |
| 47 | + try { |
| 48 | + problems = pDepend.validateResource(file); |
| 49 | + for (IProblem problem : problems) { |
| 50 | + IMarker marker = file.createMarker(IPHPDependConstants.VALIDATOR_PHP_DEPEND_MARKER); |
| 51 | + marker.setAttribute(IMarker.PROBLEM, true); |
| 52 | + marker.setAttribute(IMarker.LINE_NUMBER, problem.getSourceLineNumber()); |
| 53 | + |
| 54 | + if (problem.isWarning()) { |
| 55 | + marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); |
| 56 | + result.incrementWarning(1); |
| 57 | + } else { |
| 58 | + marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); |
| 59 | + result.incrementError(1); |
| 60 | + } |
| 61 | + marker.setAttribute(IMarker.CHAR_START, problem.getSourceStart()); |
| 62 | + marker.setAttribute(IMarker.CHAR_END, problem.getSourceEnd()); |
| 63 | + marker.setAttribute(IMarker.MESSAGE, problem.getMessage()); |
| 64 | + } |
| 65 | + } catch (CoreException e) { |
| 66 | + Logger.logException(e); |
| 67 | + } |
| 68 | + } |
16 | 69 | }
|
0 commit comments