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
|
/*****************************************************************************
* Copyright (c) 2013, 2014 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ed Seidewitz (IJI) - Initial implementation
* Ed Seidewitz (MDS) - Updated for Luna
*
*****************************************************************************/
package org.eclipse.papyrus.uml.alf.tests
import java.io.File
import org.eclipse.emf.common.util.Diagnostic
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.papyrus.uml.alf.UnitDefinition
import org.eclipse.papyrus.uml.alf.CachingDiagnostician
abstract class ParserTest {
def int parseDirectory(ResourceSet resourceSet, String directoryPath, boolean validate) {
var failures = 0;
System.out.println("Directory " + directoryPath + ":")
val directory = new File(directoryPath)
for (file : directory.listFiles()) {
failures = failures + this.parseFile(resourceSet, file, validate)
}
System.out.println()
return failures
}
def int parseFile(ResourceSet resourceSet, File file, boolean validate) {
var failures = 0;
val name = file.name;
val l = name.length
if (name.substring(l - 4, l).equals(".alf")) {
System.out.print(name + ": ")
val resource = resourceSet.getResource(URI.createFileURI(file.path), true)
failures = parseResource(resource, validate)
resource.unload()
}
return failures
}
def int parseResource(Resource resource, boolean validate) {
var failures = 0
val unit = resource.getContents().get(0) as UnitDefinition
if (unit == null) {
System.out.println("PARSE FAILED")
failures = failures + 1
} else {
val definition = unit.definition
if (definition == null) {
System.out.println("NO DEFINITION")
failures = failures + 1
} else {
System.out.println(definition.class.simpleName + " " + definition.actualName);
val errors = resource.getErrors()
if (errors.size() > 0) {
failures = failures + 1
System.out.println("SYNTACTIC ERRORS:");
for (error: errors) {
System.out.println(error)
}
System.out.println();
} else if (validate) {
val diagnostic = new CachingDiagnostician().validate(unit)
if (diagnostic.severity == Diagnostic.ERROR) {
failures = failures + 1;
System.out.println("SEMANTIC ERRORS:")
for (child: diagnostic.children) {
System.out.println(child.class.simpleName + ": " + child.message);
var exception = child.exception;
if (exception != null) {
var cause = exception.cause
while (cause != null && cause != exception) {
System.out.println("Caused by: " + cause.class.name)
System.out.println(cause.message)
exception = cause;
cause = exception.cause;
}
System.out.println()
}
}
System.out.println();
}
}
}
}
return failures
}
}
|