aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/library/ClasspathBasedLibrary.java
blob: 56bb8b9676cc5fb880f7e5473fb1e80ea5e76082 (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
/*
 * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.library;

import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.naming.QualifiedName;

import java.nio.file.Path;
import java.util.*;

public abstract class ClasspathBasedLibrary implements RefineryLibrary {
	private final Class<?> context;
	private final QualifiedName prefix;
	private final URI rootUri;

	protected ClasspathBasedLibrary(Class<?> context, QualifiedName prefix) {
        this.context = context == null ? getClass() : context;
        this.prefix = prefix;
		var contextPath = this.context.getCanonicalName().replace('.', '/') + ".class";
		var contextResource = this.context.getClassLoader().getResource(contextPath);
		if (contextResource == null) {
			throw new IllegalStateException("Failed to find library context");
		}
		var contextUri = URI.createURI(contextResource.toString());
		var segments = Arrays.copyOf(contextUri.segments(), contextUri.segmentCount() - 1);
		rootUri = URI.createHierarchicalURI(contextUri.scheme(), contextUri.authority(), contextUri.device(),
				segments, null, null);
	}

	protected ClasspathBasedLibrary(QualifiedName prefix) {
		this(null, prefix);
	}

	@Override
	public Optional<URI> resolveQualifiedName(QualifiedName qualifiedName, List<Path> libraryPaths) {
		if (qualifiedName.startsWith(prefix)) {
			return getLibraryUri(context, qualifiedName);
		}
		return Optional.empty();
	}

	@Override
	public Optional<QualifiedName> getQualifiedName(URI uri, List<Path> libraryPaths) {
		if (!uri.isHierarchical() ||
				!Objects.equals(rootUri.scheme(), uri.scheme()) ||
				!Objects.equals(rootUri.authority(), uri.authority()) ||
				!Objects.equals(rootUri.device(), uri.device()) ||
				rootUri.segmentCount() >= uri.segmentCount()) {
			return Optional.empty();
		}
		int rootSegmentCount = rootUri.segmentCount();
		int uriSegmentCount = uri.segmentCount();
		if (!uri.segment(uriSegmentCount - 1).endsWith(RefineryLibrary.FILE_NAME_SUFFIX)) {
			return Optional.empty();
		}
		var segments = new ArrayList<String>();
		int i = 0;
		while (i < rootSegmentCount) {
			if (!rootUri.segment(i).equals(uri.segment(i))) {
				return Optional.empty();
			}
			i++;
		}
		while (i < uriSegmentCount) {
			var segment = uri.segment(i);
			if (i == uriSegmentCount - 1) {
				segment = segment.substring(0, segment.length() - RefineryLibrary.FILE_NAME_SUFFIX.length());
			}
			segments.add(segment);
			i++;
		}
		var qualifiedName = QualifiedName.create(segments);
		if (!qualifiedName.startsWith(prefix)) {
			return Optional.empty();
		}
		return Optional.of(qualifiedName);
	}

	public static Optional<URI> getLibraryUri(Class<?> context, QualifiedName qualifiedName) {
		var packagePath = context.getPackageName().replace('.', '/');
		var libraryPath = String.join("/", qualifiedName.getSegments());
		var resourceName = "%s/%s%s".formatted(packagePath, libraryPath, RefineryLibrary.FILE_NAME_SUFFIX);
		var resource = context.getClassLoader().getResource(resourceName);
		if (resource == null) {
			return Optional.empty();
		}
		return Optional.of(URI.createURI(resource.toString()));
	}
}