forked from eclipse-esmf/esmf-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
359 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ect-model-resolver/src/main/java/org/eclipse/esmf/aspectmodel/resolver/GroupStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Aspect Model GitHub Resolver | ||
|
||
The plugin for the Aspect Model Resolver provides functionality to resolve aspect models from GitHub. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH | ||
~ | ||
~ See the AUTHORS file(s) distributed with this work for additional | ||
~ information regarding authorship. | ||
~ | ||
~ This Source Code Form is subject to the terms of the Mozilla Public | ||
~ License, v. 2.0. If a copy of the MPL was not distributed with this | ||
~ file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
~ | ||
~ SPDX-License-Identifier: MPL-2.0 | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-sdk-parent</artifactId> | ||
<version>DEV-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>esmf-aspect-model-github-resolver</artifactId> | ||
<name>ESMF Aspect Model GitHub Resolver</name> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-aspect-model-resolver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.jena</groupId> | ||
<artifactId>jena-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-aspect-model-urn</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-semantic-aspect-meta-model</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-aspect-meta-model-resolver</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-vavr</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-test-aspect-models</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.esmf</groupId> | ||
<artifactId>esmf-test-resources</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>5.10.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
104 changes: 104 additions & 0 deletions
104
.../src/main/java/org/eclipse/esmf/aspectmodel/resolver/github/GitHubResolutionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.eclipse.esmf.aspectmodel.resolver.github; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
|
||
import org.apache.jena.rdf.model.Model; | ||
import org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver; | ||
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy; | ||
import org.eclipse.esmf.aspectmodel.resolver.fs.StructuredModelsRoot; | ||
import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader; | ||
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn; | ||
|
||
import io.vavr.control.Try; | ||
|
||
public class GitHubResolutionStrategy implements ResolutionStrategy { | ||
private final StructuredModelsRoot root; | ||
|
||
public static void install() { | ||
AspectModelResolver.register( null, uri -> Try.of( () -> new GitHubResolutionStrategy( uri ) ) ); | ||
} | ||
|
||
public GitHubResolutionStrategy( URI baseUri ) { | ||
if ( !isUrl( baseUri ) || !isGithubUrl( baseUri ) ) | ||
throw new UnsupportedOperationException( "GitHub resolution strategy does not support URN: " + baseUri ); | ||
else { | ||
this.root = new StructuredModelsRoot( transformGithubUrlToContentLink( baseUri ).mapTry( URL::toURI ).get() ); | ||
} | ||
} | ||
|
||
@Override | ||
public Try<Model> apply( AspectModelUrn urn ) { | ||
return applyUri( root.determineAspectModelFile( urn ) ); | ||
} | ||
|
||
@Override | ||
public Try<Model> applyUri( URI uri ) { | ||
return AspectModelUrn.from( uri ).flatMap( this::apply ) | ||
.recoverWith( __ -> | ||
toURL( uri ).flatMap( GitHubResolutionStrategy::loadModel ) | ||
) | ||
.orElse( Try.failure( new UnsupportedOperationException( "GitHub resolution strategy does not support URI: " + uri ) ) ); | ||
} | ||
|
||
@Override | ||
public InputStream read( URI uri ) { | ||
return Try.success( uri ) | ||
.filter( GitHubResolutionStrategy::isGithubUrl ) | ||
.flatMap( GitHubResolutionStrategy::toURL ) | ||
.flatMap( GitHubResolutionStrategy::read ) | ||
.getOrElse( (InputStream) null ); | ||
} | ||
|
||
static Try<URL> toURL( URI uri ) { | ||
if ( isGithubUrl( uri ) ) | ||
return transformGithubUrlToContentLink( uri ); | ||
else | ||
return Try.of( uri::toURL ); | ||
} | ||
|
||
static boolean isUrl( URI uri ) { | ||
try { | ||
//noinspection ResultOfMethodCallIgnored | ||
uri.toURL(); | ||
return true; | ||
} catch ( MalformedURLException e ) { | ||
return false; | ||
} | ||
} | ||
|
||
static boolean isGithubUrl( URI uri ) { | ||
try { | ||
String host = uri.toURL().getHost(); | ||
return "github.com".equals( host ) | ||
|| "raw.githubusercontent.com".equals( host ); | ||
} catch ( Exception e ) { | ||
return false; | ||
} | ||
} | ||
|
||
static Try<Model> loadModel( URL link ) { | ||
return read( link ) | ||
.flatMap( TurtleLoader::loadTurtle ); | ||
} | ||
|
||
static Try<InputStream> read( URL link ) { | ||
return Try.withResources( link::openStream ) | ||
.of( InputStream::readAllBytes ) | ||
.map( ByteArrayInputStream::new ); | ||
} | ||
|
||
static Try<URL> transformGithubUrlToContentLink( URI githubUrl ) { | ||
String url = githubUrl.toString() | ||
.replace( "github.com", "raw.githubusercontent.com" ) | ||
.replace( "/blob/", "/" ); | ||
return Try.of( () -> new URL( url ) ); | ||
} | ||
|
||
public String toString() { | ||
return "GitHubStrategy(root=" + root + ')'; | ||
} | ||
} |
Oops, something went wrong.