Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b206b406e2834f6b93422a661282971d91d40fc (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
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2014, 2016 Borland Software Corporation and others.
 * 
 * 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:
 *     Christopher Gerking - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2m.internal.qvt.oml.project.builder;

import java.util.Collections;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.m2m.internal.qvt.oml.NLS;
import org.eclipse.m2m.internal.qvt.oml.compiler.ResolverUtils;
import org.eclipse.m2m.internal.qvt.oml.compiler.UnitResolver;
import org.eclipse.m2m.internal.qvt.oml.compiler.UnitResolverFactory;
import org.eclipse.m2m.internal.qvt.oml.emf.util.URIUtils;
import org.eclipse.m2m.internal.qvt.oml.project.Messages;
import org.eclipse.m2m.internal.qvt.oml.project.QVTOProjectPlugin;

public class WorkspaceUnitResolverFactory extends UnitResolverFactory {

	@Override
	public boolean accepts(URI uri) {
		return EMFPlugin.IS_RESOURCES_BUNDLE_AVAILABLE && (uri.isPlatformResource() || isWorkspacePath(uri));
	}

	@Override
	public UnitResolver getResolver(URI uri) {
		
		if(isWorkspacePath(uri)) {
			uri = URI.createPlatformResourceURI(uri.path(), false);
		}
		
		IResource file = URIUtils.getResource(uri);
		if (file == null) {
			return null;
		}
		
		try {	
			IContainer sourceContainer = QVTOBuilderConfig.getConfig(file.getProject()).getSourceContainer();
			if(sourceContainer != null) {
				if (!sourceContainer.exists()) {
					QVTOProjectPlugin.log(QVTOProjectPlugin.createStatus(IStatus.ERROR,
							NLS.bind(Messages.InvalidSourceContainer, sourceContainer), null));
					
					return null;
				}
				
				return new WorkspaceUnitResolver(Collections.singletonList(sourceContainer));
			}
		}
		catch (CoreException e) {
			QVTOProjectPlugin.log(e.getStatus());
		}
		
		return null;
	}

	@Override
	public String getQualifiedName(URI uri) {
		
		if(isWorkspacePath(uri)) {
			uri = URI.createPlatformResourceURI(uri.path(), false);
		}
		
		IResource resource = URIUtils.getResource(uri);
		if (resource == null) {
			return null;
		}
		
		try {
			IContainer sourceContainer = QVTOBuilderConfig.getConfig(resource.getProject()).getSourceContainer();
			URI sourceContainerUri = URIUtils.getResourceURI(sourceContainer);
			URI relativeUri = uri.deresolve(sourceContainerUri).trimFileExtension();
			assert (relativeUri.isRelative());
			
			// exclude first segment which is the source container itself
			String[] segments = relativeUri.segments();
			if(segments.length > 1) {
				return ResolverUtils.toQualifiedName(segments, 1, segments.length-1);
			}
		}
		catch(CoreException e) {
			QVTOProjectPlugin.log(e.getStatus());
		}
		
		return null;
	}
	
	private static boolean isWorkspacePath(URI uri) {
		return uri.scheme() == null && !uri.hasDevice() && !uri.hasAuthority()
				&& !uri.hasEmptyPath() && !uri.hasQuery() && !uri.hasFragment()
				&& uri.hasAbsolutePath();
	}
		
}

Back to the top