Skip to content

Commit 7e327eb

Browse files
committed
Clean up exception handling and add comments
1 parent 2c221ab commit 7e327eb

File tree

11 files changed

+80
-29
lines changed

11 files changed

+80
-29
lines changed

core/esmf-aspect-meta-model-interface/src/main/java/org/eclipse/esmf/aspectmodel/AspectModelFile.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,53 @@
2323

2424
import org.apache.jena.rdf.model.Model;
2525

26+
/**
27+
* An AspectModelFile is the abstraction of one "code unit". When its source location is in a file system, the
28+
* AspectModelFile corresponds to one file, but this does not have to be the case: An AspectModelFile could also
29+
* exist in memory, or purely virtually (e.g., as an abstraction of a sub-graph in a triple store).
30+
*/
2631
public interface AspectModelFile extends ModelElementGroup {
32+
/**
33+
* The RDF model with the contents of this AspectModelFile
34+
*
35+
* @return the model
36+
*/
2737
Model sourceModel();
2838

39+
/**
40+
* The list of Strings that are contained as a comment block at the start of the file. This is often used
41+
* for copyright and license information.
42+
*
43+
* @return the header comment
44+
*/
2945
default List<String> headerComment() {
3046
return List.of();
3147
}
3248

49+
/**
50+
* The URI that denominates the source location, if present. It can be a file:// or https:// URL, but it
51+
* could for example also be an Aspect Model URN, if it refers to a file that is part of the SAMM specification.
52+
* Generally, this should be the physical location, not a logical identifier, in other words, where was this
53+
* AspectModelFile loaded from.
54+
*
55+
* @return the source location
56+
*/
3357
Optional<URI> sourceLocation();
3458

59+
/**
60+
* Returns the {@link Namespace} this AspectModelFile is a part of
61+
*
62+
* @return the namespace
63+
*/
3564
default Namespace namespace() {
3665
throw new UnsupportedOperationException( "Uninitialized Aspect Model" );
3766
}
3867

68+
/**
69+
* Lists the model elements that are contained in this AspectModelFile
70+
*
71+
* @return the model elements
72+
*/
3973
@Override
4074
default List<ModelElement> elements() {
4175
throw new UnsupportedOperationException( "Uninitialized Aspect Model" );

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoader.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.io.ByteArrayOutputStream;
1818
import java.io.File;
1919
import java.io.FileInputStream;
20-
import java.io.FileNotFoundException;
2120
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.net.URI;
@@ -209,14 +208,14 @@ public AspectModel load( final InputStream inputStream ) {
209208
*/
210209
public AspectModel loadNamespacePackage( final File namespacePackage ) {
211210
if ( !namespacePackage.exists() || !namespacePackage.isFile() ) {
212-
throw new RuntimeException( new FileNotFoundException( "The specified file does not exist or is not a file." ) );
211+
throw new ModelResolutionException( "The specified file does not exist or is not a file." );
213212
}
214213

215214
try ( final InputStream inputStream = new FileInputStream( namespacePackage ) ) {
216215
return loadNamespacePackage( inputStream );
217-
} catch ( final IOException e ) {
218-
LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), e );
219-
throw new RuntimeException( "Error reading the file: " + namespacePackage.getAbsolutePath(), e );
216+
} catch ( final IOException exception ) {
217+
LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), exception );
218+
throw new ModelResolutionException( "Error reading the file: " + namespacePackage.getAbsolutePath(), exception );
220219
}
221220
}
222221

@@ -228,12 +227,13 @@ public AspectModel loadNamespacePackage( final File namespacePackage ) {
228227
*/
229228
public AspectModel loadNamespacePackage( final InputStream inputStream ) {
230229
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
230+
final boolean hasAspectModelsFolder;
231231
try {
232232
inputStream.transferTo( baos );
233-
} catch ( final IOException e ) {
234-
throw new RuntimeException( e );
233+
hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) );
234+
} catch ( final IOException exception ) {
235+
throw new ModelResolutionException( "Could not read from input", exception );
235236
}
236-
final boolean hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) );
237237
return loadNamespacePackageFromStream( new ByteArrayInputStream( baos.toByteArray() ), hasAspectModelsFolder );
238238
}
239239

