Skip to content

Commit

Permalink
Adds package URI as potential sources too
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Albert <[email protected]>
  • Loading branch information
juergen-albert committed Jul 4, 2024
1 parent 5138b97 commit 0c9835a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ private Map<Container, Map<String,String>> 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)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +48,7 @@ public class ResourceUriHandler implements URIHandler {


private final Map<Container, Map<String, String>> buildPathModels;
private final List<String> uriCache = new ArrayList<String>();
private String bsn;
private File base;
private URI basePath;
Expand All @@ -71,15 +74,18 @@ public ResourceUriHandler(Map<Container, Map<String, String>> 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);
});
});

}

@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);
}


Expand All @@ -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<Container, Map<String, String>> 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$
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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;
Expand Down

0 comments on commit 0c9835a

Please sign in to comment.