Skip to content

Commit 9176a4a

Browse files
committed
Introduce NamespacePackage class
1 parent e1962ec commit 9176a4a

File tree

6 files changed

+578
-191
lines changed

6 files changed

+578
-191
lines changed

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

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
package org.eclipse.esmf.aspectmodel.loader;
1515

16-
import java.io.ByteArrayInputStream;
17-
import java.io.ByteArrayOutputStream;
1816
import java.io.File;
1917
import java.io.FileInputStream;
2018
import java.io.IOException;
@@ -33,8 +31,6 @@
3331
import java.util.function.Supplier;
3432
import java.util.stream.Collectors;
3533
import java.util.stream.Stream;
36-
import java.util.zip.ZipEntry;
37-
import java.util.zip.ZipInputStream;
3834

3935
import org.eclipse.esmf.aspectmodel.AspectLoadingException;
4036
import org.eclipse.esmf.aspectmodel.AspectModelFile;
@@ -43,6 +39,7 @@
4339
import org.eclipse.esmf.aspectmodel.resolver.EitherStrategy;
4440
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
4541
import org.eclipse.esmf.aspectmodel.resolver.ModelSource;
42+
import org.eclipse.esmf.aspectmodel.resolver.NamespacePackage;
4643
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
4744
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategySupport;
4845
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
@@ -176,7 +173,8 @@ public AspectModel loadUrns( final Collection<AspectModelUrn> urns ) {
176173
}
177174

178175
/**
179-
* Load an Aspect Model from an input stream and optionally set the source location for this input
176+
* Load an Aspect Model (.ttl) from an input stream and optionally set the source location for this input. For loading
177+
* an Aspect Model Namespace Package (.zip), use {@link #loadNamespacePackage(InputStream, URI)} instead.
180178
*
181179
* @param inputStream the input stream
182180
* @param sourceLocation the source location for the model
@@ -212,70 +210,54 @@ public AspectModel loadNamespacePackage( final File namespacePackage ) {
212210
}
213211

214212
try ( final InputStream inputStream = new FileInputStream( namespacePackage ) ) {
215-
return loadNamespacePackage( inputStream );
213+
return loadNamespacePackage( inputStream, namespacePackage.toURI() );
216214
} catch ( final IOException exception ) {
217215
LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), exception );
218216
throw new AspectLoadingException( "Error reading the file: " + namespacePackage.getAbsolutePath(), exception );
219217
}
220218
}
221219

222220
/**
223-
* Load a Namespace Package (Archive) from an InputStream
221+
* Load a namespace package from binary with a given location. The location is not resolved or loaded from, but is only attached
222+
* to the files loaded from the input stream to indicate their original source, e.g., file system location or URL.
224223
*
225-
* @param inputStream the input stream
226-
* @return the Aspect Model
224+
* @param location the source location
225+
* @param binaryContent the ZIP content
226+
* @return the loaded and resolved Aspect Model
227227
*/
228-
public AspectModel loadNamespacePackage( final InputStream inputStream ) {
229-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
230-
final boolean hasAspectModelsFolder;
231-
try {
232-
inputStream.transferTo( baos );
233-
hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) );
234-
} catch ( final IOException exception ) {
235-
throw new AspectLoadingException( "Could not read from input", exception );
236-
}
237-
return loadNamespacePackageFromStream( new ByteArrayInputStream( baos.toByteArray() ), hasAspectModelsFolder );
228+
public AspectModel loadNamespacePackage( final byte[] binaryContent, final URI location ) {
229+
final List<AspectModelFile> packageFiles = new NamespacePackage( binaryContent, location ).loadContents().toList();
230+
final LoaderContext loaderContext = new LoaderContext();
231+
resolve( packageFiles, loaderContext );
232+
return loadAspectModelFiles( loaderContext.loadedFiles() );
238233
}
239234

240-
private AspectModel loadNamespacePackageFromStream( final InputStream inputStream, final boolean hasAspectModelsFolder ) {
241-
final List<AspectModelFile> aspectModelFiles = new ArrayList<>();
242-
243-
try ( final ZipInputStream zis = new ZipInputStream( inputStream ) ) {
244-
ZipEntry entry;
245-
246-
while ( ( entry = zis.getNextEntry() ) != null ) {
247-
final boolean isRelevantEntry =
248-
( hasAspectModelsFolder && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) )
249-
&& entry.getName().endsWith( ".ttl" ) )
250-
|| ( !hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ) );
251-
252-
if ( isRelevantEntry ) {
253-
final AspectModelFile aspectModelFile = migrate( AspectModelFileLoader.load( zis ) );
254-
aspectModelFiles.add( aspectModelFile );
255-
}
256-
}
257-
258-
zis.closeEntry();
259-
} catch ( final IOException exception ) {
260-
LOG.error( "Error reading the Archive input stream", exception );
261-
throw new AspectLoadingException( "Error reading the Archive input stream", exception );
262-
}
263-
235+
/**
236+
* Load a namespace package from an input stream with a given location. The location is not resolved or loaded from, but is only attached
237+
* to the files loaded from the input stream to indicate their original source, e.g., file system location or URL of the
238+
* namespace package.
239+
*
240+
* @param location the source location
241+
* @param inputStream the input stream to load the ZIP content from
242+
* @return the loaded and resolved Aspect Model
243+
*/
244+
public AspectModel loadNamespacePackage( final InputStream inputStream, final URI location ) {
245+
final List<AspectModelFile> packageFiles = new NamespacePackage( inputStream, location ).loadContents().toList();
264246
final LoaderContext loaderContext = new LoaderContext();
265-
resolve( aspectModelFiles, loaderContext );
247+
resolve( packageFiles, loaderContext );
266248
return loadAspectModelFiles( loaderContext.loadedFiles() );
267249
}
268250

269-
private boolean containsFolderInNamespacePackage( final InputStream inputStream ) throws IOException {
270-
try ( final ZipInputStream zis = new ZipInputStream( inputStream ) ) {
271-
ZipEntry entry;
272-
while ( ( entry = zis.getNextEntry() ) != null ) {
273-
if ( entry.isDirectory() && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) ) {
274-
return true;
275-
}
276-
}
277-
}
278-
return false;
251+
/**
252+
* Load a Namespace Package (Archive) from an InputStream
253+
*
254+
* @param inputStream the input stream
255+
* @return the Aspect Model
256+
* @deprecated Use {@link #loadNamespacePackage(InputStream, URI)} instead
257+
*/
258+
@Deprecated
259+
public AspectModel loadNamespacePackage( final InputStream inputStream ) {
260+
return loadNamespacePackage( inputStream, URI.create( "file:unknown" ) );
279261
}
280262

281263
private AspectModelFile migrate( final AspectModelFile file ) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public static RawAspectModelFile load( final Model model ) {
113113
return new RawAspectModelFile( model, List.of(), Optional.empty() );
114114
}
115115

116+
public static RawAspectModelFile load( final byte[] content, final Optional<URI> sourceLocation ) {
117+
return load( new ByteArrayInputStream( content ), sourceLocation );
118+
}
119+
116120
public static RawAspectModelFile load( final byte[] content ) {
117121
return load( new ByteArrayInputStream( content ) );
118122
}

0 commit comments

Comments
 (0)