@@ -245,8 +245,8 @@ private AspectModel loadNamespacePackageFromStream( final InputStream inputStrea
245245

246246
while ( ( entry = zis.getNextEntry() ) != null ) {
247247
final boolean isRelevantEntry =
248-
( hasAspectModelsFolder && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) && entry.getName()
249-
.endsWith( ".ttl" ) )
248+
( hasAspectModelsFolder && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) )
249+
&& entry.getName().endsWith( ".ttl" ) )
250250
|| ( !hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ) );
251251

252252
if ( isRelevantEntry ) {
@@ -256,26 +256,24 @@ private AspectModel loadNamespacePackageFromStream( final InputStream inputStrea
256256
}
257257

258258
zis.closeEntry();
259-
} catch ( final IOException e ) {
260-
LOG.error( "Error reading the Archive input stream", e );
261-
throw new RuntimeException( "Error reading the Archive input stream", e );
259+
} catch ( final IOException exception ) {
260+
LOG.error( "Error reading the Archive input stream", exception );
261+
throw new ModelResolutionException( "Error reading the Archive input stream", exception );
262262
}
263263

264264
final LoaderContext loaderContext = new LoaderContext();
265265
resolve( aspectModelFiles, loaderContext );
266266
return loadAspectModelFiles( loaderContext.loadedFiles() );
267267
}
268268

