-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResourceServices.java
208 lines (190 loc) · 7.1 KB
/
ResourceServices.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
* Copyright (c) 2015, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.services;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.acceleo.annotations.api.documentation.Documentation;
import org.eclipse.acceleo.annotations.api.documentation.Param;
import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
import org.eclipse.acceleo.query.validation.type.EClassifierLiteralType;
import org.eclipse.acceleo.query.validation.type.EClassifierType;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.acceleo.query.validation.type.SequenceType;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
//@formatter:off
@ServiceProvider(
value = "Services available for Resources and URIs"
)
//@formatter:on
@SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
public class ResourceServices extends AbstractServiceProvider {
@Override
protected IService<Method> getService(Method method, boolean forWorkspace) {
if ("getContents".equals(method.getName())) {
return new GetContentsService(method, this, forWorkspace);
}
return new JavaMethodService(method, this, forWorkspace);
}
// @formatter:off
@Documentation(
value = "Returns the Resource containing the given EObject. This service is equivalent to a direct call " +
"to EObject#eResource().",
params = {
@Param(name = "eObject", value = "The EObject")
},
result = "The Resource containing the given EObject."
)
// @formatter:on
public Resource eResource(EObject eObject) {
return eObject.eResource();
}
// @formatter:off
@Documentation(
value = "Returns the URI of the given Resource. This service is equivalent to a direct call to Resource#getURI()",
params = {
@Param(name = "resource", value = "The Resource which URI we seek")
},
result = "The URI of the given Resource."
)
// @formatter:on
public URI getURI(Resource resource) {
return resource.getURI();
}
// @formatter:off
@Documentation(
value = "Returns the direct content of the given Resource. This service is equivalent to a direct call to Resource#getContents()",
params = {
@Param(name = "resource", value = "The Resource which contents we seek")
},
result = "The direct content of the given Resource."
)
// @formatter:on
public List<EObject> getContents(Resource resource) {
return new ArrayList<EObject>(resource.getContents());
}
// @formatter:off
@Documentation(
value = "Returns the EObjects of the given type from the direct content of the given Resource.",
params = {
@Param(name = "resource", value = "The Resource which filtered contents we seek"),
@Param(name = "type", value = "The type that the returned EObjects must match")
},
result = "The EObjects from the direct content of the given Resource that match the given type."
)
// @formatter:on
public List<EObject> getContents(Resource resource, final EClass type) {
final List<EObject> res = new ArrayList<EObject>();
for (EObject eObj : resource.getContents()) {
if (type.isInstance(eObj)) {
res.add(eObj);
}
}
return res;
}
// @formatter:off
@Documentation(
value = "Returns the last segment of the given URI. This service is equivalent to a direct call to URI#lastSegment()",
params = {
@Param(name = "uri", value = "The URI")
},
result = "The last segment of the given URI."
)
// @formatter:on
public String lastSegment(URI uri) {
return uri.lastSegment();
}
// @formatter:off
@Documentation(
value = "Returns the extension of the file referred to by the given URI. This service is equivalent " +
"to a direct call to URI#fileExtension()",
params = {
@Param(name = "uri", value = "The URI")
},
result = "The extension of the file referred to by the given URI."
)
// @formatter:on
public String fileExtension(URI uri) {
return uri.fileExtension();
}
// @formatter:off
@Documentation(
value = "Returns \"true\" if the given URI is a platform resource URI. This service is equivalent " +
"to a direct call to URI#isPlatformResource()",
params = {
@Param(name = "uri", value = "The URI")
},
result = "\"true\" if the given URI is a platform resource URI, \"false\" otherwise."
)
// @formatter:on
public Boolean isPlatformResource(URI uri) {
return uri.isPlatformResource();
}
// @formatter:off
@Documentation(
value = "Returns \"true\" if the given URI is a platform plugin URI. This service is equivalent " +
"to a direct call to URI#isPlatformPlugin()",
params = {
@Param(name = "uri", value = "The URI")
},
result = "\"true\" if the given URI is a platform plugin URI, \"false\" otherwise."
)
// @formatter:on
public Boolean isPlatformPlugin(URI uri) {
return uri.isPlatformPlugin();
}
/**
* Resource#getContents {@link IService}.
*
* @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
*/
private static final class GetContentsService extends FilterService {
/**
* Creates a new service instance given a method and an instance.
*
* @param serviceMethod
* the method that realizes the service
* @param serviceInstance
* the instance on which the service must be called
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
*/
private GetContentsService(Method serviceMethod, Object serviceInstance, boolean forWorkspace) {
super(serviceMethod, serviceInstance, forWorkspace);
}
@Override
public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
if (argTypes.size() == 1) {
result.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
EcorePackage.eINSTANCE.getEObject())));
} else if (argTypes.size() == 2) {
result.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
((EClassifierLiteralType)argTypes.get(1)).getType())));
}
return result;
}
}
}