blob: b82b80f5c50bb721ba2b3b773a48a6df236ab830 (
plain) (
blame)
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
|
/*****************************************************************************
* Copyright (c) 2011 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:
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.operation.editor.xtext.utils;
import java.util.Collections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.operation.editor.xtext.OperationStandaloneSetupGenerated;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.StringInputStream;
import com.google.inject.Injector;
public class AlfParsingUtil {
private Injector injector;
private static final String SYNTHETIC_SCHEME = "synthetic";
/**
* Parses an Alf representation of an operation signature/body and returns an xtext resource (conforming to Operation.ecore). Users should check for parsing
* errors (getErrors). The resulting set of eObjects is available via the getContents
* operations.
*
* @param textualRepresentation
* an Alf textual representation of an operation signature/body
* @return a resource relating to the textual representation.
*/
public XtextResource getXtextResource(String textualRepresentation) {
if(injector == null) {
OperationStandaloneSetupGenerated setup = new OperationStandaloneSetupGenerated();
injector = setup.createInjectorAndDoEMFRegistration();
};
IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);
IGrammarAccess grammarAccess = injector.getInstance(IGrammarAccess.class);
XtextResource xtextResource = (XtextResource)resourceFactory.createResource(
URI.createURI(SYNTHETIC_SCHEME + ":/" + grammarAccess.getGrammar().getName() + ".operation"));
try {
xtextResource.load(new StringInputStream(textualRepresentation, xtextResource.getEncoding()), Collections.emptyMap());
} catch (Exception e) {
throw new RuntimeException(e);
}
return xtextResource;
}
/**
* test procedure
*
* @param textualRepresentation
* Alf textual representation of an operation signature/body
*/
public static void parseTest(String textualRepresentation) {
AlfParsingUtil parseAlf = new AlfParsingUtil();
XtextResource xtextResource = parseAlf.getXtextResource(textualRepresentation);
System.out.println("///////////////////////// Test begin") ;
// Display parsing errors
if(xtextResource.getErrors().size() > 0) {
for(Resource.Diagnostic error : xtextResource.getErrors()) {
System.err.println(error);
}
return;
}
else {
System.out.println("No parsing errors") ;
}
// Navigate through eobjects
EList<EObject> contents = xtextResource.getContents();
System.out.println("contents.size: " + contents.size());
// contents normally contains only one eObject of type OperationDefinitionOrStub (i.e. root of the grammar)
for (EObject o : contents) {
System.out.println(o) ;
}
System.out.println("///////////////////////// Test end") ;
}
}
|