269-
private boolean containsFolderInNamespacePackage( final InputStream inputStream ) {
269+
private boolean containsFolderInNamespacePackage( final InputStream inputStream ) throws IOException {
270270
try ( final ZipInputStream zis = new ZipInputStream( inputStream ) ) {
271271
ZipEntry entry;
272272
while ( ( entry = zis.getNextEntry() ) != null ) {
273273
if ( entry.isDirectory() && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) ) {
274274
return true;
275275
}
276276
}
277-
} catch ( final IOException e ) {
278-
throw new RuntimeException( e );
279277
}
280278
return false;
281279
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/ModelElementFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
import org.apache.jena.vocabulary.RDF;
8484
import org.apache.jena.vocabulary.RDFS;
8585

86+
/**
87+
* Used as part of the loading process in the {@link AspectModelLoader}, it creates instance for the the {@link ModelElement}s
88+
* in an AspectModel.
89+
*/
8690
public class ModelElementFactory extends AttributeValueRetriever {
8791
private final Model model;
8892
private final Map<Resource, Instantiator<?>> instantiators = new HashMap<>();
@@ -351,7 +355,7 @@ private static Resource getModelElementType( final Resource modelElement ) {
351355
return getModelElementType( superElement );
352356
}
353357

354-
public AspectModelFile getSourceLocation( Resource modelElement ) {
358+
public AspectModelFile getSourceLocation( final Resource modelElement ) {
355359
return sourceLocator.apply( modelElement );
356360
}
357361
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/modelfile/RawAspectModelFile.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
import org.apache.jena.rdf.model.Model;
2424
import org.apache.jena.rdf.model.ModelFactory;
2525

26+
/**
27+
* An implementation of an {@link AspectModelFile} that knows about the RDF model content and source location, but has not
28+
* instantiated the model elements yet. Calling {@link #elements()} on this file will throw an exception. It is intended
29+
* to represent the intermediary result of loading an Aspect Model file, possibly from a remote location.
30+
*
31+
* @param sourceModel the source RDF model
32+
* @param headerComment the header comment
33+
* @param sourceLocation the source location
34+
*/
2635
@SuppressWarnings( "OptionalUsedAsFieldOrParameterType" )
2736
@RecordBuilder
2837
public record RawAspectModelFile(

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/json/AspectModelJsonPayloadGeneratorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.stream.IntStream;
3838
import java.util.stream.LongStream;
3939
import java.util.stream.Stream;
40+
4041
import javax.xml.datatype.DatatypeConfigurationException;
4142
import javax.xml.datatype.DatatypeFactory;
4243

@@ -566,8 +567,8 @@ void testGeneratedNumbersAreWithinRange( final RDFDatatype numericModelType, fin
566567
assertNumberInRange( validator.getTestNumber(), randomRange, boundKind.orElse( null ) );
567568
assertMinValue( minValidator.getTestNumber(), randomRange.getLeft(), boundKind.orElse( null ) );
568569
assertMaxValue( maxValidator.getTestNumber(), randomRange.getRight(), boundKind.orElse( null ) );
569-
} catch ( final IOException e ) {
570-
throw new RuntimeException( e );
570+
} catch ( final IOException exception ) {
571+
throw new RuntimeException( exception );
571572
}
572573
}
573574

core/esmf-aspect-model-github-resolver/src/test/java/org/eclipse/esmf/aspectmodel/resolver/github/GitHubStrategyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ void afterEach() {
6464
System.err.println( "Could not delete file " + file );
6565
}
6666
} );
67-
} catch ( final IOException e ) {
68-
throw new RuntimeException( e );
67+
} catch ( final IOException exception ) {
68+
throw new RuntimeException( exception );
6969
}
7070
}
7171
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/JsLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public JsLibrary( final Optional<String> uri, final List<String> libraryUrls, fi
3636
code.append( new String( inputStream.readAllBytes(), StandardCharsets.UTF_8 ) );
3737
code.append( "\n" );
3838
} catch ( final IOException exception ) {
39-
throw new RuntimeException( exception );
39+
throw new ShaclValidationException( "Could not resolve JsLibrary " + uri, exception );
4040
}
4141
}
4242

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/ShaclValidationException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public class ShaclValidationException extends RuntimeException {
2525
public ShaclValidationException( final String message ) {
2626
super( message );
2727
}
28+
29+
public ShaclValidationException( final String message, final Throwable cause ) {
30+
super( message, cause );
31+
}
2832
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/Shape.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static NodeKind forNode( final RDFNode node ) {
5858
if ( node.isAnon() ) {
5959
return Shape.NodeKind.BlankNode;
6060
}
61-
throw new RuntimeException( "Invalid nodekind: " + node );
61+
throw new ShaclValidationException( "Invalid nodekind: " + node );
6262
}
6363

6464
public String humanReadable() {

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/ShapeLoader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Constraint>> builder()
135135
.put( SHACL.or(), context -> new OrConstraint( nestedShapesList( context.statement() ) ) )
136136
.put( SHACL.xone(), context -> new XoneConstraint( nestedShapesList( context.statement() ) ) )
137137
.put( SHACL.node(), context -> {
138-
// Since sh:node can recursively refer to the same NodeShape is used in when shapes define recursive structures,
138+
// Since sh:node can recursively refer to the same NodeShape it is used in when shapes define recursive structures,
139139
// the NodeConstraint is built using a Supplier for the actual NodeShape. Only if the NodeShape has not yet been
140140
// seen (i.e., it could be in the process of being built right now), create it now.
141141
final Resource resource = context.statement().getObject().asResource();
@@ -148,7 +148,7 @@ Constraint>> builder()
148148
.put( SHACL.closed(), context -> {
149149
boolean closed = context.statement().getBoolean();
150150
if ( !closed ) {
151-
throw new RuntimeException();
151+
throw new ShaclValidationException( "Value of sh:closed may only be true" );
152152
}
153153
Set<Property> ignoredProperties = Optional.ofNullable(
154154
context.statement().getSubject().getProperty( SHACL.ignoredProperties() ) )
@@ -335,11 +335,11 @@ private Path path( final Resource pathNode ) {
335335
if ( zeroOrOneStatement != null ) {
336336
return new ZeroOrOnePath( path( zeroOrOneStatement.getResource() ) );
337337
}
338-
throw new RuntimeException( "Invalid path: " + PrintUtil.print( pathNode ) );
338+
throw new ShaclValidationException( "Invalid path: " + PrintUtil.print( pathNode ) );
339339
}
340340

341341
private boolean isRdfList( final Resource resource ) {
342-
return (resource.isURIResource() && resource.getURI().equals( RDF.nil.getURI() ))
342+
return ( resource.isURIResource() && resource.getURI().equals( RDF.nil.getURI() ) )
343343
|| resource.hasProperty( RDF.rest ) || resource.hasProperty( RDF.first );
344344
}
345345

@@ -351,7 +351,7 @@ private List<Constraint> constraints( final Resource valueNode, final Optional<P
351351
return Streams.stream( valueNode.listProperties( entry.getKey() ) )
352352
.map( statement -> entry.getValue().apply( new ShapeContext( statement, path ) ) );
353353
} catch ( final Exception exception ) {
354-
throw new RuntimeException( "Could not load SHACL shape: Invalid use of " + entry.getKey() + " on " + valueNode );
354+
throw new ShaclValidationException( "Could not load SHACL shape: Invalid use of " + entry.getKey() + " on " + valueNode );
355355
}
356356
} )
357357
.collect( Collectors.toList() );

0 commit comments

Comments
 (0)