From 0c9835a72a367fd66f952e4662ae801d0cc66269 Mon Sep 17 00:00:00 2001 From: Juergen Albert Date: Thu, 4 Jul 2024 09:40:49 +0200 Subject: [PATCH] Adds package URI as potential sources too Signed-off-by: Juergen Albert --- .../emf/osgi/codegen/GeckoEmfGenerator.java | 4 ++ .../emf/osgi/codegen/ResourceUriHandler.java | 49 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/GeckoEmfGenerator.java b/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/GeckoEmfGenerator.java index 73ce5992..535dc2a1 100644 --- a/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/GeckoEmfGenerator.java +++ b/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/GeckoEmfGenerator.java @@ -221,6 +221,10 @@ private Map> extractedLocationsWithCap(Collection< ecoreSourceLocations.forEach(l -> result.put(l, ecoreLocation)); } result.put(ecoreLocation, ecoreLocation); + String uri = (String) capability.getAttributes().get("uri"); + if(uri != null) { + result.put(uri, ecoreLocation); + } } refModels.put(c, result); try (Jar jar = new Jar(f)){ diff --git a/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/ResourceUriHandler.java b/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/ResourceUriHandler.java index 8c94358b..8609b701 100644 --- a/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/ResourceUriHandler.java +++ b/org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/ResourceUriHandler.java @@ -20,7 +20,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.StringJoiner; @@ -46,6 +48,7 @@ public class ResourceUriHandler implements URIHandler { private final Map> buildPathModels; + private final List uriCache = new ArrayList(); private String bsn; private File base; private URI basePath; @@ -71,7 +74,10 @@ public ResourceUriHandler(Map> refModels, String GeckoEmfGenerator.info(" buildPath:"); refModels.forEach((c, r) -> { GeckoEmfGenerator.info(" Container: " + c); - r.forEach((k,v) -> GeckoEmfGenerator.info(" |->: " + k + " - " + v)); + r.forEach((k,v) -> { + GeckoEmfGenerator.info(" |->: " + k + " - " + v); + uriCache.add(k); + }); }); } @@ -79,7 +85,7 @@ public ResourceUriHandler(Map> refModels, String @Override public boolean canHandle(URI uri) { GeckoEmfGenerator.info("Asked to handle " + uri); //$NON-NLS-1$ - return uri.scheme().equals(UriSanatizer.RESOURCE_SCHEMA_NAME) || uri.toString().startsWith(UriSanatizer.PLATFORM_RESOURCE) || uri.toString().startsWith(UriSanatizer.PLATFORM_PLUGIN); + return uriCache.contains(uri.trimFragment().trimQuery().toString()) || uri.scheme().equals(UriSanatizer.RESOURCE_SCHEMA_NAME) || uri.toString().startsWith(UriSanatizer.PLATFORM_RESOURCE) || uri.toString().startsWith(UriSanatizer.PLATFORM_PLUGIN); } @@ -88,6 +94,15 @@ public boolean canHandle(URI uri) { public InputStream createInputStream(URI uri, Map options) throws IOException { GeckoEmfGenerator.info("Asked to open InputStream for " + uri); //$NON-NLS-1$ + GeckoEmfGenerator.info("Looking for the pure URI"); //$NON-NLS-1$ + for (Entry> entry : buildPathModels.entrySet()) { + String ecore = entry.getValue().get(uri.trimFragment().trimQuery().toString()); + if(ecore != null) { + Container c = entry.getKey(); + getInputStreamFromContainer(c, ecore); + } + } + URI theUri = UriSanatizer.trimmedSanitize(uri); if (theUri == null) { GeckoEmfGenerator.error("URI is null, InputStream cannot be created"); //$NON-NLS-1$ @@ -117,17 +132,7 @@ public InputStream createInputStream(URI uri, Map options) throws IOExcept String testUri = UriSanatizer.SCHEMA_RESOURCE + containerBSN + UriSanatizer.SLASH + path; GeckoEmfGenerator.info("comparing URIs " + testUri + " with the requested " + theUri); //$NON-NLS-1$ //$NON-NLS-2$ if(testUri.equals(theUri.toString())) { - try(Jar jar = new Jar(c.getFile())){ - Resource resource = jar.getResource(paths.getValue()); - if(resource != null) { - try { - byte[] data = IO.read(resource.openInputStream()); - return new ByteArrayInputStream(data); - } catch (Exception e) { - Exceptions.duck(e); - } - } - } + return getInputStreamFromContainer(c, paths.getValue()); } } } @@ -154,6 +159,24 @@ public InputStream createInputStream(URI uri, Map options) throws IOExcept return null; } + private InputStream getInputStreamFromContainer(Container container, String path) throws IOException { + if(path.startsWith(UriSanatizer.SLASH)) { + path = path.substring(1); + } + try(Jar jar = new Jar(container.getFile())){ + Resource resource = jar.getResource(path); + if(resource != null) { + try { + byte[] data = IO.read(resource.openInputStream()); + return new ByteArrayInputStream(data); + } catch (Exception e) { + Exceptions.duck(e); + } + } + } + return null; + } + private String getBSN(Container c) { try { Manifest manifest = null;