1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| /**
* Validate the model
* @param eObject
* @return boolean
*/
public static Vector<String> validateObject(EObject eObject, Resource resource)
{
Vector<String> messagesErrors = new Vector<String>();
messagesErrors.clear();
try {
getFile(resource).deleteMarkers(EValidator.MARKER, true, IResource.DEPTH_ZERO);
} catch (CoreException e) {
e.printStackTrace();
}
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
if (diagnostic.getSeverity() == Diagnostic.ERROR || diagnostic.getSeverity() == Diagnostic.WARNING)
{
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.eclipse.ui.views.ProblemView");
} catch (PartInitException e) {
e.printStackTrace();
}
System.err.println(diagnostic.getMessage());
for (Iterator i=diagnostic.getChildren().iterator(); i.hasNext();)
{
Diagnostic childDiagnostic = (Diagnostic)i.next();
switch (childDiagnostic.getSeverity())
{
case Diagnostic.ERROR:
case Diagnostic.WARNING:
System.err.println("\t" + childDiagnostic.getMessage());
messagesErrors.addElement(childDiagnostic.getMessage());
}
createMarkers(getFile(resource), childDiagnostic);
}
return messagesErrors;
}
return messagesErrors;
}
/**
* Return the IFile associated with the given resource
*
* @param resource the EMF resource
* @return the containing IFile or <code>null</code> if the resource is not an IFile
*/
private static IFile getFile(Resource resource)
{
URI uri = resource.getURI();
uri = resource.getResourceSet().getURIConverter().normalize(uri);
String scheme = uri.scheme();
if ("platform".equals(scheme) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0)))
{
StringBuffer platformResourcePath = new StringBuffer();
for (int j = 1; j < uri.segmentCount(); ++j)
{
platformResourcePath.append('/');
platformResourcePath.append(uri.segment(j));
}
return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString()));
}
return null;
}
/**
* This is a temporary method copied from the EMF EclipseResourceUtil to keep compatibility from 3.2M6 to 3.2RC4.<br>
* It should be removed in the next versions
*
* @param file
* @param diagnostic
*/
public static void createMarkers(IFile file, Diagnostic diagnostic)
{
EObject eObject = null;
List data = diagnostic.getData();
if (!data.isEmpty())
{
Object target = data.get(0);
if (target instanceof EObject)
{
eObject = (EObject) target;
}
}
if (diagnostic.getChildren().isEmpty())
{
try
{
IMarker marker = file.createMarker(EValidator.MARKER);
int severity = diagnostic.getSeverity();
if (severity < Diagnostic.WARNING)
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
}
else if (severity < Diagnostic.ERROR)
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
}
else
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
}
marker.setAttribute(IMarker.MESSAGE, diagnostic.getMessage());
if (eObject != null)
{
marker.setAttribute(EValidator.URI_ATTRIBUTE, EcoreUtil.getURI(eObject).toString());
}
}
catch (CoreException exception)
{
exception.printStackTrace();
}
}
else
{
String parentMessage = diagnostic.getMessage() + ". ";
for (Iterator i = diagnostic.getChildren().iterator(); i.hasNext();)
{
Diagnostic childDiagnostic = (Diagnostic) i.next();
try
{
IMarker marker = file.createMarker(EValidator.MARKER);
int severity = childDiagnostic.getSeverity();
if (severity < Diagnostic.WARNING)
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
}
else if (severity < Diagnostic.ERROR)
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
}
else
{
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
}
marker.setAttribute(IMarker.MESSAGE, parentMessage + childDiagnostic.getMessage());
if (eObject != null)
{
marker.setAttribute(EValidator.URI_ATTRIBUTE, EcoreUtil.getURI(eObject).toString());
}
}
catch (CoreException exception)
{
exception.printStackTrace();
}
}
}
} |
Partager