blob: 27fa5c7b841cccf819f4307220c1912c8db753b6 (
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
107
108
109
110
111
112
113
|
/*******************************************************************************
* Copyright (c) 2011 protos software gmbh (http://www.protos.de).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
* Juergen Haug (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.core.common.scoping;
import java.util.Map;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.XMLResource.URIHandler;
import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl;
/**
* {@linkplain URIHandler} which deresolves to relative file uri and resolves to absolute file uri. <br>
* Used to persist portable model references. <br>
* <br>
* TODO serialize room includes path <=> deresolve in StandardModelLocator
*/
public class RelativeFileURIHandler extends URIHandlerImpl {
private final static Logger LOG = Logger.getLogger(RelativeFileURIHandler.class);
public static Map<Object, Object> addToOptions(Map<Object, Object> options) {
options.put(XMIResource.OPTION_URI_HANDLER, new RelativeFileURIHandler((URIHandler) options.get(XMIResource.OPTION_URI_HANDLER)));
return options;
}
final protected URIHandler fallback;
public RelativeFileURIHandler() {
this(null);
}
public RelativeFileURIHandler(URIHandler fallback) {
this.fallback = fallback;
}
/**
* @return absolute file uri
*/
@Override
public URI resolve(URI uri) {
if (resolve && baseURI != null && uri.isFile() && uri.hasRelativePath()) {
URI baseFileURI = toFileURI(baseURI);
URI fileURI = toFileURI(uri);
URI resolvedFileURI = fileURI.resolve(baseFileURI, true);
// System.out.println("resolve: " + fileURI + " -> " + baseFileURI + " = " + resolvedFileURI);
if(EMFPlugin.IS_ECLIPSE_RUNNING) {
URI platURI = StandardModelLocator.getPlatformURI(resolvedFileURI);
return (platURI != null) ? platURI : resolvedFileURI;
} else {
return resolvedFileURI;
}
}
return (fallback != null) ? fallback.resolve(uri) : uri;
}
/**
* @return shorter relative file path
*/
@Override
public URI deresolve(URI uri) {
if (resolve && baseURI != null) {
URI baseFileURI = toFileURI(baseURI);
URI fileURI = toFileURI(uri);
URI relativeFileURI = fileURI.deresolve(baseFileURI, true, true, true);
if(relativeFileURI.isFile() && relativeFileURI.hasRelativePath()) {
// System.out.println("deresolve: " + fileURI + " -> " + baseFileURI + " = " + relativeFileURI);
return relativeFileURI;
}
}
return (fallback != null) ? fallback.deresolve(uri) : uri;
}
private static URI toFileURI(URI uri) {
if (uri.isFile()) {
return uri;
} else if (uri.isPlatform()) {
IPath path = null;
if(uri.segmentCount() == 2){
path = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.lastSegment()).getLocation();
} else if(uri.segmentCount() > 2){
path = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(false))).getLocation();
}
if(path != null) {
return URI.createFileURI(path.toOSString()).appendQuery(uri.query()).appendFragment(uri.fragment());
}
}
LOG.error("unhandled uri " + uri);
return uri;
}
}
|