Package: ResourceServices$GetContentsService

ResourceServices$GetContentsService

nameinstructionbranchcomplexitylinemethod
ResourceServices.GetContentsService(Method, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getType(Call, ValidationServices, IValidationResult, IReadOnlyQueryEnvironment, List)
M: 44 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
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: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.services;
12:
13: import com.google.common.base.Predicate;
14: import com.google.common.collect.Iterables;
15: import com.google.common.collect.Lists;
16:
17: import java.lang.reflect.Method;
18: import java.util.ArrayList;
19: import java.util.LinkedHashSet;
20: import java.util.List;
21: import java.util.Set;
22:
23: import org.eclipse.acceleo.annotations.api.documentation.Documentation;
24: import org.eclipse.acceleo.annotations.api.documentation.Param;
25: import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
26: import org.eclipse.acceleo.query.ast.Call;
27: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
28: import org.eclipse.acceleo.query.runtime.IService;
29: import org.eclipse.acceleo.query.runtime.IValidationResult;
30: import org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider;
31: import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
32: import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
33: import org.eclipse.acceleo.query.validation.type.EClassifierLiteralType;
34: import org.eclipse.acceleo.query.validation.type.EClassifierType;
35: import org.eclipse.acceleo.query.validation.type.IType;
36: import org.eclipse.acceleo.query.validation.type.SequenceType;
37: import org.eclipse.emf.common.util.URI;
38: import org.eclipse.emf.ecore.EClass;
39: import org.eclipse.emf.ecore.EObject;
40: import org.eclipse.emf.ecore.EcorePackage;
41: import org.eclipse.emf.ecore.resource.Resource;
42:
43: //@formatter:off
44: @ServiceProvider(
45:         value = "Services available for Resources and URIs"
46: )
47: //@formatter:on
48: @SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
49: public class ResourceServices extends AbstractServiceProvider {
50:         /**
51:          * {@inheritDoc}
52:          *
53:          * @see org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider#getService(java.lang.reflect.Method)
54:          */
55:         @Override
56:         protected IService getService(Method method) {
57:                 if ("getContents".equals(method.getName())) {
58:                         return new GetContentsService(method, this);
59:                 }
60:                 return new JavaMethodService(method, this);
61:         }
62:
63:         // @formatter:off
64:         @Documentation(
65:                 value = "Returns the Resource containing the given EObject. This service is equivalent to a direct call " +
66:                                 "to EObject#eResource().",
67:          params = {
68:                         @Param(name = "eObject", value = "The EObject")
69:                 },
70:                 result = "The Resource containing the given EObject."
71:         )
72:         // @formatter:on
73:         public Resource eResource(EObject eObject) {
74:                 return eObject.eResource();
75:         }
76:
77:         // @formatter:off
78:         @Documentation(
79:                 value = "Returns the URI of the given Resource. This service is equivalent to a direct call to Resource#getURI()",
80:          params = {
81:                         @Param(name = "resource", value = "The Resource which URI we seek")
82:                 },
83:                 result = "The URI of the given Resource."
84:         )
85:         // @formatter:on
86:         public URI getURI(Resource resource) {
87:                 return resource.getURI();
88:         }
89:
90:         // @formatter:off
91:         @Documentation(
92:                 value = "Returns the direct content of the given Resource. This service is equivalent to a direct call to Resource#getContents()",
93:          params = {
94:                         @Param(name = "resource", value = "The Resource which contents we seek")
95:                 },
96:                 result = "The direct content of the given Resource."
97:         )
98:         // @formatter:on
99:         public List<EObject> getContents(Resource resource) {
100:                 return new ArrayList<EObject>(resource.getContents());
101:         }
102:
103:         // @formatter:off
104:         @Documentation(
105:                 value = "Returns the EObjects of the given type from the direct content of the given Resource.",
106:          params = {
107:                         @Param(name = "resource", value = "The Resource which filtered contents we seek"),
108:                         @Param(name = "type", value = "The type that the returned EObjects must match")
109:                 },
110:                 result = "The EObjects from the direct content of the given Resource that match the given type."
111:         )
112:         // @formatter:on
113:         public List<EObject> getContents(Resource resource, final EClass type) {
114:                 return Lists.newArrayList(Iterables.filter(resource.getContents(), new Predicate<EObject>() {
115:                         @Override
116:                         public boolean apply(EObject input) {
117:                                 return type.isInstance(input);
118:                         }
119:                 }));
120:         }
121:
122:         // @formatter:off
123:         @Documentation(
124:                 value = "Returns the last segment of the given URI. This service is equivalent to a direct call to URI#lastSegment()",
125:          params = {
126:                         @Param(name = "uri", value = "The URI")
127:                 },
128:                 result = "The last segment of the given URI."
129:         )
130:         // @formatter:on
131:         public String lastSegment(URI uri) {
132:                 return uri.lastSegment();
133:         }
134:
135:         // @formatter:off
136:         @Documentation(
137:                 value = "Returns the extension of the file referred to by the given URI. This service is equivalent " +
138:                                 "to a direct call to URI#fileExtension()",
139:          params = {
140:                         @Param(name = "uri", value = "The URI")
141:                 },
142:                 result = "The extension of the file referred to by the given URI."
143:         )
144:         // @formatter:on
145:         public String fileExtension(URI uri) {
146:                 return uri.fileExtension();
147:         }
148:
149:         // @formatter:off
150:         @Documentation(
151:                 value = "Returns \"true\" if the given URI is a platform resource URI. This service is equivalent " +
152:                                 "to a direct call to URI#isPlatformResource()",
153:          params = {
154:                         @Param(name = "uri", value = "The URI")
155:                 },
156:                 result = "\"true\" if the given URI is a platform resource URI, \"false\" otherwise."
157:         )
158:         // @formatter:on
159:         public Boolean isPlatformResource(URI uri) {
160:                 return uri.isPlatformResource();
161:         }
162:
163:         // @formatter:off
164:         @Documentation(
165:                 value = "Returns \"true\" if the given URI is a platform plugin URI. This service is equivalent " +
166:                                 "to a direct call to URI#isPlatformPlugin()",
167:          params = {
168:                         @Param(name = "uri", value = "The URI")
169:                 },
170:                 result = "\"true\" if the given URI is a platform plugin URI, \"false\" otherwise."
171:         )
172:         // @formatter:on
173:         public Boolean isPlatformPlugin(URI uri) {
174:                 return uri.isPlatformPlugin();
175:         }
176:
177:         /**
178:          * Resource#getContents {@link IService}.
179:          *
180:          * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
181:          */
182:         private static final class GetContentsService extends FilterService {
183:                 /**
184:                  * Creates a new service instance given a method and an instance.
185:                  *
186:                  * @param serviceMethod
187:                  * the method that realizes the service
188:                  * @param serviceInstance
189:                  * the instance on which the service must be called
190:                  */
191:                 private GetContentsService(Method serviceMethod, Object serviceInstance) {
192:                         super(serviceMethod, serviceInstance);
193:                 }
194:
195:                 @Override
196:                 public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
197:                                 IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
198:                         final Set<IType> result = new LinkedHashSet<IType>();
199:
200:•                        if (argTypes.size() == 1) {
201:                                 result.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
202:                                                 EcorePackage.eINSTANCE.getEObject())));
203:•                        } else if (argTypes.size() == 2) {
204:                                 result.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
205:                                                 ((EClassifierLiteralType)argTypes.get(1)).getType())));
206:                         }
207:
208:                         return result;
209:                 }
210:         }
211: }